Example usage for java.util.regex Matcher reset

List of usage examples for java.util.regex Matcher reset

Introduction

In this page you can find the example usage for java.util.regex Matcher reset.

Prototype

public Matcher reset(CharSequence input) 

Source Link

Document

Resets this matcher with a new input sequence.

Usage

From source file:org.apache.kylin.monitor.ApiRequestParser.java

public void parseRequestLog(String filePath, String dPath) throws ParseException, IOException {

    logger.info("Start parsing kylin api request file " + filePath + " !");

    // writer config init
    FileSystem fs = this.getHdfsFileSystem();
    org.apache.hadoop.fs.Path resultStorePath = new org.apache.hadoop.fs.Path(dPath);
    OutputStreamWriter writer = new OutputStreamWriter(fs.append(resultStorePath));
    CSVWriter cwriter = new CSVWriter(writer, '|', CSVWriter.NO_QUOTE_CHARACTER);

    Pattern p_available = Pattern.compile("/kylin/api/(cubes|user)+.*");
    Pattern p_request = Pattern.compile(
            "^.*\\[.*KylinApiFilter.logRequest.*\\].*REQUEST:.*REQUESTER=(.*);REQ_TIME=(\\w+ (\\d{4}-\\d{2}-\\d{2}).*);URI=(.*);METHOD=(.*);QUERY_STRING=(.*);PAYLOAD=(.*);RESP_STATUS=(.*);$");
    Pattern p_uri = Pattern.compile("/kylin/api/(\\w+)(/.*/)*(.*)$");
    Matcher m_available = p_available.matcher("");
    Matcher m_request = p_request.matcher("");
    Matcher m_uri = p_uri.matcher("");

    Path path = Paths.get(filePath);
    try {/*from w w  w  .j  av a 2s . com*/
        BufferedReader reader = Files.newBufferedReader(path, ENCODING);
        String line = null;
        while ((line = reader.readLine()) != null) {
            // reset the input
            m_available.reset(line);
            m_request.reset(line);

            // filter unnecessary info
            if (m_available.find()) {
                // filter GET info
                if (m_request.find() && !m_request.group(5).equals("GET")) {

                    List<String> groups = new ArrayList<String>();
                    for (int i = 1; i <= m_request.groupCount(); i++) {
                        groups.add(m_request.group(i));
                    }

                    String uri = m_request.group(4);
                    m_uri.reset(uri);
                    if (m_uri.find()) {

                        // add target
                        groups.add(m_uri.group(1));

                        // add action
                        if (m_uri.group(1).equals("cubes")) {
                            String method = m_request.group(5);
                            if ("DELETE".equals(method)) {
                                groups.add("drop");
                            } else if ("POST".equals(method)) {
                                groups.add("save");
                            } else {
                                // add parse action
                                groups.add(m_uri.group(3));
                            }
                        }
                    }
                    groups.add(DEPLOY_ENV);
                    String[] recordArray = groups.toArray(new String[groups.size()]);
                    // write to hdfs
                    cwriter.writeNext(recordArray);
                }
            }

        }
    } catch (IOException ex) {
        logger.info("Failed to write to hdfs:", ex);
    } finally {
        writer.close();
        cwriter.close();
        fs.close();
    }

    logger.info("Finish parsing file " + filePath + " !");
}

From source file:org.failearly.dataset.util.ExtendedProperties.java

private void doResolvePropertyReference(String key, String value, Map<String, String> propertyValues) {
    if (value != null) {
        final Matcher matcher = matcher(value);
        if (matcher != null) {
            while (matcher.matches()) {
                final String referencedKey = referencedPropertyKey(matcher);
                final String referencedValue = resolveValueOfReferencedKey(referencedKey);
                checkForEndlessLoop(key, referencedKey, referencedValue);

                value = value.replace(propertyKey(referencedKey), referencedValue);

                matcher.reset(value);
            }//from   w w  w .  ja va2 s.c  o  m
        }

        propertyValues.put(key, value);
    } else {
        propertyValues.put(key, null);
    }
}

From source file:org.jboss.on.plugins.tomcat.TomcatWarDiscoveryComponent.java

