public ControllerRequest(Class<? extends Controller> controllerClass, Matcher matchedUrl) { this.controllerClass = controllerClass; if (matchedUrl.groupCount() > 0) { List<String> argsList = new ArrayList<String>(matchedUrl.groupCount()); for (int i = 1; i <= matchedUrl.groupCount(); i++) { argsList.add(matchedUrl.group(i)); } this.args = argsList; } else { this.args = new ArrayList<String>(0); } }
public static void main(String[] args) throws PatternSyntaxException { Scanner in = new Scanner(System.in); System.out.println("Enter pattern: "); String patternString = in.nextLine(); Pattern pattern = Pattern.compile(patternString); while (true) { System.out.println("Enter string to match: "); String input = in.nextLine(); if (input == null || input.equals("")) return; Matcher matcher = pattern.matcher(input); if (matcher.matches()) { System.out.println("Match"); int g = matcher.groupCount(); if (g > 0) { for (int i = 0; i < input.length(); i++) { // Print any empty groups for (int j = 1; j <= g; j++) if (i == matcher.start(j) && i == matcher.end(j)) System.out.print("()"); // Print ( for non-empty groups starting here for (int j = 1; j <= g; j++) if (i == matcher.start(j) && i != matcher.end(j)) System.out.print('('); System.out.print(input.charAt(i)); // Print ) for non-empty groups ending here for (int j = 1; j <= g; j++) if (i + 1 != matcher.start(j) && i + 1 == matcher.end(j)) System.out.print(')'); } System.out.println(); } } else System.out.println("No match"); } }
@Override public void processingInstruction(String target, String data) throws SAXException { _logger.fine("Processing Instruction " + target); _logger.fine("Processing Instruction data: " + data); if (target.equals("assemble")) { if (!_stack.isEmpty()) { ElementInfo element = _stack.get(_stack.size() - 1); Matcher matcher = PROCESSING_INSTRUCTION.matcher(data); while (matcher.find()) { if (matcher.groupCount() == 2) { String name = matcher.group(1); if (name.charAt(0) == '@') { element.inst.put(name, matcher.group(2)); } else { element.args.add(guessUntypedValue(name, matcher.group(2))); } _logger.fine( "Processing Instruction for " + element.data.getClass() + "\n\ttarget = " + target + "\n\t" + name + "=" + matcher.group(2)); } } } } }
public static void loadPermissions(URL url) throws IOException, PermissionParseException { BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); String line; Pattern ignore = Pattern.compile("^\\s*(//.*)?$"); Pattern valid = Pattern.compile("^\\s*permission\\s+(\\S+)" + "(\\s+\"([^\"]*)\"(,\\s+\"([^\"]*)\")?)?;$"); Set<Permission> perms = new HashSet<Permission>(); while ((line = in.readLine()) != null) { if (ignore.matcher(line).matches()) { continue; } Matcher matcher = valid.matcher(line); if (!matcher.matches()) { throw new PermissionParseException("invalid syntax: " + line); } int nGroups = matcher.groupCount(); String type = matcher.group(1); String name = expand(nGroups >= 3 ? matcher.group(3) : null); String actions = expand(nGroups >= 5 ? matcher.group(5) : null); try { Permission perm = getPermission(type, name, actions); perms.add(perm); } catch (Throwable e) { String message = String.format( "could not instantiate permission: " + "type=%s name=%s actions=", type, name, actions); throw new PermissionParseException(message, e); } } in.close(); permSet.addAll(perms); }
/** * Replace occurrences of "%ab" with the character represented by the hex value. Strings of * escaped characters are treated as UTF-8 byte sequences and decoded appropriately. */ private static String decode(String s) { int length = s.length(); StringBuilder str = new StringBuilder(length); Matcher matcher = PATTERN.matcher(s); int offset = 0; byte[] bb = null; while (matcher.find(offset)) { int count = matcher.groupCount(); for (int i = 0; i < count; i++) { String match = matcher.group(0); int num = match.length() / 3; if (bb == null || bb.length < num) { bb = new byte[num]; } for (int j = 0; j < num; j++) { int head = j * 3 + 1; int tail = head + 2; bb[j] = (byte) Integer.parseInt(match.substring(head, tail), 16); } try { String text = new String(bb, "UTF-8"); str.append(s.substring(offset, matcher.start())); str.append(text); } catch (UnsupportedEncodingException e) { // NOTE: This should *never* be thrown because all // JVMs are required to support UTF-8. I mean, // the strings in the .class file are all in // a modified UTF-8, for pete's sake! :) } } offset = matcher.end(); } if (offset < length) { str.append(s.substring(offset)); } return str.toString(); }