protected String detectCSS(String text) { // CSS (they are rarely returned with mimetype "unk" or "text/html") int pos = 0; Matcher cm = RE_CSS_COMMENT.matcher(text); { cm.region(pos, text.length()); while (cm.lookingAt()) { cm.region(pos = cm.end(), text.length()); } } { Matcher m = RE_CSS_AT_RULE.matcher(text); m.region(pos, text.length()); if (m.lookingAt()) { return "text/css"; } } { Matcher m = RE_CSS_RULESET_START.matcher(text); m.region(pos, text.length()); if (m.lookingAt()) { cm.region(pos = m.end(), text.length()); while (cm.lookingAt()) { cm.region(pos = cm.end(), text.length()); } Matcher sm = RE_CSS_DECLARATION.matcher(text); sm.region(pos, text.length()); if (sm.lookingAt()) { return "text/css"; } } } return null; }
protected String detectHTML(String text) { int pos = 0; { Matcher m = RE_XML_PROLOGUE.matcher(text); if (m.lookingAt()) { pos = m.end(); } } { Matcher m = RE_SGML_COMMENT.matcher(text); m.region(pos, text.length()); while (m.lookingAt()) { m.region(pos = m.end(), text.length()); } } { Matcher m = RE_DOCTYPE_HTML.matcher(text); m.region(pos, text.length()); if (m.lookingAt()) return "text/html"; } { Matcher m = RE_HTML_ELEMENTS.matcher(text); m.region(pos, text.length()); if (m.lookingAt()) return "text/html"; } { Matcher m = RE_END_TAG.matcher(text); m.region(pos, text.length()); if (m.find()) return "text/html"; } return null; }
/** * Parses the input; looks for the appropriate header. Then parses the position list until no more * positions can be found. */ private void parseInput(String input) throws IOException, PatternSyntaxException { // search for header input. once found, shuttle all input to the appropriate // handler type. Pattern pp1 = Pattern.compile(HEADER_REGEX_1); Pattern pp2 = Pattern.compile(HEADER_REGEX_2); // init final List<PositionInfo> posList = new LinkedList<PositionInfo>(); BufferedReader br = new BufferedReader(new StringReader(input)); // header parse loop String line = ParserUtils.getNextLongLine(br); while (line != null) { Matcher m = pp1.matcher(line); if (m.lookingAt()) { phase = makePhase(null, m.group(1), m.group(2)); parsePositions(br, posList); break; } m = pp2.matcher(line); if (m.lookingAt()) { phase = makePhase(m.group(1), m.group(2), m.group(3)); parsePositions(br, posList); break; } line = ParserUtils.getNextLongLine(br); } // cleanup & create array br.close(); posInfo = posList.toArray(new PositionInfo[posList.size()]); } // parseInput()
protected String detectJavaScript(String text) { int pos = 0; { Matcher m = RE_JS_WS_OR_COMMENT.matcher(text); while (m.lookingAt()) { m.region(pos = m.end(), text.length()); } } { Matcher m = RE_JS_VAR.matcher(text); m.region(pos, text.length()); if (m.lookingAt()) return "text/javascript"; } { Matcher m = RE_JS_FUNCTION.matcher(text); m.region(pos, text.length()); if (m.lookingAt()) return "text/javascript"; } { Matcher m = RE_JS_DOCUMENT_WRITE.matcher(text); m.region(pos, text.length()); if (m.lookingAt()) return "text/javascript"; } { Matcher m = RE_JSON_HEAD.matcher(text); m.region(pos, text.length()); if (m.lookingAt()) { // TODO: if resource has content-type "text/javascript", just // use it. return "application/json"; } } return null; }
public void test_lookingAt() { String testPattern = "(((abb)a)(bb))"; String testString1 = "babbabbcccabbabbabbabbabb"; String testString2 = "abbabb"; Pattern pat = Pattern.compile(testPattern); Matcher mat1 = pat.matcher(testString1); Matcher mat2 = pat.matcher(testString2); assertFalse("Should not find given pattern in 1 string", mat1.lookingAt()); mat1.region(1, 10); assertTrue("Should find given pattern in region of string", mat1.lookingAt()); assertTrue("Should find given pattern in 2 string", mat2.lookingAt()); }
/** Advance to the next token. */ public void nextToken() { previousLine = line; previousColumn = column; // Advance the line counter to the current position. while (pos < matcher.regionStart()) { if (text.charAt(pos) == '\n') { ++line; column = 0; } else { ++column; } ++pos; } // Match the next token. if (matcher.regionStart() == matcher.regionEnd()) { // EOF currentToken = ""; } else { matcher.usePattern(TOKEN); if (matcher.lookingAt()) { currentToken = matcher.group(); matcher.region(matcher.end(), matcher.regionEnd()); } else { // Take one character. currentToken = String.valueOf(text.charAt(pos)); matcher.region(pos + 1, matcher.regionEnd()); } skipWhitespace(); } }
public IToken evaluate(ICharacterScanner scanner) { if (scanner.getColumn() != 0) { return Token.UNDEFINED; } // Consume all leading spaces. StringBuffer spaces = readLeadingSpaces(scanner); // Consume the first two words. StringBuffer text = readWord(scanner); text.append(readWord(scanner)); Iterator<Pattern> patterns = tokenLookup.keySet().iterator(); while (patterns.hasNext()) { Pattern pattern = patterns.next(); if (pattern == null) { continue; } // Is there a headline? Matcher matcher = pattern.matcher(text); if (matcher.lookingAt()) { // Yes. Consume the rest of the line. readLine(scanner); return tokenLookup.get(pattern); } } // There is no headline. Rewind the scanner. unread(scanner, spaces.length() + text.length()); return Token.UNDEFINED; }
/** * Reads the tests results from the browser output and creates the modules and tests objects from * it. * * @param page Page which contains the tests results. It cannot be null. */ private void readTests(final HtmlPage page) { Validate.notNull(page, "The page cannot be null."); HtmlElement element = page.getHtmlElementById("qunit-tests"); List<HtmlElement> testResults = element.getElementsByTagName("li"); TestSuite currentModule = null; for (HtmlElement result : testResults) { Matcher matcher = summary.matcher(result.asText()); List<HtmlElement> testOutput = result.getElementsByTagName("li"); if (matcher.lookingAt()) { TestSuite module = getModule(matcher); TestCase test = buildTest(matcher); if (currentModule != module) { if (currentModule != null) { currentModule.done(); } currentModule = module; } for (HtmlElement outputLine : testOutput) { test.print(outputLine.getFirstChild().asXml()); } module.addTest(test); } } }
private void parseFile(File file, DefaultSourceDetails sourceDetails) { try { BufferedReader bf = new BufferedReader(new PreprocessingReader(new BufferedReader(new FileReader(file)))); try { String line; while ((line = bf.readLine()) != null) { Matcher m = includePattern.matcher(line); if (m.lookingAt()) { boolean isImport = "import".equals(m.group(1)); String value = m.group(2); if (isImport) { sourceDetails.getImports().add(value); } else { sourceDetails.getIncludes().add(value); } } } } finally { IOUtils.closeQuietly(bf); } } catch (IOException e) { throw new UncheckedIOException(e); } }
@Override protected boolean parseMsg(String body, Data data) { Matcher match = MARKER.matcher(body); if (!match.lookingAt()) return false; body = match.group(1); return super.parseFields(body.split("!"), data); }
public void skip(Pattern pattern) { Matcher m = pattern.matcher(currentString); if (m.lookingAt()) { currentString = currentString.substring(m.end()); } else { throw new IllegalStateException( String.format("'%s' expected in the beginning of '%s'", pattern, currentString)); } }
/** * Attempt to skip a substring starting from the beginning of the current string and matched by * the given pattern. * * @param pattern * @return true if the given pattern is matched against the beginning of the current string */ public boolean skipOptional(Pattern pattern) { Matcher m = pattern.matcher(currentString); if (m.lookingAt()) { currentString = currentString.substring(m.end()); return true; } else { return false; } }
private ExpressionType getExpressionType() { skipWhiteSpace(); for (Pattern p : patternMap.keySet()) { Matcher expMatch = p.matcher(myInput.substring(myCurrentPosition)); if (expMatch.lookingAt()) return patternMap.get(p); } throw new ParserException("Unexpected Character " + currentCharacter()); }
private static String readIntegerString() { // read chars from input following syntax of integers skipWhitespace(); if (lookChar() == EOF) return null; if (integerMatcher == null) integerMatcher = integerRegex.matcher(buffer); integerMatcher.region(pos, buffer.length()); if (integerMatcher.lookingAt()) { String str = integerMatcher.group(); pos = integerMatcher.end(); return str; } else return null; }
public final Rule matchRule(String paramString) { Matcher localMatcher = this.mPattern.matcher(paramString); if (localMatcher.lookingAt()) { for (int i = 0; i < this.mRules.length; i++) { if (localMatcher.group(i + 1) != null) { return this.mRules[i]; } } } return Rule.DEFAULT; }
public boolean containsMatch(String regex) { Pattern p = Pattern.compile(regex); for (Status status : sm.getCopyOfStatusList()) { String msg = status.getMessage(); Matcher matcher = p.matcher(msg); if (matcher.lookingAt()) { return true; } } return false; }
public String next(String regexp) throws ParseException { trim(); Pattern p = Pattern.compile(regexp); Matcher m = p.matcher(s); if (m.lookingAt()) { String r = m.group(); s = s.substring(r.length()); trim(); return r; } else throw error(); }
private static String readRealString() { // read chars from input following syntax of real numbers skipWhitespace(); if (lookChar() == EOF) return null; if (floatMatcher == null) floatMatcher = floatRegex.matcher(buffer); floatMatcher.region(pos, buffer.length()); if (floatMatcher.lookingAt()) { String str = floatMatcher.group(); pos = floatMatcher.end(); return str; } else return null; }
public int matchCount(String regex) { int count = 0; Pattern p = Pattern.compile(regex); for (Status status : sm.getCopyOfStatusList()) { String msg = status.getMessage(); Matcher matcher = p.matcher(msg); if (matcher.lookingAt()) { count++; } } return count; }
/* * public boolean lookingAt() * Attempts to match the input sequence, starting at the * beginning of the region, against the pattern. */ @Test public void testLookingAt() { final String regex = "0[0-7]+"; final String input = "0715fjsdhfksahfdkjas"; Pattern pattern = Pattern.compile(regex); Matcher m = pattern.matcher(input); boolean result = m.lookingAt(); assertTrue(result); }
public static void main(String[] args) { // ³õʼ»¯ pattern = Pattern.compile(REGEX); matcher = pattern.matcher(INPUT); System.out.println("Current REGEX is: " + REGEX); System.out.println("Current INPUT is: " + INPUT); System.out.println("lookingAt(): " + matcher.lookingAt()); System.out.println("matches(): " + matcher.matches()); }
private String readDateString() { // read chars from input // following syntax of real // numbers skipWhitespace(); if (lookChar() == EOF) return null; if (dateMatcher == null) dateMatcher = dateRegex.matcher(buffer); dateMatcher.region(pos, buffer.length()); if (dateMatcher.lookingAt()) { String str = dateMatcher.group(); pos = dateMatcher.end(); return str; } else return null; }
/** * Initialize derived fields from defining fields. This is called from constructor and from * readObject (de-serialization) * * @param definingCalendar the {@link Calendar} instance used to initialize this FastDateParser */ private void init(Calendar definingCalendar) { final StringBuilder regex = new StringBuilder(); final List<Strategy> collector = new ArrayList<Strategy>(); final Matcher patternMatcher = formatPattern.matcher(pattern); if (!patternMatcher.lookingAt()) { throw new IllegalArgumentException( "Illegal pattern character '" + pattern.charAt(patternMatcher.regionStart()) + "'"); } currentFormatField = patternMatcher.group(); Strategy currentStrategy = getStrategy(currentFormatField, definingCalendar); for (; ; ) { patternMatcher.region(patternMatcher.end(), patternMatcher.regionEnd()); if (!patternMatcher.lookingAt()) { nextStrategy = null; break; } final String nextFormatField = patternMatcher.group(); nextStrategy = getStrategy(nextFormatField, definingCalendar); if (currentStrategy.addRegex(this, regex)) { collector.add(currentStrategy); } currentFormatField = nextFormatField; currentStrategy = nextStrategy; } if (patternMatcher.regionStart() != patternMatcher.regionEnd()) { throw new IllegalArgumentException( "Failed to parse \"" + pattern + "\" ; gave up at index " + patternMatcher.regionStart()); } if (currentStrategy.addRegex(this, regex)) { collector.add(currentStrategy); } currentFormatField = null; strategies = collector.toArray(new Strategy[collector.size()]); parsePattern = Pattern.compile(regex.toString()); }
/** * Try to parse a prefix of current string by given pattern. If succeed current string will be * replaced by remaining suffix. Else will return null and current string will not change. * * @param pattern * @return array of matcher groups if pattern successfully matches prefix of current string, * otherwise - null. First element of returned array (index - 0) contains whole string matched * by given pattern. */ public String[] consumeOptional(Pattern pattern) { Matcher m = pattern.matcher(currentString); if (m.lookingAt()) { String[] result = new String[m.groupCount() + 1]; result[0] = m.group(); for (int i = 1; i <= m.groupCount(); i++) { result[i] = m.group(i); } currentString = currentString.substring(m.end()); return result; } else { return null; } }
private void populateHostAndPort() { String line = this.getStartLine(); Matcher matcher = REQUEST_LINE_PATTERN.matcher(line); if (!matcher.lookingAt()) { throw new RuntimeException("No match in HOST line: " + line); } this.host = matcher.group(2); String portString = matcher.group(3); if (portString != null && portString.length() > 0 && portString.charAt(0) == ':') { this.port = Integer.parseInt(portString.substring(1)); } else { this.port = DEFAULT_PORT; } }
/** * Returns a media type for {@code string}, or null if {@code string} is not a well-formed media * type. */ public static MediaType parse(String string) { if (string == null) return null; Matcher typeSubtype = TYPE_SUBTYPE.matcher(string); if (!typeSubtype.lookingAt()) return null; String type = typeSubtype.group(1).toLowerCase(Locale.US); String subtype = typeSubtype.group(2).toLowerCase(Locale.US); String charset = null; Matcher parameter = PARAMETER.matcher(string); for (int s = typeSubtype.end(); s < string.length(); s = parameter.end()) { parameter.region(s, string.length()); if (!parameter.lookingAt()) return null; // This is not a well-formed media type. String name = parameter.group(1); if (name == null || !name.equalsIgnoreCase("charset")) continue; if (charset != null) throw new IllegalArgumentException("Multiple charsets: " + string); charset = parameter.group(2) != null ? parameter.group(2) // Value is a token. : parameter.group(3); // Value is a quoted string. } return new MediaType(string, type, subtype, charset); }
private void insertBreakInMLC(ActionEvent e, RSyntaxTextArea textArea, int line) { Matcher m = null; int start = -1; int end = -1; try { start = textArea.getLineStartOffset(line); end = textArea.getLineEndOffset(line); String text = textArea.getText(start, end - start); m = p.matcher(text); } catch (BadLocationException ble) { // Never happens UIManager.getLookAndFeel().provideErrorFeedback(textArea); ble.printStackTrace(); return; } if (m.lookingAt()) { String leadingWS = m.group(1); String mlcMarker = m.group(2); // If the caret is "inside" any leading whitespace or MLC // marker, move it to the end of the line. int dot = textArea.getCaretPosition(); if (dot >= start && dot < start + leadingWS.length() + mlcMarker.length()) { // If we're in the whitespace before the very start of the // MLC though, just insert a normal newline if (mlcMarker.charAt(0) == '/') { handleInsertBreak(textArea, true); return; } textArea.setCaretPosition(end - 1); } boolean firstMlcLine = mlcMarker.charAt(0) == '/'; boolean nested = appearsNested(textArea, line, start + leadingWS.length() + 2); String header = leadingWS + (firstMlcLine ? " * " : "*") + m.group(3); textArea.replaceSelection("\n" + header); if (nested) { dot = textArea.getCaretPosition(); // Has changed textArea.insert("\n" + leadingWS + " */", dot); textArea.setCaretPosition(dot); } } else { handleInsertBreak(textArea, true); } }
/** Return a list of {@link Token}s corresponding to the input string. */ public static List<Token> tokenize(String input) { List<Token> result = new ArrayList<Token>(); Matcher m = TOKPAT.matcher(input.replaceFirst("^\\s+", "")); while (m.lookingAt()) { if (m.group(1) != null) { result.add(new Token(m.group(1), TokenType.FRACTION)); } else { assert m.group(2) != null; TokenType tt = (m.group(3).length() > 0) ? TokenType.STRING : TokenType.FRAGMENT; result.add(new Token(m.group(2), tt)); } input = input.substring(m.end()); m = TOKPAT.matcher(input); } return result; }
public boolean validate(Problems problems, String compName, String model) { Matcher m = ADDRESS_PATTERN.matcher(model); String realName = null; String address; if (m.lookingAt()) { if (m.groupCount() == 2) { address = m.group(2); realName = m.group(1); } else { address = m.group(1); } } else { address = model; } return validate(realName, address, problems, compName); }
public boolean containsMatch(long threshold, int level, String regex) { List<Status> filteredList = filterStatusListByTimeThreshold(sm.getCopyOfStatusList(), threshold); Pattern p = Pattern.compile(regex); for (Status status : filteredList) { if (level != status.getLevel()) { continue; } String msg = status.getMessage(); Matcher matcher = p.matcher(msg); if (matcher.lookingAt()) { return true; } } return false; }