Example usage for org.apache.commons.lang3.text StrLookup mapLookup

List of usage examples for org.apache.commons.lang3.text StrLookup mapLookup

Introduction

In this page you can find the example usage for org.apache.commons.lang3.text StrLookup mapLookup.

Prototype

public static <V> StrLookup<V> mapLookup(final Map<String, V> map) 

Source Link

Document

Returns a lookup which looks up values using a map.

Usage

From source file:org.apache.openejb.maven.jarstxt.JarsTxtMojo.java

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    if (!outputFile.getParentFile().exists()) {
        FileUtils.mkdir(outputFile.getParentFile().getAbsolutePath());
    }/*from w  w  w  . j a  va 2 s.c  o  m*/

    FileWriter writer = null;
    try {
        writer = new FileWriter(outputFile);

        final TreeSet<String> set = new TreeSet<>();

        for (final Artifact a : (Set<Artifact>) project.getArtifacts()) {
            if (!acceptScope(a.getScope()) || !acceptType(a.getType())) {
                continue;
            }

            a.setScope(Artifact.SCOPE_PROVIDED);

            final StringBuilder line = new StringBuilder("mvn:").append(a.getGroupId()).append("/")
                    .append(a.getArtifactId()).append("/").append(version(a));

            final boolean isJar = JAR.equals(a.getType());
            if (!isJar) {
                line.append("/").append(a.getType());
            }

            if (a.getClassifier() != null) {
                if (isJar) {
                    line.append("/").append(JAR);
                }
                line.append("/").append(a.getClassifier());
            }

            if (hashAlgo != null) {
                final Artifact artifact = factory.createDependencyArtifact(a.getGroupId(), a.getArtifactId(),
                        VersionRange.createFromVersion(a.getVersion()), a.getType(), a.getClassifier(),
                        a.getScope());
                try {
                    resolver.resolve(artifact, remoteRepos, local);
                } catch (final ArtifactResolutionException | ArtifactNotFoundException e) {
                    throw new MojoExecutionException(e.getMessage(), e);
                }
                final File file = artifact.getFile();
                line.append("|")
                        .append(Files.hash((Set<URL>) Collections.singleton(file.toURI().toURL()), hashAlgo))
                        .append("|").append(hashAlgo);
            }

            set.add(line.toString());
        }

        if (additionals != null) {
            if (placeHolders == null) {
                placeHolders = new HashMap<>();
            }

            final StrSubstitutor lookup = new StrSubstitutor(StrLookup.mapLookup(placeHolders));

            for (final String line : additionals) {
                final StringBuilder builder = new StringBuilder(line);
                if (hashAlgo != null) {
                    builder.append("|").append(Files.hash(urls(line, lookup), hashAlgo)).append("|")
                            .append(hashAlgo);
                }
                set.add(builder.toString());
            }
        }

        // written after to be sorted, more readable
        for (final String line : set) {
            writer.write(line);
            writer.write("\n");
        }

        writer.flush();
    } catch (IOException e) {
        getLog().error(e.getMessage(), e);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                // no-op
            }
        }
    }
}

From source file:org.basinmc.irc.bridge.github.GitHubServerHandler.java

/**
 * Retrieves the message for a certain event type and metadata.
 *
 * @param eventType an event type.//from w ww . j a v  a2s .c  o m
 * @param eventData a set of decoded event data.
 * @return a message or, if no message was found, null.
 */
@Nullable
@SuppressWarnings("unchecked")
private String getMessage(@Nonnull String eventType, @Nonnull Map eventData) {
    StrSubstitutor substitutor = new StrSubstitutor(StrLookup.mapLookup(this.flatMap(eventData)));
    Object handler = this.messageMap.get(eventType);
    String message = null;

    if (handler instanceof String) {
        message = substitutor.replace(handler);
    } else if (handler instanceof Map) {
        Map group = (Map) handler;
        String condition = (String) group.get("condition");
        Object prefix = group.get("prefix");
        Object suffix = group.get("suffix");

        if (prefix instanceof String) {
            prefix = substitutor.replace(prefix);
        } else {
            prefix = null;
        }

        if (suffix instanceof String) {
            suffix = substitutor.replace(suffix);
        } else {
            suffix = null;
        }

        if (condition == null) {
            logger.error("Group " + eventType + " does not have a condition");
        } else {
            condition = substitutor.replace(condition);
            Object childHandler = group.get(condition);

            if (childHandler instanceof String) {
                return (prefix != null ? prefix : "") + substitutor.replace(childHandler)
                        + (suffix != null ? suffix : "");
            } else if (childHandler != null) {
                logger.error("Invalid child handler type: " + childHandler.getClass());
            }
        }
    } else if (handler != null) {
        logger.error("Invalid handler type: " + handler.getClass());
    }

    return message;
}