Example usage for java.util.regex Pattern UNIX_LINES

List of usage examples for java.util.regex Pattern UNIX_LINES

Introduction

In this page you can find the example usage for java.util.regex Pattern UNIX_LINES.

Prototype

int UNIX_LINES

To view the source code for java.util.regex Pattern UNIX_LINES.

Click Source Link

Document

Enables Unix lines mode.

Usage

From source file:org.opennms.netmgt.collectd.HttpCollector.java

private static List<HttpCollectionAttribute> processResponse(final Locale responseLocale,
        final String responseBodyAsString, final HttpCollectionSet collectionSet,
        HttpCollectionResource collectionResource) {
    LOG.debug("processResponse:");
    LOG.debug("responseBody = {}", responseBodyAsString);
    LOG.debug("getmatches = {}", collectionSet.getUriDef().getUrl().getMatches());
    List<HttpCollectionAttribute> butes = new LinkedList<HttpCollectionAttribute>();
    int flags = 0;
    if (collectionSet.getUriDef().getUrl().getCanonicalEquivalence()) {
        flags |= Pattern.CANON_EQ;
    }//from  w w  w .  j a v  a2s .  c  o m
    if (collectionSet.getUriDef().getUrl().getCaseInsensitive()) {
        flags |= Pattern.CASE_INSENSITIVE;
    }
    if (collectionSet.getUriDef().getUrl().getComments()) {
        flags |= Pattern.COMMENTS;
    }
    if (collectionSet.getUriDef().getUrl().getDotall()) {
        flags |= Pattern.DOTALL;
    }
    if (collectionSet.getUriDef().getUrl().getLiteral()) {
        flags |= Pattern.LITERAL;
    }
    if (collectionSet.getUriDef().getUrl().getMultiline()) {
        flags |= Pattern.MULTILINE;
    }
    if (collectionSet.getUriDef().getUrl().getUnicodeCase()) {
        flags |= Pattern.UNICODE_CASE;
    }
    if (collectionSet.getUriDef().getUrl().getUnixLines()) {
        flags |= Pattern.UNIX_LINES;
    }
    LOG.debug("flags = {}", flags);
    Pattern p = Pattern.compile(collectionSet.getUriDef().getUrl().getMatches(), flags);
    Matcher m = p.matcher(responseBodyAsString);

    final boolean matches = m.matches();
    if (matches) {
        LOG.debug("processResponse: found matching attributes: {}", matches);
        final List<Attrib> attribDefs = collectionSet.getUriDef().getAttributes().getAttribCollection();
        final AttributeGroupType groupType = new AttributeGroupType(collectionSet.getUriDef().getName(),
                AttributeGroupType.IF_TYPE_ALL);

        final List<Locale> locales = new ArrayList<Locale>();
        if (responseLocale != null) {
            locales.add(responseLocale);
        }
        locales.add(Locale.getDefault());
        if (Locale.getDefault() != Locale.ENGLISH) {
            locales.add(Locale.ENGLISH);
        }

        for (final Attrib attribDef : attribDefs) {
            final String type = attribDef.getType();
            String value = null;
            try {
                value = m.group(attribDef.getMatchGroup());
            } catch (final IndexOutOfBoundsException e) {
                LOG.error(
                        "IndexOutOfBoundsException thrown while trying to find regex group, your regex does not contain the following group index: {}",
                        attribDef.getMatchGroup());
                LOG.error("Regex statement: {}", collectionSet.getUriDef().getUrl().getMatches());
                continue;
            }

            if (!type.matches("^([Oo](ctet|CTET)[Ss](tring|TRING))|([Ss](tring|TRING))$")) {
                Number num = null;
                for (final Locale locale : locales) {
                    try {
                        num = NumberFormat.getNumberInstance(locale).parse(value);
                        LOG.debug("processResponse: found a parsable number with locale \"{}\".", locale);
                        break;
                    } catch (final ParseException e) {
                        LOG.warn(
                                "attribute {} failed to match a parsable number with locale \"{}\"! Matched \"{}\" instead.",
                                attribDef.getAlias(), locale, value);
                    }
                }

                if (num == null) {
                    LOG.warn("processResponse: gave up attempting to parse numeric value, skipping group {}",
                            attribDef.getMatchGroup());
                    continue;
                }

                final HttpCollectionAttribute bute = new HttpCollectionAttribute(collectionResource,
                        new HttpCollectionAttributeType(attribDef, groupType), num);
                LOG.debug("processResponse: adding found numeric attribute: {}", bute);
                butes.add(bute);
            } else {
                HttpCollectionAttribute bute = new HttpCollectionAttribute(collectionResource,
                        new HttpCollectionAttributeType(attribDef, groupType), value);
                LOG.debug("processResponse: adding found string attribute: {}", bute);
                butes.add(bute);
            }
        }
    } else {
        LOG.debug("processResponse: found matching attributes: {}", matches);
    }
    return butes;
}

