List of usage examples for java.util.regex Matcher start
public int start()
From source file:com.consol.citrus.admin.service.TestCaseService.java
/** * Find all tests in give source file. Method is finding tests by their annotation presence of @CitrusTest or @CitrusXmlTest. * @param sourceFile/*from ww w.j a v a2 s . co m*/ * @param packageName * @param className * @return */ private List<Test> findTests(File sourceFile, String packageName, String className) { List<Test> tests = new ArrayList<>(); try { String sourceCode = FileUtils.readToString(new FileSystemResource(sourceFile)); Matcher matcher = Pattern.compile("[^/\\*]\\s@CitrusTest").matcher(sourceCode); while (matcher.find()) { Test test = new Test(); test.setType(TestType.JAVA); test.setClassName(className); test.setPackageName(packageName); String snippet = StringUtils.trimAllWhitespace(sourceCode.substring(matcher.start())); snippet = snippet.substring(0, snippet.indexOf("){")); String methodName = snippet.substring(snippet.indexOf("publicvoid") + 10); methodName = methodName.substring(0, methodName.indexOf("(")); test.setMethodName(methodName); if (snippet.contains("@CitrusTest(name=")) { String explicitName = snippet.substring(snippet.indexOf("name=\"") + 6); explicitName = explicitName.substring(0, explicitName.indexOf("\"")); test.setName(explicitName); } else { test.setName(className + "." + methodName); } tests.add(test); } matcher = Pattern.compile("[^/\\*]\\s@CitrusXmlTest").matcher(sourceCode); while (matcher.find()) { Test test = new Test(); test.setType(TestType.XML); test.setClassName(className); test.setPackageName(packageName); String snippet = StringUtils.trimAllWhitespace(sourceCode.substring(matcher.start())); snippet = snippet.substring(0, snippet.indexOf('{', snippet.indexOf("publicvoid"))); String methodName = snippet.substring(snippet.indexOf("publicvoid") + 10); methodName = methodName.substring(0, methodName.indexOf("(")); test.setMethodName(methodName); if (snippet.contains("@CitrusXmlTest(name=\"")) { String explicitName = snippet.substring(snippet.indexOf("name=\"") + 6); explicitName = explicitName.substring(0, explicitName.indexOf("\"")); test.setName(explicitName); } else if (snippet.contains("@CitrusXmlTest(name={\"")) { String explicitName = snippet.substring(snippet.indexOf("name={\"") + 7); explicitName = explicitName.substring(0, explicitName.indexOf("\"")); test.setName(explicitName); } else { test.setName(methodName); } if (snippet.contains("packageScan=\"")) { String packageScan = snippet.substring(snippet.indexOf("packageScan=\"") + 13); packageScan = packageScan.substring(0, packageScan.indexOf("\"")); test.setPackageName(packageScan); } if (snippet.contains("packageName=\"")) { String explicitPackageName = snippet.substring(snippet.indexOf("packageName=\"") + 13); explicitPackageName = explicitPackageName.substring(0, explicitPackageName.indexOf("\"")); test.setPackageName(explicitPackageName); } tests.add(test); } } catch (IOException e) { log.error("Failed to read test source file", e); } return tests; }
From source file:de.huberlin.wbi.hiway.am.galaxy.GalaxyTaskInstance.java
/** * A (recursive) method that is passed a file along with the JSON object in this task instance's tool state that corresponds to this file. This method is * used to populate the tool state with information on the file. * //from ww w . jav a2 s . c o m * @param name * the parameter name for this file, as specified in the workflow description * @param computeMetadata * a parameter that determines whether metadata is to be computed for this file, which will only be the case for input data * @param data * the Hi-WAY data object for this file * @param jo * the JSON object in the tool state that corresponds to this file parameter */ private void addFile(String name, boolean computeMetadata, GalaxyData data, JSONObject jo) { try { Pattern p = Pattern.compile("(_[0-9]*)?\\|"); Matcher m = p.matcher(name); // (1) if the to-be-added file is part of a repeat (and thus can be identified by an underscore and index number in its name), compute its prefix // (the repeat name) as well as its suffix (the actual parameter name) and index; use the prefix and index to obtain its JSON object from the tool // state and (recursively) call this method to proceed to (2) if (m.find()) { String prefix = name.substring(0, m.start()); String suffix = name.substring(m.end()); if (m.end() - m.start() > 2) { int index = Integer.parseInt(name.substring(m.start() + 1, m.end() - 1)); JSONArray repeatJa = jo.getJSONArray(prefix); for (int i = 0; i < repeatJa.length(); i++) { JSONObject repeatJo = repeatJa.getJSONObject(i); if (repeatJo.getInt("__index__") == index) { addFile(suffix, computeMetadata, data, repeatJo); break; } } } else { addFile(suffix, computeMetadata, data, jo.getJSONObject(prefix)); } // (2) fix both the template of this tool and the tool state of the task instance calling this method, such that they are in compliance with one // another when the tool state is used to set the parameters of the task instance at execution time } else { // (a) add several properties for this parameter to the tool state String fileName = data.getName(); JSONObject fileJo = new JSONObject(); fileJo.putOpt("path", fileName); fileJo.putOpt("name", fileName.split("\\.(?=[^\\.]+$)")[0]); Path dir = data.getLocalDirectory(); String dirString = (dir == null) ? "" : dir.toString(); if (dirString.length() == 0) { dirString = "."; } fileJo.putOpt("files_path", dirString); if (data.hasDataType()) { GalaxyDataType dataType = data.getDataType(); String fileExt = dataType.getExtension(); fileJo.putOpt("extension", fileExt); fileJo.putOpt("ext", fileExt); } // note that this metadata is an empty dictionary that will only be be filled for input data of the task instance calling this method; this is // done by executing a python script designed to populate the tool state with metadata prior to execution if (data.hasDataType()) fileJo.putOpt("metadata", new JSONObject()); jo.putOpt(name, fileJo); // (b) adjust the Python script for setting parameters at runtime to compute metadata for this file, given the computeMetadata parameter is set if (computeMetadata && data.hasDataType()) { GalaxyDataType dataType = data.getDataType(); paramScript.append("from "); paramScript.append(dataType.getFile()); paramScript.append(" import "); paramScript.append(dataType.getName()); paramScript.append("\n"); inputs.add(data); } } } catch (JSONException e) { e.printStackTrace(); System.exit(-1); } }
From source file:gtu._work.ui.RegexDirReplacer.java
/** * @param fromPattern//ww w . j ava2s. c o m * ???pattern * @param toFormat * ??pattern * @param replaceText * ?? */ String replacer(String fromPattern, String toFormat, String replaceText, File file) { String errorRtn = replaceText.toString(); try { Pattern pattern = Pattern.compile(fromPattern); Matcher matcher = pattern.matcher(replaceText); StringBuilder sb = new StringBuilder(); int startPos = 0; String tempStr = null; for (; matcher.find();) { tempStr = toFormat.toString(); sb.append(replaceText.substring(startPos, matcher.start())); for (int ii = 0; ii <= matcher.groupCount(); ii++) { tempStr = tempStr.replaceAll("#" + ii + "#", Matcher.quoteReplacement(matcher.group(ii))); System.out.println("group[" + ii + "] -- " + matcher.group(ii) + "\t--> " + tempStr); } sb.append(tempStr); startPos = matcher.end(); } sb.append(replaceText.substring(startPos)); return sb.toString(); } catch (Exception ex) { // JOptionPaneUtil.newInstance().iconErrorMessage().showMessageDialog(ex.getMessage(), // getTitle()); errMsg.append(file.getName() + ":" + ex + "\n"); return errorRtn; } }
From source file:net.dv8tion.jda.core.entities.impl.ReceivedMessage.java
@Override public String getContentStripped() { if (strippedContent != null) return strippedContent; synchronized (mutex) { if (strippedContent != null) return strippedContent; String tmp = getContentDisplay(); //all the formatting keys to keep track of String[] keys = new String[] { "*", "_", "`", "~~" }; //find all tokens (formatting strings described above) TreeSet<FormatToken> tokens = new TreeSet<>(Comparator.comparingInt(t -> t.start)); for (String key : keys) { Matcher matcher = Pattern.compile(Pattern.quote(key)).matcher(tmp); while (matcher.find()) tokens.add(new FormatToken(key, matcher.start())); }/*from w w w . j ava2 s . c o m*/ //iterate over all tokens, find all matching pairs, and add them to the list toRemove Deque<FormatToken> stack = new ArrayDeque<>(); List<FormatToken> toRemove = new ArrayList<>(); boolean inBlock = false; for (FormatToken token : tokens) { if (stack.isEmpty() || !stack.peek().format.equals(token.format) || stack.peek().start + token.format.length() == token.start) { //we are at opening tag if (!inBlock) { //we are outside of block -> handle normally if (token.format.equals("`")) { //block start... invalidate all previous tags stack.clear(); inBlock = true; } stack.push(token); } else if (token.format.equals("`")) { //we are inside of a block -> handle only block tag stack.push(token); } } else if (!stack.isEmpty()) { //we found a matching close-tag toRemove.add(stack.pop()); toRemove.add(token); if (token.format.equals("`") && stack.isEmpty()) //close tag closed the block inBlock = false; } } //sort tags to remove by their start-index and iteratively build the remaining string toRemove.sort(Comparator.comparingInt(t -> t.start)); StringBuilder out = new StringBuilder(); int currIndex = 0; for (FormatToken formatToken : toRemove) { if (currIndex < formatToken.start) out.append(tmp.substring(currIndex, formatToken.start)); currIndex = formatToken.start + formatToken.format.length(); } if (currIndex < tmp.length()) out.append(tmp.substring(currIndex)); //return the stripped text, escape all remaining formatting characters (did not have matching // open/close before or were left/right of block return strippedContent = out.toString().replace("*", "\\*").replace("_", "\\_").replace("~", "\\~"); } }
From source file:hudson.Util.java
/** * Replaces the occurrence of '$key' by <tt>resolver.get('key')</tt>. * * <p>/*w ww . j a va 2 s. c o m*/ * Unlike shell, undefined variables are left as-is (this behavior is the same as Ant.) */ public static String replaceMacro(String s, VariableResolver<String> resolver) { if (s == null) { return null; } int idx = 0; while (true) { Matcher m = VARIABLE.matcher(s); if (!m.find(idx)) { return s; } String key = m.group().substring(1); // escape the dollar sign or get the key to resolve String value; if (key.charAt(0) == '$') { value = "$"; } else { if (key.charAt(0) == '{') { key = key.substring(1, key.length() - 1); } value = resolver.resolve(key); } if (value == null) { idx = m.end(); // skip this } else { s = s.substring(0, m.start()) + value + s.substring(m.end()); idx = m.start() + value.length(); } } }
From source file:com.gargoylesoftware.htmlunit.CodeStyleTest.java
private int getIndentation(final String line) { final Matcher matcher = leadingWhitespace.matcher(line); if (matcher.find()) { return matcher.end() - matcher.start(); }/*from w w w. jav a 2 s. c o m*/ return 0; }
From source file:mitm.djigzo.web.render.impl.AbstractInlineAddClassPattern.java
private void applyPattern(StrBuilder builder) { Pattern pattern = getPattern(); if (pattern != null) { Matcher matcher = pattern.matcher(builder.toString()); StrBuilder copy = null;/* ww w. ja va 2s. c o m*/ /* * Because we will modify the StrBuilder (replacing matches etc.) we need * to keep track of changes with respect to indexes. If there is a match * we will create a copy of the line and do the replacing on that line. We * however need to correct the indexes because we are adding or removing * characters to the copy. */ int indexCorrection = 0; while (matcher.find()) { if (copy == null) { copy = new StrBuilder(builder.toString()); } String replaceWith = "<span class=\"" + className + "\">" + matcher.group() + "</span>"; copy.replace(matcher.start() + indexCorrection, matcher.end() + indexCorrection, replaceWith); indexCorrection = indexCorrection + replaceWith.length() - matcher.group().length(); } if (copy != null) { /* * Content has changed so replace it */ builder.clear(); builder.append(copy.toString()); } } }
From source file:com.manydesigns.portofino.pageactions.text.TextAction.java
protected String processAttachmentUrls(String content) { String baseUrl = StringEscapeUtils.escapeHtml(generateViewAttachmentUrl("").replace("?", "\\?")); String patternString = "([^\\s\"]+)\\s*=\\s*\"\\s*" + baseUrl + "([^\"]+)\""; Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(content); int lastEnd = 0; StringBuilder sb = new StringBuilder(); while (matcher.find()) { String hrefAttribute = matcher.group(1); String attachmentId = matcher.group(2); sb.append(content.substring(lastEnd, matcher.start())); sb.append("portofino:attachment=\"").append(attachmentId).append("\" "); sb.append("portofino:hrefAttribute=\"").append(hrefAttribute).append("\""); lastEnd = matcher.end();// w ww . j a va 2 s .c om } sb.append(content.substring(lastEnd)); return sb.toString(); }
From source file:org.cleverbus.admin.services.log.LogParser.java
/** * Processes the next log line by either appending it to the last log event, * if it's not a valid log line and last log event wasn't ignored (appendTo != null); * or by parsing it into a new event (parseTo). * * @param line the log line to process * @param parseTo the next log event to parse line into * @param appendTo the log event to append non-event lines to * @param config log parser config/*w w w.j a v a 2s.c o m*/ * @return appendTo event, if the line was appended to it; parseTo event if the line was fully parsed into this event; * or null if the line was ignored */ LogEvent parseLine(String line, LogEvent parseTo, LogEvent appendTo, LogParserConfig config) { Matcher dateMatcher = config.getDatePattern().matcher(line); if (!dateMatcher.lookingAt()) { return parseFailed(line, appendTo); } DateTime eventDate = getDate(dateMatcher.group(1), config); if (eventDate == null) { return parseFailed(line, appendTo); } // line might still not match properties and therefore not be a new log event, // so don't stop just yet, even if the date is wrong boolean skipLogLine = eventDate.isBefore(config.getFromDate()); if (skipLogLine && appendTo == null) { return null; // no point continuing, since this line wouldn't be appended anyway } parseTo.setDate(eventDate); String unmatched = line.substring(0, dateMatcher.start()) + line.substring(dateMatcher.end(), line.length()); // date matches, but is the line a new log event line? Matcher propertiesMatcher = config.getPropertiesPattern().matcher(unmatched); if (!propertiesMatcher.lookingAt()) { return parseFailed(line, appendTo); } if (skipLogLine || !parseEventProperties(propertiesMatcher, parseTo, config)) { return null; } if (unmatched != null && config.getMsg() != null && !unmatched.contains(config.getMsg())) { return null; } unmatched = unmatched.substring(0, propertiesMatcher.start()) + unmatched.substring(propertiesMatcher.end(), unmatched.length()); parseTo.setMessage(unmatched); return parseTo; }
From source file:com.joliciel.talismane.tokeniser.filters.TokenRegexFilterImpl.java
Pattern getPattern() { if (pattern == null) { // we may need to replace WordLists by the list contents String myRegex = this.regex; if (LOG.isTraceEnabled()) { LOG.trace("Regex: " + myRegex); }/* w w w . ja v a 2 s .c o m*/ if (this.autoWordBoundaries) { Boolean startsWithLetter = null; for (int i = 0; i < myRegex.length() && startsWithLetter == null; i++) { char c = myRegex.charAt(i); if (c == '\\') { i++; c = myRegex.charAt(i); if (c == 'd' || c == 'w') { startsWithLetter = true; } else if (c == 's' || c == 'W' || c == 'b' || c == 'B') { startsWithLetter = false; } else if (c == 'p') { i += 2; // skip the open curly brackets int closeCurlyBrackets = myRegex.indexOf('}', i); int openParentheses = myRegex.indexOf('(', i); int endIndex = closeCurlyBrackets; if (openParentheses > 0 && openParentheses < closeCurlyBrackets) endIndex = openParentheses; if (endIndex > 0) { String specialClass = myRegex.substring(i, endIndex); if (specialClass.equals("WordList")) { startsWithLetter = true; } } } break; } else if (c == '[' || c == '(') { // do nothing } else if (Character.isLetter(c) || Character.isDigit(c)) { startsWithLetter = true; } else { startsWithLetter = false; } } Boolean endsWithLetter = null; for (int i = myRegex.length() - 1; i >= 0 && endsWithLetter == null; i--) { char c = myRegex.charAt(i); char prevC = ' '; if (i >= 1) prevC = myRegex.charAt(i - 1); if (prevC == '\\') { if (c == 'd' || c == 'w') { endsWithLetter = true; } else if (c == 's' || c == 'W' || c == 'b' || c == 'B') { endsWithLetter = false; } else if (c == 'p') { i += 2; // skip the open curly brackets int closeCurlyBrackets = myRegex.indexOf('}', i); int openParentheses = myRegex.indexOf('(', i); int endIndex = closeCurlyBrackets; if (openParentheses < closeCurlyBrackets) endIndex = openParentheses; if (endIndex > 0) { String specialClass = myRegex.substring(i, endIndex); if (specialClass.equals("WordList") || specialClass.equals("Alpha") || specialClass.equals("Lower") || specialClass.equals("Upper") || specialClass.equals("ASCII") || specialClass.equals("Digit")) { startsWithLetter = true; } } } break; } else if (c == ']' || c == ')' || c == '+') { // do nothing } else if (c == '}') { int startIndex = myRegex.lastIndexOf('{') + 1; int closeCurlyBrackets = myRegex.indexOf('}', startIndex); int openParentheses = myRegex.indexOf('(', startIndex); int endIndex = closeCurlyBrackets; if (openParentheses > 0 && openParentheses < closeCurlyBrackets) endIndex = openParentheses; if (endIndex > 0) { String specialClass = myRegex.substring(startIndex, endIndex); if (specialClass.equals("WordList") || specialClass.equals("Alpha") || specialClass.equals("Lower") || specialClass.equals("Upper") || specialClass.equals("ASCII") || specialClass.equals("Digit")) { endsWithLetter = true; } } break; } else if (Character.isLetter(c) || Character.isDigit(c)) { endsWithLetter = true; } else { endsWithLetter = false; } } if (startsWithLetter != null && startsWithLetter) { myRegex = "\\b" + myRegex; } if (endsWithLetter != null && endsWithLetter) { myRegex = myRegex + "\\b"; } if (LOG.isTraceEnabled()) { LOG.trace("After autoWordBoundaries: " + myRegex); } } if (!this.caseSensitive || !this.diacriticSensitive) { StringBuilder regexBuilder = new StringBuilder(); for (int i = 0; i < myRegex.length(); i++) { char c = myRegex.charAt(i); if (c == '\\') { // escape - skip next regexBuilder.append(c); i++; c = myRegex.charAt(i); regexBuilder.append(c); } else if (c == '[') { // character group, don't change it regexBuilder.append(c); while (c != ']' && i < myRegex.length()) { i++; c = myRegex.charAt(i); regexBuilder.append(c); } } else if (c == '{') { // command, don't change it regexBuilder.append(c); while (c != '}' && i < myRegex.length()) { i++; c = myRegex.charAt(i); regexBuilder.append(c); } } else if (Character.isLetter(c)) { Set<String> chars = new TreeSet<String>(); chars.add("" + c); char noAccent = diacriticPattern.matcher(Normalizer.normalize("" + c, Form.NFD)) .replaceAll("").charAt(0); if (!this.caseSensitive) { chars.add("" + Character.toUpperCase(c)); chars.add("" + Character.toLowerCase(c)); chars.add("" + Character.toUpperCase(noAccent)); } if (!this.diacriticSensitive) { chars.add("" + noAccent); if (!this.caseSensitive) { chars.add("" + Character.toLowerCase(noAccent)); } } if (chars.size() == 1) { regexBuilder.append(c); } else { regexBuilder.append('['); for (String oneChar : chars) { regexBuilder.append(oneChar); } regexBuilder.append(']'); } } else { regexBuilder.append(c); } } myRegex = regexBuilder.toString(); if (LOG.isTraceEnabled()) { LOG.trace("After caseSensitive: " + myRegex); } } Matcher matcher = wordListPattern.matcher(myRegex); StringBuilder regexBuilder = new StringBuilder(); int lastIndex = 0; while (matcher.find()) { String[] params = matcher.group(1).split(","); int start = matcher.start(); int end = matcher.end(); regexBuilder.append(myRegex.substring(lastIndex, start)); String wordListName = params[0]; boolean uppercaseOptional = false; boolean diacriticsOptional = false; boolean lowercaseOptional = false; boolean firstParam = true; for (String param : params) { if (firstParam) { /* word list name */ } else if (param.equals("diacriticsOptional")) diacriticsOptional = true; else if (param.equals("uppercaseOptional")) uppercaseOptional = true; else if (param.equals("lowercaseOptional")) lowercaseOptional = true; else throw new TalismaneException( "Unknown parameter in word list " + matcher.group(1) + ": " + param); firstParam = false; } ExternalWordList wordList = externalResourceFinder.getExternalWordList(wordListName); if (wordList == null) throw new TalismaneException("Unknown word list: " + wordListName); StringBuilder sb = new StringBuilder(); boolean firstWord = true; for (String word : wordList.getWordList()) { if (!firstWord) sb.append("|"); word = Normalizer.normalize(word, Form.NFC); if (uppercaseOptional || diacriticsOptional) { String wordNoDiacritics = Normalizer.normalize(word, Form.NFD) .replaceAll("\\p{InCombiningDiacriticalMarks}+", ""); String wordLowercase = word.toLowerCase(Locale.ENGLISH); String wordLowercaseNoDiacritics = Normalizer.normalize(wordLowercase, Form.NFD) .replaceAll("\\p{InCombiningDiacriticalMarks}+", ""); String wordUppercase = wordNoDiacritics.toUpperCase(Locale.ENGLISH); boolean needsGrouping = false; if (uppercaseOptional && !word.equals(wordLowercase)) needsGrouping = true; if (diacriticsOptional && !word.equals(wordNoDiacritics)) needsGrouping = true; if (lowercaseOptional && !word.equals(wordUppercase)) needsGrouping = true; if (needsGrouping) { for (int i = 0; i < word.length(); i++) { char c = word.charAt(i); boolean grouped = false; if (uppercaseOptional && c != wordLowercase.charAt(i)) grouped = true; if (diacriticsOptional && c != wordNoDiacritics.charAt(i)) grouped = true; if (lowercaseOptional && c != wordUppercase.charAt(i)) grouped = true; if (!grouped) sb.append(c); else { sb.append("["); String group = "" + c; if (uppercaseOptional && group.indexOf(wordLowercase.charAt(i)) < 0) group += (wordLowercase.charAt(i)); if (lowercaseOptional && group.indexOf(wordUppercase.charAt(i)) < 0) group += (wordUppercase.charAt(i)); if (diacriticsOptional && group.indexOf(wordNoDiacritics.charAt(i)) < 0) group += (wordNoDiacritics.charAt(i)); if (uppercaseOptional && diacriticsOptional && group.indexOf(wordLowercaseNoDiacritics.charAt(i)) < 0) group += (wordLowercaseNoDiacritics.charAt(i)); sb.append(group); sb.append("]"); } // does this letter need grouping? } // next letter } else { sb.append(word); } // any options activated? } else { sb.append(word); } firstWord = false; } // next word in list regexBuilder.append(sb.toString()); lastIndex = end; } // next match regexBuilder.append(myRegex.substring(lastIndex)); myRegex = regexBuilder.toString(); this.pattern = Pattern.compile(myRegex, Pattern.UNICODE_CHARACTER_CLASS); } return pattern; }