1 19 20 package org.netbeans.modules.projectimport.jbuilder.parsing; 21 22 import java.io.BufferedInputStream ; 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.util.Collection ; 28 import java.util.Enumeration ; 29 import java.util.HashSet ; 30 import java.util.Iterator ; 31 import java.util.Set ; 32 import java.util.StringTokenizer ; 33 import java.util.logging.Logger ; 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 ; 43 import org.w3c.dom.Element ; 44 import org.w3c.dom.Node ; 45 import org.w3c.dom.NodeList ; 46 import org.xml.sax.InputSource ; 47 import org.xml.sax.SAXException ; 48 49 53 final class JpxBuilder extends ProjectBuilder { 54 private static final String ROOT_ELEMENT = "project"; private static final String SYS_CATEGORY = "sys"; 57 public static final String SOURCE_PATH = "SourcePath"; public static final String TEST_PATH = "TestPath"; public static final String LIBRARIES = "Libraries"; public static final String JDK = "JDK"; 62 private static final String ELEMENT = "property"; 64 private static final String CATEGORY_ATTR = "category"; private static final String NAME_ATTR = "name"; private static final String VALUE_ATTR = "value"; 68 private static final Logger logger = 69 LoggerFactory.getDefault().createLogger(JpxBuilder.class); 70 71 private String extension; 72 73 74 public JpxBuilder(final String extension) { 75 this.extension = extension; 76 } 77 78 protected String getSupportedExtension() { 79 return extension; 80 } 81 82 protected final Collection buildImpl(final File file) { 83 Collection retval = new HashSet (); 84 try { 85 retval.add(parseAndBuild(file)); 86 } catch (IOException iex) { 87 ErrorManager.getDefault().notify(iex); 88 } catch (SAXException sax) { 89 ErrorManager.getDefault().notify(sax); 90 } 91 92 assert retval.size() > 0; 93 return retval; 94 } 95 96 private ProjectModel parseAndBuild(final File jpxFile) throws IOException , SAXException { 97 String sourcePath = ""; String testPath = ""; String libraries = ""; String jdk = ""; 102 InputStream jprIs = new BufferedInputStream (new FileInputStream (jpxFile)); 103 try { 104 Document doc = XMLUtil.parse(new InputSource (jprIs), 105 false, false, null, null); 106 Element docEl = getRootElement(doc); 107 108 109 NodeList nList = docEl.getElementsByTagName(ELEMENT); for (int i = 0; i < nList.getLength(); i++) { 111 String category = null; 112 String name = null; 113 String value = null; 114 115 Node node = nList.item(i); 116 if (node.getNodeType() == Node.ELEMENT_NODE) { 117 Element property = (Element )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 sourcePathEn = new StringTokenizer (sourcePath,";"); Enumeration testPathEn = new StringTokenizer (testPath,";"); Enumeration librariesEn = new StringTokenizer (libraries,";"); 145 return createProjectModel(jpxFile, sourcePathEn,testPathEn, librariesEn, jdk); 146 } 147 148 private ProjectModel createProjectModel(final File jpxFile, Enumeration sourcePathEn, 149 Enumeration testPathEn, Enumeration libraryEn, String jdkId) { 150 File 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 allTestPaths = new HashSet (); 161 while (testPathEn.hasMoreElements()) { 162 String testPath = (String )testPathEn.nextElement(); 163 File testFile = FileUtil.normalizeFile(new File (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 sourcePath = (String )sourcePathEn.nextElement(); 171 File sourceFile = FileUtil.normalizeFile(new File (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 libraryElement = (String )libraryEn.nextElement(); 179 File prjOrArchiv = FileUtil.normalizeFile(new File (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 subPrjs = ftype.buildProjectModels(prjOrArchiv); 194 for (Iterator 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 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 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"); } 229 230 231 232 return project; 233 } 234 235 private Element getRootElement(Document doc) throws IOException { 236 Element docEl = doc.getDocumentElement(); 237 238 if (!docEl.getTagName().equals(ROOT_ELEMENT)) { String message = NbBundle.getMessage(UserLibrarySupport.class,"ERR_WrongRootElement",docEl.getTagName()); throw new IOException (message); 241 } 242 243 return docEl; 244 } 245 } 246 | Popular Tags |