Beispiel #1
1
  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();
  }
  public void testBaseUrlPath() throws Exception {
    sau1 = setupSimAu(simAuConfig(tempDirPath));
    createContent(sau1);
    crawlContent(sau1);
    CachedUrlSet cus1 = sau1.getAuCachedUrlSet();

    tempDirPath2 = getTempDir().getAbsolutePath() + File.separator;
    Configuration config2 = simAuConfig(tempDirPath2);
    config2.put("base_url", "http://anotherhost.org/some/path/");
    SimulatedArchivalUnit sau2 = setupSimAu(config2);
    createContent(sau2);
    crawlContent(sau2);
    CachedUrlSet cus2 = sau1.getAuCachedUrlSet();
    List urls1 = auUrls(sau1);
    List urls2 = auUrls(sau2);

    Pattern pat1 = Pattern.compile("http://www\\.example\\.com(/.*)$");
    Pattern pat2 = Pattern.compile("http://anotherhost\\.org/some/path(/.*)$");
    List<String> l1 = auUrls(sau1);
    List<String> l2 = auUrls(sau2);
    assertEquals(l1.size(), l2.size());
    for (int ix = 0; ix < l1.size(); ix++) {
      Matcher m1 = pat1.matcher(l1.get(ix));
      assertTrue(m1.matches());
      Matcher m2 = pat2.matcher(l2.get(ix));
      assertTrue(m2.matches());
      assertEquals(m1.group(1), m2.group(1));
    }
  }
  protected Map<String, String> getAttributes() {
    if (attributes != null) {
      return attributes;
    }

    attributes = new HashMap<String, String>();

    matcher = ADDITIONAL_ATTRIBUTES_PATTERN.matcher(firstLine);
    if (matcher.find()) {
      String s;
      Matcher attributeMatcher;

      s = matcher.group(2);
      attributeMatcher = ADDITIONAL_ATTRIBUTE_PATTERN.matcher(s);
      while (attributeMatcher.find()) {
        String key;
        String value;

        key = attributeMatcher.group(1);
        value = attributeMatcher.group(2);
        attributes.put(key.toLowerCase(Locale.ENGLISH), value);
      }
    }
    return attributes;
  }
 /**
  * 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();
   }
 }
Beispiel #5
0
public class RegexTest {
  public static final String DEST_REGEX = "^D\\w+\\w*$";
  public static final Pattern DEST_PATTERN = Pattern.compile(DEST_REGEX);
  public static Matcher DEST_MATCHER = DEST_PATTERN.matcher("");

  public static final String SOURCE_REGEX = "^S\\w+\\w*$";
  public static final Pattern SOURCE_PATTERN = Pattern.compile(SOURCE_REGEX);
  public static Matcher SOURCE_MATCHER = SOURCE_PATTERN.matcher("");

  public static final String STRING_REGEX = "^'[a-zA-Z_0-9 ]*'$";
  public static final Pattern STRING_PATTERN = Pattern.compile(STRING_REGEX);
  public static Matcher STRING_MATCHER = STRING_PATTERN.matcher("");

  public static final String VAR_REGEX = "^\\$\\w+\\w*$";
  public static final Pattern VAR_PATTERN = Pattern.compile(VAR_REGEX);
  public static Matcher VAR_MATCHER = VAR_PATTERN.matcher("");

  public static final String NUM_REGEX = "^[1-9]+[0-9]*|0|\\.[0-9]+|[0-9]+\\.[0-9]+$";
  public static final Pattern NUM_PATTERN = Pattern.compile(NUM_REGEX);
  public static Matcher NUM_MATCHER = NUM_PATTERN.matcher("");

  public static void main(String[] args) {
    String inputStr = null;
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

    try {
      while ((inputStr = reader.readLine()) != null) {
        inputStr = inputStr.trim();

        if (DEST_MATCHER.reset(inputStr).matches()) {
          System.out.println("Destination field!\n");
        } else if (SOURCE_MATCHER.reset(inputStr).matches()) {
          System.out.println("Source field!\n");
        } else if (STRING_MATCHER.reset(inputStr).matches()) {
          System.out.println("String literal!\n");
        } else if (VAR_MATCHER.reset(inputStr).matches()) {
          System.out.println("Variable!\n");
        } else if (NUM_MATCHER.reset(inputStr).matches()) {
          System.out.println("Number!\n");
        } else {
          System.out.println("Huh???\n");
        }
      }
    } catch (Exception ioe) {
      System.out.println("Exception: " + ioe.toString());
      ioe.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();
  }
 /**
  * Takes a line and looks for an archive request tag. If one is found, the information from the
  * tag is returned as an <CODE>ArchiveRequest</CODE>. If a tag is found, the data in the tag is
  * stored in the <CODE>HashMap</CODE> provided.
  *
  * @param currentLine The line in which to look for the archive tag.
  * @param group The <CODE>ArchiveGroup</CODE> to which to add the <CODE>ArchiveRequest</CODE>. Can
  *     be <CODE>null</CODE>.
  * @param template The <CODE>Template</CODE> to which the <CODE>ArchiveRequest</CODE> belongs.
  * @return The archive request tag data as an instance of <CODE>ArchiveRequest</CODE>, <CODE>null
  *     </CODE> if the line passed in does not contain an arFile tag.
  */
 private ArchiveRequest parseArchiveRequestTag(
     String currentLine, ArchiveGroup group, Template template) {
   ArchiveRequest archiveTag;
   Matcher currentMatcher = archiveRequestPattern.matcher(currentLine);
   if (currentMatcher.find()) {
     String fileName = currentMatcher.group(1);
     archiveTag = template.getArchiveRequest(fileName);
     if (archiveTag == null) {
       archiveTag = new ArchiveRequest(fileName);
       template.addArchiveRequest(archiveTag);
     }
     if (group != null) {
       // Creating a new request object because the requests
       // in the groups flagged as not in database will mean the
       // group - request association is not in database. Requests
       // in the archiveRequests map flagged as not in database
       // means the request record is not there.
       String requestFileName = archiveTag.getFileName();
       String requestFileLocation = archiveTag.getFileLocation();
       ArchiveRequest groupRequest = new ArchiveRequest(requestFileLocation, requestFileName);
       group.addArchiveRequest(groupRequest);
     }
   } else archiveTag = null;
   return archiveTag;
 }
