public static void verifyCommand( String args[], @SuppressWarnings("unused") String help, Pattern[] patterns, int low, int high) { String message = ""; if (args.length > high) { message = "too many arguments"; } else if (args.length < low) { message = "too few arguments"; } else { for (int i = 0; patterns != null && i < patterns.length && i < args.length; i++) { if (patterns[i] != null) { Matcher m = patterns[i].matcher(args[i]); if (!m.matches()) message += String.format( "Argument %s (%s) does not match %s%n", i, args[i], patterns[i].pattern()); } } } if (message.length() != 0) { StringBuilder sb = new StringBuilder(); String del = "${"; for (String arg : args) { sb.append(del); sb.append(arg); del = ";"; } sb.append("}, is not understood. "); sb.append(message); throw new IllegalArgumentException(sb.toString()); } }
public String _range(String args[]) { verifyCommand(args, _rangeHelp, _rangePattern, 2, 3); Version version = null; if (args.length >= 3) version = new Version(args[2]); else { String v = domain.getProperty("@"); if (v == null) return null; version = new Version(v); } String spec = args[1]; Matcher m = RANGE_MASK.matcher(spec); m.matches(); String floor = m.group(1); String floorMask = m.group(2); String ceilingMask = m.group(3); String ceiling = m.group(4); String left = version(version, floorMask); String right = version(version, ceilingMask); StringBuilder sb = new StringBuilder(); sb.append(floor); sb.append(left); sb.append(","); sb.append(right); sb.append(ceiling); String s = sb.toString(); VersionRange vr = new VersionRange(s); if (!(vr.includes(vr.getHigh()) || vr.includes(vr.getLow()))) { domain.error( "${range} macro created an invalid range %s from %s and mask %s", s, version, spec); } return sb.toString(); }
/** * Get the noun and verb <roots> (i.e. 'the highest' synsets in WordNet) from the particular * 'icfile' (Information Content) that you are applying. Store a noun <root>: a synset offset * number of type Integer in nounroots: an ArrayList<Integer> defined in the constructor of this * class. Store a verb <root>: a synset offset number of type Integer in verbroots: an * ArrayList<Integer> defined in the constructor of this class. * * <p>An example line in an 'icfile', showing a noun <root>: 1740n 128767 ROOT */ private void getRoots() { Pattern pn = Pattern.compile("[0-9]+n [0-9]+ ROOT"); // find noun <root> Pattern pv = Pattern.compile("[0-9]+v [0-9]+ ROOT"); // find verb <root> Matcher m = null; String root = ""; try { BufferedReader in = new BufferedReader(new FileReader(icfile)); String line; while ((line = in.readLine()) != null) { // nouns m = pn.matcher(line); if (m.matches()) { root = (line.split("\\s")[0]).split("n")[0]; // !!! double split !!! nounroots.add(Integer.parseInt(root)); } // verbs m = pv.matcher(line); if (m.matches()) { root = (line.split("\\s")[0]).split("v")[0]; // !!! double split !!! verbroots.add(Integer.parseInt(root)); } } in.close(); } catch (IOException e) { e.printStackTrace(); } }
private static String expand(String str) { if (str == null) { return null; } StringBuilder result = new StringBuilder(); Pattern re = Pattern.compile("^(.*?)\\$\\{([^}]*)\\}(.*)"); while (true) { Matcher matcher = re.matcher(str); if (matcher.matches()) { result.append(matcher.group(1)); String property = matcher.group(2); if (property.equals("/")) { property = "file.separator"; } String value = System.getProperty(property); if (value != null) { result.append(value); } str = matcher.group(3); } else { result.append(str); break; } } return result.toString(); }
public String getArtifactIdFromCoord(String coord) { Matcher m = COORD_P.matcher(coord); if (m.matches()) { return m.group(2); } else { return null; } }
@Override @EventHandler public void onChatReceived(ChatReceivedEvent event) { super.onChatReceived(event); String message = Util.stripColors(event.getMessage()); if (message.startsWith("Please register with \"/register")) { String password = Util.generateRandomString(10 + random.nextInt(6)); bot.say("/register " + password + " " + password); } else if (message.contains("You are not member of any faction.") && spamMessage != null && createFaction) { String msg = "/f create " + Util.generateRandomString(7 + random.nextInt(4)); bot.say(msg); } for (String s : captchaList) { Matcher captchaMatcher = Pattern.compile(s).matcher(message); if (captchaMatcher.matches()) bot.say(captchaMatcher.group(1)); } }
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); }
public String what(String key, boolean oneliner) throws Exception { byte[] sha; Matcher m = SHA_P.matcher(key); if (m.matches()) { sha = Hex.toByteArray(key); } else { m = URL_P.matcher(key); if (m.matches()) { URL url = new URL(key); sha = SHA1.digest(url.openStream()).digest(); } else { File jarfile = new File(key); if (!jarfile.exists()) { reporter.error("File does not exist: %s", jarfile.getCanonicalPath()); } sha = SHA1.digest(jarfile).digest(); } } reporter.trace("sha %s", Hex.toHexString(sha)); Revision revision = library.getRevision(sha); if (revision == null) { return null; } StringBuilder sb = new StringBuilder(); Formatter f = new Formatter(sb); Justif justif = new Justif(120, 20, 70, 20, 75); DateFormat dateFormat = DateFormat.getDateInstance(); try { if (oneliner) { f.format("%20s %s%n", Hex.toHexString(revision._id), createCoord(revision)); } else { f.format("Artifact: %s%n", revision.artifactId); if (revision.organization != null && revision.organization.name != null) { f.format(" (%s)", revision.organization.name); } f.format("%n"); f.format("Coordinates\t0: %s%n", createCoord(revision)); f.format("Created\t0: %s%n", dateFormat.format(new Date(revision.created))); f.format("Size\t0: %d%n", revision.size); f.format("Sha\t0: %s%n", Hex.toHexString(revision._id)); f.format("URL\t0: %s%n", createJpmLink(revision)); f.format("%n"); f.format("%s%n", revision.description); f.format("%n"); f.format("Dependencies\t0:%n"); boolean flag = false; Iterable<RevisionRef> closure = library.getClosure(revision._id, true); for (RevisionRef dep : closure) { f.format( " - %s \t2- %s \t3- %s%n", dep.name, createCoord(dep), dateFormat.format(new Date(dep.created))); flag = true; } if (!flag) { f.format(" None%n"); } f.format("%n"); } f.flush(); justif.wrap(sb); return sb.toString(); } finally { f.close(); } }