public Set<DiscoveredResourceDetails> discoverResources(
        ResourceDiscoveryContext<TomcatVHostComponent> context) {
    // Parent will discover deployed applications through JMX
    Set<DiscoveredResourceDetails> resources = super.discoverResources(context);
    Set<DiscoveredResourceDetails> result = new HashSet<DiscoveredResourceDetails>();

    TomcatVHostComponent parentComponent = context.getParentResourceComponent();
    ApplicationServerComponent applicationServerComponent = (ApplicationServerComponent) parentComponent;
    String deployDirectoryPath = applicationServerComponent.getConfigurationPath().getPath();
    String parentHost = parentComponent.getName();
    Matcher m = PATTERN_NAME.matcher("");

    for (DiscoveredResourceDetails resource : resources) {
        resource.setResourceKey(CreateResourceHelper.getCanonicalName(resource.getResourceKey()));
        Configuration pluginConfiguration = resource.getPluginConfiguration();
        String name = pluginConfiguration.getSimpleValue(PLUGIN_CONFIG_NAME, "");
        m.reset(name);
        if (m.matches()) {
            String host = m.group(1);
            // skip entries that are not for this vHost
            if (!host.equalsIgnoreCase(parentHost)) {
                continue;
            }//from ww w.j ava 2  s . c  om

            // get some info from the MBean (it seems awkward I have to query for the bean I'm basically dealing with)
            EmsConnection connection = context.getParentResourceComponent().getEmsConnection();
            EmsBean warBean = connection.getBean(resource.getResourceKey());
            // this refresh is important in case EMS is caching a stale version of this object. It can happen if
            // a user deletes and then recreates the same object.
            List<EmsAttribute> contextRootAttribs = warBean.refreshAttributes(EMS_ATTRIBUTE_PATH);
            String contextRoot = (String) contextRootAttribs.get(0).getValue();
            List<EmsAttribute> docBaseAttribs = warBean.refreshAttributes(EMS_ATTRIBUTE_DOC_BASE);
            String docBase = (String) docBaseAttribs.get(0).getValue();
            File docBaseFile = new File(docBase);
            String filename = (docBaseFile.isAbsolute()) ? docBase
                    : (deployDirectoryPath + File.separator + docBase);
            try {
                filename = new File(filename).getCanonicalPath();
            } catch (IOException e) {
                // leave path as is
                log.warn("Unexpected discovered web application path: " + filename);
            }
            if ("".equals(contextRoot)) {
                contextRoot = "/";
            }
            pluginConfiguration.put(new PropertySimple(TomcatWarComponent.PROPERTY_VHOST, host));
            pluginConfiguration.put(new PropertySimple(TomcatWarComponent.PROPERTY_CONTEXT_ROOT, contextRoot));
            pluginConfiguration.put(new PropertySimple(TomcatWarComponent.PROPERTY_FILENAME, filename));
            pluginConfiguration.put(new PropertySimple(TomcatWarComponent.PROPERTY_RESPONSE_TIME_LOG_FILE,
                    getResponseTimeLogFile(parentComponent.getCatalinaBase(), host, contextRoot)));
            resource.setResourceName(resource.getResourceName().replace("{contextRoot}",
                    (("/".equals(contextRoot)) ? docBase : contextRoot)));

            result.add(resource);
        } else {
            log.warn("Skipping discovered web application with unexpected name: " + name);
        }
    }

    // Find apps in the deploy directory that have not been deployed. This can happen if the vhost is
    // not autodeploying
    Set<DiscoveredResourceDetails> undeployedWarResources = discoverUndeployed(context, result);

    // Merge. The addAll operation will only add items that are not already present, so resources discovered
    // by JMX will be used instead of those found by the file system scan.
    result.addAll(undeployedWarResources);

    return result;
}

From source file:org.cloudata.core.common.conf.CloudataConf.java

