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.yamj.filescanner.ScannerManagementImpl.java

/**
 * Scan a directory (and recursively any other directories contained
 *
 * @param library//w  w w  .  j a  va2  s  .com
 * @param parentDto
 * @param directory
 */
private StageDirectoryDTO scanDir(Library library, File directory) {
    DirectoryType dirType = DirectoryEnding.check(directory);
    StageDirectoryDTO stageDir;

    LOG.info("Scanning directory '{}', detected type - {}", library.getRelativeDir(directory), dirType);

    if (dirType == DirectoryType.BLURAY || dirType == DirectoryType.DVD) {
        // Don't scan BLURAY or DVD structures
        LOG.info("Skipping directory '{}' as its a {} type", directory.getAbsolutePath(), dirType);
        library.getStatistics().increment(dirType == DirectoryType.BLURAY ? StatType.BLURAY : StatType.DVD);
        stageDir = null;
    } else if (DIR_EXCLUSIONS.containsKey(directory.getName().toLowerCase())) {
        LOG.info("Skipping directory '{}' as its in the exclusion list.", directory.getAbsolutePath());
        stageDir = null;
    } else {
        try {
            if (FileUtils.directoryContains(directory, new File(directory, FILE_MJBIGNORE))) {
                LOG.debug("Exclusion file '{}' found, skipping scanning of directory {}.", FILE_MJBIGNORE,
                        directory.getName());
                return null;
            }
        } catch (IOException ex) {
            LOG.trace("Failed to seach for '{}' in the directory {}", FILE_MJBIGNORE, directory.getName());
        }

        stageDir = new StageDirectoryDTO();
        stageDir.setPath(directory.getAbsolutePath());
        stageDir.setDate(directory.lastModified());

        library.getStatistics().increment(StatType.DIRECTORY);

        List<File> currentFileList = Arrays.asList(directory.listFiles());
        FileTypeComparator comp = new FileTypeComparator(Boolean.FALSE);
        Collections.sort(currentFileList, comp);

        /*
         * We need to scan the directory and look for any of the exclusion filenames.
         *
         * We then build a list of those excluded extensions, so that when we scan the filename list we can exclude the unwanted files.
         */
        List<String> exclusions = new ArrayList<String>();
        for (File file : currentFileList) {
            if (file.isFile()) {
                String lcFilename = file.getName().toLowerCase();
                if (DIR_EXCLUSIONS.containsKey(lcFilename)) {
                    if (CollectionUtils.isEmpty(DIR_EXCLUSIONS.get(lcFilename))) {
                        // Because the value is null or empty we exclude the whole directory, so quit now.
                        LOG.debug("Exclusion file '{}' found, skipping scanning of directory {}.", lcFilename,
                                file.getParent());
                        // All files to be excluded, so quit
                        return null;
                    } else {
                        // We found a match, so add it to our local copy
                        LOG.debug("Exclusion file '{}' found, will exclude all {} file types", lcFilename,
                                DIR_EXCLUSIONS.get(lcFilename).toString());
                        exclusions.addAll(DIR_EXCLUSIONS.get(lcFilename));
                        // Skip to the next file, theres no need of further processing
                        continue;
                    }
                }
            } else {
                // First directory we find, we can stop (because we sorted the files first)
                break;
            }
        }

        // Create a precompiled Matcher for use later (Doesn't matter what the values are)
        Matcher matcher = Pattern.compile(FILE_MJBIGNORE).matcher(FILE_MJBIGNORE);

        // Scan the directory properly
        for (File file : currentFileList) {
            boolean excluded = Boolean.FALSE;
            if (file.isFile()) {
                String lcFilename = file.getName().toLowerCase();
                if (exclusions.contains(FilenameUtils.getExtension(lcFilename))
                        || DIR_EXCLUSIONS.containsKey(lcFilename)) {
                    LOG.debug(
                            "File name '{}' excluded because it's listed in the exlusion list for this directory",
                            file.getName());
                    continue;
                }

                // Process the DIR_IGNORE_FILES
                for (Pattern pattern : DIR_IGNORE_FILES) {
                    matcher.reset(lcFilename).usePattern(pattern);
                    if (matcher.matches()) {
                        // Found the file pattern, so skip the file
                        LOG.debug("File name '{}' excluded because it matches exlusion pattern '{}'",
                                file.getName(), pattern.pattern());
                        excluded = Boolean.TRUE;
                        break;
                    }
                }

                if (!excluded) {
                    stageDir.addStageFile(scanFile(file));
                    library.getStatistics().increment(StatType.FILE);
                }
            } else {
                // First directory we find, we can stop (because we sorted the files first)
                break;
            }
        }

        library.addDirectory(stageDir);
        queueForSending(library, stageDir);

        // Resort the files with directories first
        comp.setDirectoriesFirst(Boolean.TRUE);
        Collections.sort(currentFileList, comp);

        // Now scan the directories
        for (File scanDir : currentFileList) {
            if (scanDir.isDirectory()) {
                if (scanDir(library, scanDir) == null) {
                    LOG.info("Not adding directory '{}', no files found or all excluded",
                            scanDir.getAbsolutePath());
                }
            } else {
                // First file we find, we can stop (because we are sorted directories first)
                break;
            }
        }
    }
    return stageDir;
}

