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

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

Introduction

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

Prototype

public static int countMatches(String str, String sub) 

Source Link

Document

Counts how many times the substring appears in the larger String.

Usage

From source file:org.openvpms.web.echo.text.TitledTextArea.java

/**
 * Determines the no. of lines of text to display.
 *
 * @param text the text. May be <tt>null</tt>
 * @return the lines to display/*  ww  w . jav a 2 s. com*/
 */
protected int getLinesToDisplay(String text) {
    int lines = minHeight;
    if (text != null) {
        lines = StringUtils.countMatches(text, "\n");
        if (lines > maxHeight) {
            lines = maxHeight;
        } else if (lines < minHeight) {
            lines = minHeight;
        }
    }
    return lines;
}

From source file:org.overlord.sramp.server.mvn.services.MavenMetaDataBuilder.java

/**
 * Builds the Maven Metadata based on a URL, which format is supposed to be /groupId.../artifactId/versionId/filename
 *
 * @param url/*from  w  w w.j a  v  a  2s  .  c  o  m*/
 *            the url
 * @return the maven meta data
 */
public static MavenMetaData build(String url) {
    String groupId = ""; //$NON-NLS-1$
    String artifactId = ""; //$NON-NLS-1$
    String version = ""; //$NON-NLS-1$
    String type = ""; //$NON-NLS-1$
    String classifier = ""; //$NON-NLS-1$
    String parentType = ""; //$NON-NLS-1$
    String fileName = ""; //$NON-NLS-1$
    String snapshotId = ""; //$NON-NLS-1$

    if (url.startsWith("/")) { //$NON-NLS-1$
        url = url.substring(1);
    }

    int lastPathSegmentIdx = url.lastIndexOf('/');
    String lastPathSegment = null;
    if (lastPathSegmentIdx != -1) {
        lastPathSegment = url.substring(lastPathSegmentIdx + 1);
    }
    //This means the last part contains a file with a file extension
    if (lastPathSegment != null && lastPathSegment.indexOf('.') != -1) {
        String[] tokens = url.split("/"); //$NON-NLS-1$
        if (tokens != null && tokens.length > 0) {
            //There could be files either in the artifact folder(maven-metadata.xml) or in the version folder.
            //Then for that it is mandatory to have more than 2 tokens. At least one for groupId, one for artifactId and one for fileName
            if (tokens.length >= 3) {

                if (lastPathSegment.contains("maven-metadata.xml")) { //$NON-NLS-1$
                    String possibleGroupId = ""; //$NON-NLS-1$

                    boolean isGroup = false;
                    // There are checked the different possibilities about
                    // groupsIds.
                    // It is checked in s-ramp

                    // It is checked from the end to the beginning to avoid
                    // the case that an artifact is contained in a groupId
                    // that contains subgroups
                    // First store the biggest possibility, not until the
                    // last token. The last would be the filename and the
                    // previous could be the artifactId
                    for (int i = 0; i < tokens.length - 2; i++) {
                        possibleGroupId += tokens[i];
                        if (i < tokens.length - 1) {
                            possibleGroupId += "."; //$NON-NLS-1$
                        }

                    }
                    // For example for org.overlord.test the number of
                    // checks is going to be 3
                    int iterations = StringUtils.countMatches(possibleGroupId, ".") + 1; //$NON-NLS-1$
                    for (int i = 0; i < iterations; i++) {
                        if (isGroupId(possibleGroupId)) {
                            isGroup = true;
                            break;
                        }
                        if (possibleGroupId.lastIndexOf(".") != -1) { //$NON-NLS-1$
                            possibleGroupId = possibleGroupId.substring(0, possibleGroupId.lastIndexOf(".")); //$NON-NLS-1$
                        }
                    }
                    // If there is a group in s-ramp that matches the
                    // request
                    if (isGroup) {
                        // This means there is an existing groupId in s-ramp
                        groupId = possibleGroupId;
                        String lastGroupIdToken = ""; //$NON-NLS-1$
                        if (groupId.contains(".")) { //$NON-NLS-1$
                            lastGroupIdToken = groupId.substring(groupId.lastIndexOf(".") + 1); //$NON-NLS-1$
                        } else {
                            lastGroupIdToken = groupId;
                        }
                        int groupIdIndex = 0;
                        for (groupIdIndex = 0; groupIdIndex < tokens.length; groupIdIndex++) {
                            if (tokens[groupIdIndex].equals(lastGroupIdToken)) {
                                break;
                            }
                        }

                        if (tokens.length == (groupIdIndex + 3)) {
                            // This means it comes a file under the artifact
                            // id.
                            // Example:
                            // /org/test/artifact/maven-metadata.xml
                            // where org/test is the groupId
                            artifactId = tokens[groupIdIndex + 1];
                            if (!existVersion(groupId, tokens[groupIdIndex + 2])) {
                                fileName = tokens[groupIdIndex + 2];
                            }
                        } else if (tokens.length == (groupIdIndex + 4)) {
                            // This means it comes a file under the version
                            // id.
                            // Example:
                            // /org/test/artifact/0.0.1/maven-metadata.xml
                            // where
                            // org/test is the groupId abd
                            // artifactId=artifact
                            // and version=0.0.1
                            artifactId = tokens[groupIdIndex + 1];
                            version = tokens[groupIdIndex + 2];
                            fileName = tokens[groupIdIndex + 3];
                        }

                    } else {
                        // If the groupId can not be found in s-ramp then,
                        // it is built a default metadata.
                        // Remember that this method is called, both listing
                        // content and creating new content
                        version = tokens[tokens.length - 2];
                        for (int i = 0; i < tokens.length - 2; i++) {
                            if (i < tokens.length - 3) {
                                if (i != 0) {
                                    groupId += "."; //$NON-NLS-1$
                                }
                                groupId += tokens[i];
                            } else {
                                artifactId = tokens[i];
                            }
                        }
                        fileName = tokens[tokens.length - 1];

                    }
                } else {
                    //If the groupId can not be found in s-ramp then, it is built a default metadata.
                    //Remember that this method is called, both listing content and creating new content
                    version = tokens[tokens.length - 2];
                    for (int i = 0; i < tokens.length - 2; i++) {
                        if (i < tokens.length - 3) {
                            if (i != 0) {
                                groupId += "."; //$NON-NLS-1$
                            }
                            groupId += tokens[i];
                        } else {
                            artifactId = tokens[i];
                        }
                    }
                    fileName = tokens[tokens.length - 1];

                }

                //It is parsed the fileName to get information about the classifier
                if (StringUtils.isNotBlank(fileName)) {
                    type = fileName.substring(fileName.lastIndexOf(".") + 1); //$NON-NLS-1$

                    classifier = ""; //$NON-NLS-1$
                    //We know the information about classifier comes in files that are under the version folder

                    if (StringUtils.isNotBlank(version)) {
                        String versionParsed = ""; //$NON-NLS-1$
                        //When the version contains a -SNAPSHOT, then the filename is artifactId-versionWithoutSNAPSHOT
                        //The snapshot keyword is substitute by a timestamp-counter
                        if (version.contains("-SNAPSHOT")) { //$NON-NLS-1$
                            versionParsed = version.substring(0, version.lastIndexOf("-SNAPSHOT")); //$NON-NLS-1$
                        } else {
                            versionParsed = version;
                        }
                        if (!fileName.contains(version)
                                && fileName.startsWith(artifactId + "-" + versionParsed)) { //$NON-NLS-1$
                            String without_extension = fileName.substring(0, fileName.lastIndexOf(".")); //$NON-NLS-1$
                            if (StringUtils.countMatches(without_extension, "-") >= 2) { //$NON-NLS-1$
                                int versionIdx = without_extension.indexOf(versionParsed);
                                int versionLen = versionParsed.length();
                                int classifierIdx = versionIdx + versionLen;
                                if (classifierIdx < without_extension.length()) {
                                    //If it is an snapshop
                                    if (version.contains("SNAPSHOT") && !version.equals(versionParsed)) { //$NON-NLS-1$
                                        String rest = without_extension.substring(classifierIdx + 1);
                                        //All the tokens from the clasifierIdx to the end are splitted by '-'
                                        String[] tokens_file_name = rest.split("-"); //$NON-NLS-1$
                                        if (tokens_file_name.length > 0) {
                                            //It is expected a timestamp
                                            if (isMavenTimeStamp(tokens_file_name[0])) {
                                                if (tokens_file_name.length > 1) {
                                                    try {
                                                        //The it is expected maybe a counter
                                                        String[] counter = StringUtils
                                                                .split(tokens_file_name[1], "."); //$NON-NLS-1$
                                                        Integer.parseInt(counter[0]);
                                                        snapshotId = tokens_file_name[0] + "-" + counter[0]; //$NON-NLS-1$
                                                        if (tokens_file_name.length > 2) {
                                                            classifier = tokens_file_name[2];
                                                        } else if (counter.length == 2) {
                                                            parentType = counter[1];
                                                        }
                                                    } catch (NumberFormatException nfe) {
                                                        //In case the parseInt throw an exception it means there is not any counter, and then the classifier goes to the end of the filename
                                                        classifier = rest
                                                                .substring(tokens_file_name[0].length() + 1);
                                                    }
                                                }
                                            } else {
                                                classifier = rest;
                                            }
                                        }
                                    } else {
                                        //This means the version is not snapshot or there is no timestamp in the file
                                        classifier = without_extension.substring(classifierIdx);
                                    }

                                    if (StringUtils.isNotBlank(classifier)) {
                                        if (classifier.contains(".")) {//$NON-NLS-1$
                                            if (classifier.startsWith(".")) { //$NON-NLS-1$
                                                parentType = classifier.substring(1);
                                                classifier = ""; //$NON-NLS-1$
                                            } else {
                                                parentType = classifier
                                                        .substring(classifier.lastIndexOf(".") + 1); //$NON-NLS-1$
                                                classifier = classifier.substring(0,
                                                        classifier.lastIndexOf(".")); //$NON-NLS-1$
                                            }
                                        }
                                    }
                                }
                            }
                        }

                    }

                }

            }
        }
    }
    //If the type, belongs to the MavenFileExtensionEnum, it means than it is sha1 or md5.
    if (MavenFileExtensionEnum.value(type) != null) {
        //Then the parent type has to be filled. We know that the parent type can be taken from the filename
        String without_ext = fileName.substring(0, fileName.lastIndexOf("." + type)); //$NON-NLS-1$
        if (without_ext.contains(".")) { //$NON-NLS-1$
            parentType = without_ext.substring(without_ext.lastIndexOf(".") + 1); //$NON-NLS-1$
        }
    }

    return new MavenMetaData(groupId, artifactId, version, type, classifier, snapshotId, parentType, fileName);

}