Beispiel #8
0
  /**
   * Chooses files that match the specified pattern.
   *
   * @param file file filter
   * @param content content filter
   * @param root root directory
   * @return sorted file paths
   * @throws InterruptedException interruption
   */
  String[] filter(final String file, final String content, final IOFile root)
      throws InterruptedException {

    final long id = ++filterId;
    final TreeSet<String> results = new TreeSet<>();
    final int[] search = new TokenParser(Token.lc(Token.token(content))).toArray();

    // glob pattern
    final ProjectCache pc = cache(root);
    if (file.contains("*") || file.contains("?")) {
      final Pattern pt = Pattern.compile(IOFile.regex(file));
      for (final String path : pc) {
        final int offset = offset(path, true);
        if (pt.matcher(path.substring(offset)).matches() && filterContent(path, search)) {
          results.add(path);
          if (results.size() >= MAXHITS) break;
        }
        if (id != filterId) throw new InterruptedException();
      }
    } else {
      // starts-with, contains, camel case
      final String pttrn = file.toLowerCase(Locale.ENGLISH).replace('\\', '/');
      final HashSet<String> exclude = new HashSet<>();
      final boolean pathSearch = pttrn.indexOf('/') != -1;
      for (int i = 0; i < (pathSearch ? 2 : 3); i++) {
        filter(pttrn, search, i, results, exclude, pathSearch, pc, id);
      }
    }
    return results.toArray(new String[results.size()]);
  }
 public boolean accept(File pathname) {
   if (pathname.isDirectory()) {
     return true;
   } else {
     return pattern.matcher(pathname.getName()).matches();
   }
 }
 static void scanfile(Scanner input, String filename, Pattern pattern) {
   int numLines = 1;
   while (input.hasNextLine()) {
     String line = input.nextLine();
     boolean matches = pattern.matcher(line).find();
     if (matches && !options.reverse_match) {
       messages.exit_status = messages.EXIT_SUCCESS;
       if (options.filename_only) {
         out.printf(filename + "\n");
         return;
       } else if (options.number_lines) {
         out.printf(numLines + ": " + "%s:%s%n", filename, line);
       } else {
         out.printf("%s:%s%n", filename, line);
       }
     } else if (!matches && options.reverse_match) {
       if (options.number_lines) {
         out.printf(numLines + ": " + "%s:%s%n", filename, line);
       } else {
         out.printf("%s:%s%n", filename, line);
       }
       messages.exit_status = messages.EXIT_SUCCESS;
     }
     numLines++;
   }
 }