From source file:org.apache.nifi.processors.standard.InvokeHTTP.java

private Request.Builder setHeaderProperties(final ProcessContext context, Request.Builder requestBuilder,
        final FlowFile requestFlowFile) {
    // check if we should send the a Date header with the request
    if (context.getProperty(PROP_DATE_HEADER).asBoolean()) {
        requestBuilder = requestBuilder.addHeader("Date", DATE_FORMAT.print(System.currentTimeMillis()));
    }//w  w w.java 2s  . com

    for (String headerKey : dynamicPropertyNames) {
        String headerValue = context.getProperty(headerKey).evaluateAttributeExpressions(requestFlowFile)
                .getValue();
        requestBuilder = requestBuilder.addHeader(headerKey, headerValue);
    }

    // iterate through the flowfile attributes, adding any attribute that
    // matches the attributes-to-send pattern. if the pattern is not set
    // (it's an optional property), ignore that attribute entirely
    if (regexAttributesToSend != null && requestFlowFile != null) {
        Map<String, String> attributes = requestFlowFile.getAttributes();
        Matcher m = regexAttributesToSend.matcher("");
        for (Map.Entry<String, String> entry : attributes.entrySet()) {
            String headerKey = trimToEmpty(entry.getKey());

            // don't include any of the ignored attributes
            if (IGNORED_ATTRIBUTES.contains(headerKey)) {
                continue;
            }

            // check if our attribute key matches the pattern
            // if so, include in the request as a header
            m.reset(headerKey);
            if (m.matches()) {
                String headerVal = trimToEmpty(entry.getValue());
                requestBuilder = requestBuilder.addHeader(headerKey, headerVal);
            }
        }
    }
    return requestBuilder;
}

From source file:org.canova.api.conf.Configuration.java

