Example usage for org.apache.commons.lang StringUtils getCommonPrefix

List of usage examples for org.apache.commons.lang StringUtils getCommonPrefix

Introduction

In this page you can find the example usage for org.apache.commons.lang StringUtils getCommonPrefix.

Prototype

public static String getCommonPrefix(String[] strs) 

Source Link

Document

Compares all Strings in an array and returns the initial sequence of characters that is common to all of them.

Usage

From source file:biz.netcentric.cq.tools.actool.installationhistory.impl.HistoryUtils.java

public static void setHistoryNodeProperties(final Node historyNode, AcInstallationHistoryPojo history)
        throws ValueFormatException, VersionException, LockException, ConstraintViolationException,
        RepositoryException {//ww  w.ja  v a 2  s  .  c om

    historyNode.setProperty(PROPERTY_INSTALLATION_DATE, history.getInstallationDate().toString());
    historyNode.setProperty(PROPERTY_SUCCESS, history.isSuccess());
    historyNode.setProperty(PROPERTY_EXECUTION_TIME, history.getExecutionTime());

    String messageHistory = history.getVerboseMessageHistory();

    // 16777216 bytes = ~ 16MB was the error in #145, assuming chars*2, hence 16777216 / 2 = 8MB, using 7MB to consider the BSON
    // overhead
    if (messageHistory.length() > (7 * 1024 * 1024)) {
        messageHistory = history.getMessageHistory(); // just use non-verbose history for this case
    }
    historyNode.setProperty(PROPERTY_MESSAGES, messageHistory);
    historyNode.setProperty(PROPERTY_TIMESTAMP, history.getInstallationDate().getTime());
    historyNode.setProperty(PROPERTY_SLING_RESOURCE_TYPE, "/apps/netcentric/actool/components/historyRenderer");

    Map<String, String> configFileContentsByName = history.getConfigFileContentsByName();
    if (configFileContentsByName != null) {
        String commonPrefix = StringUtils.getCommonPrefix(
                configFileContentsByName.keySet().toArray(new String[configFileContentsByName.size()]));
        String crxPackageName = history.getCrxPackageName(); // for install hook case
        historyNode.setProperty(PROPERTY_INSTALLED_FROM,
                StringUtils.defaultString(crxPackageName) + commonPrefix);
    }

}

From source file:biz.netcentric.cq.tools.actool.installationhistory.impl.AcHistoryServiceImpl.java

private void persistInstalledConfigurations(final Session session, final Node historyNode,
        AcInstallationHistoryPojo history) {
    try {/* ww  w. j  av a 2s  .  c  om*/

        Map<String, String> configFileContentsByName = history.getConfigFileContentsByName();
        if (configFileContentsByName == null) {
            return;
        }

        String commonPrefix = StringUtils.getCommonPrefix(
                configFileContentsByName.keySet().toArray(new String[configFileContentsByName.size()]));

        for (String fullConfigFilePath : configFileContentsByName.keySet()) {
            File targetPathFile = new File(INSTALLED_CONFIGS_NODE_NAME + "/"
                    + StringUtils.substringAfter(fullConfigFilePath, commonPrefix));
            File targetPathParentDir = targetPathFile.getParentFile();
            Node configFolder = JcrUtils.getOrCreateByPath(historyNode,
                    targetPathParentDir != null ? targetPathParentDir.getPath() : targetPathFile.getPath(),
                    false, JcrConstants.NT_FOLDER, JcrConstants.NT_FOLDER, false);
            ByteArrayInputStream configFileInputStream = new ByteArrayInputStream(
                    configFileContentsByName.get(fullConfigFilePath).getBytes());
            JcrUtils.putFile(configFolder, targetPathFile.getName(), "text/yaml", configFileInputStream);
        }

        history.addVerboseMessage("Saved installed configuration files under : " + historyNode.getPath() + "/"
                + INSTALLED_CONFIGS_NODE_NAME);
    } catch (RepositoryException e) {
        String message = e.toString();
        history.addError(message);
        LOG.error("Exception while saving history node " + historyNode + ": " + message, e);
    }
}

From source file:io.swagger.codegen.languages.JavascriptClientCodegen.java

private String findCommonPrefixOfVars(List<String> vars) {
    String prefix = StringUtils.getCommonPrefix(vars.toArray(new String[vars.size()]));
    // exclude trailing characters that should be part of a valid variable
    // e.g. ["status-on", "status-off"] => "status-" (not "status-o")
    return prefix.replaceAll("[a-zA-Z0-9]+\\z", "");
}

From source file:com.liferay.events.global.mobile.Utils.java

/**
 * Calculates the number of characters from the beginning of the strings that match exactly one-to-one,
 * up to a maximum of four (4) characters.
 *
 * @param first  The first string.//  www  .  j  ava  2  s  . co  m
 * @param second The second string.
 * @return A number between 0 and 4.
 */
private static int commonPrefixLength(final CharSequence first, final CharSequence second) {
    final int result = StringUtils.getCommonPrefix(new String[] { first.toString(), second.toString() })
            .length();

    // Limit the result to 4.
    return result > 4 ? 4 : result;
}

From source file:org.apache.maven.shared.release.util.ReleaseUtil.java

