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

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

Introduction

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

Prototype

public static String substringBeforeLast(String str, String separator) 

Source Link

Document

Gets the substring before the last occurrence of a separator.

Usage

From source file:com.adobe.acs.commons.wcm.impl.PropertyMergePostProcessor.java

/**
 * Merges the values found in the the source properties into the destination
 * property as a multi-value. The values of the source properties and
 * destination properties must all be the same property type.
 *
 * The unique set of properties will be stored in
 *
 * @param resource the resource to look for the source and destination
 * properties on/* w w  w  . j a va  2s  .c  o  m*/
 * @param destination the property to store the collected properties.
 * @param sources the properties to collect values from for merging
 * @param typeHint the data type that should be used when reading and
 * storing the data
 * @param allowDuplicates true to allow duplicates values in the destination
 * property; false to make values unique
 * @return Optional resource updated, if any
 */
protected final <T> Optional<Resource> merge(final Resource resource, final String destination,
        final Collection<String> sources, final Class<T> typeHint, final boolean allowDuplicates)
        throws PersistenceException {

    ResourceResolver rr = resource.getResourceResolver();

    // Create an empty array of type T
    @SuppressWarnings("unchecked")
    final T[] emptyArray = (T[]) Array.newInstance(typeHint, 0);

    Collection<T> collectedValues;

    if (allowDuplicates) {
        collectedValues = new ArrayList<>();
    } else {
        collectedValues = new LinkedHashSet<>();
    }

    for (final String source : sources) {
        Resource sourceProperties = resource;
        String sourceParam = source;
        if (source.contains("/")) {
            sourceParam = StringUtils.substringAfterLast(source, "/");
            sourceProperties = rr.getResource(resource, StringUtils.substringBeforeLast(source, "/"));
        }
        T[] tmp = sourceProperties.adaptTo(ModifiableValueMap.class).get(sourceParam, emptyArray);
        collectedValues.addAll(Arrays.asList(tmp));
    }

    Resource targetResource = resource;
    String targetProperty = destination;
    if (destination.contains("/")) {
        targetProperty = StringUtils.substringAfterLast(destination, "/");
        targetResource = rr.getResource(resource, StringUtils.substringBeforeLast(destination, "/"));
    }
    ModifiableValueMap targetProperties = targetResource.adaptTo(ModifiableValueMap.class);

    final T[] currentValues = targetProperties.get(targetProperty, emptyArray);

    if (!collectedValues.equals(Arrays.asList(currentValues))) {
        targetProperties.put(targetProperty, collectedValues.toArray(emptyArray));

        return Optional.of(targetResource);
    } else {
        return Optional.empty();
    }
}

From source file:info.magnolia.module.admininterface.AdminTreeMVCHandler.java

public void deleteNode(String path) throws Exception {
    String parentPath = StringUtils.substringBeforeLast(path, "/"); //$NON-NLS-1$
    String label = StringUtils.substringAfterLast(path, "/"); //$NON-NLS-1$
    deleteNode(parentPath, label);//w w  w  .j a v a  2 s . c  o m
}

From source file:com.activecq.samples.workflow.impl.LocalizedTagTitleExtractorProcessWorkflow.java

/**
 * Returns localized Tag Titles for all the ancestor tags to the tags supplied in "tagPaths"
 * <p/>// ww w  . jav  a 2  s  .co m
 * Tags in
 *
 * @param tags
 * @param locale
 * @param tagManager
 * @return
 */
private List<String> tagsToUpstreamLocalizedTagTitles(Tag[] tags, Locale locale, TagManager tagManager) {
    List<String> localizedTagTitles = new ArrayList<String>();

    for (final Tag tag : tags) {
        String tagID = tag.getTagID();

        boolean isLast = false;

        int count = StringUtils.countMatches(tagID, PATH_DELIMITER);
        while (count >= MIN_TAG_DEPTH && count >= 0) {

            final Tag ancestorTag = tagManager.resolve(tagID);
            if (ancestorTag != null) {
                localizedTagTitles.add(getLocalizedTagTitle(ancestorTag, locale));
            }

            if (isLast) {
                break;
            }

            tagID = StringUtils.substringBeforeLast(tagID, PATH_DELIMITER);
            count = StringUtils.countMatches(tagID, PATH_DELIMITER);

            if (count <= 0) {
                isLast = true;
            }
        }
    }

    return localizedTagTitles;
}