From source file:org.overlord.sramp.server.services.mvn.MavenMetaDataBuilder.java

/**
 * Builds the Maven Metadata based on a URL, which format is supposed to be /groupId.../artifactId/versionId/filename
 *
 * @param url/*  w  w  w. j ava2 s  .c om*/
 *            the url
 * @return the maven meta data
 */
public static MavenMetaData build(String url) {
    String groupId = ""; //$NON-NLS-1$
    String artifactId = ""; //$NON-NLS-1$
    String version = ""; //$NON-NLS-1$
    String type = ""; //$NON-NLS-1$
    String classifier = ""; //$NON-NLS-1$
    String parentType = ""; //$NON-NLS-1$
    String fileName = ""; //$NON-NLS-1$
    String snapshotId = ""; //$NON-NLS-1$

    if (url.startsWith("/")) { //$NON-NLS-1$
        url = url.substring(1);
    }

    int lastPathSegmentIdx = url.lastIndexOf('/');
    String lastPathSegment = null;
    if (lastPathSegmentIdx != -1) {
        lastPathSegment = url.substring(lastPathSegmentIdx + 1);
    }
    //This means the last part contains a file with a file extension
    if (lastPathSegment != null && lastPathSegment.indexOf('.') != -1) {
        String[] tokens = url.split("/"); //$NON-NLS-1$
        if (tokens != null && tokens.length > 0) {
            //There could be files either in the artifact folder(maven-metadata.xml) or in the version folder.
            //Then for that it is mandatory to have more than 2 tokens. At least one for groupId, one for artifactId and one for fileName
            if (tokens.length >= 3) {

                if (lastPathSegment.contains("maven-metadata.xml")) { //$NON-NLS-1$
                    String possibleGroupId = ""; //$NON-NLS-1$

                    boolean isGroup = false;
                    // There are checked the different possibilities about
                    // groupsIds.
                    // It is checked in s-ramp

                    // It is checked from the end to the beginning to avoid
                    // the case that an artifact is contained in a groupId
                    // that contains subgroups
                    // First store the biggest possibility, not until the
                    // last token. The last would be the filename and the
                    // previous could be the artifactId
                    for (int i = 0; i < tokens.length - 2; i++) {
                        possibleGroupId += tokens[i];
                        if (i < tokens.length - 1) {
                            possibleGroupId += "."; //$NON-NLS-1$
                        }

                    }
                    // For example for org.overlord.test the number of
                    // checks is going to be 3
                    int iterations = StringUtils.countMatches(possibleGroupId, ".") + 1; //$NON-NLS-1$
                    for (int i = 0; i < iterations; i++) {
                        if (isGroupId(possibleGroupId)) {
                            isGroup = true;
                            break;
                        }
                        if (possibleGroupId.lastIndexOf(".") != -1) { //$NON-NLS-1$
                            possibleGroupId = possibleGroupId.substring(0, possibleGroupId.lastIndexOf(".")); //$NON-NLS-1$
                        }
                    }
                    // If there is a group in s-ramp that matches the
                    // request
                    if (isGroup) {
                        // This means there is an existing groupId in s-ramp
                        groupId = possibleGroupId;
                        String lastGroupIdToken = ""; //$NON-NLS-1$
                        if (groupId.contains(".")) { //$NON-NLS-1$
                            lastGroupIdToken = groupId.substring(groupId.lastIndexOf(".") + 1); //$NON-NLS-1$
                        } else {
                            lastGroupIdToken = groupId;
                        }
                        int groupIdIndex = 0;
                        for (groupIdIndex = 0; groupIdIndex < tokens.length; groupIdIndex++) {
                            if (tokens[groupIdIndex].equals(lastGroupIdToken)) {
                                break;
                            }
                        }

                        if (tokens.length == (groupIdIndex + 3)) {
                            // This means it comes a file under the artifact
                            // id.
                            // Example:
                            // /org/test/artifact/maven-metadata.xml
                            // where org/test is the groupId
                            artifactId = tokens[groupIdIndex + 1];
                            if (!existVersion(groupId, tokens[groupIdIndex + 2])) {
                                fileName = tokens[groupIdIndex + 2];
                            }
                        } else if (tokens.length == (groupIdIndex + 4)) {
                            // This means it comes a file under the version
                            // id.
                            // Example:
                            // /org/test/artifact/0.0.1/maven-metadata.xml
                            // where
                            // org/test is the groupId abd
                            // artifactId=artifact
                            // and version=0.0.1
                            artifactId = tokens[groupIdIndex + 1];
                            version = tokens[groupIdIndex + 2];
                            fileName = tokens[groupIdIndex + 3];
                        }

                    } else {
                        // If the groupId can not be found in s-ramp then,
                        // it is built a default metadata.
                        // Remember that this method is called, both listing
                        // content and creating new content
                        version = tokens[tokens.length - 2];
                        for (int i = 0; i < tokens.length - 2; i++) {
                            if (i < tokens.length - 3) {
                                if (i != 0) {
                                    groupId += "."; //$NON-NLS-1$
                                }
                                groupId += tokens[i];
                            } else {
                                artifactId = tokens[i];
                            }
                        }
                        fileName = tokens[tokens.length - 1];

                    }
                } else {
                    //If the groupId can not be found in s-ramp then, it is built a default metadata.
                    //Remember that this method is called, both listing content and creating new content
                    version = tokens[tokens.length - 2];
                    for (int i = 0; i < tokens.length - 2; i++) {
                        if (i < tokens.length - 3) {
                            if (i != 0) {
                                groupId += "."; //$NON-NLS-1$
                            }
                            groupId += tokens[i];
                        } else {
                            artifactId = tokens[i];
                        }
                    }
                    fileName = tokens[tokens.length - 1];

                }

                //It is parsed the fileName to get information about the classifier
                if (StringUtils.isNotBlank(fileName)) {
                    type = fileName.substring(fileName.lastIndexOf(".") + 1); //$NON-NLS-1$

                    classifier = ""; //$NON-NLS-1$
                    //We know the information about classifier comes in files that are under the version folder

                    if (StringUtils.isNotBlank(version)) {
                        String versionParsed = ""; //$NON-NLS-1$
                        //When the version contains a -SNAPSHOT, then the filename is artifactId-versionWithoutSNAPSHOT
                        //The snapshot keyword is substitute by a timestamp-counter
                        if (version.contains("-SNAPSHOT")) { //$NON-NLS-1$
                            versionParsed = version.substring(0, version.lastIndexOf("-SNAPSHOT")); //$NON-NLS-1$
                        } else {
                            versionParsed = version;
                        }
                        if (fileName.startsWith(artifactId + "-" + versionParsed)) { //$NON-NLS-1$
                            String without_extension = fileName.substring(0, fileName.lastIndexOf(".")); //$NON-NLS-1$
                            if (StringUtils.countMatches(without_extension, "-") >= 2) { //$NON-NLS-1$
                                int versionIdx = without_extension.indexOf(versionParsed);
                                int versionLen = versionParsed.length();
                                int classifierIdx = versionIdx + versionLen;
                                if (classifierIdx < without_extension.length()) {
                                    //If it is an snapshop
                                    if (version.contains("SNAPSHOT") && !version.equals(versionParsed)) { //$NON-NLS-1$
                                        String rest = without_extension.substring(classifierIdx + 1);
                                        //All the tokens from the clasifierIdx to the end are splitted by '-'
                                        String[] tokens_file_name = rest.split("-"); //$NON-NLS-1$
                                        if (tokens_file_name.length > 0) {
                                            //It is expected a timestamp
                                            if (isMavenTimeStamp(tokens_file_name[0])) {
                                                if (tokens_file_name.length > 1) {
                                                    try {
                                                        //The it is expected maybe a counter
                                                        String[] counter = StringUtils
                                                                .split(tokens_file_name[1], "."); //$NON-NLS-1$
                                                        Integer.parseInt(counter[0]);
                                                        snapshotId = tokens_file_name[0] + "-" + counter[0]; //$NON-NLS-1$
                                                        if (tokens_file_name.length > 2) {
                                                            classifier = tokens_file_name[2];
                                                        } else if (counter.length == 2) {
                                                            parentType = counter[1];
                                                        }
                                                    } catch (NumberFormatException nfe) {
                                                        //In case the parseInt throw an exception it means there is not any counter, and then the classifier goes to the end of the filename
                                                        classifier = rest
                                                                .substring(tokens_file_name[0].length() + 1);
                                                    }
                                                }
                                            } else {
                                                classifier = rest;
                                            }
                                        }
                                    } else {
                                        //This means the version is not snapshot or there is no timestamp in the file
                                        classifier = without_extension.substring(classifierIdx);
                                    }

                                    if (StringUtils.isNotBlank(classifier)) {
                                        if (classifier.contains(".")) {//$NON-NLS-1$
                                            if (classifier.startsWith(".")) { //$NON-NLS-1$
                                                parentType = classifier.substring(1);
                                                classifier = ""; //$NON-NLS-1$
                                            } else {
                                                parentType = classifier
                                                        .substring(classifier.lastIndexOf(".") + 1); //$NON-NLS-1$
                                                classifier = classifier.substring(0,
                                                        classifier.lastIndexOf(".")); //$NON-NLS-1$
                                            }
                                        }
                                    }
                                }
                            }
                        }

                    }

                }

            }
        }
    }
    //If the type, belongs to the MavenFileExtensionEnum, it means than it is sha1 or md5.
    if (MavenFileExtensionEnum.value(type) != null) {
        //Then the parent type has to be filled. We know that the parent type can be taken from the filename
        String without_ext = fileName.substring(0, fileName.lastIndexOf("." + type)); //$NON-NLS-1$
        if (without_ext.contains(".")) { //$NON-NLS-1$
            parentType = without_ext.substring(without_ext.lastIndexOf(".") + 1); //$NON-NLS-1$
        }
    }

    return new MavenMetaData(groupId, artifactId, version, type, classifier, snapshotId, parentType, fileName);

}