private String substituteVars(String expr) {
    if (expr == null) {
        return null;
    }/*from ww w  .j  a v  a 2  s . com*/
    Matcher match = varPat.matcher("");
    String eval = expr;
    int MAX_SUBST = 20;
    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 = null;
        try {
            val = System.getProperty(var);
        } catch (SecurityException se) {
            LOG.warn("Unexpected SecurityException in Configuration", se);
        }
        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:conf.Configuration.java

private String substituteVars(String expr) {
    if (expr == null) {
        return null;
    }//from   w  ww  .j a v a2s.  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 = null;
        try {
            val = System.getProperty(var);
        } catch (SecurityException se) {
            LOG.warn("Unexpected SecurityException in Configuration", se);
        }
        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:org.pentaho.hadoop.shim.HadoopConfigurationLocator.java

/**
 * Exclude jars contained in exclude.jars property in config.properties file from the list of URLs
 *
 * @param urls                 the list of all the URLs to add to the class loader
 * @param excludedJarsProperty exclude.jars property from a config.properties file
 * @return The rest of the jars in {@code urls} after excluding the jars listed in {@code excludedJarsProperty}.
 *//*from w ww  .j av a2s .  c o m*/

protected List<URL> filterJars(List<URL> urls, String excludedJarsProperty) {

    Pattern pattern;
    Matcher matcher;
    String[] excludedJars;

    if (!(excludedJarsProperty == null || excludedJarsProperty.trim().isEmpty())) {
        excludedJars = excludedJarsProperty.split(",");
        if (excludedJars != null) {
            for (String excludedJar : excludedJars) {
                pattern = Pattern.compile(".*/" + excludedJar.toLowerCase() + "-.*\\.jar$");
                matcher = pattern.matcher("");
                Iterator<URL> iterator = urls.listIterator();
                while (iterator.hasNext()) {
                    URL url = iterator.next();
                    if (url.toString().toLowerCase().contains(excludedJar.toLowerCase())) {
                        if (excludedJar.endsWith(".jar")
                                || url.toString().toLowerCase().contains(excludedJar.toLowerCase() + ".jar")) {
                            iterator.remove();
                        } else {
                            if (matcher.reset(url.toString().toLowerCase()).matches()) {
                                iterator.remove();
                            }
                        }
                    }
                }
            }
        }
    }
    return urls;
}

From source file:com.xpn.xwiki.plugin.mailsender.MailSenderPlugin.java

/**
 * Splits a raw mail into headers and the actual content, filling in a {@link Mail} object. This method should be
 * compliant with RFC 2822 as much as possible. If the message accidentally starts with what looks like a mail
 * header, then that line <strong>WILL</strong> be considered a header; no check on the semantics of the header is
 * performed./*from  ww w  . j  a  v a2 s.  com*/
 * 
 * @param rawMessage the raw content of the message that should be parsed
 * @param toMail the {@code Mail} to create
 * @throws IllegalArgumentException if the target Mail or the content to parse are null or the empty string
 */
protected void parseRawMessage(String rawMessage, Mail toMail) {
    // Sanity check
    if (toMail == null) {
        throw new IllegalArgumentException("The target Mail can't be null");
    } else if (rawMessage == null) {
        throw new IllegalArgumentException("rawMessage can't be null");
    } else if (StringUtils.isBlank(rawMessage)) {
        throw new IllegalArgumentException("rawMessage can't be empty");
    }

    try {
        // The message is read line by line
        BufferedReader input = new BufferedReader(new StringReader(rawMessage));
        String line;
        StringWriter result = new StringWriter();
        PrintWriter output = new PrintWriter(result);
        boolean headersFound = false;

        line = input.readLine();
        // Additional headers are at the start. Parse them and put them in the Mail object.
        // Warning: no empty lines are allowed before the headers.
        Matcher m = SMTP_HEADER.matcher(line);
        while (line != null && m.matches()) {
            String header = m.group(1);
            String value = m.group(2);
            line = input.readLine();
            while (line != null && (line.startsWith(" ") || line.startsWith("\t"))) {
                value += line;
                line = input.readLine();
            }
            if (header.equals(SUBJECT)) {
                toMail.setSubject(value);
            } else if (header.equals(FROM)) {
                toMail.setFrom(value);
            } else {
                toMail.setHeader(header, value);
            }
            if (line != null) {
                m.reset(line);
            }
            headersFound = true;
        }

        // There should be one empty line here, separating the body from the headers.
        if (headersFound && line != null && StringUtils.isBlank(line)) {
            line = input.readLine();
        } else {
            if (headersFound) {
                LOGGER.warn("Mail body does not contain an empty line between the headers and the body.");
            }
        }

        // If no text exists after the headers, return
        if (line == null) {
            toMail.setTextPart("");
            return;
        }

        do {
            // Mails always use \r\n as EOL
            output.print(line + "\r\n");
        } while ((line = input.readLine()) != null);

        toMail.setTextPart(result.toString());
    } catch (IOException ioe) {
        // Can't really happen here
        LOGGER.error("Unexpected IO exception while preparing a mail", ioe);
    }
}

From source file:org.archive.crawler.frontier.AbstractFrontier.java

/**
 * Import URIs from the given file (in recover-log-like format, with
 * a 3-character 'type' tag preceding a URI with optional hops/via).
 * /*w  w w.ja va 2  s.  co m*/
 * If 'includeOnly' is true, the URIs will only be imported into 
 * the frontier's alreadyIncluded structure, without being queued.
 * 
 * Only imports URIs if their first tag field matches the acceptTags 
 * pattern.
 * 
 * @param source File recovery log file to use (may be .gz compressed)
 * @param applyScope whether to apply crawl scope to URIs
 * @param includeOnly whether to only add to included filter, not schedule
 * @param forceFetch whether to force fetching, even if already seen 
 * (ignored if includeOnly is set)
 * @param acceptTags String regex; only lines whose first field 
 * match will be included
 * @return number of lines in recovery log (for reference)
 * @throws IOException
 */
public long importRecoverFormat(File source, boolean applyScope, boolean includeOnly, boolean forceFetch,
        String acceptTags) throws IOException {
    DecideRule scope = (applyScope) ? getScope() : null;
    FrontierJournal newJournal = getFrontierJournal();
    Matcher m = Pattern.compile(acceptTags).matcher("");
    BufferedReader br = ArchiveUtils.getBufferedReader(source);
    String read;
    int lineCount = 0;
    try {
        while ((read = br.readLine()) != null) {
            lineCount++;
            if (read.length() < 4) {
                continue;
            }
            String lineType = read.substring(0, 3);
            m.reset(lineType);
            if (m.matches()) {
                try {
                    String uriHopsViaString = read.substring(3).trim();
                    CrawlURI curi = CrawlURI.fromHopsViaString(uriHopsViaString);
                    if (scope != null) {
                        sheetOverlaysManager.applyOverlaysTo(curi);
                        try {
                            KeyedProperties.loadOverridesFrom(curi);
                            if (!scope.accepts(curi)) {
                                // skip out-of-scope URIs if so configured
                                continue;
                            }
                        } finally {
                            KeyedProperties.clearOverridesFrom(curi);
                        }
                    }
                    if (includeOnly) {
                        considerIncluded(curi);
                        newJournal.included(curi);
                    } else {
                        curi.setForceFetch(forceFetch);
                        schedule(curi);
                    }
                } catch (URIException e) {
                    logger.log(Level.WARNING, "Problem line: " + read, e);
                }
            }
            if ((lineCount % PROGRESS_INTERVAL) == 0) {
                // every 1 million lines, print progress
                logger.info("at line " + lineCount + (includeOnly ? " (include-only)" : "")
                        + " alreadyIncluded count = " + discoveredUriCount());
            }
        }
    } catch (EOFException e) {
        // expected in some uncleanly-closed recovery logs; ignore
    } finally {
        br.close();
    }
    return lineCount;
}

From source file:org.structr.module.JarConfigurationProvider.java

/**
 * Scans the class path and returns a Set containing all structr
 * modules./*from www . jav a  2 s  .  co m*/
 *
 * @return a Set of active module names
 */
private Set<String> getResourcesToScan() {

    String classPath = System.getProperty("java.class.path");
    Set<String> modules = new LinkedHashSet<>();
    Pattern pattern = Pattern.compile(".*(structr).*(war|jar)");
    Matcher matcher = pattern.matcher("");

    for (String jarPath : classPath.split("[".concat(pathSep).concat("]+"))) {

        String lowerPath = jarPath.toLowerCase();

        if (lowerPath.endsWith(classesDir) || lowerPath.endsWith(testClassesDir)) {

            modules.add(jarPath);

        } else {

            String moduleName = lowerPath.substring(lowerPath.lastIndexOf(pathSep) + 1);

            matcher.reset(moduleName);

            if (matcher.matches()) {

                modules.add(jarPath);
            }

        }

    }

    for (String resource : Services.getInstance().getResources()) {

        String lowerResource = resource.toLowerCase();

        if (lowerResource.endsWith(".jar") || lowerResource.endsWith(".war")) {

            modules.add(resource);
        }

    }

    return modules;
}

From source file:com.glaf.core.config.Configuration.java

private String substituteVars(String expr) {
    if (expr == null) {
        return null;
    }/*from  w w  w.j av a  2s  .c  o m*/
    Matcher match = VAR_PATTERN.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 = null;
        try {
            val = System.getProperty(var);
        } catch (SecurityException se) {
            LOG.warn("Unexpected SecurityException in Configuration", se);
        }
        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:co.cask.cdap.common.conf.Configuration.java

private String substituteVars(String expr) {
    if (expr == null) {
        return null;
    }/*from  www  .jav  a 2s  . com*/
    Matcher match = VAR_PAT.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 = null;
        try {
            val = System.getProperty(var);
        } catch (SecurityException se) {
            LOG.warn("Unexpected SecurityException in Configuration", se);
        }
        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);
}