Example usage for java.util.regex Matcher end

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

Introduction

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

Prototype

public int end() 

Source Link

Document

Returns the offset after the last character matched.

Usage

From source file:com.google.acre.script.NHttpAsyncUrlfetch.java

private Scriptable callback_result(long start_time, URL url, HttpResponse res, boolean system,
        boolean log_to_user, String response_encoding) {
    BrowserCompatSpecFactory bcsf = new BrowserCompatSpecFactory();
    CookieSpec cspec = bcsf.newInstance(null);
    String protocol = url.getProtocol();
    boolean issecure = ("https".equals(protocol));
    int port = url.getPort();
    if (port == -1)
        port = 80;//w  w w  .  j  av a 2 s  .  co  m
    CookieOrigin origin = new CookieOrigin(url.getHost(), port, url.getPath(), issecure);

    Object body = "";
    int status = res.getStatusLine().getStatusCode();

    Context ctx = Context.getCurrentContext();
    Scriptable out = ctx.newObject(_scope);
    Scriptable headers = ctx.newObject(_scope);
    Scriptable cookies = ctx.newObject(_scope);

    out.put("status", out, status);
    out.put("headers", out, headers);
    out.put("cookies", out, cookies);

    Header content_type_header = null;

    StringBuilder response_header_log = new StringBuilder();
    for (Header h : res.getAllHeaders()) {
        if (h.getName().equalsIgnoreCase("set-cookie")) {
            String set_cookie = h.getValue();
            Matcher m = Pattern.compile("\\s*(([^,]|(,\\s*\\d))+)").matcher(set_cookie);
            while (m.find()) {
                Header ch = new BasicHeader("Set-Cookie", set_cookie.substring(m.start(), m.end()));
                try {
                    List<Cookie> pcookies = cspec.parse(ch, origin);
                    for (Cookie c : pcookies) {
                        cookies.put(c.getName(), cookies, new AcreCookie(c).toJsObject(_scope));
                    }
                } catch (MalformedCookieException e) {
                    throw new RuntimeException(e);
                }
            }
        } else if (h.getName().equalsIgnoreCase("content-type")) {
            content_type_header = h;
        }

        response_header_log.append(h.getName() + ": " + h.getValue() + "\r\n");
        headers.put(h.getName(), headers, h.getValue());
    }

    String charset = null;
    if (content_type_header != null) {
        HeaderElement values[] = content_type_header.getElements();
        if (values.length == 1) {
            NameValuePair param = values[0].getParameterByName("charset");
            if (param != null) {
                charset = param.getValue();
            }
        }
    }

    if (charset == null)
        charset = response_encoding;

    // read body
    HttpEntity ent = res.getEntity();
    try {
        if (ent != null) {
            InputStream res_stream = ent.getContent();
            Header cenc = ent.getContentEncoding();
            if (cenc != null && res_stream != null) {
                HeaderElement[] codecs = cenc.getElements();
                for (HeaderElement codec : codecs) {
                    if (codec.getName().equalsIgnoreCase("gzip")) {
                        res_stream = new GZIPInputStream(res_stream);
                    }
                }
            }

            long first_byte_time = 0;
            long end_time = 0;
            if (content_type_header != null && (content_type_header.getValue().startsWith("image/")
                    || content_type_header.getValue().startsWith("application/octet-stream")
                    || content_type_header.getValue().startsWith("multipart/form-data"))) {
                // HttpClient's InputStream doesn't support mark/reset, so
                // wrap it with one that does.
                BufferedInputStream bufis = new BufferedInputStream(res_stream);
                bufis.mark(2);
                bufis.read();
                first_byte_time = System.currentTimeMillis();
                bufis.reset();
                byte[] data = IOUtils.toByteArray(bufis);

                end_time = System.currentTimeMillis();
                body = new JSBinary();
                ((JSBinary) body).set_data(data);

                try {
                    if (res_stream != null)
                        res_stream.close();
                } catch (IOException e) {
                    // ignore
                }
            } else if (res_stream == null || charset == null) {
                first_byte_time = end_time = System.currentTimeMillis();
                body = "";
            } else {
                StringWriter writer = new StringWriter();
                Reader reader = new InputStreamReader(res_stream, charset);
                int i = reader.read();
                first_byte_time = System.currentTimeMillis();
                writer.write(i);
                IOUtils.copy(reader, writer);
                end_time = System.currentTimeMillis();
                body = writer.toString();

                try {
                    reader.close();
                    writer.close();
                } catch (IOException e) {
                    // ignore
                }
            }

            long reading_time = end_time - first_byte_time;
            long waiting_time = first_byte_time - start_time;

            String httprephdr = response_header_log.toString();
            // XXX need to log start-time of request
            _logger.syslog4j("DEBUG", "urlfetch.response.async", "URL", url.toString(), "Status",
                    Integer.toString(status), "Headers", httprephdr, "Reading time", reading_time,
                    "Waiting time", waiting_time);

            if (system && log_to_user) {
                _response.userlog4j("DEBUG", "urlfetch.response.async", "URL", url.toString(), "Status",
                        Integer.toString(status), "Headers", httprephdr);

            }

            // XXX seems like AcreResponse should be able to use
            // the statistics object to generate x-metaweb-cost
            // given a bit of extra information

            Statistics.instance().collectUrlfetchTime(start_time, first_byte_time, end_time);

            _costCollector.collect((system) ? "asuc" : "auuc").collect((system) ? "asuw" : "auuw",
                    waiting_time);

        }

    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    out.put("body", out, body);

    return out;
}

From source file:it.bradipao.berengar.DbTool.java

public static String jsonMinify(String jsonString) {
    String tokenizer = "\"|(/\\*)|(\\*/)|(//)|\\n|\\r";
    String magic = "(\\\\)*$";
    Boolean in_string = false;/* w  ww .  j ava  2s .com*/
    Boolean in_multiline_comment = false;
    Boolean in_singleline_comment = false;
    String tmp = "";
    String tmp2 = "";
    List<String> new_str = new ArrayList<String>();
    Integer from = 0;
    String lc = "";
    String rc = "";

    Pattern pattern = Pattern.compile(tokenizer);
    Matcher matcher = pattern.matcher(jsonString);

    Pattern magicPattern = Pattern.compile(magic);
    Matcher magicMatcher = null;
    Boolean foundMagic = false;

    if (!matcher.find())
        return jsonString;
    else
        matcher.reset();

    while (matcher.find()) {
        lc = jsonString.substring(0, matcher.start());
        rc = jsonString.substring(matcher.end(), jsonString.length());
        tmp = jsonString.substring(matcher.start(), matcher.end());

        if (!in_multiline_comment && !in_singleline_comment) {
            tmp2 = lc.substring(from);
            if (!in_string)
                tmp2 = tmp2.replaceAll("(\\n|\\r|\\s)*", "");
            new_str.add(tmp2);
        }
        from = matcher.end();

        if (tmp.charAt(0) == '\"' && !in_multiline_comment && !in_singleline_comment) {
            magicMatcher = magicPattern.matcher(lc);
            foundMagic = magicMatcher.find();
            if (!in_string || !foundMagic || (magicMatcher.end() - magicMatcher.start()) % 2 == 0) {
                in_string = !in_string;
            }
            from--;
            rc = jsonString.substring(from);
        } else if (tmp.startsWith("/*") && !in_string && !in_multiline_comment && !in_singleline_comment) {
            in_multiline_comment = true;
        } else if (tmp.startsWith("*/") && !in_string && in_multiline_comment && !in_singleline_comment) {
            in_multiline_comment = false;
        } else if (tmp.startsWith("//") && !in_string && !in_multiline_comment && !in_singleline_comment) {
            in_singleline_comment = true;
        } else if ((tmp.startsWith("\n") || tmp.startsWith("\r")) && !in_string && !in_multiline_comment
                && in_singleline_comment) {
            in_singleline_comment = false;
        } else if (!in_multiline_comment && !in_singleline_comment
                && !tmp.substring(0, 1).matches("\\n|\\r|\\s")) {
            new_str.add(tmp);
        }
    }

    new_str.add(rc);
    StringBuffer sb = new StringBuffer();
    for (String str : new_str)
        sb.append(str);

    return sb.toString();
}

From source file:sapience.injectors.stax.inject.StringBasedStaxStreamInjector.java

/**
 * Helper method, taking a XML string like <ows:Metadata xmlns:ows=\"http://ogc.org/ows\" xmlns:xlink=\"http://wrc.org/xlink\" 
 * xlink:href=\"http://dude.com\"></ows:Metadata> from the reference 
 * and checks if /*w ww . jav  a2  s . c o m*/
 * a  the used prefixes match the globally used ones and
 * b) any of the declared namespaces are redundant 
 * 
 * The same is true for the XPath definition
 * 
 * @param resultingXMLString
 * @param context
 */