From source file:es.urjc.mctwp.image.impl.collection.fs.ImageContentCollectionFSImpl.java

/**
 * Finds any content into a collection//from   ww w.  j av  a 2  s  . c  o  m
 * 
 * @param collection
 * @param imageId
 * @return file content
 */
private File findContent(File collection, String imageId) {
    File result = null;

    File[] auxLst = collection.listFiles(icff);
    if (auxLst != null) {
        for (File file : auxLst) {
            String id = StringUtils.substringBeforeLast(file.getName(), FilenameUtils.EXTENSION_SEPARATOR_STR);

            if (id.equals(imageId)) {
                result = file;
                break;
            }
        }
    }

    return result;
}

From source file:adalid.util.velocity.BaseBuilder.java

private boolean copyTextFiles() {
    Collection<File> files = FileUtils.listFiles(projectFolder, textFileFilter(), textDirFilter());
    //      ColUtils.sort(files);
    final String oldJava1 = "textoFilasPorPagina1Validator1.setMaximum(50L);";
    final String newJava1 = "textoFilasPorPagina1Validator1.setMaximum(${Constants.getDefaultRowsPerPageLimit()}L);";
    final String oldJSP01 = "id=\"table1\" width=\"1200\"";
    final String newJSP01 = "id=\"table1\" width=\"${project_max_view_width}\"";
    final String oldJSP02 = "id=\"tableRowGroup1\" rows=\"10\"";
    final String newJSP02 = "id=\"tableRowGroup1\" rows=\"${Constants.getDefaultRowsPerPage()}\"";
    final String oldJSP03 = "style=\"width: 1200px\" styleClass=\"pdq-grid-detalle-1\"";
    final String newJSP03 = "style=\"width: ${project_max_view_width}px\" styleClass=\"pdq-grid-detalle-1\"";
    final String oldJSPF1 = "id=\"messageGroup1\" style=\"width: 1200px";
    final String newJSPF1 = "id=\"messageGroup1\" style=\"width: ${project_max_view_width}px";
    String source, target, targetParent;
    boolean java, jrxml, jsp, jspf, properties, sh, bat, sql, xml;
    String[] precedingWords = { "extends", "import", "new", "@see", "@throws" };
    SmallFile smallSource;//from   www.  ja v  a  2  s . co m
    List<String> sourceLines;
    List<String> targetLines = new ArrayList<>();
    for (File file : files) {
        source = file.getPath();
        java = StringUtils.endsWithIgnoreCase(source, ".java");
        jrxml = StringUtils.endsWithIgnoreCase(source, ".jrxml");
        jsp = StringUtils.endsWithIgnoreCase(source, ".jsp");
        jspf = StringUtils.endsWithIgnoreCase(source, ".jspf");
        properties = StringUtils.endsWithIgnoreCase(source, ".properties");
        sh = StringUtils.endsWithIgnoreCase(source, ".sh");
        bat = StringUtils.endsWithIgnoreCase(source, ".bat");
        sql = StringUtils.endsWithIgnoreCase(source, ".sql");
        xml = StringUtils.endsWithIgnoreCase(source, ".xml");
        target = source.replace(projectFolderPath, velocityTemplatesTargetFolderPath);
        targetParent = StringUtils.substringBeforeLast(target, FS);
        targetLines.clear();
        if (java) {
            javaHeading(targetLines);
        } else if (jsp || jspf) {
            jspHeading(targetLines);
        } else if (properties) {
            propertiesHeading(targetLines);
        } else if (sql) {
            sqlHeading(targetLines);
        } else if (xml || jrxml) {
            xmlHeading(targetLines);
        } else {
            genericHeading(targetLines);
        }
        FilUtils.mkdirs(targetParent);
        smallSource = new SmallFile(source);
        sourceLines = smallSource.read();
        check(smallSource);
        if (smallSource.isNotEmpty()) {
            for (String line : sourceLines) {
                if (StringUtils.isNotBlank(line)) {
                    if (java && line.matches(PACKAGE_REGEX)) {
                        line = "package $package;";
                    } else {
                        line = line.replace("$", "${dollar}");
                        line = line.replace("#", "${pound}");
                        line = line.replace("\\", "${backslash}");
                        line = line.replace(project, PROJECT_ALIAS);
                        line = line.replace(PROJECT, PROJECT_ALIAS_UPPER_CASE);
                        if (java) {
                            for (String word : precedingWords) {
                                line = replaceAliasWithRootPackageName(line, word + " ", ".");
                            }
                            line = replaceAliasWithRootPackageName(line, "String BASE_NAME = ", ".");
                            line = replaceAliasWithRootPackageName(line, "new ResourceBundleHandler(", ".");
                            line = line.replace(oldJava1, newJava1);
                        }
                        if (jrxml) {
                            line = replaceAliasWithRootPackageName(line, "<import value=\"", ".");
                        }
                        if (jsp) {
                            line = replaceAliasWithRootPackageName(line, "<%@ page import=\"", ".");
                            line = line.replace(oldJSP01, newJSP01);
                            line = line.replace(oldJSP02, newJSP02);
                            line = line.replace(oldJSP03, newJSP03);
                        }
                        if (jspf) {
                            line = line.replace(oldJSPF1, newJSPF1);
                        }
                        if (properties) {
                            line = StrUtils.replaceAfter(line, PROJECT_ALIAS + ".", ROOT_PACKAGE_NAME + ".",
                                    "${pound}");
                            line = StrUtils.replaceAfter(line, PROJECT_ALIAS + ".", ROOT_PACKAGE_NAME + ".",
                                    "file.resource.loader.class");
                            line = replaceAliasWithRootPackageName(line, "log4j.category.", "=");
                            line = replaceAliasWithRootPackageName(line, "log4j.additivity.", "=");
                        }
                        if (sh) {
                            line = StrUtils.replaceAfter(line, PROJECT_ALIAS, DATABASE_NAME, "dbname=");
                        }
                        if (bat) {
                            line = StrUtils.replaceAfter(line, PROJECT_ALIAS, DATABASE_NAME, "dbname=");
                        }
                        if (xml) {
                            line = replaceAliasWithDatabaseName(line, "jdbc/", "");
                            line = replaceAliasWithDatabaseName(line, "localhost:5432/", "");
                            line = StrUtils.replaceAfter(line, PROJECT_ALIAS + "-pool", DATABASE_NAME + "-pool",
                                    "pool-name=");
                            line = replaceAliasWithRootPackageName(line, "<logger category=\"", "\">");
                        }
                    }
                }
                targetLines.add(line);
            }
        }
        if (write(target, targetLines, WINDOWS_CHARSET)) {
            textFilesCopied++;
            createTextFilePropertiesFile(source, target);
        }
    }
    return true;
}