public static String getCommonBasedir(List<MavenProject> reactorProjects, String separator) throws IOException {
    String[] baseDirs = new String[reactorProjects.size()];
    int idx = 0;/*  ww  w .  ja  v a2  s .  c  o  m*/
    for (MavenProject p : reactorProjects) {
        String dir = p.getBasedir().getCanonicalPath();

        // always end with separator so that we know what is a path and what is a partial directory name in the
        // next call
        if (!dir.endsWith(separator)) {
            dir = dir + separator;
        }
        baseDirs[idx++] = dir;
    }

    String basedir = StringUtils.getCommonPrefix(baseDirs);

    int separatorPos = basedir.lastIndexOf(separator);
    if (!basedir.endsWith(separator) && separatorPos >= 0) {
        basedir = basedir.substring(0, separatorPos);
    }

    if (basedir.endsWith(separator) && basedir.length() > 1) {
        basedir = basedir.substring(0, basedir.length() - 1);
    }

    return basedir;
}

From source file:org.jboss.loom.utils.as7.AS7ModuleUtils.java

/**
 *  Returns the name of the module which uses given .jar.
 *  For example, file at modules/system/layers/base/com/h2database/h2/main/h2-1.3.168.jar
 *  should return "com.h2database.h2"./*ww w  .j a va 2s . co m*/
 * 
 *  The current implementation is naive, assuming that the .jar file is in the module's root dir, where module.xml is.
 * 
 *  This method behavior is likely to change with various versions of EAP.
 */
public static String identifyModuleContainingJar(AS7Config as7Config, File jar) {

    String modAbsPath = as7Config.getModulesDir().getPath();
    String jarAbsPath = jar.getParentFile().getParentFile().getPath();

    String commonPrefix = StringUtils.getCommonPrefix(new String[] { modAbsPath, jarAbsPath });
    String diff = jarAbsPath.substring(commonPrefix.length());

    String modName = StringUtils.removeStart(diff, "/");
    return modName.replace('/', '.');
}

From source file:org.onosproject.store.service.DocumentPath.java

/**
 * Returns the path that points to the least common ancestor of the specified
 * collection of paths.//from  ww  w .  ja  v a 2s  .c  o m
 * @param paths collection of path
 * @return path to least common ancestor
 */
public static DocumentPath leastCommonAncestor(Collection<DocumentPath> paths) {
    if (CollectionUtils.isEmpty(paths)) {
        return null;
    }
    return DocumentPath.from(
            StringUtils.getCommonPrefix(paths.stream().map(DocumentPath::toString).toArray(String[]::new)));
}

From source file:se.kth.climate.fast.netcdfparquet.Main.java

private static String findTitle(File[] files) {
    String[] cleanedFileNames = new String[files.length];
    for (int i = 0; i < files.length; i++) {
        File f = files[i];/*from  ww  w .j  ava2s  . c  om*/
        String fName = f.getName();
        String fNameNoSuffix = FilenameUtils.removeExtension(fName);
        String fNameNoDotsNoDash = fNameNoSuffix.replace(".", "_").replace("-", "_");
        cleanedFileNames[i] = fNameNoDotsNoDash;
    }
    if (cleanedFileNames.length == 1) {
        return cleanedFileNames[0];
    }
    String prefix = StringUtils.getCommonPrefix(cleanedFileNames);
    if (prefix.endsWith("_")) {
        prefix = prefix.substring(0, prefix.length() - 1);
    }
    return prefix;
}

From source file:v7cr.ReviewTab.java

private void compressTree(Tree tree, Collection<String> nodes) {

    List<String> orgPaths = new ArrayList<String>(nodes);

    int end = orgPaths.size();

    for (int start = 0; start < end; start++) {
        String prefix = StringUtils.getCommonPrefix(orgPaths.subList(start, end).toArray(EMPTY_STRING_ARRAY));
        if (!prefix.endsWith("/")) {
            prefix = StringUtils.substringBeforeLast(prefix, "/") + "/";
        }/* ww  w. ja  v a 2  s  .  co  m*/
        String first = orgPaths.get(start);
        String extendedPrefix = prefix + StringUtils.substringBefore(first.substring(prefix.length()), "/");
        if (!extendedPrefix.equals(first)) {
            extendedPrefix += "/";
            if (tree.containsId(prefix)) {
                tree.addItem(extendedPrefix);
                tree.setParent(extendedPrefix, prefix);
            } else {
                tree.setParent(extendedPrefix, connectPath(tree, extendedPrefix));
            }
            tree.setChildrenAllowed(extendedPrefix, true);
            int last;
            for (last = start; last < end; last++) {
                String l = orgPaths.get(last);
                if (l.startsWith(extendedPrefix)) {
                    tree.setParent(l, extendedPrefix);
                } else {
                    last++;
                    break;
                }
            }
            tree.setItemCaption(extendedPrefix, extendedPrefix + "(" + (last - 1 - start) + ")");

            if (last - start > 5) {
                compressTree(tree, orgPaths.subList(start, last - 1));
            }
            start = last - 1;

        } else {
            if (!tree.containsId(prefix)) {
                prefix = connectPath(tree, prefix);
            }

            if (!prefix.equals(first)) {
                tree.setParent(first, prefix);
            }

        }
    }

}