From source file:org.ow2.clif.jenkins.parser.clif.ClifParser.java

protected static String extractTestPlanName(String clifTestPlanName) {
    int nbUnderScore = StringUtils.countMatches(clifTestPlanName, "_");
    if (nbUnderScore < 2) {
        return clifTestPlanName;
    }//from  ww w. j ava2s. c o  m
    int dateUnderScore = lastOrdinalIndexOf(clifTestPlanName, "_", 2);
    return clifTestPlanName.substring(0, dateUnderScore);
}

From source file:org.ow2.clif.jenkins.parser.clif.ParsingContext.java

protected String getTestPlanShortName() {
    int nbUnderScore = StringUtils.countMatches(this.test.getName(), "_");
    if (nbUnderScore < 2) {
        return this.test.getName();
    }//from w  w  w. j  a v a  2 s  .  c o m
    int dateUnderScore = lastOrdinalIndexOf(this.test.getName(), "_", 2);
    return this.test.getName().substring(0, dateUnderScore);
}

From source file:org.pentaho.di.core.Const.java

/**
 * Split the given string using the given delimiter and enclosure strings.
 *
 * The delimiter and enclosures are not regular expressions (regexes); rather they are literal strings that will be
 * quoted so as not to be treated like regexes.
 *
 * This method expects that the data contains an even number of enclosure strings in the input; otherwise the results
 * are undefined//  w  w  w . jav a  2  s.  co m
 *
 * @param stringToSplit
 *          the String to split
 * @param delimiter
 *          the delimiter string
 * @param enclosure
 *          the enclosure string
 * @return an array of strings split on the delimiter (ignoring those in enclosures), or null if the string to split
 *         is null.
 */
