Example usage for org.apache.commons.lang3 StringUtils stripStart

List of usage examples for org.apache.commons.lang3 StringUtils stripStart

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils stripStart.

Prototype

public static String stripStart(final String str, final String stripChars) 

Source Link

Document

Strips any of a set of characters from the start of a String.

A null input String returns null .

Usage

From source file:org.docx4j.toc.TocEntry.java

public void setEntryValue(P sourceP) {

    /*/*from w  ww . j  a  va2 s.  c  o m*/
      Certain Run formatting on entries is re-used, including:
         font face
         italic
         text highlight
         hidden (honoured)
         small caps
      but not
         font size
         font color
         underline
     */

    // Step 1: create a clone of the P
    P clonedP = (P) XmlUtils.deepCopy(sourceP);

    // Step 2: make a List<R>, comprising the w:r/w:t contents,
    // with styles resolved
    List<Object> runsFound = TocHelper.getAllElementsFromObject(clonedP, R.class);

    R lastRun = null;
    for (Object o : runsFound) {

        R r = (R) o;
        List<Object> textsFound = TocHelper.getAllElementsFromObject(r, Text.class);
        if (textsFound.size() > 0) {

            R newR = new R();

            if (r.getRPr() == null) {
                newR.setRPr(wmlObjectFactory.createRPr());
            } else {
                // Resolve the formatting
                newR.setRPr(getEffectiveRPr(r.getRPr()));
                //               newR.setRPr(
                //                     r.getRPr());

                // Step 3: strip/filter unwanted run formatting
                nullify(newR.getRPr());

                if (newR.getRPr() == null) {
                    newR.setRPr(wmlObjectFactory.createRPr());
                }
            }

            // apply hyperlink if appropriate
            if (hyperlink) {
                RStyle rstyle = wmlObjectFactory.createRStyle();
                newR.getRPr().setRStyle(rstyle);
                rstyle.setVal(HYPERLINK);
            }

            newR.getContent().addAll(textsFound);
            entryValues.add(newR);
            lastRun = newR;
        }

    }

    // drop any trailing space off last run (Word does this)
    if (lastRun != null) {

        int size = lastRun.getContent().size();
        if (size > 0) {
            Text lastText = (Text) lastRun.getContent().get(size - 1);
            String val = lastText.getValue();
            if (val != null) {
                lastText.setValue(StringUtils.stripEnd(val, null));
            }
        }
    }

    // drop leading space off the first run
    R firstRun = null;
    if (runsFound.size() > 0) {
        firstRun = (R) runsFound.get(0);
        List<Object> textsFound = TocHelper.getAllElementsFromObject(firstRun, Text.class);
        int size = textsFound.size();
        if (size > 0) {
            Text firstText = (Text) textsFound.get(0);
            String val = firstText.getValue();
            if (val != null) {
                firstText.setValue(StringUtils.stripStart(val, null));
            }
        }
    }

}

From source file:org.eclipse.jdt.ls.core.internal.contentassist.JavadocCompletionProposal.java

private String prepareTemplate(String text, String lineDelimiter, boolean addGap) {
    boolean endWithLineDelimiter = text.endsWith(lineDelimiter);
    String[] lines = text.split(lineDelimiter);
    StringBuilder buf = new StringBuilder();
    for (int i = 0; i < lines.length; i++) {
        String line = lines[i];/*from   w ww .  j  av a2s  .c o m*/
        if (addGap) {
            String stripped = StringUtils.stripStart(line, WHITESPACES);
            if (stripped.startsWith(ASTERISK)) {
                if (!stripped.equals(ASTERISK)) {
                    int index = line.indexOf(ASTERISK);
                    buf.append(line.substring(0, index + 1));
                    buf.append(" ${0}");
                    buf.append(lineDelimiter);
                }
                addGap = false;
            }
        }
        buf.append(StringUtils.stripEnd(line, WHITESPACES));
        if (i < lines.length - 1 || endWithLineDelimiter) {
            buf.append(lineDelimiter);
        }
    }
    return buf.toString();
}

From source file:org.esigate.util.UriUtils.java

/**
 * Concatenates 2 {@link URI} by taking the beginning of the first (up to the path) and the end of the other
 * (starting from the path). While concatenating, checks that there is no doubled "/" character between the path
 * fragments.//  w  ww  .j  av  a2  s.  c  o m
 * 
 * @param base
 *            the base uri
 * @param relPath
 *            the path to concatenate with the base uri
 * @return the concatenated uri
 */
public static URI concatPath(URI base, String relPath) {
    String resultPath = base.getPath() + StringUtils.stripStart(relPath, "/");
    try {
        URI result = new URI(base.getScheme(), base.getUserInfo(), base.getHost(), base.getPort(), resultPath,
                null, null);
        return result;
    } catch (URISyntaxException e) {
        throw new InvalidUriException(e);
    }
}

From source file:org.jtwig.loader.impl.ClasspathLoader.java