private String substituteVars(String expr) {
    if (expr == null) {
        return null;
    }//from  ww  w .ja va2  s. c  o m
    Matcher match = varPat.matcher("");
    String eval = expr;
    for (int s = 0; s < MAX_SUBST; s++) {
        match.reset(eval);
        if (!match.find()) {
            return eval;
        }
        String var = match.group();
        var = var.substring(2, var.length() - 1); // remove ${ .. }
        String val = System.getProperty(var);
        if (val == null) {
            val = getRaw(var);
        }
        if (val == null) {
            return eval; // return literal ${var}: var is unbound
        }
        // substitute
        eval = eval.substring(0, match.start()) + val + eval.substring(match.end());
    }
    throw new IllegalStateException("Variable substitution depth too large: " + MAX_SUBST + " " + expr);
}

From source file:de.innovationgate.wgpublisher.RTFEncodingFormatter.java

public String format(Object obj) throws FormattingException {

    try {//from   w  w w .j a v a2s  . c  om

        String text = (String) obj;
        _generateDataURL = (Boolean) _scriptletEngineParameters
                .get(RhinoExpressionEngine.SCRIPTLETOPTION_IMAGEURL_AS_DATAURL);
        if (_generateDataURL == null) {
            _generateDataURL = Boolean.FALSE;
        }

        // Step one: Resolve scriptlets
        RhinoExpressionEngine engine = ExpressionEngineFactory.getTMLScriptEngine();
        text = engine.resolveScriptlets(text, _context, _scriptletEngineParameters);

        // Step two: Filter out all old relative links and replace them with dynamic URLs
        // We can ONLY do this in pure display mode. If field gets rendered for RTF editor we must prevent this because
        // it would lead to the storage of those dynamic URLs
        if (!_editMode) {
            // replace qualified and unqualified attachment links
            text = WGUtils.strReplace(text, "=\"../", this, true);
            // replace content links
            text = replaceContentLinks(text);
        }

        if (_editMode && WGUtils.stringToBoolean(System.getProperty(SYSPROP_FORCE_HTML_CLEANUP, "false"))) {
            // remove RTF-Editor declarations if contained in item value (possible due to copy&paste operations in the CM)
            Pattern rtfEditorDeclarationPatter = Pattern.compile("(<span[^>]*class=\"WGA-Item[^\"]*[^>]*>)",
                    Pattern.DOTALL);
            Matcher matcher = rtfEditorDeclarationPatter.matcher(text);
            while (matcher.find()) {
                String spanTag = matcher.group();
                if (spanTag.contains("display:none")) {
                    text = matcher.replaceFirst("<span style=\"display:none\">");
                } else {
                    text = matcher.replaceFirst("<span>");
                }
                matcher.reset(text);
            }
            while (!removeNoOpSpanTags(text).equals(text)) {
                text = removeNoOpSpanTags(text);
            }
        }

        return text;

    } catch (WGException e) {
        throw new FormattingException("Exception on RTF-Encoding", e);
    }
}

From source file:org.failearly.dataset.util.ExtendedProperties.java

private void checkForEndlessLoop(String key, String nextKey, String nextValue) {
    final Matcher matcher = matcher(nextValue);
    if (matcher != null) {
        final Set<String> keys = new HashSet<>();
        keys.add(key);//from w ww  .  j ava  2 s.  c o  m
        keys.add(nextKey);

        while (matcher.matches()) {
            final String referencedKey = referencedPropertyKey(matcher);
            if (keys.contains(referencedKey)) {
                throwEndlessRecursionDetected(key, referencedKey);
            }

            keys.add(referencedKey);
            final String referencedValue = resolveValueOfReferencedKey(referencedKey);
            matcher.reset(referencedValue);
        }
    }
}

From source file:org.apache.kylin.monitor.QueryParser.java