public static String[] splitString(String stringToSplit, String delimiter, String enclosure) {

    ArrayList<String> splitList = null;

    // Handle "bad input" cases
    if (stringToSplit == null) {
        return null;
    }
    if (delimiter == null) {
        return (new String[] { stringToSplit });
    }

    // Split the string on the delimiter, we'll build the "real" results from the partial results
    String[] delimiterSplit = stringToSplit.split(Pattern.quote(delimiter));

    // At this point, if the enclosure is null or empty, we will return the delimiter split
    if (isEmpty(enclosure)) {
        return delimiterSplit;
    }

    // Keep track of partial splits and concatenate them into a legit split
    StringBuffer concatSplit = null;

    if (delimiterSplit != null && delimiterSplit.length > 0) {

        // We'll have at least one result so create the result list object
        splitList = new ArrayList<String>();

        // Proceed through the partial splits, concatenating if the splits are within the enclosure
        for (String currentSplit : delimiterSplit) {
            if (!currentSplit.contains(enclosure)) {

                // If we are currently concatenating a split, we are inside an enclosure. Since this
                // split doesn't contain an enclosure, we can concatenate it (with a delimiter in front).
                // If we're not concatenating, the split is fine so add it to the result list.
                if (concatSplit != null) {
                    concatSplit.append(delimiter);
                    concatSplit.append(currentSplit);
                } else {
                    splitList.add(currentSplit);
                }
            } else {
                // Find number of enclosures in the split, and whether that number is odd or even.
                int numEnclosures = StringUtils.countMatches(currentSplit, enclosure);
                boolean oddNumberOfEnclosures = (numEnclosures % 2 != 0);
                boolean addSplit = false;

                // This split contains an enclosure, so either start or finish concatenating
                if (concatSplit == null) {
                    concatSplit = new StringBuffer(currentSplit); // start concatenation
                    addSplit = !oddNumberOfEnclosures;
                } else {
                    // Check to make sure a new enclosure hasn't started within this split. This method expects
                    // that there are no non-delimiter characters between a delimiter and a starting enclosure.

                    // At this point in the code, the split shouldn't start with the enclosure, so add a delimiter
                    concatSplit.append(delimiter);

                    // Add the current split to the concatenated split
                    concatSplit.append(currentSplit);

                    // If the number of enclosures is odd, the enclosure is closed so add the split to the list
                    // and reset the "concatSplit" buffer. Otherwise continue
                    addSplit = oddNumberOfEnclosures;
                }
                if (addSplit) {
                    splitList.add(concatSplit.toString());
                    concatSplit = null;
                    addSplit = false;
                }
            }
        }
    }

    // Return list as array
    return splitList.toArray(new String[splitList.size()]);
}