Beispiel #11
0
  private InstanceList readFile() throws IOException {

    String NL = System.getProperty("line.separator");
    Scanner scanner = new Scanner(new FileInputStream(fileName), encoding);

    ArrayList<Pipe> pipeList = new ArrayList<Pipe>();
    pipeList.add(new CharSequence2TokenSequence(Pattern.compile("\\p{L}\\p{L}+")));
    pipeList.add(new TokenSequence2FeatureSequence());

    InstanceList testing = new InstanceList(new SerialPipes(pipeList));

    try {
      while (scanner.hasNextLine()) {

        String text = scanner.nextLine();
        text = text.replaceAll("\\x0d", "");

        Pattern patten = Pattern.compile("^(.*?),(.*?),(.*)$");
        Matcher matcher = patten.matcher(text);

        if (matcher.find()) {
          docIds.add(matcher.group(1));
          testing.addThruPipe(new Instance(matcher.group(3), null, "test instance", null));
        }
      }
    } finally {
      scanner.close();
    }

    return testing;
  }
 public static String removePrimerLR(String pname) {
   Matcher m = primerLRPattern.matcher(pname);
   if (!m.matches()) {
     throw new IllegalArgumentException(pname);
   }
   return m.group(1);
 }
Beispiel #13
0
  /**
   * Get dependencies of a source file.
   *
   * @param path The canonical path of source file.
   * @return Path of dependencies.
   */
  private ArrayList<String> getDependencies(String path) {
    if (!dependenceMap.containsKey(path)) {
      ArrayList<String> dependencies = new ArrayList<String>();
      Matcher m = PATTERN_REQUIRE.matcher(read(path, charset));

      while (m.find()) {
        // Decide which root path to use.
        // Path wrapped in <> is related to root path.
        // Path wrapped in "" is related to parent folder of the source file.
        String root = null;

        if (m.group(1).equals("<")) {
          root = this.root;
        } else {
          root = new File(path).getParent();
        }

        // Get path of required file.
        String required = m.group(2);

        File f = new File(root, required);

        if (f.exists()) {
          dependencies.add(canonize(f));
        } else {
          App.exit("Cannot find required file " + required + " in " + path);
        }
      }

      dependenceMap.put(path, dependencies);
    }

    return dependenceMap.get(path);
  }
 public static String getChrom(String chromName) {
   Matcher m = chromPattern.matcher(chromName);
   if (!m.matches()) {
     return chromName;
   }
   return m.group(1);
 }
Beispiel #15
0
  private void copy(File workspaceDir, InputStream in, Pattern glob, boolean overwrite)
      throws Exception {

    Jar jar = new Jar("dot", in);
    try {
      for (Entry<String, Resource> e : jar.getResources().entrySet()) {

        String path = e.getKey();
        bnd.trace("path %s", path);

        if (glob != null && !glob.matcher(path).matches()) continue;

        Resource r = e.getValue();
        File dest = Processor.getFile(workspaceDir, path);
        if (overwrite
            || !dest.isFile()
            || dest.lastModified() < r.lastModified()
            || r.lastModified() <= 0) {

          bnd.trace("copy %s to %s", path, dest);

          File dp = dest.getParentFile();
          if (!dp.exists() && !dp.mkdirs()) {
            throw new IOException("Could not create directory " + dp);
          }

          IO.copy(r.openInputStream(), dest);
        }
      }
    } finally {
      jar.close();
    }
  }
