List of usage examples for java.util.regex Matcher end
public int end(String name)
From source file:com.edgenius.wiki.render.filter.MacroFilter.java
private void resetRegion(final int initPos, final CharSequence input, final List<Region> list) { final List<Region> pairRegions = new ArrayList<Region>(); singleMacroProvider.replaceByTokenVisitor(input, new TokenVisitor<Matcher>() { public void handleMatch(StringBuffer buffer, Matcher result) { String macroName = result.group(1); if (macroName != null && !macroName.startsWith("$")) { Macro macro = macroMgr.getMacro(macroName); if (macro != null && macro.isPaired()) { String body = result.group(0); int start = result.start(0); int end = result.end(0); Region pair = new Region(start, end); //no parameter, then mark as unknown, otherwise, must be a start macro if (StringUtils.isBlank(result.group(2))) { pair.setKey(MACRO_REGION_KEY_UNKNOWN); } else { pair.setKey(MACRO_REGION_KEY_START); }//from www . j a v a 2 s .c o m //just for temporary to remember the macro name... pair.setContent(macroName); pair.setBody(body); //sum to list pairRegions.add(pair); } } } }); int size = pairRegions.size(); if (size > 0) { StringBuffer inputBuf = new StringBuffer(input); for (int idx = 0; idx < size; idx++) { Region reg = pairRegions.get(idx); int deep = 0; Region pair = null; //looking for pairs... for (int chIdx = idx + 1; chIdx < size; chIdx++) { Region next = pairRegions.get(chIdx); if (StringUtils.equalsIgnoreCase(reg.getContent(), next.getContent())) { //start is unknown (no attribute), then end must be unknown if (MACRO_REGION_KEY_UNKNOWN.equals(reg.getKey()) && MACRO_REGION_KEY_UNKNOWN.equals(next.getKey())) { //matched pair = next; //skip all internal node - which is handle by embedded recursive idx = chIdx; break; } if (MACRO_REGION_KEY_START.equals(reg.getKey()) && MACRO_REGION_KEY_UNKNOWN.equals(next.getKey())) { if (deep == 0) { //matched; pair = next; //skip all internal node - which is handle by embedded recursive idx = chIdx; break; } else { //just another inner same name macro matched, deep minus deep--; } } if (MACRO_REGION_KEY_START.equals(next.getKey())) { //ok, it gets another start, in 4th scenarios - then add deep deep++; } } } //ok, success find paired if (pair != null) { int start = initPos + reg.getStart(); int end = initPos + pair.getEnd(); int contentStart = initPos + reg.getEnd(); int contentEnd = initPos + pair.getStart(); String macroName = reg.getContent(); Macro macro = macroMgr.getMacro(macroName); boolean immutable = macro instanceof ImmutableContentMacro; list.add(new Region(MacroFilter.this, immutable, start, end, contentStart, contentEnd)); if (macro.isProcessEmbedded() && (end > start)) { resetRegion(contentStart, inputBuf.subSequence(contentStart - initPos, contentEnd - initPos), list); } } } } }
From source file:com.streamsets.pipeline.stage.processor.fieldmask.FieldMaskProcessor.java
@VisibleForTesting String regExMask(Field field, FieldMaskConfig fieldMaskConfig) { String value = field.getValueAsString(); Matcher matcher = regExToPatternMap.get(fieldMaskConfig.regex).matcher(value); if (matcher.matches()) { int groupCount = matcher.groupCount(); //create a masked string of the same length as the original string StringBuilder resultString = new StringBuilder(); for (int i = 0; i < value.length(); i++) { resultString.append(MASK_CHAR); }/*from w w w .j a va2 s. c o m*/ //for each group that needs to be shown, replace the masked string with the original string characters at those //positions Set<Integer> groupsToShow = regexToGroupsToShowMap.get(fieldMaskConfig.regex); if (groupsToShow != null && !groupsToShow.isEmpty()) { for (int i = 1; i <= groupCount; i++) { if (groupsToShow.contains(i)) { resultString.replace(matcher.start(i), matcher.end(i), matcher.group(i)); } } } return resultString.toString(); } return field.getValueAsString(); }
From source file:mobi.jenkinsci.server.core.net.ProxyUtil.java
private String replaceLinks(final String linkPattern, final int linkIndex, final String newLinkPrefix, final String remoteBasePath, final String content) { log.debug("Rewriting links to basePath " + remoteBasePath); final boolean isUrlEncodingNeeded = newLinkPrefix.indexOf('?') >= 0; final Pattern pattern = Pattern.compile(linkPattern, Pattern.CASE_INSENSITIVE | Pattern.DOTALL); final Matcher m = pattern.matcher(content); // determines if the link is absolute final StringBuffer buffer = new StringBuffer(); int lastEnd = 0; while (m.find()) { final String link = m.group(linkIndex).trim(); if (link.startsWith("data")) { continue; }/*from w w w .j a v a2 s.c om*/ if (!isFullLinkWithProtocol(link)) { String newURI = getRelativeLink(newLinkPrefix, remoteBasePath, isUrlEncodingNeeded, link); buffer.append(getSubstringBetweenLinks(linkIndex, content, m, lastEnd)); buffer.append(newURI.trim()); lastEnd = m.end(linkIndex); } } buffer.append(content.substring(lastEnd)); return buffer.toString(); }
From source file:com.hichinaschool.flashcards.libanki.Media.java
/** * Percent-escape UTF-8 characters in local image filenames. * /* w w w .j a v a2s. com*/ * @param string The string to search for image references and escape the filenames. * @return The string with the filenames of any local images percent-escaped as UTF-8. */ public String escapeImages(String string) { Matcher m = fMediaRegexps[1].matcher(string); StringBuffer sb = new StringBuffer(); while (m.find()) { if (fRemoteFilePattern.matcher(m.group(2)).find()) { m.appendReplacement(sb, m.group()); } else { String tagBegin = m.group(1).substring(0, m.start(2)); String fname = m.group(2); String tagEnd = m.group(1).substring(m.end(2)); String tag = tagBegin + Uri.encode(fname) + tagEnd; m.appendReplacement(sb, tag); } } m.appendTail(sb); return sb.toString(); }
From source file:com.cyberway.issue.crawler.extractor.ExtractorHTML.java
/** * Run extractor.// w w w . j a v a 2s . c o m * This method is package visible to ease testing. * @param curi CrawlURI we're processing. * @param cs Sequence from underlying ReplayCharSequence. This * is TRANSIENT data. Make a copy if you want the data to live outside * of this extractors' lifetime. */ void extract(CrawlURI curi, CharSequence cs) { Matcher tags = TextUtils.getMatcher(RELEVANT_TAG_EXTRACTOR, cs); while (tags.find()) { if (Thread.interrupted()) { break; } if (tags.start(8) > 0) { // comment match // for now do nothing } else if (tags.start(7) > 0) { // <meta> match int start = tags.start(5); int end = tags.end(5); assert start >= 0 : "Start is: " + start + ", " + curi; assert end >= 0 : "End is :" + end + ", " + curi; if (processMeta(curi, cs.subSequence(start, end))) { // meta tag included NOFOLLOW; abort processing break; } } else if (tags.start(5) > 0) { // generic <whatever> match int start5 = tags.start(5); int end5 = tags.end(5); assert start5 >= 0 : "Start is: " + start5 + ", " + curi; assert end5 >= 0 : "End is :" + end5 + ", " + curi; int start6 = tags.start(6); int end6 = tags.end(6); assert start6 >= 0 : "Start is: " + start6 + ", " + curi; assert end6 >= 0 : "End is :" + end6 + ", " + curi; processGeneralTag(curi, cs.subSequence(start6, end6), cs.subSequence(start5, end5)); } else if (tags.start(1) > 0) { // <script> match int start = tags.start(1); int end = tags.end(1); assert start >= 0 : "Start is: " + start + ", " + curi; assert end >= 0 : "End is :" + end + ", " + curi; assert tags.end(2) >= 0 : "Tags.end(2) illegal " + tags.end(2) + ", " + curi; processScript(curi, cs.subSequence(start, end), tags.end(2) - start); } else if (tags.start(3) > 0) { // <style... match int start = tags.start(3); int end = tags.end(3); assert start >= 0 : "Start is: " + start + ", " + curi; assert end >= 0 : "End is :" + end + ", " + curi; assert tags.end(4) >= 0 : "Tags.end(4) illegal " + tags.end(4) + ", " + curi; processStyle(curi, cs.subSequence(start, end), tags.end(4) - start); } } TextUtils.recycleMatcher(tags); }
From source file:io.kahu.hawaii.util.logger.DefaultLogManager.java
void maskPasswords(final LoggingConfiguration config) throws JSONException { // Look at all fields that may contain a URL, and mask passwords in // query string parameters for (String field : config.getUrlFields()) { Object value = getContext(field); if (value == null || value == JSONObject.NULL || value == JSONObject.EXPLICIT_NULL) { continue; }/* w w w . jav a 2 s . c o m*/ if (!(value instanceof String)) { // If it's not a string, just play it safe and replace the // entire parameter putContext(field, MASKED_PASSWORD); continue; } // It's a String so iterate over the QS parameters String url = (String) value; int p1 = url.indexOf('?'); boolean changed = false; while (p1 >= 0) { int p2 = url.indexOf('=', p1 + 1); if (p2 < 0) { break; } String qsParam = url.substring(p1 + 1, p2); int p3 = url.indexOf('&', p2 + 1); if (p3 < 0) { p3 = url.length(); } String qsValue = url.substring(p2 + 1, p3); if (config.getPasswordParameters().contains(qsParam)) { qsValue = MASKED_PASSWORD; url = url.substring(0, p2 + 1) + qsValue + url.substring(p3); changed = true; } p1 = url.indexOf('&', p1 + 1); } if (changed) { putContext(field, url); } } // Next, look at all parameter objects, which are String -> List of // String mappings for (String field : config.getParameterFields()) { Object value = getContext(field); if (value == null || value == JSONObject.NULL || value == JSONObject.EXPLICIT_NULL) { continue; } if (!(value instanceof JSONObject)) { // If it's not a JSONObject, just play it safe and replace the // entire parameter putContext(field, MASKED_PASSWORD); continue; } // It's a JSONObject, so discover and nuke all possible password // fields JSONObject params = (JSONObject) value; boolean changed = false; for (String key : config.getPasswordParameters()) { if (params.has(key)) { JSONArray a = new JSONArray(); a.put(MASKED_PASSWORD); params.put(key, a); changed = true; } } if (changed) { putContext(field, params); } } // Next, look at all request/response headers, which are stored as an // array of strings for (String field : config.getHeaderFields()) { Object value = getContext(field); if (value == null || value == JSONObject.NULL || value == JSONObject.EXPLICIT_NULL) { continue; } if (!(value instanceof JSONArray)) { // If it's not a JSONArray, just play it safe and replace the // entire parameter putContext(field, MASKED_PASSWORD); continue; } // It's a JSONArray, so discover and nuke all possible password // headers JSONArray headers = (JSONArray) value; boolean changed = false; for (String key : config.getPasswordParameters()) { for (int i = 0; i < headers.length(); i++) { String header = headers.getString(i); if (header.startsWith(key + ":")) { headers.put(i, key + ": " + MASKED_PASSWORD); changed = true; } } } if (changed) { putContext(field, headers); } } // Finally, look at all body fields for password patterns for (String field : config.getBodyFields()) { Object value = getContext(field); if (value == null || value == JSONObject.NULL || value == JSONObject.EXPLICIT_NULL) { continue; } if (!(value instanceof String)) { // If it's not a string, just play it safe and replace the // entire parameter putContext(field, MASKED_PASSWORD); continue; } // It's a String, so apply all password patterns to mask the // passwords String body = (String) value; boolean changed = false; if (looksLikeJson(body)) { body = removePasswordFieldsFromJsonBody(body, Arrays.asList(config.getBodyPasswordFields())); changed = (body != null); } else { for (Pattern pattern : config.getBodyPasswordPatterns()) { Matcher m = pattern.matcher(body); int i = 0; while (m.find(i)) { body = body.substring(0, m.start(1)) + MASKED_PASSWORD + body.substring(m.end(1)); i = m.end() - m.group(1).length() + MASKED_PASSWORD.length(); m = pattern.matcher(body); changed = true; } } } if (changed) { putContext(field, body); } } }
From source file:mergedoc.core.Comment.java
/** * ? Java ??? Javadoc ??????// w w w . j av a 2s.c om * ???????????????? * <p> * @param tagName ?? * @return ? */ private List<String> createWrapTagList(String tagName) { List<String> tagValues = null; if (srcBody.contains(tagName)) { String undeco = FastStringUtils.replaceAll(srcBody, "(?m)^ *\\* *", ""); Pattern pat = PatternCache.getPattern("(?s)" + tagName + " *(.*?)([^\\{]@\\w+|/\\s*$)"); Matcher mat = pat.matcher(undeco); for (int start = 0; mat.find(start); start = mat.end(1)) { if (tagValues == null) { tagValues = new LinkedList<String>(); } tagValues.add(mat.group(1)); } } return tagValues; }
From source file:com.github.gekoh.yagen.ddl.CreateDDL.java
private static String getIfExistsDropStatement(Dialect dialect, String sql, String name) { Matcher matcher = DROP_TABLE_PATTERN.matcher(sql); if (matcher.find()) { StringBuilder res = new StringBuilder(); res.append(sql.substring(0, matcher.start(1) >= 0 ? matcher.start(1) : matcher.start(2))) .append(getNameAndIfExistsWhenSupported(dialect, name == null ? matcher.group(2) : name)) .append(sql.substring(matcher.end(3) >= 0 ? matcher.end(3) : matcher.end(2))); sql = res.toString();/*from w w w .j av a 2 s. co m*/ } return sql; }
From source file:com.edgenius.wiki.render.filter.MacroFilter.java
/** *///w w w .j a va 2 s .c o m private void checkGroup(final int initPos, CharSequence input, final LinkedList<GroupProcessor> stack, List<GroupProcessor> processors) { final List<Region> pairRegions = new ArrayList<Region>(); singleMacroProvider.replaceByTokenVisitor(input, new TokenVisitor<Matcher>() { public void handleMatch(StringBuffer buffer, Matcher result) { String macroName = result.group(1); if (macroName != null && !macroName.startsWith("$")) { Macro macro = macroMgr.getMacro(macroName); if (macro != null) { //IMPORTANT: here does not check Macro.isPair() and also put it into pairRegions for following process //it is the sequence of process must keep consistant with physical sequence in input text, //for example, {table}{cell}...{rowdiv}, rowdiv is single and must be after cell int start = result.start(0); int end = result.end(0); Region pair = new Region(start, end); //no parameter, then mark as unknown, otherwise, must be a start macro if (StringUtils.isBlank(result.group(2))) { pair.setKey(MACRO_REGION_KEY_UNKNOWN); } else { pair.setKey(MACRO_REGION_KEY_START); } //just for temporary to remember the macro name... pair.setContent(macroName); //sum to list pairRegions.add(pair); } } } }); int size = pairRegions.size(); if (size > 0) { StringBuffer inputBuf = new StringBuffer(input); for (int idx = 0; idx < size; idx++) { Region reg = pairRegions.get(idx); Macro macro = macroMgr.getMacro(reg.getContent()); if (macro.isPaired()) { int deep = 0; Region pair = null; //looking for pairs... for (int chIdx = idx + 1; chIdx < size; chIdx++) { Region next = pairRegions.get(chIdx); if (StringUtils.equalsIgnoreCase(reg.getContent(), next.getContent())) { //start is unknown (no attribute), then end must be unknown if (MACRO_REGION_KEY_UNKNOWN.equals(reg.getKey()) && MACRO_REGION_KEY_UNKNOWN.equals(next.getKey())) { //matched pair = next; //skip all internal node - which is handle by embedded recursive idx = chIdx; break; } if (MACRO_REGION_KEY_START.equals(reg.getKey()) && MACRO_REGION_KEY_UNKNOWN.equals(next.getKey())) { if (deep == 0) { //matched; pair = next; //skip all internal node - which is handle by embedded recursive idx = chIdx; break; } else { //just another inner same name macro matched, deep minus 1 deep--; } } if (MACRO_REGION_KEY_START.equals(next.getKey())) { //ok, it gets another start, in 4th scenarios - then add deep deep++; } } } //ok, success find paired if (pair != null) { int start = initPos + reg.getStart(); int end = initPos + pair.getEnd(); int contentStart = initPos + reg.getEnd(); int contentEnd = initPos + pair.getStart(); GroupProcessor currProcessor = stack.size() == 0 ? null : stack.getLast(); if (currProcessor != null) { currProcessor.adoptChild(macro, start, end); } if (macro.isProcessEmbedded() && (end > start)) { if (macro.hasChildren() != null) { stack.add(((GroupProcessorMacro) macro).newGroupProcessor(macro, start, end)); } checkGroup(contentStart, inputBuf.subSequence(contentStart - initPos, contentEnd - initPos), stack, processors); if (macro.hasChildren() != null) { //pop the current one, means it is a completed GroupProcessor processors.add(stack.removeLast()); } } } } else { //single macro - directly detect if it is child GroupProcessor currProcessor = stack.size() == 0 ? null : stack.getLast(); if (currProcessor != null) { currProcessor.adoptChild(macro, initPos + reg.getStart(), initPos + reg.getEnd()); } } } } }
From source file:cn.dreampie.resource.LessSource.java
private void resolveImports() throws IOException { Matcher importMatcher = IMPORT_PATTERN.matcher(normalizedContent); while (importMatcher.find()) { String importedResource = importMatcher.group(5); importedResource = importedResource.matches(".*\\.(le?|c)ss$") ? importedResource : importedResource + ".less"; String importType = importMatcher.group(3) == null ? importedResource.substring(importedResource.lastIndexOf(".") + 1) : importMatcher.group(3); if (importType.equals("less")) { logger.debug("Importing %s", importedResource); if (!imports.containsKey(importedResource)) { LessSource importedLessSource = new LessSource(getImportedResource(importedResource)); imports.put(importedResource, importedLessSource); normalizedContent = includeImportedContent(importedLessSource, importMatcher); importMatcher = IMPORT_PATTERN.matcher(normalizedContent); } else { normalizedContent = normalizedContent.substring(0, importMatcher.start(1)) + normalizedContent.substring(importMatcher.end(1)); importMatcher = IMPORT_PATTERN.matcher(normalizedContent); }/*from www . ja v a 2 s . c o m*/ } } }