private void processNamespace(StringBuilder sb, NamespaceContext global, LocalNamespaceContext local) {

    Matcher prefixMatcher = prefixPattern.matcher(sb);
    Matcher nsMatcher = nsPattern.matcher(sb);
    String prefix;
    String uri;

    /* process the local namespaces */
    while (nsMatcher.find()) {
        int start = nsMatcher.start();
        int end = nsMatcher.end();
        StringBuilder sbu = new StringBuilder(sb.substring(start, end));
        String thisPrefix = sbu.substring(sbu.indexOf(":") + 1, sbu.lastIndexOf("="));
        String thisUri = sbu.substring(sbu.indexOf("\"") + 1, sbu.lastIndexOf("\""));
        // add to local namespace context
        local.put(thisPrefix, thisUri);

        if ((prefix = global.getPrefix(thisUri)) != null) {
            // namespace is registered, let's remove it
            sb.delete(start - 1, end);

            // we have to reset, since we changed the state of the matched string with the deletion
            nsMatcher.reset();
        }

    }

    /* change the prefixes */
    try {
        while (prefixMatcher.find()) {
            int start = prefixMatcher.start();
            int end = prefixMatcher.end();

            String localprefix = sb.substring(start + 1, end - 1);
            if ((global.getNamespaceURI(localprefix) == null)
                    && (uri = local.getNamespaceURI(localprefix)) != null) {
                // get the other prefix
                prefix = global.getPrefix(uri);

                if ((prefix != null) && (!(localprefix.contentEquals(prefix)))) {
                    sb.replace(start + 1, end - 1, prefix);
                    prefixMatcher.reset();
                }
            }
        }
    } catch (StringIndexOutOfBoundsException e) {
        // we do nothing here
    }

}

