From 55570e2e292201a88c39da1984772c234fa9f232 Mon Sep 17 00:00:00 2001 From: Andrew Finkbeiner Date: Sun, 17 Apr 2011 18:54:45 -0700 Subject: feature: Add ability to pull a signal watch list from a java file --- .../osee/ote/ui/message/watch/SignalStripper.java | 205 +++++++++++++++++++++ 1 file changed, 205 insertions(+) create mode 100644 plugins/org.eclipse.osee.ote.ui.message/src/org/eclipse/osee/ote/ui/message/watch/SignalStripper.java (limited to 'plugins/org.eclipse.osee.ote.ui.message') diff --git a/plugins/org.eclipse.osee.ote.ui.message/src/org/eclipse/osee/ote/ui/message/watch/SignalStripper.java b/plugins/org.eclipse.osee.ote.ui.message/src/org/eclipse/osee/ote/ui/message/watch/SignalStripper.java new file mode 100644 index 00000000000..7d0e3482807 --- /dev/null +++ b/plugins/org.eclipse.osee.ote.ui.message/src/org/eclipse/osee/ote/ui/message/watch/SignalStripper.java @@ -0,0 +1,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 "); + } + 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 fullyQualifiedMessageNameToElementListMap = getMessageClassToElementsNamesMap(fileAsString); + String mwiAsString = generateMwiAsString(fullyQualifiedMessageNameToElementListMap); + return mwiAsString; + } + + private String generateMwiAsString(HashCollection fullyQualifiedMessageNameToElementListMap) { + StringBuilder builder = new StringBuilder(); + for( String className : fullyQualifiedMessageNameToElementListMap.keySet()) + { + Collection 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 getMessageClassToElementsNamesMap(String fileAsString) { + List importedMessages = extractMessageImports(fileAsString); + Map variableNameToMessageClassNameMap = findVariablesAndCreateVariableToClassMap(importedMessages,fileAsString); + HashCollection messageClassToElementNamesMap = findElementsUsed(variableNameToMessageClassNameMap,fileAsString); + return messageClassToElementNamesMap; + } + + private List extractMessageImports(String fileAsString) { + List retVal = new ArrayList(); + 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 findVariablesAndCreateVariableToClassMap(List importedMessages, String fileAsString) { + Map retVal = new HashMap(); + 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 findElementsUsed(Map variableNameToMessageClassNameMap, String fileAsString) { + HashCollection retVal = new HashCollection(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; + } + +} -- cgit v1.2.3