protected String normalizeName(String name) {
    if (StringUtils.startsWithIgnoreCase(name, "classpath:")) {
        name = StringUtils.removeStartIgnoreCase(name, "classpath:");
    }//from  w  ww.j a va2s.c o m
    name = "/" + StringUtils.strip(name, "/\\");
    String path = new File(name).toURI().normalize().getRawPath();
    return StringUtils.stripStart(path, "/\\");
}

From source file:org.kalypso.commons.java.util.StringUtilities.java

/**
 * Spans a string onto lines, thus it inserts NEWLINE chars at lineLength + 1 for each line. It firsts removes all
 * existing NEWLINE chars.// ww w.  ja v a2s  . c o m
 * 
 * @param str
 *          the string to span
 * @param lineLength
 *          the number of chars one line must have
 * @param keepWords
 *          when true words are not cut at the end of the line but rather postponed on the next line
 * @return newly spaned string or null if str is null
 */
public static String spanOverLines(final String str, final int lineLength, final boolean keepWords,
        final int alignment) {
    if (str == null)
        return null;

    if (lineLength == 0)
        return str;

    str.replaceAll("\\n", ""); //$NON-NLS-1$ //$NON-NLS-2$

    final StringBuffer bf = new StringBuffer();
    int i = 0;
    while (true) {
        if (i + lineLength > str.length()) {
            String line = str.substring(i, str.length());
            if (alignment == StringUtilities.ALIGNMENT_LEFT)
                line = StringUtils.stripStart(line, null);
            if (alignment == StringUtilities.ALIGNMENT_RIGHT) {
                line = StringUtils.stripEnd(line, null);
                line = StringUtils.leftPad(line, lineLength);
            }
            bf.append(line);
            break;
        }

        int curLineLength = lineLength;
        if (keepWords && !Character.isWhitespace(str.charAt(i + lineLength - 2))
                && !Character.isWhitespace(str.charAt(i + lineLength - 1))
                && !Character.isWhitespace(str.charAt(i + lineLength))) {
            curLineLength = lineLength - 3;
            while (curLineLength > 0 && !Character.isWhitespace(str.charAt(i + curLineLength)))
                curLineLength--;

            if (curLineLength == 0)
                curLineLength = lineLength;
            if (curLineLength != lineLength)
                curLineLength++;
        }

        String line = str.substring(i, i + curLineLength);
        if (alignment == StringUtilities.ALIGNMENT_LEFT)
            line = StringUtils.stripStart(line, null);
        if (alignment == StringUtilities.ALIGNMENT_RIGHT) {
            line = StringUtils.stripEnd(line, null);
            line = StringUtils.leftPad(line, lineLength);
        }

        bf.append(line).append(System.getProperty("line.separator")); //$NON-NLS-1$

        i = i + curLineLength;
    }

    return bf.toString();
}

From source file:org.pepstock.jem.ant.ScriptFactory.java

/**
 * Reads the script extracting the meta data for JEM.
 * //  www.  ja v a2 s.c  o  m
 * @param content script content
 * @return a lit of properties with JEM properties
 * @throws Exception if any error occurs
 */
private Properties getProperties(String content) throws JclFactoryException, IOException {
    Properties jemProperties = new Properties();
    // flag to check if it's inside of meta data reading
    boolean isInJemConfig = false;
    // if it's able to parse this script
    // It's difficult to validate a script. All is based on different 
    // tags used in the comments
    boolean isScript = false;

    StringBuilder propertiesStrings = new StringBuilder();
    // reads script
    StringReader contentReader = new StringReader(content);
    List<String> lines = IOUtils.readLines(contentReader);
    // scans lines
    for (String line : lines) {
        // if is a comment
        if (StringUtils.startsWithIgnoreCase(line, getCommentCharSequence())) {
            // removes the first part and trim spaces
            String postComment = StringUtils.stripStart(line, getCommentCharSequence()).trim();

            // checks if is the begin element
            if (getBeginElement().equalsIgnoreCase(postComment)) {
                // if flag is true, means the begin element is written twice
                if (isInJemConfig) {
                    throw new JclFactoryException(
                            AntMessage.JEMA070E.toMessage().getFormattedMessage(getBeginElement()));
                }
                // sets flags
                isInJemConfig = true;
                isScript = true;
            } else if (getEndElement().equalsIgnoreCase(postComment)) {
                // checks if is the end element
                // if the flag is false, means that there isn't begin element
                if (!isInJemConfig) {
                    throw new JclFactoryException(
                            AntMessage.JEMA071E.toMessage().getFormattedMessage(getBeginElement()));
                }
                // sets false and break
                isInJemConfig = false;
                break;
            } else if (isInJemConfig) {
                //reads properties if flag is true
                propertiesStrings.append(postComment).append(System.getProperty("line.separator"));
            }
        } else if (isInJemConfig) {
            // if we are here. that means the comment line stops before closing the metadata
            throw new JclFactoryException(AntMessage.JEMA071E.toMessage().getFormattedMessage(getEndElement()));
        }
    }
    // if we are here, that means it gets the end before closing the metadata
    if (isInJemConfig) {
        throw new JclFactoryException(AntMessage.JEMA071E.toMessage().getFormattedMessage(getEndElement()));
    }
    // if we are here, there isn't the begin element and then the script is not of the type of the factory
    if (!isScript) {
        throw new JclFactoryException(AntMessage.JEMA071E.toMessage().getFormattedMessage(getBeginElement()));
    }

    // loads properties
    StringReader reader = new StringReader(propertiesStrings.toString());

    jemProperties.load(reader);
    return jemProperties;
}