From source file:com.esri.geoportal.commons.csw.client.impl.Client.java

/**
 * Creates fixed record./*from   w ww.  j a  v a 2 s .  c  om*/
 *
 * @param cswResponse response
 * @param recordId record id.
 * @return record data
 */
private String makeResourceFromCswResponse(String cswResponse, String recordId) {
    Pattern cswRecordStart = Pattern.compile("<csw:Record>");
    Pattern cswRecordEnd = Pattern.compile("</csw:Record>");

    Matcher cswRecordStartMatcher = cswRecordStart.matcher(cswResponse);
    Matcher cswRecordEndMatcher = cswRecordEnd.matcher(cswResponse);

    if (cswRecordStartMatcher.find() && cswRecordEndMatcher.find()) {
        String dcResponse = cswResponse.substring(cswRecordStartMatcher.end(), cswRecordEndMatcher.start());
        StringBuilder xml = new StringBuilder();
        xml.append(
                "<?xml version=\"1.0\"?><rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:ows=\"http://www.opengis.net/ows\" xmlns:dct=\"http://purl.org/dc/terms/\">");
        xml.append("<rdf:Description ");
        if (recordId.length() > 0) {
            xml.append("rdf:about=\"").append(StringEscapeUtils.escapeXml11(recordId)).append("\"");
        }
        xml.append(">");

        xml.append(dcResponse);

        xml.append("</rdf:Description>");
        xml.append("</rdf:RDF>");

        return xml.toString();
    }

    return cswResponse;
}

From source file:azkaban.utils.PropsUtils.java

private static String resolveVariableReplacement(String value, Props props,
        LinkedHashSet<String> visitedVariables) {
    StringBuffer buffer = new StringBuffer();
    int startIndex = 0;

    Matcher matcher = VARIABLE_REPLACEMENT_PATTERN.matcher(value);
    while (matcher.find(startIndex)) {
        if (startIndex < matcher.start()) {
            // Copy everything up front to the buffer
            buffer.append(value.substring(startIndex, matcher.start()));
        }/*from  www .j av a2s .  c  o  m*/

        String subVariable = matcher.group(1);
        // Detected a cycle
        if (visitedVariables.contains(subVariable)) {
            throw new IllegalArgumentException(
                    String.format("Circular variable substitution found: [%s] -> [%s]",
                            StringUtils.join(visitedVariables, "->"), subVariable));
        } else {
            // Add substitute variable and recurse.
            String replacement = props.get(subVariable);
            visitedVariables.add(subVariable);

            if (replacement == null) {
                throw new UndefinedPropertyException(
                        String.format("Could not find variable substitution for variable(s) [%s]",
                                StringUtils.join(visitedVariables, "->")));
            }

            buffer.append(resolveVariableReplacement(replacement, props, visitedVariables));
            visitedVariables.remove(subVariable);
        }

        startIndex = matcher.end();
    }

    if (startIndex < value.length()) {
        buffer.append(value.substring(startIndex));
    }

    return buffer.toString();
}