From source file:org.pentaho.di.repository.pur.PurRepository.java

@Override
public RepositoryDirectoryInterface loadRepositoryDirectoryTree(String path, String filter, int depth,
        boolean showHidden, boolean includeEmptyFolder, boolean includeAcls) throws KettleException {

    // First check for possibility of speedy algorithm
    if (filter == null && "/".equals(path) && includeEmptyFolder) {
        return initRepositoryDirectoryTree(loadRepositoryFileTreeFolders("/", -1, includeAcls, showHidden));
    }//from  ww w  . j  av  a 2 s  . c  o  m
    //load count levels from root to destination path to load folder tree
    int fromRootToDest = StringUtils.countMatches(path, "/");
    //create new root directory "/"
    RepositoryDirectory dir = new RepositoryDirectory();
    //fetch folder tree from root "/" to destination path for populate folder
    RepositoryFileTree rootDirTree = loadRepositoryFileTree("/", "*", fromRootToDest, showHidden, includeAcls,
            FILES_TYPE_FILTER.FOLDERS);
    //populate directory by folder tree
    fillRepositoryDirectoryFromTree(dir, rootDirTree);

    RepositoryDirectoryInterface destinationDir = dir.findDirectory(path);
    //search for goal path and filter
    RepositoryFileTree repoTree = loadRepositoryFileTree(path, filter, depth, showHidden, includeAcls,
            FILES_TYPE_FILTER.FILES_FOLDERS);
    //populate the directory with founded files and subdirectories with files
    fillRepositoryDirectoryFromTree(destinationDir, repoTree);

    if (includeEmptyFolder) {
        RepositoryDirectoryInterface folders = initRepositoryDirectoryTree(
                loadRepositoryFileTree(path, null, depth, showHidden, includeAcls, FILES_TYPE_FILTER.FOLDERS));
        return copyFrom(folders, destinationDir);
    } else {
        return destinationDir;
    }
}