From source file:org.sleuthkit.autopsy.imageanalyzer.gui.navpanel.GroupTreeItem.java

/**
 * Recursive method to add a grouping at a given path.
 *
 * @param path Full path (or subset not yet added) to add
 * @param g Group to add//from   ww  w .jav  a  2s  . co  m
 * @param tree True if it is part of a tree (versus a list)
 */
void insert(String path, DrawableGroup g, Boolean tree) {
    if (tree) {
        String cleanPath = StringUtils.stripStart(path, "/");

        // get the first token
        String prefix = StringUtils.substringBefore(cleanPath, "/");

        // Are we at the end of the recursion?
        if ("".equals(prefix)) {
            getValue().setGroup(g);
        } else {
            GroupTreeItem prefixTreeItem = childMap.get(prefix);
            if (prefixTreeItem == null) {
                final GroupTreeItem newTreeItem = new GroupTreeItem(prefix, null, comp);

                prefixTreeItem = newTreeItem;
                childMap.put(prefix, prefixTreeItem);
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        synchronized (getChildren()) {
                            getChildren().add(newTreeItem);
                        }
                    }
                });

            }

            // recursively go into the path
            prefixTreeItem.insert(StringUtils.stripStart(cleanPath, prefix), g, tree);
        }
    } else {
        GroupTreeItem treeItem = childMap.get(path);
        if (treeItem == null) {
            final GroupTreeItem newTreeItem = new GroupTreeItem(path, g, comp);
            newTreeItem.setExpanded(true);
            childMap.put(path, newTreeItem);

            Platform.runLater(new Runnable() {
                @Override
                public void run() {
                    synchronized (getChildren()) {
                        getChildren().add(newTreeItem);
                        if (comp != null) {
                            FXCollections.sort(getChildren(), comp);
                        }
                    }
                }
            });

        }
    }
}

From source file:org.sleuthkit.autopsy.imageanalyzer.gui.navpanel.NavPanel.java

@SuppressWarnings("fallthrough")
private static List<String> groupingToPath(DrawableGroup g) {

    if (g.groupKey.getAttribute() == DrawableAttribute.PATH) {
        String path = g.groupKey.getValueDisplayName();

        String cleanPath = StringUtils.stripStart(path, "/");
        String[] tokens = cleanPath.split("/");

        return Arrays.asList(tokens);
    } else {//  w  w w.  j  av  a 2s.c  om
        return Arrays.asList(g.groupKey.getValueDisplayName());
    }
}

From source file:org.sleuthkit.autopsy.imagegallery.gui.navpanel.GroupTree.java

private static List<String> groupingToPath(DrawableGroup g) {
    String path = g.getGroupByValueDislpayName();
    if (g.getGroupByAttribute() == DrawableAttribute.PATH) {
        String[] cleanPathTokens = StringUtils.stripStart(path, "/").split("/");
        return Arrays.asList(cleanPathTokens);
    } else {// www . j  a  va2  s.  c  o m
        String stripStart = StringUtils.strip(path, "/");
        return Arrays.asList(stripStart);
    }
}

From source file:org.sleuthkit.autopsy.imagegallery.gui.navpanel.GroupTreeItem.java

/**
 * Recursive method to add a grouping at a given path.
 *
 * @param path Full path (or subset not yet added) to add
 * @param g    Group to add/*from   w  ww .j a v a2s . co m*/
 * @param tree True if it is part of a tree (versus a list)
 */
void insert(String path, DrawableGroup g, Boolean tree) {
    if (tree) {
        String cleanPath = StringUtils.stripStart(path, "/");

        // get the first token
        String prefix = StringUtils.substringBefore(cleanPath, "/");

        // Are we at the end of the recursion?
        if ("".equals(prefix)) {
            getValue().setGroup(g);
        } else {
            GroupTreeItem prefixTreeItem = childMap.get(prefix);
            if (prefixTreeItem == null) {
                final GroupTreeItem newTreeItem = new GroupTreeItem(prefix, null, comp);

                prefixTreeItem = newTreeItem;
                childMap.put(prefix, prefixTreeItem);
                Platform.runLater(() -> {
                    synchronized (getChildren()) {
                        getChildren().add(newTreeItem);
                    }
                });

            }

            // recursively go into the path
            prefixTreeItem.insert(StringUtils.stripStart(cleanPath, prefix), g, tree);
        }
    } else {
        GroupTreeItem treeItem = childMap.get(path);
        if (treeItem == null) {
            final GroupTreeItem newTreeItem = new GroupTreeItem(path, g, comp);
            newTreeItem.setExpanded(true);
            childMap.put(path, newTreeItem);

            Platform.runLater(() -> {
                synchronized (getChildren()) {
                    getChildren().add(newTreeItem);
                    if (comp != null) {
                        FXCollections.sort(getChildren(), comp);
                    }
                }
            });

        }
    }
}