BatchFileCreator.java

00001 package edu.stanford.hci.r3.config;
00002 
00003 import java.io.File;
00004 import java.io.FileNotFoundException;
00005 import java.io.FileReader;
00006 import java.io.IOException;
00007 import java.util.HashSet;
00008 import java.util.Set;
00009 
00010 import javax.xml.stream.FactoryConfigurationError;
00011 import javax.xml.stream.XMLInputFactory;
00012 import javax.xml.stream.XMLStreamConstants;
00013 import javax.xml.stream.XMLStreamException;
00014 import javax.xml.stream.XMLStreamReader;
00015 
00016 import edu.stanford.hci.r3.util.DebugUtils;
00017 import edu.stanford.hci.r3.util.files.FileUtils;
00018 import edu.stanford.hci.r3.util.xml.TagType;
00019 
00034 public class BatchFileCreator {
00035 
00036         public enum Attributes {
00037                 EXCLUDING, KIND, PATH
00038         }
00039 
00040         private class ClassPathEntry {
00041 
00042                 public boolean excluding = false;
00043 
00044                 public String kind;
00045 
00046                 public String path;
00047 
00048                 public String toString() {
00049                         return kind + "\texclude?:" + excluding + "\t" + path;
00050                 }
00051         }
00052 
00053         public enum Nodes {
00054                 ATTRIBUTE, ATTRIBUTES, CLASSPATH, CLASSPATHENTRY
00055         }
00056 
00060         private static final String DELIM = ";";
00061 
00065         public static void main(String[] args) {
00066                 System.out.println("Creating Batch File...");
00067 
00068                 final File currentWorkingDir = new File(".");
00069                 final File classpathFile = new File(".classpath");
00070 
00071                 System.out.println(currentWorkingDir.getAbsolutePath());
00072                 System.out.println(classpathFile.getAbsolutePath());
00073 
00074                 final BatchFileCreator creator = new BatchFileCreator();
00075                 try {
00076                         final String classPathString = creator.parseFile(classpathFile, "", new HashSet<String>());
00077 
00078                         File batchFile = new File("PenServer.bat");
00079                         FileUtils.writeStringToFile("java -classpath " + classPathString
00080                                         + " edu.stanford.hci.r3.pen.streaming.PenServerTrayApp \n pause", batchFile);
00081 
00082                         File batchFile2 = new File("ActionReceiver.bat");
00083                         FileUtils.writeStringToFile("java -classpath " + classPathString
00084                                         + " edu.stanford.hci.r3.actions.remote.ActionReceiverTrayApp \n pause", batchFile2);
00085 
00086                         System.out.println(classPathString);
00087                 } catch (FileNotFoundException e) {
00088                         e.printStackTrace();
00089                 } catch (XMLStreamException e) {
00090                         e.printStackTrace();
00091                 }
00092         }
00093 
00094         private StringBuffer classpathBuffer = new StringBuffer();
00095 
00096         private ClassPathEntry currentNode;
00097 
00098         private String prefix;
00099 
00103         private String recentText = "";
00104 
00105         private Set<String> referencePaths;
00106 
00119         private String parseFile(File f, String pathPrefix, Set<String> srcP) throws FileNotFoundException,
00120                         XMLStreamException {
00121                 referencePaths = srcP;
00122                 prefix = pathPrefix;
00123 
00124                 // Create an input factory
00125                 final XMLInputFactory xmlif = XMLInputFactory.newInstance();
00126                 // Create an XML stream reader
00127                 final XMLStreamReader xmlr = xmlif.createXMLStreamReader(new FileReader(f));
00128                 // Loop over XML input stream and process events
00129                 while (xmlr.hasNext()) {
00130                         processEvent(xmlr);
00131                         xmlr.next();
00132                 }
00133 
00134                 return classpathBuffer.toString();
00135         }
00136 
00141         private void processAttribute(XMLStreamReader xmlr, int index) {
00142 
00143                 // String prefix = xmlr.getAttributePrefix(index);
00144                 // String namespace = xmlr.getAttributeNamespace(index);
00145                 final String localName = xmlr.getAttributeName(index).toString();
00146                 try {
00147                         final Attributes type = Attributes.valueOf(localName.toUpperCase());
00148                         final String value = xmlr.getAttributeValue(index);
00149                         switch (type) {
00150                         case PATH:
00151                                 currentNode.path = value;
00152                                 break;
00153                         case EXCLUDING:
00154                                 currentNode.excluding = true;
00155                                 break;
00156                         case KIND:
00157                                 currentNode.kind = value;
00158                                 break;
00159                         }
00160                 } catch (IllegalArgumentException iae) {
00161                         // System.out.println("Not Handling Attribute: " + localName);
00162                 }
00163         }
00164 
00168         private void processAttributes(XMLStreamReader xmlr) {
00169                 for (int i = 0; i < xmlr.getAttributeCount(); i++) {
00170                         processAttribute(xmlr, i);
00171                 }
00172         }
00173 
00177         private void processEvent(XMLStreamReader xmlr) {
00178                 int start = 0;
00179                 int length = 0;
00180                 String text = "";
00181 
00182                 switch (xmlr.getEventType()) {
00183                 case XMLStreamConstants.START_ELEMENT:
00184                         recentText = "";
00185                         processTag(xmlr, TagType.BEGIN_TAG);
00186                         processAttributes(xmlr);
00187                         break;
00188                 case XMLStreamConstants.END_ELEMENT:
00189                         processTag(xmlr, TagType.END_TAG);
00190                         recentText = "";
00191                         break;
00192                 case XMLStreamConstants.CHARACTERS:
00193                         start = xmlr.getTextStart();
00194                         length = xmlr.getTextLength();
00195                         text = new String(xmlr.getTextCharacters(), start, length);
00196                         recentText += text;
00197                         break;
00198                 case XMLStreamConstants.SPACE:
00199                         start = xmlr.getTextStart();
00200                         length = xmlr.getTextLength();
00201                         text = new String(xmlr.getTextCharacters(), start, length);
00202                         break;
00203 
00204                 // case XMLStreamConstants.COMMENT:
00205                 // case XMLStreamConstants.PROCESSING_INSTRUCTION:
00206                 // if (xmlr.hasText()) {
00207                 // String piOrComment = xmlr.getText();
00208                 // }
00209                 // break;
00210 
00211                 }
00212         }
00213 
00218         private void processTag(XMLStreamReader xmlr, TagType beginOrEnd) {
00219                 if (xmlr.hasName()) {
00220                         final String localName = xmlr.getLocalName();
00221                         DebugUtils.println(localName);
00222                         final Nodes type = Nodes.valueOf(localName.toUpperCase());
00223                         if (beginOrEnd == TagType.BEGIN_TAG) {
00224                                 switch (type) {
00225                                 case CLASSPATH:
00226                                         // nothing
00227                                         break;
00228                                 case CLASSPATHENTRY:
00229                                         // create a new classpathentry object to hold the information
00230                                         currentNode = new ClassPathEntry();
00231                                         break;
00232                                 }
00233                         } else { // END
00234                                 final String currentKind = currentNode.kind;
00235                                 final String currentPath = currentNode.path;
00236                                 switch (type) {
00237                                 case CLASSPATH:
00238                                         // nothing
00239                                         break;
00240                                 case CLASSPATHENTRY:
00241                                         // only if eclipse considers this path "included"
00242                                         if (!currentNode.excluding && !currentKind.equals("con") && !currentKind.equals("var")) {
00243                                                 // if it's a src/ type, we gotta recursively compute the classpath
00244                                                 if (currentKind.equals("src")) {
00245                                                         if (currentPath.equals("src")) {
00246                                                                 // don't do anything
00247                                                                 // e.g., path="src"
00248                                                         } else if (currentPath.startsWith("/")) {
00249                                                                 // references other projects...
00250 
00251                                                                 // check if this src path has been used before
00252                                                                 // if so, skip it!
00253                                                                 if (referencePaths.contains(currentPath)) {
00254                                                                         // System.out.println("Has it already!");
00255                                                                 } else {
00256                                                                         System.out.println(currentNode);
00257 
00258                                                                         referencePaths.add(currentPath);
00259                                                                         File cpFile = null;
00260                                                                         try {
00261                                                                                 cpFile = new File(new File(new File(".."), currentPath), ".classpath")
00262                                                                                                 .getCanonicalFile();
00263                                                                         } catch (IOException e1) {
00264                                                                                 e1.printStackTrace();
00265                                                                         }
00266                                                                         System.out.println(cpFile.getAbsolutePath());
00267 
00268                                                                         String cpString = "";
00269                                                                         try {
00270                                                                                 cpString = new BatchFileCreator().parseFile(cpFile, ".."
00271                                                                                                 + currentPath + "/", referencePaths);
00272                                                                         } catch (FileNotFoundException e) {
00273                                                                                 e.printStackTrace();
00274                                                                         } catch (XMLStreamException e) {
00275                                                                                 e.printStackTrace();
00276                                                                         }
00277                                                                         // from the recursive call
00278                                                                         classpathBuffer.append(cpString);
00279                                                                 }
00280                                                         } else {
00281                                                                 DebugUtils.println("Unknown: " + currentPath);
00282                                                         }
00283                                                 } else if (currentKind.equals("lib") && currentPath.startsWith("/")) {
00284                                                         // e.g., /HCILib/lib/mlibwrapper_jai.jar
00285                                                         // make sure it's not in the already included paths...
00286 
00287                                                         if (referencePaths.contains(currentPath)) {
00288                                                                 // has it already!
00289                                                         } else {
00290                                                                 classpathBuffer.append(prefix + ".." + currentPath + DELIM);
00291                                                                 System.out.println(currentNode);
00292                                                                 referencePaths.add(currentPath);
00293                                                         }
00294                                                 } else {
00295                                                         System.out.println(currentNode);
00296                                                         // otherwise, add it to the current classpath string
00297                                                         classpathBuffer.append(prefix + currentPath + DELIM);
00298                                                 }
00299                                         }
00300                                         break;
00301                                 }
00302                         }
00303                 }
00304         }
00305 }

Generated on Sat Apr 14 18:21:33 2007 for R3 Paper Toolkit by  doxygen 1.4.7