1 6 7 package org.netbeans.modules.piagetproject.layout; 8 9 10 import java.io.File ; 11 import javax.xml.parsers.SAXParserFactory ; 12 import javax.xml.parsers.SAXParser ; 13 14 public class LayoutParser { 15 16 17 Layout l; 18 LayoutBuilder builder; 19 20 public LayoutParser() { 21 } 22 23 public Layout parse(File f){ 24 25 SAXParserFactory factory = SAXParserFactory.newInstance(); 26 27 try{ 28 SAXParser saxParser = factory.newSAXParser(); 29 XMLParserHandler handler = new XMLParserHandler(); 30 saxParser.parse(f, handler); 31 l = handler.getResultLayout(); 32 if(l==null)return null; 33 String dirpath = getDirName(f.getAbsolutePath()); 35 if(!l.name.equalsIgnoreCase("editor")){ 36 l.wstcref = exploreCorrespondingDirectory(dirpath); 37 } 38 return l; 39 }catch(Exception e){ 40 e.printStackTrace(System.out); 41 return null; 42 } 43 44 } 45 46 private String exploreCorrespondingDirectory(String dirpath){ 47 try{ 48 File [] wstcrefFiles = LayoutBuilder.filesEndingWith("wstcref", dirpath); 49 if(wstcrefFiles==null) return null; 50 StringBuffer buffer = new StringBuffer (); 51 String woutExtension; 52 String [] fileNames = new String [wstcrefFiles.length]; 53 for(int i = 0; i<wstcrefFiles.length; i++){ 54 woutExtension = wstcrefFiles[i].getName().substring(0, wstcrefFiles[i].getName().lastIndexOf('.')); 55 fileNames[i] = new String (woutExtension); 56 buffer.append("\n+ "+woutExtension); 57 } 58 LayoutBuilder.analyzeMode(l.name, fileNames); 59 return buffer.toString(); 60 }catch(Exception e){ 61 e.printStackTrace(System.out); 62 return null; 63 } 64 } 65 66 private String getDirName(String filePath){ 67 final String SEP = System.getProperty("file.separator"); 68 String currentDir = new String (filePath); 69 int index = currentDir.lastIndexOf(SEP); 70 if(index>0) return currentDir.substring(0, index) + SEP + l.name; 71 else return l.name; 72 } 73 } | Popular Tags |