List of usage examples for java.util.regex Matcher replaceFirst
public String replaceFirst(Function<MatchResult, String> replacer)
From source file:org.infoscoop.request.filter.FeedJsonFilter.java
protected InputStream postProcess(ProxyRequest request, InputStream responseStream) throws IOException { boolean getSummaries = false; try {/* w w w . j av a2s .c o m*/ getSummaries = Boolean.parseBoolean(request.getFilterParameter("gs")); } catch (Exception ex) { // ignore ? } if (log.isDebugEnabled()) log.debug("GET_SUMMARIES: " + getSummaries); try { String r1Str = request.getResponseBodyAsString("UTF-8"); Pattern pattern = Pattern.compile("rssDate : new Date\\(.*\\),"); Matcher matcher = pattern.matcher(r1Str); r1Str = matcher.replaceFirst(""); JSONObject r1 = new JSONObject(r1Str); if (r1.getInt("statusCode") != 0) throw new IOException(r1.optString("errorMessage")); //FIXME JSONObject feed = new JSONObject(); feed.put("Title", r1.optString("title")); feed.put("Url", r1.optString("url")); feed.put("Link", r1.optString("link")); feed.put("Description", r1.optString("description")); feed.put("ErrorMsg", r1.optString("errorMessage")); JSONArray entries = new JSONArray(); feed.put("Entry", entries); JSONArray items = r1.getJSONArray("items"); for (int i = 0; i < items.length(); i++) { JSONObject item = items.getJSONObject(i); JSONObject entry = new JSONObject(); entry.put("Title", item.optString("title")); entry.put("Link", item.optString("link")); if (item.has("creator")) ; entry.put("Author", item.getString("creator")); if (getSummaries) entry.put("Summary", item.getString("description")); if (item.has("dateLong")) { long dateLong = -1; try { dateLong = Long.parseLong(item.getString("dateLong")); } catch (Exception ex) { // ignore ? } if (dateLong > 0) entry.put("Date", (long) (dateLong / 1000)); } entries.put(entry); } return new ByteArrayInputStream(feed.toString().getBytes("UTF-8")); } catch (Exception ex) { // FIXME throw new RuntimeException(ex); } }
From source file:org.wso2.carbon.dashboard.migratetool.DSMigrationTool.java
/** * Replace a specific word from specific text using given replacement text * @param srcWord original string// w ww . j a v a2s . co m * @param toReplace text to be replaced * @param replacement replacement text * @return modified text */ protected String replace(String srcWord, String toReplace, String replacement) { Pattern pattern = Pattern.compile(toReplace, Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(srcWord); String result = matcher.replaceFirst(replacement); return result; }
From source file:AIR.Common.Web.PatternUrlRewriter.java
private boolean rewrite(String url, Pattern p, String replacement, _Ref<URL> output) throws MalformedURLException { if (p == null) { output.set(new URL(url)); return false; }//from w w w. j a v a 2 s. c o m Matcher m = p.matcher(url); if (m.matches()) { output.set(new URL(m.replaceFirst(replacement))); return true; } return false; }
From source file:info.magnolia.objectfactory.configuration.ComponentConfigurationReader.java
/** * @deprecated TODO very ugly hack to force documents to be validated against OUR DTD. * We could use an EntityResolver, but it seems that with SAX, the documents will only be * validated if they original have a doctype declaration. * We could also use http://doctypechanger.sourceforge.net/ * ... or switch to XmlSchema. */// w ww . jav a 2 s . c o m private Reader replaceDtd(Reader reader) throws IOException { String content = IOUtils.toString(reader); // remove doctype Pattern pattern = Pattern.compile("<!DOCTYPE .*>"); Matcher matcher = pattern.matcher(content); content = matcher.replaceFirst(""); // set doctype to the dtd try { Document doc = new SAXBuilder().build(new StringReader(content)); doc.setDocType(new DocType("components", dtdUrl)); // write the xml to the string XMLOutputter outputter = new XMLOutputter(); StringWriter writer = new StringWriter(); outputter.output(doc, writer); final String replacedDtd = writer.toString(); return new StringReader(replacedDtd); } catch (JDOMException e) { throw new ComponentConfigurationException(e); } }
From source file:net.sf.zekr.common.util.VelocityUtils.java
public String processAya(String str) { if (StringUtils.isEmpty(str)) { return ""; }//from w w w . j a v a2 s. c om str = StringUtils.replace(str, "\\\\", "<br/>"); Matcher m; int i = 1; do { m = COMMENTARY.matcher(str); str = m.replaceFirst("<span class=\"commentHandle\">(" + i++ + ")</span> <span class=\"commentText\" style=\"display: none\">$1</span>"); } while (m.find()); return str; }
From source file:org.opencms.db.CmsRewriteAliasMatcher.java
/** * Tries to rewrite a given path, and either returns the rewrite result or null if no * rewrite alias matched the path.<p> * * @param path the path to match// www . j a va 2 s .com * @return the rewrite result or null if no rewrite alias matched */ public RewriteResult match(String path) { for (CmsRewriteAlias alias : m_aliases) { try { Pattern pattern = Pattern.compile(alias.getPatternString()); Matcher matcher = pattern.matcher(path); if (matcher.matches()) { String newPath = matcher.replaceFirst(alias.getReplacementString()); return new RewriteResult(newPath, alias); } } catch (PatternSyntaxException e) { LOG.warn(e.getLocalizedMessage(), e); } catch (IndexOutOfBoundsException e) { LOG.warn(e.getLocalizedMessage(), e); } } return null; }
From source file:org.broadleafcommerce.cms.url.service.URLHandlerServiceImpl.java
protected URLHandler checkForMatches(String requestURI) { URLHandler currentHandler = null;//from w w w . j a va2 s. c om try { List<URLHandler> urlHandlers = findAllURLHandlers(); for (URLHandler urlHandler : urlHandlers) { currentHandler = urlHandler; String incomingUrl = currentHandler.getIncomingURL(); if (!incomingUrl.startsWith("^")) { if (incomingUrl.startsWith("/")) { incomingUrl = "^" + incomingUrl + "$"; } else { incomingUrl = "^/" + incomingUrl + "$"; } } Pattern p = urlPatternMap.get(incomingUrl); if (p == null) { p = Pattern.compile(incomingUrl); urlPatternMap.put(incomingUrl, p); } Matcher m = p.matcher(requestURI); if (m.find()) { String newUrl = m.replaceFirst(urlHandler.getNewURL()); if (newUrl.equals(urlHandler.getNewURL())) { return urlHandler; } else { return new URLHandlerDTO(newUrl, urlHandler.getUrlRedirectType()); } } } } catch (RuntimeException re) { if (currentHandler != null) { // We don't want an invalid regex to cause tons of logging if (LOG.isWarnEnabled()) { LOG.warn("Error parsing URL Handler (incoming =" + currentHandler.getIncomingURL() + "), outgoing = ( " + currentHandler.getNewURL() + "), " + requestURI); } } } return null; }
From source file:info.magnolia.module.model.reader.BetwixtModuleDefinitionReader.java
/** * @deprecated TODO very ugly hack to force documents to be validated against OUR DTD. * We could use an EntityResolver, but it seems that with SAX, the documents will only be * validated if they original have a doctype declaration. * We could also use http://doctypechanger.sourceforge.net/ * ... or switch to XmlSchema.//from ww w . ja va 2 s . c o m */ private Reader replaceDtd(Reader reader) throws IOException { String content = IOUtils.toString(reader); // remove doctype Pattern pattern = Pattern.compile("<!DOCTYPE .*>"); Matcher matcher = pattern.matcher(content); content = matcher.replaceFirst(""); // set doctype to the dtd try { Document doc = new SAXBuilder().build(new StringReader(content)); doc.setDocType(new DocType("module", dtdUrl)); // write the xml to the string XMLOutputter outputter = new XMLOutputter(); StringWriter writer = new StringWriter(); outputter.output(doc, writer); final String replacedDtd = writer.toString(); return new StringReader(replacedDtd); } catch (JDOMException e) { throw new RuntimeException(e); // TODO } }
From source file:com.anrisoftware.globalpom.textmatch.tokentemplate.DefaultTokensTemplate.java
private void replace(Matcher matcher, String replace) { if (template.isEscape()) { replace = escapeReplace(replace); }//from w w w. j a v a2s . com replacement = matcher.replaceFirst(replace); log.replacedArgument(this); }