You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lenya.apache.org by eg...@apache.org on 2003/10/02 14:33:58 UTC

cvs commit: cocoon-lenya/src/java/org/apache/lenya/search Grep.java

egli        2003/10/02 05:33:58

  Modified:    src/java/org/apache/lenya/search Grep.java
  Log:
  Added a method to find all occurences of a pattern.
  
  Revision  Changes    Path
  1.3       +46 -2     cocoon-lenya/src/java/org/apache/lenya/search/Grep.java
  
  Index: Grep.java
  ===================================================================
  RCS file: /home/cvs/cocoon-lenya/src/java/org/apache/lenya/search/Grep.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Grep.java	30 Sep 2003 09:03:28 -0000	1.2
  +++ Grep.java	2 Oct 2003 12:33:58 -0000	1.3
  @@ -107,6 +107,50 @@
       }
   
       /**
  +     * Find all occurences of pattern in a file.
  +     * 
  +     * @param file the file to search for occurences of pattern
  +     * @param pattern the pattern to search for
  +     * @param group which group in the pattern to return
  +     * 
  +     * @return an <code>array</code> of occurences of pattern 
  +     * (i.e. the groupth group of the match)
  +     * 
  +     * @throws IOException if the file could not be read.
  +     * 
  +     */
  +    public static String[] findPattern(File file, Pattern pattern, int group)
  +        throws IOException {
  +
  +        ArrayList occurences = new ArrayList();
  +
  +        // Open the file and then get a channel from the stream
  +        FileInputStream fis = new FileInputStream(file);
  +        FileChannel fc = fis.getChannel();
  +
  +        // Get the file's size and then map it into memory
  +        int sz = (int)fc.size();
  +        MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz);
  +
  +        // Decode the file into a char buffer
  +        CharBuffer cb = decoder.decode(bb);
  +
  +        // Perform the search
  +        Matcher pm = pattern.matcher(cb); // Pattern matcher
  +
  +        while (pm.find()) {
  +            occurences.add(pm.group(group));
  +        }
  +
  +        // Close the channel and the stream
  +        fc.close();
  +        fis.close();
  +
  +        return (String[])occurences.toArray(new String[occurences.size()]);
  +
  +    }
  +
  +    /**
        * Find all files below the given file which contain the given pattern.
        * 
        * @param file the file where to start the search for the pattern.
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: lenya-cvs-unsubscribe@cocoon.apache.org
For additional commands, e-mail: lenya-cvs-help@cocoon.apache.org