From source file:eionet.cr.util.FolderUtil.java

/**
 * returns parent folder URI.// ww  w . j  ava 2  s . c om
 * @param uri this URI
 * @return parent uri
 */
public static String extractParentFolderUri(String uri) {
    return StringUtils.substringBeforeLast(uri, "/");
}

From source file:adalid.util.velocity.MavenArchetypeBuilder.java

private boolean copyTextFiles() {
    Collection<File> files = FileUtils.listFiles(projectFolder, textFileFilter(), textDirFilter());
    //      ColUtils.sort(files);
    String source, target, targetParent, template, clazz;
    boolean java;
    Charset cs;/*from w  w w  .  j  a va2  s. c om*/
    SmallFile smallSource;
    for (File file : files) {
        source = file.getPath();
        java = StringUtils.endsWithIgnoreCase(source, ".java");
        target = source.replace(projectFolderPath, velocityTemplatesTargetFolderPath);
        template = StringUtils.removeStart(target, velocityTemplatesTargetFolderPath + FS).replace('\\', '/');
        clazz = StringUtils
                .removeStartIgnoreCase(StringUtils.removeEndIgnoreCase(template, ".java"), "src/main/java/")
                .replace('/', '.');
        if (java) {
            classes.add(clazz);
            sources.add(template);
        } else {
            resources.add(template);
        }
    }
    String alias = alias(false);
    String ALIAS = alias(true);
    String packageX1 = packageName + ".";
    String packageX2 = packageName + ";";
    List<String> sourceLines;
    List<String> targetLines = new ArrayList<>();
    for (File file : files) {
        source = file.getPath();
        java = StringUtils.endsWithIgnoreCase(source, ".java");
        target = source.replace(projectFolderPath, velocityTemplatesTargetFolderPath);
        targetParent = StringUtils.substringBeforeLast(target, FS);
        targetLines.clear();
        FilUtils.mkdirs(targetParent);
        smallSource = new SmallFile(source);
        sourceLines = smallSource.read();
        check(smallSource);
        if (smallSource.isNotEmpty()) {
            for (String line : sourceLines) {
                if (StringUtils.isNotBlank(line)) {
                    line = line.replace(group, "${groupId}");
                    line = line.replace(artifact, "${artifactId}");
                    line = line.replace(project + "ap", alias + "ap");
                    line = line.replace(PROJECT + "AP", ALIAS + "AP");
                    line = line.replace("package " + packageX1, "package ${package}." + packageX1);
                    line = line.replace("package " + packageX2, "package ${package}." + packageX2);
                    for (String name : classes) {
                        if (name.startsWith(packageX1)) {
                            line = line.replace(name, "${package}." + name);
                        }
                    }
                }
                targetLines.add(line);
            }
        }
        cs = java ? StandardCharsets.UTF_8 : WINDOWS_CHARSET;
        if (write(target, targetLines, cs)) {
            textFilesCopied++;
        }
    }
    return true;
}