From source file:org.pentaho.di.trans.steps.combinationlookup.CombinationLookupIT.java

public void testUseDefaultSchemaName() throws Exception {
    String schemaName = "";
    String tableName = "tableName";
    String schemaTable = "default.tableName";
    String technicalKeyField = "technicalKeyField";

    DatabaseMeta databaseMeta = spy(new DatabaseMeta(databasesXML[0]) {
        @Override//from w w  w. j a  va  2s. co m
        public String getFieldDefinition(ValueMetaInterface v, String tk, String pk, boolean use_autoinc) {
            return "someValue";
        }
    });
    when(databaseMeta.getQuotedSchemaTableCombination(schemaName, tableName)).thenReturn(schemaTable);

    CombinationLookupMeta clm = new CombinationLookupMeta();
    clm.setTechnicalKeyField(technicalKeyField);
    clm.setKeyLookup(new String[] { "keyLookup1", "keyLookup2" });
    clm.setDatabaseMeta(databaseMeta);
    clm.setTablename(tableName);
    clm.setSchemaName(schemaName);

    StepMeta stepMeta = mock(StepMeta.class);

    RowMetaInterface rowMetaInterface = mock(RowMetaInterface.class);
    when(rowMetaInterface.size()).thenReturn(1);

    Repository repository = mock(Repository.class);
    IMetaStore metaStore = mock(IMetaStore.class);

    SQLStatement sqlStatement = clm.getSQLStatements(new TransMeta(), stepMeta, rowMetaInterface, repository,
            metaStore);

    String sql = sqlStatement.getSQL();
    Assert.assertTrue(StringUtils.countMatches(sql, schemaTable) == 3);
}

