Grep.java

Go to the documentation of this file.
00001 package edu.rice.cs.hpc.data.util;
00002 
00003 import java.io.BufferedReader;
00004 import java.io.BufferedWriter;
00005 import java.io.FileInputStream;
00006 import java.io.FileOutputStream;
00007 import java.io.IOException;
00008 import java.io.InputStreamReader;
00009 import java.io.OutputStreamWriter;
00010 import java.util.regex.Matcher;
00011 import java.util.regex.Pattern;
00012 
00013 /***
00014  * 
00015  * grep class
00016  *
00017  */
00018 public class Grep 
00019 {
00020 
00029     static public void grep(String fin, String fout, String pattern, boolean shouldMatch)
00030             throws IOException
00031     {
00032         // using specific grep to remove metric tag
00033         removeMetrics(fin, fout);
00034     }
00035     
00036     
00037     /***
00038      * Simplified grep specifically against metric tags in an xml file
00039      * This method is not a generic grep, it assumes that all tags are fit in one line
00040      * 
00041      * @param fin
00042      * @param fout
00043      * @throws IOException
00044      */
00045     private static void removeMetrics(String fin, String fout) 
00046             throws IOException 
00047     {
00048         //String encoding = "UTF-8";
00049         
00050         final BufferedReader inputFile = new BufferedReader(new InputStreamReader(new FileInputStream(fin)));
00051         final BufferedWriter outputFile = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fout))); 
00052         
00053         Pattern metricTag = Pattern.compile("<M ");
00054         Matcher matcher = metricTag.matcher("");
00055         
00056         String inputLine;
00057         while ((inputLine = inputFile.readLine()) != null) {
00058             matcher.reset(inputLine);
00059             if (!matcher.lookingAt()) {
00060                 outputFile.write(inputLine);
00061                 outputFile.newLine();
00062             }
00063         }
00064         inputFile.close();
00065         outputFile.close();
00066     }
00067 
00072     static public void main(String args[])
00073     {
00074         if (args.length>1)
00075         {
00076             final String file = args[0];
00077             final String fout = args[1];
00078             try {
00079                 Grep.removeMetrics(file, fout);
00080             } catch (IOException e) {
00081                 // TODO Auto-generated catch block
00082                 e.printStackTrace();
00083             }
00084         }
00085     }
00086 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1