KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > projectimport > jbuilder > parsing > JpxBuilder


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.projectimport.jbuilder.parsing;
21
22 import java.io.BufferedInputStream JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Set JavaDoc;
32 import java.util.StringTokenizer JavaDoc;
33 import java.util.logging.Logger JavaDoc;
34 import org.netbeans.modules.projectimport.j2seimport.AbstractProject;
35 import org.netbeans.modules.projectimport.j2seimport.LoggerFactory;
36 import org.netbeans.modules.projectimport.j2seimport.ProjectModel;
37 import org.openide.ErrorManager;
38 import org.openide.filesystems.FileObject;
39 import org.openide.filesystems.FileUtil;
40 import org.openide.util.NbBundle;
41 import org.openide.xml.XMLUtil;
42 import org.w3c.dom.Document JavaDoc;
43 import org.w3c.dom.Element JavaDoc;
44 import org.w3c.dom.Node JavaDoc;
45 import org.w3c.dom.NodeList JavaDoc;
46 import org.xml.sax.InputSource JavaDoc;
47 import org.xml.sax.SAXException JavaDoc;
48
49 /**
50  *
51  * @author Radek Matous
52  */

53 final class JpxBuilder extends ProjectBuilder {
54     private static final String JavaDoc ROOT_ELEMENT = "project";//NOI18N
55
private static final String JavaDoc SYS_CATEGORY = "sys";//NOI18N
56

57     public static final String JavaDoc SOURCE_PATH = "SourcePath";//NOI18N
58
public static final String JavaDoc TEST_PATH = "TestPath";//NOI18N
59
public static final String JavaDoc LIBRARIES = "Libraries";//NOI18N
60
public static final String JavaDoc JDK = "JDK";//NOI18N
61

62     private static final String JavaDoc ELEMENT = "property";//NOI18N
63

64     private static final String JavaDoc CATEGORY_ATTR = "category";//NOI18N
65
private static final String JavaDoc NAME_ATTR = "name";//NOI18N
66
private static final String JavaDoc VALUE_ATTR = "value";//NOI18N
67

68     private static final Logger JavaDoc logger =
69             LoggerFactory.getDefault().createLogger(JpxBuilder.class);
70     
71     private String JavaDoc extension;
72     
73     /** Creates a new instance of JpxResourceType */
74     public JpxBuilder(final String JavaDoc extension) {
75         this.extension = extension;
76     }
77     
78     protected String JavaDoc getSupportedExtension() {
79         return extension;
80     }
81     
82     protected final Collection JavaDoc/*<ProjectModel>*/ buildImpl(final File JavaDoc file) {
83         Collection JavaDoc retval = new HashSet JavaDoc();
84         try {
85             retval.add(parseAndBuild(file));
86         } catch (IOException JavaDoc iex) {
87             ErrorManager.getDefault().notify(iex);
88         } catch (SAXException JavaDoc sax) {
89             ErrorManager.getDefault().notify(sax);
90         }
91         
92         assert retval.size() > 0;
93         return retval;
94     }
95     
96     private ProjectModel parseAndBuild(final File JavaDoc jpxFile) throws IOException JavaDoc, SAXException JavaDoc {
97         String JavaDoc sourcePath = "";//NOI18N
98
String JavaDoc testPath = "";//NOI18N
99
String JavaDoc libraries = "";//NOI18N
100
String JavaDoc jdk = "";//NOI18N
101

102         InputStream JavaDoc jprIs = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(jpxFile));
103         try {
104             Document JavaDoc doc = XMLUtil.parse(new InputSource JavaDoc(jprIs),
105                     false, false, null, null);
106             Element JavaDoc docEl = getRootElement(doc);
107             
108             
109             NodeList JavaDoc nList = docEl.getElementsByTagName(ELEMENT);//NOI18N
110
for (int i = 0; i < nList.getLength(); i++) {
111                 String JavaDoc category = null;
112                 String JavaDoc name = null;
113                 String JavaDoc value = null;
114                 
115                 Node JavaDoc node = nList.item(i);
116                 if (node.getNodeType() == Node.ELEMENT_NODE) {
117                     Element JavaDoc property = (Element JavaDoc)node;
118                     category = property.getAttribute(CATEGORY_ATTR);
119                     name = property.getAttribute(NAME_ATTR);
120                     value = property.getAttribute(VALUE_ATTR);
121                     
122                     if (category != null && name != null && value != null) {
123                         if (SOURCE_PATH.equals(name)) {
124                             sourcePath = value;
125                         } if (TEST_PATH.equals(name)) {
126                             testPath = value;
127                         } else if (LIBRARIES.equals(name)) {
128                             libraries = value;
129                         } else if (JDK.equals(name)) {
130                             jdk = value;
131                         }
132                     }
133                 }
134             }
135         } finally {
136             if (jprIs != null) {
137                 jprIs.close();
138             }
139         }
140         
141         Enumeration JavaDoc sourcePathEn = new StringTokenizer JavaDoc(sourcePath,";");//NOI18N
142
Enumeration JavaDoc testPathEn = new StringTokenizer JavaDoc(testPath,";");//NOI18N
143
Enumeration JavaDoc librariesEn = new StringTokenizer JavaDoc(libraries,";");//NOI18N
144

145         return createProjectModel(jpxFile, sourcePathEn,testPathEn, librariesEn, jdk);
146     }
147     
148     private ProjectModel createProjectModel(final File JavaDoc jpxFile, Enumeration JavaDoc sourcePathEn,
149             Enumeration JavaDoc testPathEn, Enumeration JavaDoc libraryEn, String JavaDoc jdkId) {
150         File JavaDoc projectDir = jpxFile.getParentFile();
151         assert projectDir.exists();
152         assert projectDir.isDirectory();
153         
154         
155         FileObject prjDirFo = FileUtil.toFileObject(projectDir);
156         assert prjDirFo != null;
157         
158         AbstractProject project = new AbstractProject(jpxFile.getParentFile().getName(), prjDirFo);
159         
160         Set JavaDoc allTestPaths = new HashSet JavaDoc();
161         while (testPathEn.hasMoreElements()) {
162             String JavaDoc testPath = (String JavaDoc)testPathEn.nextElement();
163             File JavaDoc testFile = FileUtil.normalizeFile(new File JavaDoc(projectDir, testPath));
164             AbstractProject.SourceRoot asr;
165             asr = new AbstractProject.SourceRoot(testFile.getName(), testFile);
166             allTestPaths.add(asr);
167         }
168         
169         while (sourcePathEn.hasMoreElements()) {
170             String JavaDoc sourcePath = (String JavaDoc)sourcePathEn.nextElement();
171             File JavaDoc sourceFile = FileUtil.normalizeFile(new File JavaDoc(projectDir, sourcePath));
172             AbstractProject.SourceRoot asr;
173             asr = new AbstractProject.SourceRoot(sourceFile.getName(), sourceFile);
174             project.addSourceRoot(asr);
175         }
176         
177         while (libraryEn.hasMoreElements()) {
178             String JavaDoc libraryElement = (String JavaDoc)libraryEn.nextElement();
179             File JavaDoc prjOrArchiv = FileUtil.normalizeFile(new File JavaDoc(projectDir, libraryElement));
180             FileObject fo = FileUtil.toFileObject(prjOrArchiv);
181             
182             boolean isProject = ProjectBuilder.isProjectFile(prjOrArchiv) && fo != null;
183             boolean isArchiv = (isProject) ? false : (fo != null && FileUtil.isArchiveFile(fo));
184             boolean isUserLib = (isProject || isArchiv) ? false : !prjOrArchiv.exists();
185             isUserLib = (isUserLib) ? (libraryElement.indexOf('/') == -1 ||
186                     libraryElement.indexOf('\\') == -1) : false;
187             
188             
189             if (isProject) {
190                 ProjectBuilder ftype = ProjectBuilder.getProvider(prjOrArchiv);
191                 
192                 if (ftype instanceof JpxBuilder) {
193                     Collection JavaDoc subPrjs = ftype.buildProjectModels(prjOrArchiv);
194                     for (Iterator JavaDoc it = subPrjs.iterator(); it.hasNext(); ) {
195                         AbstractProject subPrjDef = (AbstractProject)it.next();
196                         if (subPrjDef != null) {
197                             project.addDependency(subPrjDef);
198                         }
199                     }
200                 }
201                 
202             } else if (isArchiv) {
203                 
204                 prjOrArchiv = FileUtil.normalizeFile(prjOrArchiv);
205                 project.addLibrary(new AbstractProject.Library(prjOrArchiv));
206                 
207             } else if (isUserLib) {
208                 
209                 final String JavaDoc libraryName = libraryElement;
210                 AbstractProject.UserLibrary aulib = UserLibrarySupport.getInstance(libraryName,projectDir);
211                 
212                 aulib = (aulib != null) ? aulib : new AbstractProject.UserLibrary(libraryName, true);
213                 project.addUserLibrary(aulib);
214             }
215         }
216         
217         if (jdkId != null) {
218             project.setJdkId(jdkId);
219             File JavaDoc jdkFolder = JdkSupport.getJKDDirectory(jdkId, projectDir);
220             if (jdkFolder != null) {
221                 project.setJDKDirectory(jdkFolder);
222             } else {
223                 project.setInvalidJDK(jdkId);
224             }
225             
226         } else {
227             logger.finest("no JDK found");//NOI18N
228
}
229         
230         
231         
232         return project;
233     }
234     
235     private Element JavaDoc getRootElement(Document JavaDoc doc) throws IOException JavaDoc {
236         Element JavaDoc docEl = doc.getDocumentElement();
237         
238         if (!docEl.getTagName().equals(ROOT_ELEMENT)) { // NOI18N
239
String JavaDoc message = NbBundle.getMessage(UserLibrarySupport.class,"ERR_WrongRootElement",docEl.getTagName());// NOI18N
240
throw new IOException JavaDoc(message);
241         }
242         
243         return docEl;
244     }
245 }
246
Popular Tags