List of usage examples for java.util.regex Matcher appendReplacement
public Matcher appendReplacement(StringBuilder sb, String replacement)
From source file:org.codice.ddf.spatial.ogc.csw.catalog.common.source.CswFilterFactory.java
/** * The WKT passed into the spatial methods has the coordinates ordered in LON/LAT. This method * will convert the WKT to LAT/LON ordering. *//*from www . j a va 2s . com*/ private String convertWktToLatLonOrdering(String wktInLonLat) { if (cswAxisOrder != CswAxisOrder.LON_LAT) { LOGGER.debug("Converting WKT from LON/LAT coordinate ordering to LAT/LON coordinate ordering."); // Normalize all whitespace in WKT before processing. wktInLonLat = normalizeWhitespaceInWkt(wktInLonLat); Matcher matcher = COORD_PATTERN.matcher(wktInLonLat); StringBuffer stringBuffer = new StringBuffer(); while (matcher.find()) { String lonLatCoord = matcher.group(); String latLonCoord = StringUtils.reverseDelimited(lonLatCoord, ' '); LOGGER.debug("Converted LON/LAT coord: ({}) to LAT/LON coord: ({}).", lonLatCoord, latLonCoord); matcher.appendReplacement(stringBuffer, latLonCoord); } matcher.appendTail(stringBuffer); String wktInLatLon = stringBuffer.toString(); LOGGER.debug("Original WKT with coords in LON/LAT ordering: {}", wktInLonLat); LOGGER.debug("Converted WKT with coords in LAT/LON ordering: {}", wktInLatLon); return wktInLatLon; } else { LOGGER.debug("The configured CSW source requires coordinates in LON/LAT ordering."); return wktInLonLat; } }
From source file:org.codice.ddf.spatial.ogc.csw.catalog.source.CswFilterFactory.java
/** * The WKT passed into the spatial methods has the coordinates ordered in LON/LAT. This method * will convert the WKT to LAT/LON ordering. *///from w w w.ja v a 2s . c o m private String convertWktToLatLonOrdering(String wktInLonLat) { if (!isLonLatOrder) { LOGGER.debug("Converting WKT from LON/LAT coordinate ordering to LAT/LON coordinate ordering."); // Normalize all whitespace in WKT before processing. wktInLonLat = normalizeWhitespaceInWkt(wktInLonLat); Matcher matcher = COORD_PATTERN.matcher(wktInLonLat); StringBuffer stringBuffer = new StringBuffer(); while (matcher.find()) { String lonLatCoord = matcher.group(); String latLonCoord = StringUtils.reverseDelimited(lonLatCoord, ' '); LOGGER.debug("Converted LON/LAT coord: ({}) to LAT/LON coord: ({}).", lonLatCoord, latLonCoord); matcher.appendReplacement(stringBuffer, latLonCoord); } matcher.appendTail(stringBuffer); String wktInLatLon = stringBuffer.toString(); LOGGER.debug("Original WKT with coords in LON/LAT ordering: {}", wktInLonLat); LOGGER.debug("Converted WKT with coords in LAT/LON ordering: {}", wktInLatLon); return wktInLatLon; } else { LOGGER.debug("The configured CSW source requires coordinates in LON/LAT ordering."); return wktInLonLat; } }
From source file:com.android.tools.idea.editors.theme.ThemeEditorUtilsTest.java
private void compareWithGoldenFile(@NotNull String text, @NotNull String goldenFile) throws IOException { final File file = new File(goldenFile); String goldenText = FileUtils.readFileToString(file); goldenText = goldenText.replace("$$ANDROID_SDK_PATH", sdkPlatformPath); Matcher matcher = OPERATION_PATTERN.matcher(goldenText); StringBuffer processedGoldenText = new StringBuffer(); while (matcher.find()) { String operation = matcher.group(1); String value = matcher.group(2); if (operation.equals("MAKE_URL")) { value = SdkUtils.fileToUrl(new File(value)).toString(); } else if (operation.equals("MAKE_SYSTEM_DEPENDENT_PATH")) { value = PathUtil.toSystemDependentName(value); // escape all the backslashes so they don't get treated as backreferences by the regex engine later if (File.separatorChar == '\\') { value = value.replace("\\", "\\\\"); }/*from ww w . ja v a 2s. c o m*/ } else { // Ignore if we don't know how to handle that - may be accidental pattern match continue; } matcher.appendReplacement(processedGoldenText, value); } matcher.appendTail(processedGoldenText); // Add line breaks after "<BR/>" tags for results that are easier to read. // Golden files are already have these line breaks, so there's no need to process them the same way. text = StringUtil.replace(text, "<BR/>", "<BR/>\n"); assertEquals(String.format("Comparing to golden file %s failed", file.getCanonicalPath()), processedGoldenText.toString(), text); }
From source file:com.ichi2.libanki.importer.Anki2Importer.java
private String _mungeMedia(long mid, String fields) { // running splitFields() on every note is fairly expensive and actually not necessary //String[] fs = Utils.splitFields(fields); //for (int i = 0; i < fs.length; ++i) { for (Pattern p : Media.mRegexps) { //Matcher m = p.matcher(fs[i]); Matcher m = p.matcher(fields); StringBuffer sb = new StringBuffer(); int fnameIdx = Media.indexOfFname(p); while (m.find()) { String fname = m.group(fnameIdx); BufferedInputStream srcData = _srcMediaData(fname); BufferedInputStream dstData = _dstMediaData(fname); if (srcData == null) { // file was not in source, ignore m.appendReplacement(sb, m.group(0)); continue; }// www . ja va 2 s. c o m // if model-local file exists from a previous import, use that int extPos = fname.lastIndexOf("."); if (extPos <= 0) { extPos = fname.length(); } String lname = String.format(Locale.US, "%s_%d%s", fname.substring(0, extPos), mid, fname.substring(extPos)); if (mDst.getMedia().have(lname)) { m.appendReplacement(sb, m.group(0).replace(fname, lname)); continue; } else if (dstData == null || compareMedia(srcData, dstData)) { // if missing or the same, pass // unmodified // need to copy? if (dstData == null) { _writeDstMedia(fname, srcData); } m.appendReplacement(sb, m.group(0)); continue; } // exists but does not match, so we need to dedupe _writeDstMedia(lname, srcData); m.appendReplacement(sb, m.group(0).replace(fname, lname)); } m.appendTail(sb); //fs[i] = sb.toString(); fields = sb.toString(); } //} return fields; }
From source file:org.codelibs.fess.helper.ViewHelper.java
public String getContentTitle(final Map<String, Object> document) { final FessConfig fessConfig = ComponentUtil.getFessConfig(); String title = DocumentUtil.getValue(document, fessConfig.getIndexFieldTitle(), String.class); if (StringUtil.isBlank(title)) { title = DocumentUtil.getValue(document, fessConfig.getIndexFieldFilename(), String.class); if (StringUtil.isBlank(title)) { title = DocumentUtil.getValue(document, fessConfig.getIndexFieldUrl(), String.class); }/*from ww w. j av a2 s. c om*/ } final int size = fessConfig.getResponseMaxTitleLengthAsInteger(); if (size > -1) { title = StringUtils.abbreviate(title, size); } final String value = LaFunctions.h(title); if (!fessConfig.isResponseHighlightContentTitleEnabled()) { return value; } return getQuerySet().map(querySet -> { final Matcher matcher = Pattern .compile(querySet.stream().map(LaFunctions::h).map(Pattern::quote) .collect(Collectors.joining("|")), Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE) .matcher(value); final StringBuffer buf = new StringBuffer(value.length() + 100); while (matcher.find()) { matcher.appendReplacement(buf, highlightTagPre + matcher.group(0) + highlightTagPost); } matcher.appendTail(buf); return buf.toString(); }).orElse(value); }
From source file:com.ibm.jaggr.service.impl.modulebuilder.css.CSSModuleBuilder.java
/** * Minifies a CSS string by removing comments and excess white-space, as well as * some unneeded tokens.//from w ww . ja v a 2 s.c om * * @param css The contents of a CSS file as a String * @param uri The URI for the CSS file * @return */ protected String minify(String css, IResource res) { // replace all quoted strings and url(...) patterns with unique ids so that // they won't be affected by whitespace removal. LinkedList<String> quotedStringReplacements = new LinkedList<String>(); Matcher m = quotedStringPattern.matcher(css); StringBuffer sb = new StringBuffer(); int i = 0; while (m.find()) { String text = (m.group(1) != null) ? ("url(" + StringUtils.trim(m.group(1)) + ")") : //$NON-NLS-1$ //$NON-NLS-2$ m.group(0); quotedStringReplacements.add(i, text); String replacement = "%%" + QUOTED_STRING_MARKER + (i++) + "__%%"; //$NON-NLS-1$ //$NON-NLS-2$ m.appendReplacement(sb, ""); //$NON-NLS-1$ sb.append(replacement); } m.appendTail(sb); css = sb.toString(); // Get rid of extra whitespace css = whitespacePattern.matcher(css).replaceAll(" "); //$NON-NLS-1$ css = endsPattern.matcher(css).replaceAll(""); //$NON-NLS-1$ css = closeBracePattern.matcher(css).replaceAll("}"); //$NON-NLS-1$ m = delimitersPattern.matcher(css); sb = new StringBuffer(); while (m.find()) { String text = m.group(1); m.appendReplacement(sb, ""); //$NON-NLS-1$ sb.append(text.length() == 1 ? text : text.replace(" ", "")); //$NON-NLS-1$ //$NON-NLS-2$ } m.appendTail(sb); css = sb.toString(); // restore quoted strings and url(...) patterns m = QUOTED_STRING_MARKER_PAT.matcher(css); sb = new StringBuffer(); while (m.find()) { i = Integer.parseInt(m.group(1)); m.appendReplacement(sb, ""); //$NON-NLS-1$ sb.append(quotedStringReplacements.get(i)); } m.appendTail(sb); css = sb.toString(); return css.toString(); }
From source file:com.ikanow.infinit.e.application.utils.LogstashConfigUtils.java
public static String validateLogstashInput(String sourceKey, String config, StringBuffer errorMessage, boolean isAdmin) { if (null == _props) { _props = new PropertiesManager(); String allowedInputs = _props.getProperty("harvest.logstash.allowed_inputs"); if ((null == allowedInputs) || (allowedInputs.isEmpty())) { allowedInputs = "collectd,drupal_dblog,gelf,gemfire,imap,irc,lumberjack,s3,snmptrap,sqs,syslog,twitter,udp,xmpp,zenoss"; // currently *not* allowed by default: elasticsearch,eventlog,exec,file,ganglia,generator,graphite,heroku,jmx,log4j,pipe,puppet_facter,rabbitmq,redit,relp,sqlite,stdin,stomp,tcp,unix,varnishlog,websocket,wmi,zeromq }/*from ww w.ja va 2s.c o m*/ _allowedInputs.addAll(Arrays.asList(allowedInputs.toLowerCase().split("\\s*,\\s*"))); String allowedFilters = _props.getProperty("harvest.logstash.allowed_filters"); if ((null == allowedFilters) || (allowedFilters.isEmpty())) { allowedFilters = "advisor,alter,anonymize,checksum,cidr,cipher,clone,collate,csv,date,dns,drop,elapsed,extractnumbers,fingerprint,geoip,gelfify,grep,grok,grokdiscovery,l18n,json,json_encode,kv,metaevent,metrics,multiline,mutate,noop,prune,punct,railsparallelrequest,range,sleep,split,sumnumbers,syslog_pri,throttle,translate,unique,urldecode,useragent,uuid,wms,wmts,xml"; // currently *not* allowed by default: elasticsearch,ruby,zeromq } _allowedFilters.addAll(Arrays.asList(allowedFilters.toLowerCase().split("\\s*,\\s*"))); } //TESTED (3_2a) // Configuration validation, phase 1 errorMessage.append("Validation error:"); BasicDBObject jsonifiedConfig = parseLogstashConfig(config, errorMessage); if (null == jsonifiedConfig) { return null; } errorMessage.setLength(0); // Configuration validation, phase 2 - very basic checks on the structure of the object Object input = jsonifiedConfig.get("input"); if ((null == input) || !(input instanceof BasicDBObject)) { // Does input exist? errorMessage.append( "Invalid input format, should be 'input { INPUT_TYPE { ... } }' (only one INPUT_TYPE) and also contain a filter, no \"s around them. (0)"); return null; } //TESTED (3_1d) else { // Check there's only one input type and (unless admin) it's one of the allowed types BasicDBObject inputDbo = (BasicDBObject) input; if (1 != inputDbo.size()) { errorMessage.append( "Invalid input format, should be 'input { INPUT_TYPE { ... } }' (only one INPUT_TYPE) and also contain a filter, no \"s around them. (1)"); return null; } //TESTED if (!isAdmin) { for (String key : inputDbo.keySet()) { if (!_allowedInputs.contains(key.toLowerCase())) { errorMessage.append("Security error, non-admin not allowed input type " + key + ", allowed options: " + _allowedInputs.toString()); return null; } //TESTED } } //TESTED (3_1abc) } Object filter = jsonifiedConfig.get("filter"); if ((null == filter) || !(filter instanceof BasicDBObject)) { // Does filter exist? errorMessage.append( "Invalid input format, should be 'input { INPUT_TYPE { ... } }' (only one INPUT_TYPE) and also contain a filter, no \"s around them. (2)"); return null; } //TESTED (3_2d) else { // Check there's only one input type and (unless admin) it's one of the allowed types if (!isAdmin) { BasicDBObject filterDbo = (BasicDBObject) filter; for (String key : filterDbo.keySet()) { if (!_allowedFilters.contains(key.toLowerCase())) { errorMessage.append("Security error, non-admin not allowed filter type " + key + ", allowed options: " + _allowedFilters.toString()); return null; } //TESTED } } //TESTED (3_2abc) } // Configuration validation, phase 3 Matcher m = null; m = _validationRegexInputReplace.matcher(config); if (!m.find()) { errorMessage.append( "Invalid input format, should be 'input { INPUT_TYPE { ... } }' (only one INPUT_TYPE) and also contain a filter, no \"s around them. (3)"); return null; } //TESTED (see above) else { // If admin check on allowed types String inputType = m.group(2).toLowerCase(); // If it's a file-based plugin then replace sincedb_path (check that it's not used during the JSON-ification): if (inputType.equalsIgnoreCase("file") || inputType.equalsIgnoreCase("s3")) { config = _validationRegexInputReplace.matcher(config) .replaceFirst("$1\n sincedb_path => \"_XXX_DOTSINCEDB_XXX_\"\n"); } //TESTED } //TESTED m = _validationRegexNoSourceKey.matcher(config); // (this won't help malicious changes to source key, but will let people know they're not supposed to) if (m.find()) { errorMessage.append( "Not allowed to reference sourceKey - this is automatically appended by the logstash harvester"); return null; } //TESTED // OK now need to append the sourceKey at each stage of the pipeline to really really ensure that nobody sets sourceKey to be different m = _validationRegexAppendFields.matcher(config); StringBuffer newConfig = new StringBuffer(); if (m.find()) { m.appendReplacement(newConfig, "add_field => [ \"sourceKey\", \"" + sourceKey + "\"] \n\n" + m.group() + " \n if [sourceKey] == \"" + sourceKey + "\" { \n\n "); } else { errorMessage.append( "Invalid input format, should be 'input { INPUT_TYPE { ... } }' (only one INPUT_TYPE) and also contain a filter, no \"s around them. (4)"); return null; } m.appendTail(newConfig); config = newConfig.toString(); config = config.replaceAll("}[^}]*$", ""); // (remove the last }) config += "\n\n mutate { update => [ \"sourceKey\", \"" + sourceKey + "\"] } \n}\n}\n"; // double check the sourceKey hasn't been overwritten and close the if from above //TESTED (syntactically correct and does overwrite sourceKey everywhere - success_2_2) return config; }
From source file:hudson.plugins.emailext.plugins.content.BuildLogMultilineRegexContent.java
private String getContent(BufferedReader reader) throws IOException { final Pattern pattern = Pattern.compile(regex); final boolean asHtml = matchedSegmentHtmlStyle != null; escapeHtml = asHtml || escapeHtml;//from ww w . ja va2 s . com StringBuilder line = new StringBuilder(); StringBuilder fullLog = new StringBuilder(); int ch; // Buffer log contents including line terminators, and remove console notes while ((ch = reader.read()) != -1) { if (ch == '\r' || ch == '\n') { if (line.length() > 0) { // Remove console notes (JENKINS-7402) fullLog.append(ConsoleNote.removeNotes(line.toString())); line.setLength(0); } fullLog.append((char) ch); } else { line.append((char) ch); } } // Buffer the final log line if it has no line terminator if (line.length() > 0) { // Remove console notes (JENKINS-7402) fullLog.append(ConsoleNote.removeNotes(line.toString())); } StringBuilder content = new StringBuilder(); int numMatches = 0; boolean insidePre = false; int lastMatchEnd = 0; final Matcher matcher = pattern.matcher(fullLog); while (matcher.find()) { if (maxMatches != 0 && ++numMatches > maxMatches) { break; } if (showTruncatedLines) { if (matcher.start() > lastMatchEnd) { // Append information about truncated lines. int numLinesTruncated = countLineTerminators( fullLog.subSequence(lastMatchEnd, matcher.start())); if (numLinesTruncated > 0) { insidePre = stopPre(content, insidePre); appendLinesTruncated(content, numLinesTruncated, asHtml); } } } if (asHtml) { insidePre = startPre(content, insidePre); } if (substText != null) { final StringBuffer substBuf = new StringBuffer(); matcher.appendReplacement(substBuf, substText); // Remove prepended text between matches final String segment = substBuf.substring(matcher.start() - lastMatchEnd); appendMatchedSegment(content, segment, escapeHtml, matchedSegmentHtmlStyle); } else { appendMatchedSegment(content, matcher.group(), escapeHtml, matchedSegmentHtmlStyle); } lastMatchEnd = matcher.end(); } if (showTruncatedLines) { if (fullLog.length() > lastMatchEnd) { // Append information about truncated lines. int numLinesTruncated = countLineTerminators(fullLog.subSequence(lastMatchEnd, fullLog.length())); if (numLinesTruncated > 0) { insidePre = stopPre(content, insidePre); appendLinesTruncated(content, numLinesTruncated, asHtml); } } } stopPre(content, insidePre); return content.toString(); }
From source file:com.google.livingstories.server.dataservices.entities.BaseContentEntity.java
/** * Examines the content for anchor tags that look like they point to external pages * (in general, any link that doesn't start with 'javascript:', and adds a * target="_blank" attribute to them, if there isn't a target attribute already. * @param content Content to fix up./*from w w w . j av a2 s . co m*/ * @return The modified content string, with links fixed to pop up new windows. */ private String fixLinks(String content) { Matcher matcher = EXTERNAL_LINK_PATTERN.matcher(content); StringBuffer sb = new StringBuffer(); while (matcher.find()) { String link = matcher.group(0); if (!TARGET_ATTR_PATTERN.matcher(link).find()) { link = link.replace(">", DEFAULT_LINK_TARGET + ">"); } matcher.appendReplacement(sb, Matcher.quoteReplacement(link)); } matcher.appendTail(sb); return sb.toString(); }
From source file:hudson.model.AbstractItem.java
/** * Writes {@code config.xml} to the specified output stream. * The user must have at least {@link #EXTENDED_READ}. * If he lacks {@link #CONFIGURE}, then any {@link Secret}s detected will be masked out. *///ww w . j a v a2s . com @Restricted(NoExternalUse.class) public void writeConfigDotXml(OutputStream os) throws IOException { checkPermission(EXTENDED_READ); XmlFile configFile = getConfigFile(); if (hasPermission(CONFIGURE)) { IOUtils.copy(configFile.getFile(), os); } else { String encoding = configFile.sniffEncoding(); String xml = FileUtils.readFileToString(configFile.getFile(), encoding); Matcher matcher = SECRET_PATTERN.matcher(xml); StringBuffer cleanXml = new StringBuffer(); while (matcher.find()) { if (Secret.decrypt(matcher.group(1)) != null) { matcher.appendReplacement(cleanXml, ">********<"); } } matcher.appendTail(cleanXml); org.apache.commons.io.IOUtils.write(cleanXml.toString(), os, encoding); } }