From source file:be.makercafe.apps.makerbench.editors.JFXMillEditor.java

private static StyleSpans<Collection<String>> computeHighlighting(String text) {
    Matcher matcher = PATTERN.matcher(text);
    int lastKwEnd = 0;
    StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>();
    while (matcher.find()) {
        String styleClass = matcher.group("KEYWORD") != null ? "keyword"
                : matcher.group("PAREN") != null ? "paren"
                        : matcher.group("BRACE") != null ? "brace"
                                : matcher.group("BRACKET") != null ? "bracket"
                                        : matcher.group("SEMICOLON") != null ? "semicolon"
                                                : matcher.group("STRING") != null ? "string"
                                                        : matcher.group("COMMENT") != null ? "comment"
                                                                : null; /*
                                                                        * never
                                                                        * happens
                                                                        */
        assert styleClass != null;
        spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd);
        spansBuilder.add(Collections.singleton(styleClass), matcher.end() - matcher.start());
        lastKwEnd = matcher.end();// www  . j  a  va 2s .  c  o  m
    }
    spansBuilder.add(Collections.emptyList(), text.length() - lastKwEnd);
    return spansBuilder.create();
}

From source file:org.openmrs.module.rheashradapter.util.RsmsNotificationHandler.java

private Location getLocation(MSH msh) throws HL7Exception {
    String hl7Location = msh.getSendingFacility().getNamespaceID().toString();
    Location location = null;/*from w  w w  . j av a  2  s  . c  om*/

    List<Location> locationsList = Context.getLocationService().getAllLocations();
    for (Location l : locationsList) {
        String fosaid = l.getDescription();
        String elid = null;

        if (fosaid != null) {
            final Matcher matcher = Pattern.compile(":").matcher(fosaid);
            if (matcher.find()) {
                elid = fosaid.substring(matcher.end()).trim();
                if (elid.equals(hl7Location)) {
                    location = l;
                }
            }
        }
    }

    return location;
}

From source file:com.xpn.xwiki.internal.template.WikiTemplateRenderer.java

private StringContent getStringContent(String template) throws IOException, ParseException {
    InputSource source = getTemplateStream(template);

    if (source == null) {
        return null;
    }//from  w  w  w.  ja v  a  2s  .c  om

    String content;
    try {
        if (source instanceof StringInputSource) {
            content = source.toString();
        } else if (source instanceof ReaderInputSource) {
            content = IOUtils.toString(((ReaderInputSource) source).getReader());
        } else if (source instanceof InputStreamInputSource) {
            content = IOUtils.toString(((InputStreamInputSource) source).getInputStream(), "UTF-8");
        } else {
            // Unsupported type
            return null;
        }
    } finally {
        source.close();
    }

    Matcher matcher = FIRSTLINE.matcher(content);

    if (matcher.find()) {
        content = content.substring(matcher.end());

        String syntaxString = matcher.group(2);
        Syntax syntax = this.syntaxFactory.createSyntaxFromIdString(syntaxString);

        String mode = matcher.group(1);
        switch (mode) {
        case "source":
            return new StringContent(content, syntax, null);
        case "raw":
            return new StringContent(content, null, syntax);
        default:
            break;
        }
    }

    // The default is xhtml to support old templates
    return new StringContent(content, null, Syntax.XHTML_1_0);
}

From source file:com.logsniffer.reader.support.FormattedTextReader.java

/**
 * @param formatPattern/*  ww w . jav  a  2s  .  com*/
 *            the formatPattern to set
 */