public void parseQueryLog(String filePath, String dPath) throws ParseException, IOException {

    logger.info("Start parsing file " + filePath + " !");

    //        writer config init
    FileSystem fs = this.getHdfsFileSystem();
    org.apache.hadoop.fs.Path resultStorePath = new org.apache.hadoop.fs.Path(dPath);
    OutputStreamWriter writer = new OutputStreamWriter(fs.append(resultStorePath));
    CSVWriter cwriter = new CSVWriter(writer, '|', CSVWriter.NO_QUOTE_CHARACTER);

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
    Pattern p_query_start = Pattern.compile("^\\[.*\\]:\\[(.*),.*\\]\\[.*\\]\\[.*QueryService.logQuery.*\\].*");
    Pattern p_query_end = Pattern.compile("^Message:(.*)$");
    Pattern p_query_body = Pattern.compile(
            "^\\[.*\\]:\\[((\\d{4}-\\d{2}-\\d{2}).*)\\]\\[.*\\]\\[.*\\].*\n^=+\\[QUERY\\]=+\n^SQL:(.*)\n^User:(.*)\n^Success:(.*)\n^Duration:(.*)\n^Project:(.*)\n^(Realization Names|Cube Names): \\[(.*)\\]\n^Cuboid Ids: \\[(.*)\\]\n^Total scan count:(.*)\n^Result row count:(.*)\n^Accept Partial:(.*)\n(^Is Partial Result:(.*)\n)?^Hit Cache:(.*)\n^Message:(.*)",
            Pattern.MULTILINE);/*from   w ww.java 2 s .c  om*/
    Matcher m_query_start = p_query_start.matcher("");
    Matcher m_query_end = p_query_end.matcher("");
    Matcher m_query_body = p_query_body.matcher("");

    boolean query_start = false;
    StringBuffer query_body = new StringBuffer("");
    Path path = Paths.get(filePath);
    try {
        BufferedReader reader = Files.newBufferedReader(path, ENCODING);
        String line = null;
        while ((line = reader.readLine()) != null) {
            m_query_start.reset(line); //reset the input
            m_query_end.reset(line);

            // set start flag ,clear StringBuffer
            if (m_query_start.find()) {
                query_start = true;
                query_body = new StringBuffer("");
            }
            if (query_start) {
                query_body.append(line + "\n");
            }
            if (m_query_end.find()) {
                query_start = false;
                m_query_body.reset(query_body);
                logger.info("parsing query...");
                logger.info(query_body);
                //                    skip group(8) and group(14)
                if (m_query_body.find()) {
                    ArrayList<String> groups = new ArrayList<String>();
                    int grp_count = m_query_body.groupCount();
                    for (int i = 1; i <= grp_count; i++) {
                        if (i != 8 && i != 14) {
                            String grp_item = m_query_body.group(i);
                            grp_item = grp_item == null ? "" : grp_item.trim();
                            groups.add(grp_item);
                        }
                    }

                    long start_time = format.parse(groups.get(0)).getTime()
                            - (int) (Double.parseDouble(groups.get(5)) * 1000);
                    groups.set(0, format.format(new Date(start_time)));
                    groups.add(DEPLOY_ENV);
                    String[] recordArray = groups.toArray(new String[groups.size()]);
                    //                        write to hdfs
                    cwriter.writeNext(recordArray);

                }

            }

        }
    } catch (IOException ex) {
        logger.info("Failed to write to hdfs:", ex);
    } finally {
        if (writer != null) {
            writer.close();
        }
        if (cwriter != null) {
            cwriter.close();
        }
        if (fs != null) {
            fs.close();
        }
    }

    logger.info("Finish parsing file " + filePath + " !");

}

From source file:candr.yoclip.Parser.java

/**
 * Creates a {@code ParsedOptionParameter} for the option property parameter at the head of the queue. The value of the parsed option will
 * contain option property key and value. As an example if the parameters queue head contains "<code>-Dkey=value</code>" the parsed option value
 * will be the string "<code>key=value</code>" (assuming the bean contains a field annotated with {@link candr.yoclip.annotation.OptionProperties
 * OptionProperties})./*from  w w w.j a v  a  2 s  .com*/
 *
 * @param parameters The current queue of command parameters.
 * @return a parsed option parameter or {@code null} in the following cases.
 * <ul>
 * <li>The parameters queue is empty.</li>
 * <li>The bean does not contain option property annotations.</li>
 * <li>The head of the parameters queue does not match an option property annotation on the bean.</li>
 * </ul>
 */
