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

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

Introduction

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

Prototype

public static String strip(String str, String stripChars) 

Source Link

Document

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

Usage

From source file:eu.europeana.corelib.edm.utils.EdmUtils.java

private static String getSetterMethodName(Class<?> clazz, boolean list) {
    StringBuilder sb = new StringBuilder("set");
    String clazzName = clazz.getSimpleName();
    if (StringUtils.equals("Rights", clazzName) && list) {
        clazzName = "Right";
    }/* www . j a  v a  2s.  c  o m*/
    if (StringUtils.equals("SameAs", clazzName) && list) {
        clazzName = "SameA";
    }
    if (StringUtils.equals("Aggregates", clazzName) && list) {
        clazzName = "Aggregate";
    }
    if (StringUtils.equals("Incorporates", clazzName) && list) {
        clazzName = "Incorporate";
    }
    clazzName = StringUtils.strip(clazzName, "_1");
    clazzName = StringUtils.strip(clazzName, "1");

    sb.append(clazzName);
    if (list) {
        sb.append("List");
    }
    return sb.toString();
}

From source file:adalid.commons.velocity.Writer.java

private Object invoke(File templatePropertiesFile, ForEachVariable variable, Object object) {
    String pattern = "failed to get a valid getter for for-each variable \"{0}\"";
    pattern += HINT// w w w.  j av a 2  s .c o m
            + "a valid getter is a public zero-arguments method that returns either an object or a collection";
    String message;
    if (variable.getter == null) {
        Set<String> getters = gettersOf(variable.token);
        String strip = StringUtils.strip(getters.toString(), "[]");
        pattern += HINT + object.getClass() + " doesn''t implement any of these: " + strip;
        pattern += HINT + "add property \"{0}.getter\" to file \"{1}\"";
        message = MessageFormat.format(pattern, variable.token, templatePropertiesFile);
        return invoke(object, getters, message);
    } else {
        pattern += HINT + "check property \"{0}.getter\" at file \"{1}\"";
        message = MessageFormat.format(pattern, variable.token, templatePropertiesFile);
        return invoke(object, variable.getter, message);
    }
}

From source file:nl.nn.adapterframework.util.StringTagger.java

/**
 * Returns the first value as a <code>String</code>.
 * In case of a single value, it returns that value. In case of multiple values,
 * it returns the//  w w w  .  j  a va2  s .co m
 * Use {@link #get} to get the list of values as a <code>String</code><br>
 * Use {@link #Values} to get a list of multi-values as a <code>Vector</code>.<br>
 * @param token the key of the value to retrieve
 */
public String Value(String token) {
    String val;
    Vector tmp = (Vector) multitokens.get(token);
    if (tmp != null && tmp.size() > 0) {
        val = (String) tmp.elementAt(0);
        if (val != null) {
            val = StringUtils.strip(val, "\""); // added stripping daniel
            return val;
        } else {
            return null;
        }
    } else {
        return null;
    }
}

From source file:org.apache.atlas.hive.hook.HiveHookIT.java

private String stripListBrackets(String listElements) {
    return StringUtils.strip(StringUtils.strip(listElements, "["), "]");
}

From source file:org.apache.cocoon.xml.dom.DOMUtil.java

/**
 * Get the value of the node specified by the XPath.
 * This works similar to xsl:value-of. If the node does not exist <CODE>null</CODE>
 * is returned.//from   w  w  w. j ava2 s .  co  m
 *
 * @param root The node to start the search.
 * @param path XPath search expression.
 * @return     The value of the node or <CODE>null</CODE>
 */
public static String getValueOfNode(XPathProcessor processor, Node root, String path)
        throws ProcessingException {
    if (path == null) {
        throw new ProcessingException("Not a valid XPath: " + path);
    }
    if (root != null) {
        path = StringUtils.strip(path, "/");
        Node node = XPathUtil.searchSingleNode(processor, root, path);
        if (node != null) {
            return getValueOfNode(node);
        }
    }
    return null;
}

From source file:org.apache.cocoon.xml.dom.DOMUtil.java

