Example usage for java.util.regex PatternSyntaxException printStackTrace

List of usage examples for java.util.regex PatternSyntaxException printStackTrace

Introduction

In this page you can find the example usage for java.util.regex PatternSyntaxException printStackTrace.

Prototype

public void printStackTrace(PrintStream s) 

Source Link

Document

Prints this throwable and its backtrace to the specified print stream.

Usage

From source file:jp.ikedam.jenkins.plugins.viewcopy_builder.SetRegexOperation.java

/**
 * @param doc//w ww .j ava2 s  .  c  o  m
 * @param env
 * @param logger
 * @return
 * @see jp.ikedam.jenkins.plugins.viewcopy_builder.ViewcopyOperation#perform(org.w3c.dom.Document, hudson.EnvVars, java.io.PrintStream)
 */
@Override
public Document perform(Document doc, EnvVars env, PrintStream logger) {
    if (StringUtils.isEmpty(getRegex())) {
        logger.println("Regular expression is not specified.");
        return null;
    }

    String expandedRegex = StringUtils.trim(env.expand(getRegex()));
    if (StringUtils.isEmpty(expandedRegex)) {
        logger.println("Regular expression got to empty.");
        return null;
    }

    try {
        Pattern.compile(expandedRegex);
    } catch (PatternSyntaxException e) {
        e.printStackTrace(logger);
        return null;
    }

    Node regexNode;
    try {
        regexNode = getNode(doc, "/*/includeRegex");
    } catch (XPathExpressionException e) {
        e.printStackTrace(logger);
        return null;
    }

    if (regexNode == null) {
        // includeRegex is not exist.
        // create new one.
        regexNode = doc.createElement("includeRegex");
        doc.getDocumentElement().appendChild(regexNode);
    }

    regexNode.setTextContent(expandedRegex);
    logger.println(String.format("Set includeRegex to %s", expandedRegex));

    return doc;
}

From source file:jp.ikedam.jenkins.plugins.jobcopy_builder.ReplaceRegExpOperation.java

/**
 * Returns modified XML Document of the job configuration.
 *
 * Replace the strings in the job configuration: only applied to strings in text nodes, so the XML structure is never destroyed.
 *
 * @param doc/*from   w w w.  j ava  2 s .c om*/
 *            XML Document of the job to be copied (job/NAME/config.xml)
 * @param env
 *            Variables defined in the build.
 * @param logger
 *            The output stream to log.
 * @return modified XML Document. Return null if an error occurs.
 * @see jp.ikedam.jenkins.plugins.jobcopy_builder.AbstractXmlJobcopyOperation#perform(org.w3c.dom.Document, hudson.EnvVars, java.io.PrintStream)
 */
@Override
public Document perform(final Document doc, final EnvVars env, final PrintStream logger) {
    final String fromStr = getFromStr();
    String toStr = getToStr();

    if (StringUtils.isEmpty(fromStr)) {
        logger.println("From String is empty");
        return null;
    }
    if (toStr == null) {
        toStr = "";
    }
    final String expandedFromStr = isExpandFromStr() ? env.expand(fromStr) : maskSpecialChars(fromStr);

    Pattern pattern;
    try {
        pattern = Pattern.compile(expandedFromStr);
    } catch (final PatternSyntaxException e) {
        logger.println("Error on regular expression: " + e.getMessage());
        return null;
    }

    String expandedToStr = isExpandToStr() ? env.expand(toStr) : maskSpecialChars(toStr);
    if (StringUtils.isEmpty(expandedFromStr)) {
        logger.println("From String got to be empty");
        return null;
    }
    if (expandedToStr == null) {
        expandedToStr = "";
    }

    logger.print("Replacing with RegExp: " + expandedFromStr + " -> " + expandedToStr);
    try {
        // Retrieve all text nodes.
        final NodeList textNodeList = getNodeList(doc, "//text()");

        // Perform replacing to all text nodes.
        // NodeList does not implement Collection, and foreach is not usable.
        for (int i = 0; i < textNodeList.getLength(); ++i) {
            final Node node = textNodeList.item(i);
            final String nodeValue = node.getNodeValue();
            String newNodeValue = nodeValue;
            final Matcher matcher = pattern.matcher(nodeValue);
            // check all occurance
            while (matcher.find()) {
                newNodeValue = matcher.replaceAll(expandedToStr);
            }
            node.setNodeValue(newNodeValue);
        }
        logger.println("");

        return doc;
    } catch (final Exception e) {
        logger.print("Error occured in XML operation");
        e.printStackTrace(logger);
        return null;
    }
}