protected ParsedOption<T> getParsedOptionProperty(final Queue<String> parameters) {

    ParsedOption<T> parsedOption = null;

    String parameter = parameters.peek();
    if (!StringUtils.isEmpty(parameter)) {

        final String prefix = getParserOptions().getPrefix();
        if (parameter.startsWith(prefix)) {

            parameter = parameter.substring(prefix.length());
            for (final Pair<Matcher, String> propertyMatcher : getPropertyMatchers()) {

                final Matcher matcher = propertyMatcher.getLeft();
                if (matcher.reset(parameter).matches()) {

                    parameters.remove();

                    // this always succeeds because the parser has set up the property matcher
                    final String optionParametersKey = propertyMatcher.getRight();
                    final ParserOption<T> optionParameter = getParserOptions().get(optionParametersKey);
                    final String value = matcher.group(1);
                    parsedOption = new ParsedOption<T>(optionParameter, value);

                    break;
                }
            }
        }
    }

    return parsedOption;
}

From source file:org.apache.tika.parser.ocr.TesseractOCRConfig.java

/**
 * Add a key-value pair to pass to Tesseract using its -c command line option.
 * To see the possible options, run tesseract --print-parameters.
 *
 * You may also add these parameters in TesseractOCRConfig.properties; any
 * key-value pair in the properties file where the key contains an underscore
 * is passed directly to Tesseract./* w  w w  .  ja v a2  s  . com*/
 *
 * @param key
 * @param value
 */
public void addOtherTesseractConfig(String key, String value) {
    if (key == null) {
        throw new IllegalArgumentException("key must not be null");
    }
    if (value == null) {
        throw new IllegalArgumentException("value must not be null");
    }

    Matcher m = ALLOWABLE_OTHER_PARAMS_PATTERN.matcher(key);
    if (!m.find()) {
        throw new IllegalArgumentException("Key contains illegal characters: " + key);
    }
    m.reset(value);
    if (!m.find()) {
        throw new IllegalArgumentException("Value contains illegal characters: " + value);
    }

    otherTesseractConfig.put(key.trim(), value.trim());
}

From source file:org.springjutsu.validation.util.RequestUtils.java

/**
 * Used by successView and validationFailureView.
 * If the user specifies a path containing RESTful url
 * wildcards, evaluate those wildcard expressions against 
 * the current model map, and plug them into the url.
 * If the wildcard is a multisegmented path, get the top level
 * bean from the model map, and fetch the sub path using 
 * a beanwrapper instance.// ww  w .  j  a  va 2  s  .c o m
 * @param viewName The view potentially containing wildcards
 * @param model the model map 
 * @param request the request
 * @return a wildcard-substituted view name
 */
@SuppressWarnings("unchecked")
public static String replaceRestPathVariables(String viewName, Map<String, Object> model,
        HttpServletRequest request) {
    String newViewName = viewName;
    Matcher matcher = Pattern.compile(PATH_VAR_PATTERN).matcher(newViewName);
    while (matcher.find()) {
        String match = matcher.group();
        String varName = match.substring(1, match.length() - 1);
        String baseVarName = null;
        String subPath = null;
        if (varName.contains(".")) {
            baseVarName = varName.substring(0, varName.indexOf("."));
            subPath = varName.substring(varName.indexOf(".") + 1);
        } else {
            baseVarName = varName;
        }
        Map<String, String> uriTemplateVariables = (Map<String, String>) request
                .getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
        if (uriTemplateVariables != null && uriTemplateVariables.containsKey(varName)) {
            newViewName = newViewName.replace(match, String.valueOf(uriTemplateVariables.get(varName)));
        } else {
            Object resolvedObject = model.get(baseVarName);
            if (resolvedObject == null) {
                throw new IllegalArgumentException(varName + " is not present in model.");
            }
            if (subPath != null) {
                BeanWrapperImpl beanWrapper = new BeanWrapperImpl(resolvedObject);
                resolvedObject = beanWrapper.getPropertyValue(subPath);
            }
            if (resolvedObject == null) {
                throw new IllegalArgumentException(varName + " is not present in model.");
            }
            newViewName = newViewName.replace(match, String.valueOf(resolvedObject));
        }
        matcher.reset(newViewName);
    }
    return newViewName;
}