/**
 * Return the <CODE>Node</CODE> from the DOM Node <CODE>rootNode</CODE>
 * using the XPath expression <CODE>path</CODE>.
 * If the node does not exist, it is created and then returned.
 * This is a very simple method for creating new nodes. If the
 * XPath contains selectors ([,,,]) or "*" it is of course not
 * possible to create the new node. So if you use such XPaths
 * the node must exist beforehand./* w ww  .j  a va 2 s  .  c o  m*/
 * An simple exception is if the expression contains attribute
 * test to values (e.g. [@id = 'du' and @number = 'you'],
 * the attributes with the given values are added. The attributes
 * must be separated with 'and'.
 * Another problem are namespaces: XPath requires sometimes selectors for
 * namespaces, e.g. : /*[namespace-uri()="uri" and local-name()="name"]
 * Creating such a node with a namespace is not possible right now as we use
 * a very simple XPath parser which is not able to parse all kinds of selectors
 * correctly.
 *
 * @param rootNode The node to start the search.
 * @param path     XPath expression for searching the node.
 * @return         The node specified by the path.
 * @throws ProcessingException If no path is specified or the XPath engine fails.
 * @deprecated
 */
public static Node selectSingleNode(Node rootNode, String path) throws ProcessingException {
    // Now we have to parse the string
    // First test:  path? rootNode?
    if (path == null) {
        throw new ProcessingException("XPath is required.");
    }
    if (rootNode == null)
        return rootNode;

    if (path.length() == 0 || path.equals("/") == true)
        return rootNode;

    // now the first "quick" test is if the node exists using the
    // full XPathAPI
    try {
        Node testNode = getSingleNode(rootNode, path);
        if (testNode != null)
            return testNode;
    } catch (javax.xml.transform.TransformerException local) {
        throw new ProcessingException(
                "Transforming exception during selectSingleNode with path: '" + path + "'. Exception: " + local,
                local);
    }
    // Remove leading "/" on both ends
    path = StringUtils.strip(path, "/");

    // now step through the nodes!
    Node parent = rootNode;
    int pos;
    int posSelector;
    do {
        pos = path.indexOf("/"); // get next separator
        posSelector = path.indexOf("[");
        if (posSelector != -1 && posSelector < pos) {
            posSelector = path.indexOf("]");
            pos = path.indexOf("/", posSelector);
        }

        String nodeName;
        boolean isAttribute = false;
        if (pos != -1) { // found separator
            nodeName = path.substring(0, pos); // string until "/"
            path = path.substring(pos + 1); // rest of string after "/"
        } else {
            nodeName = path;
        }

        // test for attribute spec
        if (nodeName.startsWith("@") == true) {
            isAttribute = true;
        }

        Node singleNode;
        try {
            singleNode = getSingleNode(parent, nodeName);
        } catch (javax.xml.transform.TransformerException localException) {
            throw new ProcessingException("XPathUtil.selectSingleNode: " + localException.getMessage(),
                    localException);
        }

        // create node if necessary
        if (singleNode == null) {
            Node newNode;
            // delete XPath selectors
            int posSelect = nodeName.indexOf("[");
            String XPathExp = null;
            if (posSelect != -1) {
                XPathExp = nodeName.substring(posSelect + 1, nodeName.length() - 1);
                nodeName = nodeName.substring(0, posSelect);
            }
            if (isAttribute == true) {
                try {
                    newNode = getOwnerDocument(rootNode).createAttributeNS(null, nodeName.substring(1));
                    ((Element) parent).setAttributeNodeNS((org.w3c.dom.Attr) newNode);
                    parent = newNode;
                } catch (DOMException local) {
                    throw new ProcessingException("Unable to create new DOM node: '" + nodeName + "'.", local);
                }
            } else {
                try {
                    newNode = getOwnerDocument(rootNode).createElementNS(null, nodeName);
                } catch (DOMException local) {
                    throw new ProcessingException("Unable to create new DOM node: '" + nodeName + "'.", local);
                }
                if (XPathExp != null) {
                    java.util.List attrValuePairs = new java.util.ArrayList(4);
                    boolean noError = true;

                    String attr;
                    String value;
                    // scan for attributes
                    java.util.StringTokenizer tokenizer = new java.util.StringTokenizer(XPathExp, "= ");
                    while (tokenizer.hasMoreTokens() == true) {
                        attr = tokenizer.nextToken();
                        if (attr.startsWith("@") == true) {
                            if (tokenizer.hasMoreTokens() == true) {
                                value = tokenizer.nextToken();
                                if (value.startsWith("'") && value.endsWith("'"))
                                    value = value.substring(1, value.length() - 1);
                                if (value.startsWith("\"") && value.endsWith("\""))
                                    value = value.substring(1, value.length() - 1);
                                attrValuePairs.add(attr.substring(1));
                                attrValuePairs.add(value);
                            } else {
                                noError = false;
                            }
                        } else if (attr.trim().equals("and") == false) {
                            noError = false;
                        }
                    }
                    if (noError == true) {
                        for (int l = 0; l < attrValuePairs.size(); l = l + 2) {
                            ((Element) newNode).setAttributeNS(null, (String) attrValuePairs.get(l),
                                    (String) attrValuePairs.get(l + 1));
                        }
                    }
                }
                parent.appendChild(newNode);
                parent = newNode;
            }
        } else {
            parent = singleNode;
        }
    } while (pos != -1);
    return parent;
}

From source file:org.apache.cocoon.xml.dom.DOMUtil.java

/**
 * Return the <CODE>Node</CODE> from the DOM Node <CODE>rootNode</CODE>
 * using the XPath expression <CODE>path</CODE>.
 * If the node does not exist, it is created and then returned.
 * This is a very simple method for creating new nodes. If the
 * XPath contains selectors ([,,,]) or "*" it is of course not
 * possible to create the new node. So if you use such XPaths
 * the node must exist beforehand.//w  w w.j ava  2s. c  om
 * An simple exception is if the expression contains attribute
 * test to values (e.g. [@id = 'du' and @number = 'you'],
 * the attributes with the given values are added. The attributes
 * must be separated with 'and'.
 * Another problem are namespaces: XPath requires sometimes selectors for
 * namespaces, e.g. : /*[namespace-uri()="uri" and local-name()="name"]
 * Creating such a node with a namespace is not possible right now as we use
 * a very simple XPath parser which is not able to parse all kinds of selectors
 * correctly.
 *
 * @param rootNode  The node to start the search.
 * @param path      XPath expression for searching the node.
 * @param processor The XPath processor to use
 * @return          The node specified by the path.
 * @throws ProcessingException If no path is specified or the XPath engine fails.
 */
public static Node selectSingleNode(Node rootNode, String path, XPathProcessor processor)
        throws ProcessingException {
    // Now we have to parse the string
    // First test:  path? rootNode?
    if (path == null) {
        throw new ProcessingException("XPath is required.");
    }
    if (rootNode == null)
        return rootNode;

    if (path.length() == 0 || path.equals("/") == true)
        return rootNode;

    // now the first "quick" test is if the node exists using the
    // full XPathAPI
    try {
        Node testNode = getSingleNode(rootNode, path, processor);
        if (testNode != null)
            return testNode;
    } catch (javax.xml.transform.TransformerException local) {
        throw new ProcessingException(
                "Transforming exception during selectSingleNode with path: '" + path + "'. Exception: " + local,
                local);
    }

    // remove leading "/" oon both ends
    path = StringUtils.strip(path, "/");

    // now step through the nodes!
    Node parent = rootNode;
    int pos;
    int posSelector;
    do {
        pos = path.indexOf("/"); // get next separator
        posSelector = path.indexOf("[");
        if (posSelector != -1 && posSelector < pos) {
            posSelector = path.indexOf("]");
            pos = path.indexOf("/", posSelector);
        }

        String nodeName;
        boolean isAttribute = false;
        if (pos != -1) { // found separator
            nodeName = path.substring(0, pos); // string until "/"
            path = path.substring(pos + 1); // rest of string after "/"
        } else {
            nodeName = path;
        }

        // test for attribute spec
        if (nodeName.startsWith("@") == true) {
            isAttribute = true;
        }

        Node singleNode;
        try {
            singleNode = getSingleNode(parent, nodeName, processor);
        } catch (javax.xml.transform.TransformerException localException) {
            throw new ProcessingException("XPathUtil.selectSingleNode: " + localException.getMessage(),
                    localException);
        }

        // create node if necessary
        if (singleNode == null) {
            Node newNode;
            // delete XPath selectors
            int posSelect = nodeName.indexOf("[");
            String XPathExp = null;
            if (posSelect != -1) {
                XPathExp = nodeName.substring(posSelect + 1, nodeName.length() - 1);
                nodeName = nodeName.substring(0, posSelect);
            }
            if (isAttribute == true) {
                try {
                    newNode = getOwnerDocument(rootNode).createAttributeNS(null, nodeName.substring(1));
                    ((Element) parent).setAttributeNodeNS((org.w3c.dom.Attr) newNode);
                    parent = newNode;
                } catch (DOMException local) {
                    throw new ProcessingException("Unable to create new DOM node: '" + nodeName + "'.", local);
                }
            } else {
                try {
                    newNode = getOwnerDocument(rootNode).createElementNS(null, nodeName);
                } catch (DOMException local) {
                    throw new ProcessingException("Unable to create new DOM node: '" + nodeName + "'.", local);
                }
                if (XPathExp != null) {
                    java.util.List attrValuePairs = new java.util.ArrayList(4);
                    boolean noError = true;

                    String attr;
                    String value;
                    // scan for attributes
                    java.util.StringTokenizer tokenizer = new java.util.StringTokenizer(XPathExp, "= ");
                    while (tokenizer.hasMoreTokens() == true) {
                        attr = tokenizer.nextToken();
                        if (attr.startsWith("@") == true) {
                            if (tokenizer.hasMoreTokens() == true) {
                                value = tokenizer.nextToken();
                                if (value.startsWith("'") && value.endsWith("'"))
                                    value = value.substring(1, value.length() - 1);
                                if (value.startsWith("\"") && value.endsWith("\""))
                                    value = value.substring(1, value.length() - 1);
                                attrValuePairs.add(attr.substring(1));
                                attrValuePairs.add(value);
                            } else {
                                noError = false;
                            }
                        } else if (attr.trim().equals("and") == false) {
                            noError = false;
                        }
                    }
                    if (noError == true) {
                        for (int l = 0; l < attrValuePairs.size(); l = l + 2) {
                            ((Element) newNode).setAttributeNS(null, (String) attrValuePairs.get(l),
                                    (String) attrValuePairs.get(l + 1));
                        }
                    }
                }
                parent.appendChild(newNode);
                parent = newNode;
            }
        } else {
            parent = singleNode;
        }
    } while (pos != -1);
    return parent;
}

From source file:org.apache.cocoon.xml.dom.DOMUtil.java

/**
 * Get the value of the node specified by the XPath.
 * This works similar to xsl:value-of. If the node does not exist <CODE>null</CODE>
 * is returned./*from w  w  w. ja v  a2  s  .co  m*/
 *
 * @param root The node to start the search.
 * @param path XPath search expression.
 * @return     The value of the node or <CODE>null</CODE>
 * @deprecated
 */
public static String getValueOf(Node root, String path) throws ProcessingException {
    if (path == null) {
        throw new ProcessingException("Not a valid XPath: " + path);
    }
    if (root == null)
        return null;
    path = StringUtils.strip(path, "/");

    try {
        Node node = getSingleNode(root, path);
        if (node != null) {
            return getValueOfNode(node);
        }
    } catch (javax.xml.transform.TransformerException localException) {
        throw new ProcessingException("XPathUtil.selectSingleNode: " + localException.getMessage(),
                localException);
    }
    return null;
}

From source file:org.apache.cocoon.xml.dom.DOMUtil.java

/**
 * Get the value of the node specified by the XPath.
 * This works similar to xsl:value-of. If the node does not exist <CODE>null</CODE>
 * is returned./*w  w w  . j a v  a 2 s.co  m*/
 *
 * @param root The node to start the search.
 * @param path XPath search expression.
 * @param processor The XPath processor to use
 * @return     The value of the node or <CODE>null</CODE>
 */
public static String getValueOf(Node root, String path, XPathProcessor processor) throws ProcessingException {
    if (path == null) {
        throw new ProcessingException("Not a valid XPath: " + path);
    }
    if (root == null)
        return null;
    path = StringUtils.strip(path, "/");

    try {
        Node node = getSingleNode(root, path, processor);
        if (node != null) {
            return getValueOfNode(node);
        }
    } catch (javax.xml.transform.TransformerException localException) {
        throw new ProcessingException("XPathUtil.selectSingleNode: " + localException.getMessage(),
                localException);
    }
    return null;
}

From source file:org.apache.crunch.kafka.offset.hdfs.HDFSOffsetWriter.java

/**
 * Converts a {@code fileName} into the time the offsets were persisted.
 *
 * @param fileName the file name to parse.
 * @return the time in milliseconds since epoch that the offsets were stored.
 * @throws IllegalArgumentException if the {@code fileName} is not of the correct format or is {@code null} or
 *                                  empty.
 *//* w w w. ja va 2  s. com*/
public static long fileNameToPersistenceTime(String fileName) {
    if (StringUtils.isBlank(fileName)) {
        throw new IllegalArgumentException("the 'fileName' cannot be 'null' or empty");
    }
    String formattedTimeString = StringUtils.strip(fileName, FILE_FORMAT_EXTENSION);
    DateTime persistedTime = FILE_FORMATTER.parseDateTime(formattedTimeString);
    return persistedTime.getMillis();
}