hengehog

Cleaner.java

Dec 23rd, 2012
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.67 KB | None | 0 0
  1. import java.io.BufferedWriter;
  2. import java.io.File;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.net.URISyntaxException;
  6. import java.util.Scanner;
  7.  
  8. public class Cleaner
  9. {
  10.     static File input = null;
  11.     static File output = null;
  12.     static File config = null;
  13.     public static void main(String[] args)  
  14.     {
  15.         //Find config file
  16.         java.net.URL configPath = ClassLoader.getSystemResource("config.txt");
  17.         if(configPath==null)
  18.         {
  19.             System.out.println("The config file was not valid try again");
  20.         }
  21.         try {
  22.                 config = new File(configPath.toURI());
  23.             } catch (URISyntaxException e1) {
  24.                 e1.printStackTrace();
  25.             }
  26.         //Find output file
  27.         java.net.URL outputPath = ClassLoader.getSystemResource("output.txt");
  28.         if(outputPath==null)
  29.         {
  30.             System.out.println("The output file was not valid try again");
  31.         }
  32.             try {
  33.                 output = new File(outputPath.toURI());
  34.             } catch (URISyntaxException e1) {
  35.                 //cannot find output file;
  36.                 e1.printStackTrace();
  37.             }
  38.         //Find input file
  39.         java.net.URL inputPath = ClassLoader.getSystemResource("input.txt");
  40.         if(inputPath==null)
  41.         {
  42.             System.out.println("The input file was not valid try again");
  43.         }
  44.         try {
  45.             input = new File(inputPath.toURI());
  46.         } catch (URISyntaxException e1) {
  47.             e1.printStackTrace();
  48.         }
  49.         //Run the scanner
  50.         try {
  51.             Cleaner(input, output, config);
  52.         } catch (IOException e) {
  53.                     //cannot find the output file
  54.         }
  55.     }
  56.    
  57.     public static void Cleaner(File input, File output, File conFig) throws IOException
  58.      {
  59.         //Create scanners and writers
  60.         Scanner inFile = new Scanner(input);
  61.         Scanner inConfigFile = new Scanner(config);
  62.         BufferedWriter out = new BufferedWriter(new FileWriter(output));
  63.         System.out.println("Processing");
  64.         String Line = null;
  65.         while(inFile.hasNextLine())
  66.         {
  67.             StringBuilder outputLine = new StringBuilder();
  68.             Line = inFile.nextLine();
  69.             //remove parentheses and brackets
  70.             Line = Line.replaceAll("\\([^\\(]*\\)", "");
  71.             Line = Line.replaceAll("\\[[^\\[]*\\]", "");
  72.                
  73.             char []LineAsChars = Line.toCharArray();
  74.             //Remove numbers from the beginning, when done stop.
  75.             for(int i = 0; i < LineAsChars.length ;i++)
  76.             {
  77.                 if(LineAsChars[i] == 45)
  78.                 {
  79.                     LineAsChars[i] = 32;
  80.                 }
  81.                 else if(DeleteChar(LineAsChars[i]))
  82.                 {
  83.                     LineAsChars[i] = 32;               
  84.                 }
  85.                 else
  86.                 {
  87.                     break;
  88.                 }
  89.                 //System.out.println(outputLine.append(LineAsChars));
  90.  
  91.             }
  92.             outputLine.append(LineAsChars);
  93.             //normalization Yes this is horrible insane regex code, if you can improve it that would be great.
  94.             String outputString = outputLine.toString().trim();
  95.             //remove leading dash and trailing \
  96.             if(outputString.length()> 0)
  97.             {
  98.                 if(outputString.substring(0,1).equals("-"))
  99.                 {
  100.                     outputString = outputString.substring(2);
  101.                 }
  102.                 if(outputString.substring(outputString.length()-1,outputString.length()).equals("\\"))
  103.                 {
  104.                     outputString = outputString.substring(0,outputString.length()-1);
  105.                 }
  106.                 if(outputString.contains("|"))
  107.                 {
  108.                     outputString = outputString.substring(0, outputString.indexOf("|") - 1);
  109.                 }
  110.                 if(!outputString.contains("-"))
  111.                 {
  112.                     outputString = " ";
  113.                 }
  114.                
  115.                
  116.             }
  117.             outputString = outputString.trim();
  118.        
  119.             //Do replacements from the config file which is setup like this: XXXX$YYYYY (you want to replace XXXX with YYYY
  120.             while(inConfigFile.hasNextLine())
  121.             {
  122.                 String configLine = inConfigFile.nextLine();
  123.                 String[] theTwo = configLine.split("\\$");
  124.                 String ReplaceA = theTwo[0];
  125.                 String ReplaceB = theTwo[1];
  126.                 //replace it!
  127.                 outputString = outputString.replaceAll(ReplaceA,ReplaceB);
  128.                
  129.             }
  130.             outputString = outputString.trim();
  131.             //Print the cleaned line
  132.             try{
  133.                
  134.                 out.write(outputString);
  135.                 out.newLine();
  136.                 //Close the output stream
  137.                         }catch (Exception e){//Catch exception if any
  138.                   System.err.println("Error: " + e.getMessage());
  139.                 }           System.out.println(outputString);
  140.         }
  141.         System.out.println("Done");
  142.         out.close();
  143.      }
  144.     //removes the number and other stuff at the beginning of lines.
  145.     public static Boolean DeleteChar (char valueIn)
  146.     {
  147.         if(valueIn >=48 && valueIn <=57)
  148.         {
  149.             return true;
  150.         }
  151.         //if valueIn== ) || valueIn == - || valueIn == .
  152.         if(valueIn== 41|| valueIn == 45|| valueIn == 46)
  153.         {
  154.             return true;
  155.         }
  156.         if(valueIn >=65 &&valueIn <=90 ||valueIn >=97 &&valueIn <=122)
  157.         {
  158.             //this line is done
  159.             return false;
  160.         }
  161.         else
  162.         {
  163.            
  164.             return false;
  165.         }
  166.     }
  167.     public static class writer {
  168.         public static void log(String message) throws IOException {
  169.            
  170.            
  171.         }
  172.     }
  173.    
  174. }
Advertisement
Add Comment
Please, Sign In to add comment