From source file:org.pentaho.di.trans.steps.dimensionlookup.DimensionLookupMetaTest.java

@Test
public void testUseDefaultSchemaName() throws Exception {
    KettleEnvironment.init();/*from   ww  w .  ja  va  2  s . c o m*/

    String schemaName = "";
    String tableName = "tableName";
    String schemaTable = "default.tableName";
    String keyField = "keyField";

    DatabaseMeta databaseMeta = spy(new DatabaseMeta(databaseXML) {
        @Override
        public String getFieldDefinition(ValueMetaInterface v, String tk, String pk, boolean use_autoinc) {
            return "someValue";
        }
    });
    when(databaseMeta.getQuotedSchemaTableCombination(schemaName, tableName)).thenReturn(schemaTable);

    DimensionLookupMeta dlm = new DimensionLookupMeta();
    dlm.setUpdate(true);
    dlm.setDatabaseMeta(databaseMeta);
    dlm.setTableName(tableName);
    dlm.setSchemaName(schemaName);
    dlm.setKeyLookup(new String[] { "keyLookup1", "keyLookup2" });
    dlm.setKeyStream(new String[] { "keyStream1", "keyStream2" });
    dlm.setFieldLookup(new String[] { "fieldLookup1", "fieldLookup2" });
    dlm.setFieldStream(new String[] { "FieldStream1", "FieldStream2" });
    dlm.setFieldUpdate(new int[] { 1, 2 });
    dlm.setKeyField(keyField);

    StepMeta stepMeta = mock(StepMeta.class);

    RowMetaInterface rowMetaInterface = mock(RowMetaInterface.class);
    when(rowMetaInterface.size()).thenReturn(1);

    Repository repository = mock(Repository.class);
    IMetaStore metaStore = mock(IMetaStore.class);

    SQLStatement sqlStatement = dlm.getSQLStatements(new TransMeta(), stepMeta, rowMetaInterface, repository,
            metaStore);

    String sql = sqlStatement.getSQL();
    assertEquals(3, StringUtils.countMatches(sql, schemaTable));
}