Beispiel #16
0
 @Override
 public void actionPerformed(ActionEvent e) {
   if (td.getTabCount() > 0) {
     TextDocument ta = (TextDocument) td.getComponentAt(td.getSelectedIndex());
     Pattern pn = Pattern.compile(tf1.getText());
     Matcher mt = pn.matcher(ta.getText());
     if (e.getSource() == jb2) { // 取代
       ta.setText(mt.replaceAll(tf2.getText()));
     } else if (e.getSource() == jb1) { // 尋找
       Highlighter hl = ta.getHighlighter();
       hl.removeAllHighlights();
       while (mt.find()) {
         try {
           hl.addHighlight(
               mt.start(), mt.end(), new DefaultHighlighter.DefaultHighlightPainter(null));
         } catch (Exception ex) {
         }
       } // 開啟及關閉介面
     } else if (e.getSource() == replace_searchMenuItem) {
       System.out.println("Replace/Search is show:" + !show);
       if (show) {
         getContentPane().remove(jp);
         show = false;
       } else {
         getContentPane().add(jp, BorderLayout.SOUTH);
         show = true;
       }
       validate(); // 刷新容器
     }
   } else if (e.getSource() == replace_searchMenuItem) {
     JOptionPane.showMessageDialog(
         null, "尚無檔案,無法使用!", "Repace/Search error", JOptionPane.ERROR_MESSAGE);
   }
 }
  public boolean isPresent2(String patternStr) {
    Pattern p = Pattern.compile(patternStr);
    Matcher m = p.matcher(string2);
    boolean b = m.matches();

    return b;
  }
 public String getArtifactIdFromCoord(String coord) {
   Matcher m = COORD_P.matcher(coord);
   if (m.matches()) {
     return m.group(2);
   } else {
     return null;
   }
 }
 public boolean accept(File f) {
   if (f == null) {
     return false;
   }
   if (f.isDirectory()) {
     return true;
   }
   return pattern.matcher(f.getName()).matches();
 }
 public synchronized String format(String message) {
   Matcher matcher = variablePattern.matcher(message);
   while (matcher.find()) {
     String variable = matcher.group();
     variable = variable.substring(1);
     if (variable.startsWith("{") && variable.endsWith("}"))
       variable = variable.substring(1, variable.length() - 1);
     String value = variables.get(variable);
     if (value == null) value = "";
     message =
         message.replaceFirst(Pattern.quote(matcher.group()), Matcher.quoteReplacement(value));
   }
   matcher = colorPattern.matcher(message);
   while (matcher.find())
     message =
         message.substring(0, matcher.start()) + "\247" + message.substring(matcher.end() - 1);
   return message;
 }
 public Coords(String entry) {
   Matcher m = coordsPattern.matcher(entry);
   if (!m.matches()) {
     throw new IllegalArgumentException(entry);
   }
   chrom = m.group(1);
   start = Integer.parseInt(m.group(2));
   end = Integer.parseInt(m.group(3));
   strand = m.group(4).charAt(0);
 }
 public BasicSpellChecker(String file) throws IOException {
   BufferedReader in = new BufferedReader(new FileReader(file));
   Pattern p = Pattern.compile("\\w+");
   for (String temp = ""; temp != null; temp = in.readLine()) {
     Matcher m = p.matcher(temp.toLowerCase());
     while (m.find())
       nWords.put((temp = m.group()), nWords.containsKey(temp) ? nWords.get(temp) + 1 : 1);
   }
   in.close();
 }
  // todo: give options for document splitting. A line or the whole file or
  // sentence splitting as now
  public Iterator<List<IN>> getIterator(Reader r) {
    Tokenizer<IN> tokenizer = tokenizerFactory.getTokenizer(r);
    // PTBTokenizer.newPTBTokenizer(r, false, true);
    List<IN> words = new ArrayList<IN>();
    IN previous = tokenFactory.makeToken();
    StringBuilder prepend = new StringBuilder();

    /*
     * This changes SGML tags into whitespace -- it should maybe be moved
     * elsewhere
     */
    while (tokenizer.hasNext()) {
      IN w = tokenizer.next();
      String word = w.get(CoreAnnotations.TextAnnotation.class);
      Matcher m = sgml.matcher(word);
      if (m.matches()) {

        String before = StringUtils.getNotNullString(w.get(CoreAnnotations.BeforeAnnotation.class));
        String after = StringUtils.getNotNullString(w.get(CoreAnnotations.AfterAnnotation.class));
        prepend.append(before).append(word);
        String previousTokenAfter =
            StringUtils.getNotNullString(previous.get(CoreAnnotations.AfterAnnotation.class));
        previous.set(AfterAnnotation.class, previousTokenAfter + word + after);
        // previous.appendAfter(w.word() + w.after());
      } else {

        String before = StringUtils.getNotNullString(w.get(CoreAnnotations.BeforeAnnotation.class));
        if (prepend.length() > 0) {
          w.set(BeforeAnnotation.class, prepend.toString() + before);
          // w.prependBefore(prepend.toString());
          prepend = new StringBuilder();
        }
        words.add(w);
        previous = w;
      }
    }

    List<List<IN>> sentences = wts.process(words);
    String after = "";
    IN last = null;
    for (List<IN> sentence : sentences) {
      int pos = 0;
      for (IN w : sentence) {
        w.set(PositionAnnotation.class, Integer.toString(pos));
        after = StringUtils.getNotNullString(w.get(CoreAnnotations.AfterAnnotation.class));
        w.remove(AfterAnnotation.class);
        last = w;
      }
    }
    if (last != null) {
      last.set(AfterAnnotation.class, after);
    }

    return sentences.iterator();
  }
 protected boolean shouldIgnoreBCCAddress(String address) {
   MilterServerService milterService = Config.getConfig().getMilterServerService();
   List<String> ignoreAddresses = milterService.getIgnoreBCCAddress();
   Matcher m = headerPattern2.matcher(address.toLowerCase(Locale.ENGLISH).trim());
   if (m.matches()) {
     String mailAddress = m.group(1);
     for (String ignoreAddress : ignoreAddresses) {
       if (ignoreAddress.equalsIgnoreCase(mailAddress)) return true;
     }
   } else {
     m = headerPattern3.matcher(address.toLowerCase(Locale.ENGLISH).trim());
     if (m.matches()) {
       String mailAddress = m.group(1);
       for (String ignoreAddress : ignoreAddresses) {
         if (ignoreAddress.equalsIgnoreCase(mailAddress)) return true;
       }
     }
   }
   return false;
 }
  public int getStatus() {
    if (status != null) {
      return status;
    }

    matcher = STATUS_PATTERN.matcher(firstLine);
    if (matcher.find()) {
      status = Integer.parseInt(matcher.group(1));
    }
    return status;
  }
  public String getResult() {
    if (result != null) {
      return result;
    }

    matcher = RESULT_PATTERN.matcher(firstLine);
    if (matcher.find()) {
      result = matcher.group(1);
    }
    return result;
  }
 public static int[] decodePrimerName(String pname) {
   Matcher m = primerNamePattern.matcher(pname);
   if (!m.matches()) {
     throw new IllegalArgumentException(pname);
   }
   int[] a = new int[3];
   a[0] = Integer.parseInt(m.group(1));
   a[1] = Integer.parseInt(m.group(2));
   a[2] = Integer.parseInt(m.group(3));
   return a;
 }
Beispiel #28
0
  String filter(String[] args, boolean include) {
    verifyCommand(args, String.format(_filterHelp, args[0]), null, 3, 3);

    Collection<String> list = new ArrayList<String>(Processor.split(args[1]));
    Pattern pattern = Pattern.compile(args[2]);

    for (Iterator<String> i = list.iterator(); i.hasNext(); ) {
      if (pattern.matcher(i.next()).matches() == include) i.remove();
    }
    return Processor.join(list);
  }
  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[] list(String regex) {
   Pattern pattern = Pattern.compile(regex);
   ArrayList<String> slist = new ArrayList<String>();
   int count = 0;
   for (String s : dirList) {
     if (pattern.matcher(s).matches()) {
       count++;
       slist.add(s);
     }
   }
   return slist.toArray(new String[count]);
 }