List of usage examples for java.util.regex Matcher start
public int start()
From source file:com.npower.dm.hibernate.management.DDFTreeManagementBeanImpl.java
public DDFNode findDDFNode(Set<DDFNode> rootNodesSet, String nodePath) { for (Iterator<DDFNode> nodes = rootNodesSet.iterator(); nodes.hasNext();) { DDFNode node = (DDFNode) nodes.next(); if (nodePath.equals(node.getName())) { return node; }/* w w w . ja v a 2 s . c om*/ Matcher matcher = DDFTreeHelper.DynamicNamePattern.matcher(nodePath); boolean equals = matcher.matches(); matcher.reset(); boolean found = matcher.find(); boolean startWith = false; if (found) { int index = matcher.start(); startWith = (index == 0) ? true : false; } if (equals && (node.getName() == null || node.getName().trim().length() == 0)) { return node; } else if (node.getName() == null || node.getName().trim().length() == 0) { if (startWith) { return findDDFNode(node.getChildren(), nodePath.substring(nodePath.indexOf("/") + 1, nodePath.length())); } else { continue; } } else if (nodePath.startsWith(node.getName() + "/")) { return findDDFNode(node.getChildren(), nodePath.substring(nodePath.indexOf("/") + 1, nodePath.length())); } } return null; }
From source file:eu.mihosoft.vrl.fxscad.MainController.java
/** * Initializes the controller class.//from w w w.ja v a2 s . co m * * @param url * @param rb */ @Override public void initialize(URL url, ResourceBundle rb) { // codeArea.textProperty().addListener((ov, oldText, newText) -> { Matcher matcher = KEYWORD_PATTERN.matcher(newText); int lastKwEnd = 0; StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>(); while (matcher.find()) { spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd); spansBuilder.add(Collections.singleton("keyword"), matcher.end() - matcher.start()); lastKwEnd = matcher.end(); } spansBuilder.add(Collections.emptyList(), newText.length() - lastKwEnd); codeArea.setStyleSpans(0, spansBuilder.create()); }); EventStream<Change<String>> textEvents = EventStreams.changesOf(codeArea.textProperty()); textEvents.reduceSuccessions((a, b) -> b, Duration.ofMillis(500)).subscribe(code -> { if (autoCompile) { compile(code.getNewValue()); } }); codeArea.replaceText("CSG cube = new Cube(2).toCSG()\n" + "CSG sphere = new Sphere(1.25).toCSG()\n" + "\n" + "cube.difference(sphere)"); editorContainer.setContent(codeArea); subScene = new SubScene(viewGroup, 100, 100, true, SceneAntialiasing.BALANCED); subScene.widthProperty().bind(viewContainer.widthProperty()); subScene.heightProperty().bind(viewContainer.heightProperty()); PerspectiveCamera subSceneCamera = new PerspectiveCamera(false); subScene.setCamera(subSceneCamera); viewContainer.getChildren().add(subScene); }
From source file:info.mikaelsvensson.devtools.sitesearch.SiteSearchPlugin.java
private boolean modifyStringBuilder(final StringBuilder sb, final Pattern insertionPointPattern, final ModifyAction modifyAction, final String text) { Matcher matcher = insertionPointPattern.matcher(sb); if (matcher.find()) { switch (modifyAction) { case APPEND: sb.insert(matcher.end(), text); return true; case PREPEND: sb.insert(matcher.start(), text); return true; case REPLACE: sb.replace(matcher.start(), matcher.end(), text); return true; }/*from w w w . ja va 2s . c o m*/ } return false; }
From source file:sapience.injectors.stax.inject.ModelBasedStaxStreamInjector.java
/** * If the reference is a attribute (e.g. sawsdl:modelreference), we add it here (by creating * the according XMLEvent). The ref/* w w w . j a v a 2 s .com*/ * @param w * @param ref * @param se * @throws XMLStreamException */ private StartElement handleAttribute(XMLEventWriter w, Reference ref, StartElement se) throws XMLStreamException { /* we are having attributes which are in both, the reference and the current element. We only add * a new Attribute event, if it is not already contained in the Start Element * * Example: * reference <element ns:attr1="value" reference="http://example.com"> * element <element ns:attr1="value"> */ StringBuilder referenceString = new StringBuilder(ref.getTarget().toString()); Matcher matcher = findAttributeInReference.matcher(referenceString); List<Attribute> attributeList = new ArrayList<Attribute>(); // copy namespaces LocalNamespaceContext lnc = new LocalNamespaceContext((BaseNsContext) se.getNamespaceContext()); while (matcher.find()) { int start = matcher.start(); int end = matcher.end(); String key = null; String prefix = null; String value = null; // [ns:attr1, "value"] String[] l = referenceString.substring(start, end).split("="); if (l.length > 0) { // [ns, attr1] String[] n = l[0].split(":"); if (n.length == 2) { key = n[1]; prefix = n[0]; } else { key = n[0]; } if (l.length == 2) { value = l[1].substring(1, l[1].length() - 1); // remove "" } } // check if this is a namespace definition if ((prefix != null) && ("xmlns".contentEquals(prefix))) { lnc.put(key, value); } else { QName name = null; // create QName if (prefix != null) { name = new QName(null, key, prefix); } else { String namespaceURI = se.getNamespaceContext().getNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX); name = new QName(namespaceURI, key); } if (name != null) { Attribute created = getXMLEventFactory().createAttribute(name, value); attributeList.add(created); } } } // remove redundant attribute from reference list Iterator<?> it = se.getAttributes(); while (it.hasNext()) { Attribute ae = (Attribute) it.next(); for (Attribute ar : attributeList) { if ((ar.getName().getLocalPart().contentEquals(ae.getName().getLocalPart())) && (ar.getValue().contentEquals(ae.getValue()))) { //System.out.println("Attribute removed! -> " + ar.getName() + "= " + ar.getValue()); attributeList.remove(ar); break; } } } // merge everything again Iterator<? extends Attribute> it2 = se.getAttributes(); while (it2.hasNext()) { attributeList.add(it2.next()); } // create a new element with the attribute set and return it return StartElementEventImpl.construct(se.getLocation(), se.getName(), attributeList.iterator(), lnc.getNamespaces().iterator(), lnc); }
From source file:com.timrae.rikaidroid.MainActivity.java
/** * Add the reading to the kanji as Ruby furigana, ensuring that there is only furigana above * the kanji, not above any hiragana included in the word. * @param kanji a word in kanji// w w w . j a v a 2 s. c o m * @param reading the hiragana reading for the word * @return a String with the reading correctly added to the kanji as Ruby */ private String makeFurigana(String kanji, String reading) { Matcher kanaMatcher = KANA_REGEXP.matcher(kanji); // All characeters are kanji; simple replacement will work if (!kanaMatcher.find()) { return String.format(RUBY, kanji, reading); } // Strip off any kana from the beginning of the word StringBuilder output = new StringBuilder(); if (kanaMatcher.start() == 0) { String prefix = kanaMatcher.group(); kanji = kanji.substring(prefix.length()); reading = reading.substring(prefix.length()); output.append(prefix); kanaMatcher = KANA_REGEXP.matcher(kanji); } else { kanaMatcher.reset(); } // Keep track of number of kana added to output to see if the algorithm was successful int numKana = output.length(); // Now step through each kanji int lastKanaEnd = 0; int lastReadingKanaEnd = 0; while (kanaMatcher.find()) { // Find the next kana in the kanji string int kanaStart = kanaMatcher.start(); String currentKana = kanaMatcher.group(); // Extract the kanji in-between the current kana and the previous kana String currentKanji = kanji.substring(lastKanaEnd, kanaStart); // Set the end index of current kana in kanji string for next loop iteration lastKanaEnd = kanaMatcher.end(); // Find the current kana in the reading string // Not perfect. Here we take the first occurrence at least number of kanji after the last kana int readingKanaStart = reading.indexOf(currentKana, lastReadingKanaEnd + currentKanji.length()); // Extract the reading in-between the kana found in the kanji this time and last time String currentReading = reading.substring(lastReadingKanaEnd, readingKanaStart); // Set the end index of current kana in reading string for next loop iteration lastReadingKanaEnd = readingKanaStart + currentKana.length(); // Append current kanji and reading to the StringBuilder as furigana output.append(String.format(RUBY, currentKanji, currentReading)); // Append the current kana to the StringBuilder (outside the furigana) output.append(currentKana); // Keep track of number of kana addded to see if the algorithm was successful numKana += currentReading.length() + currentKana.length(); } // Add any kanji / reading at the end of the string to the builder if (lastKanaEnd < kanji.length()) { String currentKanji = kanji.substring(lastKanaEnd + 1); String currentReading = reading.substring(lastReadingKanaEnd + 1); output.append(String.format(RUBY, currentKanji, currentReading)); numKana += currentReading.length(); } // Do sanity check, returning naiive substitution if it failed if (numKana < reading.length()) { return String.format(RUBY, kanji, reading); } return output.toString().trim(); }
From source file:gate.creole.splitter.RegexSentenceSplitter.java
@Override public void execute() throws ExecutionException { interrupted = false;/*from w w w .j a v a 2 s . co m*/ int lastProgress = 0; fireProgressChanged(lastProgress); //get pointers to the annotation sets AnnotationSet outputAS = (outputASName == null || outputASName.trim().length() == 0) ? document.getAnnotations() : document.getAnnotations(outputASName); String docText = document.getContent().toString(); /* If the document's content is empty or contains only whitespace, * we drop out right here, since there's nothing to sentence-split. */ if (docText.trim().length() < 1) { return; } Matcher internalSplitMatcher = internalSplitsPattern.matcher(docText); Matcher externalSplitMatcher = externalSplitsPattern.matcher(docText); Matcher nonSplitMatcher = nonSplitsPattern.matcher(docText); //store all non split locations in a list of pairs List<int[]> nonSplits = new LinkedList<int[]>(); while (nonSplitMatcher.find()) { nonSplits.add(new int[] { nonSplitMatcher.start(), nonSplitMatcher.end() }); } //this lists holds the next matches at each step List<MatchResult> nextSplitMatches = new ArrayList<MatchResult>(); //initialise matching process MatchResult internalMatchResult = null; if (internalSplitMatcher.find()) { internalMatchResult = internalSplitMatcher.toMatchResult(); nextSplitMatches.add(internalMatchResult); } MatchResult externalMatchResult = null; if (externalSplitMatcher.find()) { externalMatchResult = externalSplitMatcher.toMatchResult(); nextSplitMatches.add(externalMatchResult); } MatchResultComparator comparator = new MatchResultComparator(); int lastSentenceEnd = 0; while (!nextSplitMatches.isEmpty()) { //see which one matches first Collections.sort(nextSplitMatches, comparator); MatchResult nextMatch = nextSplitMatches.remove(0); if (nextMatch == internalMatchResult) { //we have a new internal split; see if it's vetoed or not if (!veto(nextMatch, nonSplits)) { //split is not vetoed try { //add the split annotation FeatureMap features = Factory.newFeatureMap(); features.put("kind", "internal"); outputAS.add(new Long(nextMatch.start()), new Long(nextMatch.end()), "Split", features); //generate the sentence annotation int endOffset = nextMatch.end(); //find the first non whitespace character starting from where the //last sentence ended while (lastSentenceEnd < endOffset && Character.isWhitespace(Character.codePointAt(docText, lastSentenceEnd))) { lastSentenceEnd++; } //if there is any useful text between the two offsets, generate //a new sentence if (lastSentenceEnd < nextMatch.start()) { outputAS.add(new Long(lastSentenceEnd), new Long(endOffset), ANNIEConstants.SENTENCE_ANNOTATION_TYPE, Factory.newFeatureMap()); } //store the new sentence end lastSentenceEnd = endOffset; } catch (InvalidOffsetException e) { // this should never happen throw new ExecutionException(e); } } //prepare for next step if (internalSplitMatcher.find()) { internalMatchResult = internalSplitMatcher.toMatchResult(); nextSplitMatches.add(internalMatchResult); } else { internalMatchResult = null; } } else if (nextMatch == externalMatchResult) { //we have a new external split; see if it's vetoed or not if (!veto(nextMatch, nonSplits)) { //split is not vetoed try { //generate the split FeatureMap features = Factory.newFeatureMap(); features.put("kind", "external"); outputAS.add(new Long(nextMatch.start()), new Long(nextMatch.end()), "Split", features); //generate the sentence annotation //find the last non whitespace character, going backward from //where the external skip starts int endOffset = nextMatch.start(); while (endOffset > lastSentenceEnd && Character.isSpaceChar(Character.codePointAt(docText, endOffset - 1))) { endOffset--; } //find the first non whitespace character starting from where the //last sentence ended while (lastSentenceEnd < endOffset && Character.isSpaceChar(Character.codePointAt(docText, lastSentenceEnd))) { lastSentenceEnd++; } //if there is any useful text between the two offsets, generate //a new sentence if (lastSentenceEnd < endOffset) { outputAS.add(new Long(lastSentenceEnd), new Long(endOffset), ANNIEConstants.SENTENCE_ANNOTATION_TYPE, Factory.newFeatureMap()); } //store the new sentence end lastSentenceEnd = nextMatch.end(); } catch (InvalidOffsetException e) { // this should never happen throw new ExecutionException(e); } } //prepare for next step if (externalSplitMatcher.find()) { externalMatchResult = externalSplitMatcher.toMatchResult(); nextSplitMatches.add(externalMatchResult); } else { externalMatchResult = null; } } else { //malfunction throw new ExecutionException("Invalid state - cannot identify match!"); } //report progress int newProgress = 100 * lastSentenceEnd / docText.length(); if (newProgress - lastProgress > 20) { lastProgress = newProgress; fireProgressChanged(lastProgress); } } //while(!nextMatches.isEmpty()){ fireProcessFinished(); }
From source file:net.sf.jasperreports.engine.query.JRJdbcQueryExecuter.java
protected boolean isProcedureCall(String queryString) throws SQLException { if (!OracleProcedureCallHandler.isOracle(connection)) { //only supporting oracle for now return false; }//ww w. j a v a 2 s. c o m Matcher matcher = PROCEDURE_CALL_PATTERN.matcher(queryString); return matcher.find() && matcher.start() == 0; }
From source file:be.docarch.odt2braille.PEF.java
private int countPages(File brailleFile, Volume volume) throws IOException { int pageCount = 0; FileInputStream fileInputStream = new FileInputStream(brailleFile); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8"); String brfInput = IOUtils.toString(inputStreamReader); try {/*from w w w. jav a 2s . c o m*/ Matcher matcher = Pattern.compile("(\f|\uE000)").matcher(brfInput); pageCount = 1; while (matcher.find()) { char ch = brfInput.charAt(matcher.start()); if (ch == '\f') { pageCount++; } else { if (volume.getTableOfContent()) { pageCount--; } break; } } } finally { if (inputStreamReader != null) { inputStreamReader.close(); fileInputStream.close(); } } return pageCount; }
From source file:com.jsmartframework.web.manager.ExpressionHandler.java
public Object getExpressionValue(Object expr) { if (expr != null) { String evalExpr = expr.toString(); Matcher matcher = EL_PATTERN.matcher(evalExpr); if (!matcher.find()) { return expr; }//from ww w. jav a2s . co m boolean hasMoreGroup = false; StringBuffer exprBuffer = new StringBuffer(); Object result = evaluateExpression(evalExpr.substring(matcher.start() + 2, matcher.end() - 1)); matcher.appendReplacement(exprBuffer, result != null ? Matcher.quoteReplacement(result.toString()) : "null"); while (matcher.find()) { hasMoreGroup = true; Object object = evaluateExpression(evalExpr.substring(matcher.start() + 2, matcher.end() - 1)); matcher.appendReplacement(exprBuffer, object != null ? Matcher.quoteReplacement(object.toString()) : "null"); } if (hasMoreGroup || result instanceof String) { return matcher.appendTail(exprBuffer).toString(); } else { return result; } } return null; }
From source file:com.g3net.tool.StringUtils.java
/** * /* w w w. j av a2 s .co m*/ * @param srcStr * @param regexp * @param ignoreCase * @return */ public static int indexOf(String srcStr, String regexp, boolean ignoreCase) { Pattern p = null; if (ignoreCase) { p = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE); } else { p = Pattern.compile(regexp); } Matcher m = p.matcher(srcStr); while (m.find()) { // log.info(m.group()+":"+m.start()+":"+m.end()); return m.start(); } return -1; // sql3.regionMatches(ignoreCase, toffset, other, ooffset, len) // log.info(m.matches()); }