Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4b36170d0e8db804fd22c05602e859804fd4dfb3 (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
195
196
197
198
199
200
201
202
203
204
205
/*
 * Created on Apr 14, 2011
 *
 * PLACE_YOUR_DISTRIBUTION_STATEMENT_RIGHT_HERE
 */
package org.eclipse.osee.ote.ui.message.watch;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.osee.framework.jdk.core.type.HashCollection;
import org.eclipse.osee.framework.jdk.core.util.Lib;

/**
 * @author Michael P. Masterson
 */
public class SignalStripper {

   /**
    * 
    * @param args Requires one argument
    * @throws IOException
    */
   public static void main(String[] args) throws IOException {
      if( args.length != 1 )
      {
         throw new IllegalArgumentException("Usage: SignalStripper <Path to folder containing java scripts>");
      }
      File folderToStartAt = new File(args[0]);
      new SignalStripper().buildMwiForEachScript(folderToStartAt);
   }

   private void buildMwiForEachScript(File folderToStartAt) throws FileNotFoundException {
      File[] scriptFiles = findFiles(folderToStartAt);
      System.out.println("Updating " + scriptFiles.length + " projects...");
      
      for( File project : scriptFiles ){
         generateMwi(project);
      }
      System.out.println("Done.");

   }

   /**
    * @param folderToStartAt 
    * @return
    * @throws FileNotFoundException
    */
   private File[] findFiles(File folderToStartAt) throws FileNotFoundException {
      if( !folderToStartAt.exists() || !folderToStartAt.isDirectory() )
         throw new FileNotFoundException("Workspace root not found:" + folderToStartAt.getAbsolutePath());

      File[] scriptProjects = folderToStartAt.listFiles(new FilenameFilter() {

         @Override
         public boolean accept(File dir, String name) {
            return name.endsWith(".java");
         }
      });
      return scriptProjects;
   }

   private void generateMwi(File scriptFile) {
      try {
         System.out.println("-----------------------------------------------------------------");
         System.out.println("Looking at script " + scriptFile.getName());
         
         String fileAsString = Lib.fileToString(scriptFile);
         String mwiAsString = generateStringToWrite(fileAsString);
         if( mwiAsString != null )
            writeMwi(scriptFile, mwiAsString);
         else {
            System.err.println("No messages found for " + scriptFile);
            System.err.flush();
         }
         
      }
      catch (IOException ex) {
         System.err.println("Problem writing mwi files.");
         ex.printStackTrace();
      }
   }

   /**
    * 
    * @param fileAsString
    * @return String to use when writing an mwi file or null if something went wrong
    * @throws IOException
    */
   public String generateStringToWrite(String fileAsString) {
      HashCollection<String, String> fullyQualifiedMessageNameToElementListMap = getMessageClassToElementsNamesMap(fileAsString);
      String mwiAsString = generateMwiAsString(fullyQualifiedMessageNameToElementListMap);
      return mwiAsString;
   }
 
   private String generateMwiAsString(HashCollection<String, String> fullyQualifiedMessageNameToElementListMap) {
      StringBuilder builder = new StringBuilder();
      for( String className : fullyQualifiedMessageNameToElementListMap.keySet())
      {
         Collection<String> elements = fullyQualifiedMessageNameToElementListMap.getValues(className);
         for(String element : elements)
         {
            builder.append(className).append("+").append(element).append("\n");
         }
         
      }
      
      if( builder.length() == 0)
         return null;
      else 
         return "version=2.0\n" + builder.toString();
   }

   private void writeMwi(File scriptFile, String mwiAsString) throws IOException {
      String absolutePath = scriptFile.getAbsolutePath();
      String fileNameWithoutExtension = absolutePath.substring(0, absolutePath.length()-5);
      
      File outputFile = new File(fileNameWithoutExtension + ".mwi");
      
      BufferedWriter outputStream = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8"));
      System.out.println("Writing " + outputFile.getName());
      outputStream.write(mwiAsString);
      outputStream.flush();
      outputStream.close();
      
   }

   private HashCollection<String, String> getMessageClassToElementsNamesMap(String fileAsString) {
      List<String> importedMessages = extractMessageImports(fileAsString);
      Map<String, String> variableNameToMessageClassNameMap = findVariablesAndCreateVariableToClassMap(importedMessages,fileAsString);
      HashCollection<String, String> messageClassToElementNamesMap = findElementsUsed(variableNameToMessageClassNameMap,fileAsString);
      return messageClassToElementNamesMap;
   }

   private List<String> extractMessageImports(String fileAsString) {
      List<String> retVal = new ArrayList<String>();
      Pattern pattern = Pattern.compile("import ((\\w|\\.)+?\\.[A-Z0-9_]+);");
      Matcher matcher = pattern.matcher(fileAsString);
      while( matcher.find()){
         String fullyQualifiesMessageClass = matcher.group(1);
         if( fullyQualifiesMessageClass.contains("enum"))
            continue;
         
         retVal.add(fullyQualifiesMessageClass);
      }
      return retVal;
   }

   private Map<String, String> findVariablesAndCreateVariableToClassMap(List<String> importedMessages, String fileAsString) {
      Map<String, String> retVal = new HashMap<String, String>();
      for( String fullyQualifiedMessage : importedMessages)
      {
         
         String[] split = fullyQualifiedMessage.split("\\.");
         String className = split[split.length-1];
         String variableName = findVariableNameFor(className, fileAsString);
         
         retVal.put(variableName, fullyQualifiedMessage);
      }
      return retVal;
   }

   private String findVariableNameFor(String className, String fileAsString) {
      Pattern pattern = Pattern.compile("\\s" + className + "\\s+(\\w+)\\s*(\\=|;)");
      Matcher matcher = pattern.matcher(fileAsString);
      if( matcher.find())
      {
         return matcher.group(1);
      }
      return null;
   }

   private HashCollection<String, String> findElementsUsed(Map<String, String> variableNameToMessageClassNameMap, String fileAsString) {
      HashCollection<String, String> retVal = new  HashCollection<String, String>(false,HashSet.class); 
      Pattern pattern = Pattern.compile("\\W(\\w+)\\.([A-Z0-9_]+)\\.");
      Matcher matcher = pattern.matcher(fileAsString);
      while( matcher.find()) {
         String variable = matcher.group(1);
         String className = variableNameToMessageClassNameMap.get(variable);
         String elementName = matcher.group(2);
         
         if( className != null) // it's possible someone forgot to instantiate something
            retVal.put(className, elementName);
         else {
            retVal.size();
         }
      }
         
      return retVal;
   }

}

Back to the top