From source file:org.pentaho.di.trans.steps.update.UpdateMetaTest.java

@Test
public void testUseDefaultSchemaName() throws Exception {
    String schemaName = "";
    String tableName = "tableName";
    String schemaTable = "default.tableName";

    DatabaseMeta databaseMeta = spy(new DatabaseMeta(databaseXML) {
        @Override//from ww w.ja  v  a 2s .  com
        public String getFieldDefinition(ValueMetaInterface v, String tk, String pk, boolean use_autoinc) {
            return "someValue";
        }
    });
    when(databaseMeta.getQuotedSchemaTableCombination(schemaName, tableName)).thenReturn(schemaTable);

    ValueMetaInterface valueMeta = mock(ValueMetaInterface.class);
    when(valueMeta.clone()).thenReturn(mock(ValueMetaInterface.class));
    RowMetaInterface rowMetaInterface = mock(RowMetaInterface.class);
    when(rowMetaInterface.size()).thenReturn(1);
    when(rowMetaInterface.searchValueMeta(anyString())).thenReturn(valueMeta);

    UpdateMeta updateMeta = new UpdateMeta();
    updateMeta.setDatabaseMeta(databaseMeta);
    updateMeta.setTableName(tableName);
    updateMeta.setSchemaName(schemaName);
    updateMeta.setKeyLookup(new String[] { "KeyLookup1", "KeyLookup2" });
    updateMeta.setKeyStream(new String[] { "KeyStream1", "KeyStream2" });
    updateMeta.setUpdateLookup(new String[] { "updateLookup1", "updateLookup2" });
    updateMeta.setUpdateStream(new String[] { "UpdateStream1", "UpdateStream2" });

    SQLStatement sqlStatement = updateMeta.getSQLStatements(new TransMeta(), mock(StepMeta.class),
            rowMetaInterface, mock(Repository.class), mock(IMetaStore.class));
    String sql = sqlStatement.getSQL();

    Assert.assertTrue(StringUtils.countMatches(sql, schemaTable) == 2);
}