From source file:org.unitime.timetable.onlinesectioning.custom.purdue.DegreeWorksPlanScraper.java

public String getDegreePlans(OnlineSectioningServer server, XStudent student) throws SectioningException {
    ClientResource resource = null;//www .  ja v  a 2 s. c om
    try {
        resource = new ClientResource(getDegreeWorksApiSite());
        resource.setNext(iClient);
        // resource.addQueryParameter("terms", getBannerTerm(server.getAcademicSession()));
        resource.addQueryParameter("studentId", getBannerId(student));
        String effectiveOnly = getDegreeWorksApiEffectiveOnly();
        if (effectiveOnly != null)
            resource.addQueryParameter("effectiveOnly", effectiveOnly);

        resource.setChallengeResponse(ChallengeScheme.HTTP_BASIC, getDegreeWorksApiUser(),
                getDegreeWorksApiPassword());
        try {
            resource.get(MediaType.APPLICATION_JSON);
        } catch (ResourceException exception) {
            try {
                String response = IOUtils.toString(resource.getResponseEntity().getReader());
                Pattern pattern = Pattern.compile(getDegreeWorksErrorPattern(),
                        Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.UNIX_LINES);
                Matcher match = pattern.matcher(response);
                if (match.find())
                    throw new SectioningException(match.group(1));
            } catch (SectioningException e) {
                throw e;
            } catch (Throwable t) {
                throw exception;
            }
            throw exception;
        }

        return IOUtils.toString(resource.getResponseEntity().getReader());
    } catch (SectioningException e) {
        throw e;
    } catch (Exception e) {
        throw new SectioningException(e.getMessage());
    } finally {
        if (resource != null) {
            if (resource.getResponse() != null)
                resource.getResponse().release();
            resource.release();
        }
    }
}

From source file:pl.otros.logview.gui.message.pattern.PropertyPatternMessageColorizer.java

public void init(InputStream in) throws ConfigurationException {
    propertiesConfiguration = new PropertiesConfiguration();
    propertiesConfiguration.setDelimiterParsingDisabled(true);
    propertiesConfiguration.load(in, "UTF-8");
    configuration = new DataConfiguration(propertiesConfiguration);
    configuration.setDelimiterParsingDisabled(true);
    String pa = configuration.getString(PROP_PATTERN);
    int flags = 0;
    flags = flags | (configuration.getBoolean(PROP_PATTERN_CANON_EQ, false) ? Pattern.CANON_EQ : 0);
    flags = flags//ww  w . ja va 2 s . co  m
            | (configuration.getBoolean(PROP_PATTERN_CASE_INSENSITIVE, false) ? Pattern.CASE_INSENSITIVE : 0);
    flags = flags | (configuration.getBoolean(PROP_PATTERN_COMMENTS, false) ? Pattern.COMMENTS : 0);
    flags = flags | (configuration.getBoolean(PROP_PATTERN_DOTALL, false) ? Pattern.DOTALL : 0);
    flags = flags | (configuration.getBoolean(PROP_PATTERN_LITERAL, false) ? Pattern.LITERAL : 0);
    flags = flags | (configuration.getBoolean(PROP_PATTERN_MULTILINE, false) ? Pattern.MULTILINE : 0);
    flags = flags | (configuration.getBoolean(PROP_PATTERN_UNICODE_CASE, false) ? Pattern.UNICODE_CASE : 0);
    flags = flags | (configuration.getBoolean(PROP_PATTERN_UNIX_LINES, false) ? Pattern.UNIX_LINES : 0);

    pattern = Pattern.compile(pa, flags);
    groupCount = countGroups(pattern);
    name = configuration.getString(PROP_NAME, "NAME NOT SET!");
    description = configuration.getString(PROP_DESCRIPTION, "DESCRIPTION NOT SET!");
    testMessage = configuration.getString(PROP_TEST_MESSAGE, "");
    version = configuration.getInt(PROP_VERSION, 1);
}