KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > piagetproject > layout > LayoutParser


1 /*
2  * XMLLayoutParser.java
3  *
4  * Created on April 22, 2005, 10:16 AM
5  */

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