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