From source file:de.xirp.ui.widgets.dialogs.ProfileDialog.java

/**
 * Sets the entered values to the/*from   w  w w.java2s .co  m*/
 * {@link de.xirp.profile.Profile profile}.
 * 
 * @see de.xirp.profile.Profile
 */
private void putValues() {
    profile.setName(name.getText());
    for (String name : bots.getItems()) {
        profile.addRobotFileName(StringUtils.substringBeforeLast(name, ".")); //$NON-NLS-1$
    }
    profile.setComplete(isCompleted());
}

From source file:info.magnolia.module.admininterface.AdminTreeMVCHandler.java

/**
 * @param path/*from   w  ww.  jav  a 2s. c o  m*/
 * @param recursive
 */
public void activateNode(String path, boolean recursive) throws ExchangeException, RepositoryException {

    String parentPath = StringUtils.substringBeforeLast(path, "/");
    if (StringUtils.isEmpty(parentPath)) {
        parentPath = "/";
    }

    Syndicator syndicator = getActivationSyndicator(path);
    if (recursive) {
        activateNodeRecursive(syndicator, parentPath, path);
    } else {
        syndicator.activate(parentPath, path);
    }
}

From source file:de.fme.topx.component.TopXSearchComponent.java

/**
 * reads the display path, the sitename and sitepath for correct linking in
 * ui/*from w w  w .j  a  v a2s.  c o m*/
 * 
 * @param nodeRef
 * @param newNode
 */
private void setPathAttributes(NodeRef nodeRef, Node newNode) {
    Path path = getNodeService().getPath(nodeRef);
    String displayPath = path.toDisplayPath(nodeService, permissionService);
    newNode.setDisplayPath(displayPath);
    String pathCutted = StringUtils.substringAfter(StringUtils.substringAfter(displayPath, "/"), "/");
    if (pathCutted.startsWith("Sites/")) {
        String siteName = StringUtils.substringBetween(displayPath, "Sites/", "/");
        newNode.setSiteName(siteName);
        String documentPath = StringUtils.substringAfter(displayPath,
                "Sites/" + siteName + "/documentLibrary/");
        String sitePath = StringUtils.substringBeforeLast(documentPath, "/");
        newNode.setSitePath(sitePath);
    }

    String parentNodeRef = getPrimaryParent(nodeRef);
    newNode.setParentNodeRef(parentNodeRef);
}