Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1b6bcbdf6e5489c122ecb7250e6d90a858e422ea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.jdk.core.text;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import javax.xml.parsers.ParserConfigurationException;
import org.eclipse.osee.framework.jdk.core.text.change.ChangeSet;
import org.eclipse.osee.framework.jdk.core.util.Lib;

/**
 * @author Ryan D. Brooks
 */
public abstract class Rule {
   private String outExtension;
   private String currentOutfileName;
   private File inputFile;
   private String subdirectoryName;
   private Pattern fileNamePattern;
   protected boolean ruleWasApplicable;
   protected Logger logger;

   public Rule() {
      this("done");
   }

   public Rule(String outExtension) {
      this.outExtension = outExtension;
      this.ruleWasApplicable = false;
      logger = Logger.getLogger(this.getClass().getName());
      logger.setLevel(Level.ALL);
      this.subdirectoryName = null;
   }

   public abstract ChangeSet computeChanges(CharSequence seq);

   public void process(Collection<File> list) {
      for (File file : list) {
         try {
            process(file);
         } catch (Exception ex) {
            System.out.println(currentOutfileName + ": " + ex.getMessage());
         }
      }
   }

   public void process(File file) throws IOException {
      if (file.isDirectory()) {
         List<File> files = Lib.recursivelyListFiles(file, fileNamePattern);
         for (File aFile : files) {
            try {
               process(aFile);
            } catch (Exception ex) {
               System.out.println(currentOutfileName + ": " + ex.getMessage());
            }
         }
      } else {
         inputFile = file;
         process(file, getResultFile(file));
      }
   }

   public void process(File inFile, File outFile) throws IOException {
      File subdirectory;
      if (subdirectoryName != null) {
         File parent = outFile.getParentFile();
         subdirectory = new File(parent, subdirectoryName);
         subdirectory.mkdir();
         outFile = new File(subdirectory, outFile.getName());
      }

      this.currentOutfileName = outFile.getName();
      if (inFile.exists()) {
         RulesLogHandler handler = null;
         ChangeSet changeSet = null;
         try {
            handler = new RulesLogHandler(new File(Lib.changeExtension(outFile.getPath(), "xml")));
            logger.addHandler(handler);
            ruleWasApplicable = false;
            changeSet = computeChanges(Lib.fileToCharBuffer(inFile));
         } catch (ParserConfigurationException ex) {
            logger.log(Level.SEVERE, ex.toString(), ex);
         } finally {
            if (handler != null) {
               handler.close();
               logger.removeHandler(handler);
            }
         }
         if (ruleWasApplicable) {
            if (subdirectoryName == null) {
               System.out.println("Rule was applied to " + currentOutfileName);
            } else
               System.out.println("Rule was applied to " + subdirectoryName + currentOutfileName);

            if (changeSet != null) changeSet.applyChanges(outFile);
         }
         // else {
         // System.out.println("Not applicable to " + currentFileName);
         // }

      } else {
         System.out.println("The file " + inFile + " does not exist!");
      }
   }

   protected File getResultFile(File file) {

      if (outExtension == null) {
         return file;
      }

      return new File(Lib.removeExtension(file.getPath()) + "." + outExtension);
   }

   public static void main(String[] args) throws Exception {
      if (args.length < 3) {
         System.out.println("Usage: " + Rule.class.getName() + " <ruleClassPath> <ruleClass> <file list>");
         return;
      }

      String ruleName = args[1];
      String classPath = args[0];
      try {
         URLClassLoader classLoader = new URLClassLoader(new URL[] {Lib.getUrlFromString(classPath)});
         System.out.println("class path: " + classLoader.getURLs()[0]);
         Object obj = classLoader.loadClass(ruleName).newInstance();

         if (obj instanceof org.eclipse.osee.framework.jdk.core.text.Rule) {
            Rule rule = (Rule) obj;
            for (int i = 2; i < args.length; i++) {
               try {
                  rule.process(new File(args[i]));
               } catch (Exception ex) {
                  System.out.println("Exception in Rule!!! " + rule.currentOutfileName + ": " + ex.getMessage());
                  ex.printStackTrace();
               }
            }
         } else {
            throw new IllegalArgumentException(ruleName + " is not of type text.Rule.");
         }
      } catch (InstantiationException ex) {
         System.out.println(ex);
      } catch (IllegalAccessException ex) {
         System.out.println(ex);
      } catch (ClassNotFoundException ex) {
         System.out.println(ex);
      }
   }

   public boolean ruleWasApplicable() {
      return ruleWasApplicable;
   }

   /**
    * @return Returns the currentFileName.
    */
   public String getCurrentOutfileName() {
      return currentOutfileName;
   }

   /**
    * @param ruleWasApplicable The ruleWasApplicable to set.
    */
   public void setRuleWasApplicable(boolean ruleWasApplicable) {
      this.ruleWasApplicable = ruleWasApplicable;
   }

   public File getInputFile() {
      return inputFile;
   }

   public void setSubdirectoryNameToPlaceResultFilesIn(String subdirectoryName) {
      this.subdirectoryName = subdirectoryName;
   }

   public void setFileNamePattern(Pattern fileNamePattern) {
      this.fileNamePattern = fileNamePattern;
   }
}

Back to the top