@Override
protected void init() throws FormatException {
    super.init();
    if (parsingPattern == null) {
        // Only if not yet parsed
        if (formatPattern != null) {
            final ArrayList<Specifier> specs = new ArrayList<Specifier>();
            final StringBuilder parsingPatternStr = new StringBuilder();
            final Matcher m = SPECIFIER_PATTERN.matcher(formatPattern);
            int leftPos = 0;
            while (m.find()) {
                if (m.start() > leftPos) {
                    parsingPatternStr.append(Pattern.quote(formatPattern.substring(leftPos, m.start())));
                }
                leftPos = m.end();
                final String minWidthStr = m.group(2);
                final String maxWidthStr = m.group(4);
                final String specName = m.group(5);
                final String specModifier = m.group(7);
                int minWidth = -1;
                if (minWidthStr != null && minWidthStr.length() > 0) {
                    minWidth = Integer.parseInt(minWidthStr);
                }
                int maxWidth = -1;
                if (maxWidthStr != null && maxWidthStr.length() > 0) {
                    maxWidth = Integer.parseInt(maxWidthStr);
                }
                Specifier spec = null;
                for (final Specifier specTest : createSupportedSpecifiers()) {
                    if (specTest.getSpecifierKey().equals(specName)) {
                        spec = specTest;
                        break;
                    }
                }
                if (spec == null) {
                    logger.debug(
                            "Format specifier {} in pattern '{}' is unknown and will be parsed as simple text pattern",
                            specName, formatPattern);
                    spec = new ArbitraryTextSpecifier(specName, false);
                } else if (spec instanceof IgnoreSpecifier) {
                    logger.debug("Format specifier '{}' in pattern '{}' is ignored", specName, formatPattern);
                    continue;
                }
                spec.setMaxWidth(maxWidth);
                spec.setMinWidth(minWidth);
                spec.setModifier(specModifier);
                parsingPatternStr.append("(");
                parsingPatternStr.append(spec.getRegex());
                parsingPatternStr.append(")");
                spec.fieldName = specifiersFieldMapping.get(specName);
                if (StringUtils.isBlank(spec.fieldName)) {
                    spec.fieldName = specName;
                }
                specs.add(spec);
            }
            parsingPatternStr.append(Pattern.quote(formatPattern.substring(leftPos)));
            parsingPattern = Pattern.compile(parsingPatternStr.toString());
            parsingSpecifiers = specs.toArray(new Specifier[specs.size()]);
            logger.debug("Prepared parsing pattern '{}' for log4j conversion pattern: {}", parsingPattern,
                    formatPattern);
        } else {
            parsingSpecifiers = null;
            parsingPattern = null;
        }
    }
}

From source file:gtu.youtube.JavaYoutubeVideoUrlHandler.java

private List<DataFinal> findDataGroup(String baseUrlString) {
    List<DataFinal> rtnLst = new ArrayList<DataFinal>();
    List<DataConfig> lst = new ArrayList<DataConfig>();

    // parse Paramster
    for (Field f : DataFinal.class.getDeclaredFields()) {
        Pattern ptn = Pattern.compile(f.getName());
        Matcher mth = ptn.matcher(baseUrlString);
        while (mth.find()) {

            String paramStr = "";
            if (f.getName().equals("url")) {
                paramStr = this.getParamStr(baseUrlString, mth.end() + "=".length(), "\\,");
            } else if (f.getName().equals("quality")) {
                paramStr = this.getParamStr_forCatch(f.getName(), baseUrlString, mth.end() + "=".length(),
                        "\\w+");
            } else if (f.getName().equals("type")) {
                paramStr = this.getParamStr_forCatch(f.getName(), baseUrlString, mth.end() + "=".length(),
                        "video\\/\\w+\\;\\scodecs\\=\".*?\"");
            } else if (f.getName().equals("itag")) {
                paramStr = this.getParamStr_forCatch(f.getName(), baseUrlString, mth.end() + "=".length(),
                        "\\d+");
            }/* www .j  av a  2  s  .  c  om*/

            DataConfig d = new DataConfig();
            d.paramStr = paramStr;
            d.name = f.getName();
            d.start = mth.start();
            d.end = mth.end() + ("=" + paramStr).length();

            lst.add(d);
        }
    }

    // URL
    for (int ii = 0; ii < lst.size(); ii++) {
        DataConfig d1 = lst.get(ii);

        if (d1.name.equals("url")) {
            // ? type
            d1.paramStr = d1.paramStr.replaceAll("type\\=video\\/\\w+\\;\\scodecs\\=\"[\\w\\.]+,?", "");
            d1.paramStr = d1.paramStr.replaceAll("type\\=video\\/\\w+\\;\\scodecs\\=\"[\\w\\.]+,\\s[\\w\\.]+\"",
                    "");

            // ? quality
            d1.paramStr = d1.paramStr.replaceAll("quality\\=\\w+", "");

            System.out.println("correct url = " + d1.paramStr);
        }
    }

    // Grouping
    List<DataConfig> urlLst = getMatchIndexDataConfig("url", lst);
    for (int ii = 0; ii < urlLst.size(); ii++) {
        DataConfig urlData = urlLst.get(ii);
        DataConfig qualityData = getIndex("quality", getMatchIndexDataConfig("quality", lst), ii);
        DataConfig typeData = getIndex("type", getMatchIndexDataConfig("type", lst), ii);

        DataFinal dd = new DataFinal();
        dd.url = urlData;
        dd.quality = qualityData;
        dd.type = typeData;
        dd.itag = getMockItag(urlData.paramStr);

        rtnLst.add(dd);
    }
    return rtnLst;
}