001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.projectimport.jbuilder.parsing;
043:
044: import java.io.BufferedInputStream;
045: import java.io.File;
046: import java.io.FileInputStream;
047: import java.io.IOException;
048: import java.io.InputStream;
049: import java.util.Collection;
050: import java.util.Enumeration;
051: import java.util.HashSet;
052: import java.util.Iterator;
053: import java.util.Set;
054: import java.util.StringTokenizer;
055: import java.util.logging.Logger;
056: import org.netbeans.modules.projectimport.j2seimport.AbstractProject;
057: import org.netbeans.modules.projectimport.j2seimport.LoggerFactory;
058: import org.netbeans.modules.projectimport.j2seimport.ProjectModel;
059: import org.openide.ErrorManager;
060: import org.openide.filesystems.FileObject;
061: import org.openide.filesystems.FileUtil;
062: import org.openide.util.NbBundle;
063: import org.openide.xml.XMLUtil;
064: import org.w3c.dom.Document;
065: import org.w3c.dom.Element;
066: import org.w3c.dom.Node;
067: import org.w3c.dom.NodeList;
068: import org.xml.sax.InputSource;
069: import org.xml.sax.SAXException;
070:
071: /**
072: *
073: * @author Radek Matous
074: */
075: final class JpxBuilder extends ProjectBuilder {
076: private static final String ROOT_ELEMENT = "project";//NOI18N
077: private static final String SYS_CATEGORY = "sys";//NOI18N
078:
079: public static final String SOURCE_PATH = "SourcePath";//NOI18N
080: public static final String TEST_PATH = "TestPath";//NOI18N
081: public static final String LIBRARIES = "Libraries";//NOI18N
082: public static final String JDK = "JDK";//NOI18N
083:
084: private static final String ELEMENT = "property";//NOI18N
085:
086: private static final String CATEGORY_ATTR = "category";//NOI18N
087: private static final String NAME_ATTR = "name";//NOI18N
088: private static final String VALUE_ATTR = "value";//NOI18N
089:
090: private static final Logger logger = LoggerFactory.getDefault()
091: .createLogger(JpxBuilder.class);
092:
093: private String extension;
094:
095: /** Creates a new instance of JpxResourceType */
096: public JpxBuilder(final String extension) {
097: this .extension = extension;
098: }
099:
100: protected String getSupportedExtension() {
101: return extension;
102: }
103:
104: protected final Collection/*<ProjectModel>*/buildImpl(
105: final File file) {
106: Collection retval = new HashSet();
107: try {
108: retval.add(parseAndBuild(file));
109: } catch (IOException iex) {
110: ErrorManager.getDefault().notify(iex);
111: } catch (SAXException sax) {
112: ErrorManager.getDefault().notify(sax);
113: }
114:
115: assert retval.size() > 0;
116: return retval;
117: }
118:
119: private ProjectModel parseAndBuild(final File jpxFile)
120: throws IOException, SAXException {
121: String sourcePath = "";//NOI18N
122: String testPath = "";//NOI18N
123: String libraries = "";//NOI18N
124: String jdk = "";//NOI18N
125:
126: InputStream jprIs = new BufferedInputStream(
127: new FileInputStream(jpxFile));
128: try {
129: Document doc = XMLUtil.parse(new InputSource(jprIs), false,
130: false, null, null);
131: Element docEl = getRootElement(doc);
132:
133: NodeList nList = docEl.getElementsByTagName(ELEMENT);//NOI18N
134: for (int i = 0; i < nList.getLength(); i++) {
135: String category = null;
136: String name = null;
137: String value = null;
138:
139: Node node = nList.item(i);
140: if (node.getNodeType() == Node.ELEMENT_NODE) {
141: Element property = (Element) node;
142: category = property.getAttribute(CATEGORY_ATTR);
143: name = property.getAttribute(NAME_ATTR);
144: value = property.getAttribute(VALUE_ATTR);
145:
146: if (category != null && name != null
147: && value != null) {
148: if (SOURCE_PATH.equals(name)) {
149: sourcePath = value;
150: }
151: if (TEST_PATH.equals(name)) {
152: testPath = value;
153: } else if (LIBRARIES.equals(name)) {
154: libraries = value;
155: } else if (JDK.equals(name)) {
156: jdk = value;
157: }
158: }
159: }
160: }
161: } finally {
162: if (jprIs != null) {
163: jprIs.close();
164: }
165: }
166:
167: Enumeration sourcePathEn = new StringTokenizer(sourcePath, ";");//NOI18N
168: Enumeration testPathEn = new StringTokenizer(testPath, ";");//NOI18N
169: Enumeration librariesEn = new StringTokenizer(libraries, ";");//NOI18N
170:
171: return createProjectModel(jpxFile, sourcePathEn, testPathEn,
172: librariesEn, jdk);
173: }
174:
175: private ProjectModel createProjectModel(final File jpxFile,
176: Enumeration sourcePathEn, Enumeration testPathEn,
177: Enumeration libraryEn, String jdkId) {
178: File projectDir = jpxFile.getParentFile();
179: assert projectDir.exists();
180: assert projectDir.isDirectory();
181:
182: FileObject prjDirFo = FileUtil.toFileObject(projectDir);
183: assert prjDirFo != null;
184:
185: AbstractProject project = new AbstractProject(jpxFile
186: .getParentFile().getName(), prjDirFo);
187:
188: Set allTestPaths = new HashSet();
189: while (testPathEn.hasMoreElements()) {
190: String testPath = (String) testPathEn.nextElement();
191: File testFile = FileUtil.normalizeFile(new File(projectDir,
192: testPath));
193: AbstractProject.SourceRoot asr;
194: asr = new AbstractProject.SourceRoot(testFile.getName(),
195: testFile);
196: allTestPaths.add(asr);
197: }
198:
199: while (sourcePathEn.hasMoreElements()) {
200: String sourcePath = (String) sourcePathEn.nextElement();
201: File sourceFile = FileUtil.normalizeFile(new File(
202: projectDir, sourcePath));
203: AbstractProject.SourceRoot asr;
204: asr = new AbstractProject.SourceRoot(sourceFile.getName(),
205: sourceFile);
206: project.addSourceRoot(asr);
207: }
208:
209: while (libraryEn.hasMoreElements()) {
210: String libraryElement = (String) libraryEn.nextElement();
211: File prjOrArchiv = FileUtil.normalizeFile(new File(
212: projectDir, libraryElement));
213: FileObject fo = FileUtil.toFileObject(prjOrArchiv);
214:
215: boolean isProject = ProjectBuilder
216: .isProjectFile(prjOrArchiv)
217: && fo != null;
218: boolean isArchiv = (isProject) ? false
219: : (fo != null && FileUtil.isArchiveFile(fo));
220: boolean isUserLib = (isProject || isArchiv) ? false
221: : !prjOrArchiv.exists();
222: isUserLib = (isUserLib) ? (libraryElement.indexOf('/') == -1 || libraryElement
223: .indexOf('\\') == -1)
224: : false;
225:
226: if (isProject) {
227: ProjectBuilder ftype = ProjectBuilder
228: .getProvider(prjOrArchiv);
229:
230: if (ftype instanceof JpxBuilder) {
231: Collection subPrjs = ftype
232: .buildProjectModels(prjOrArchiv);
233: for (Iterator it = subPrjs.iterator(); it.hasNext();) {
234: AbstractProject subPrjDef = (AbstractProject) it
235: .next();
236: if (subPrjDef != null) {
237: project.addDependency(subPrjDef);
238: }
239: }
240: }
241:
242: } else if (isArchiv) {
243:
244: prjOrArchiv = FileUtil.normalizeFile(prjOrArchiv);
245: project.addLibrary(new AbstractProject.Library(
246: prjOrArchiv));
247:
248: } else if (isUserLib) {
249:
250: final String libraryName = libraryElement;
251: AbstractProject.UserLibrary aulib = UserLibrarySupport
252: .getInstance(libraryName, projectDir);
253:
254: aulib = (aulib != null) ? aulib
255: : new AbstractProject.UserLibrary(libraryName,
256: true);
257: project.addUserLibrary(aulib);
258: }
259: }
260:
261: if (jdkId != null) {
262: project.setJdkId(jdkId);
263: File jdkFolder = JdkSupport.getJKDDirectory(jdkId,
264: projectDir);
265: if (jdkFolder != null) {
266: project.setJDKDirectory(jdkFolder);
267: } else {
268: project.setInvalidJDK(jdkId);
269: }
270:
271: } else {
272: logger.finest("no JDK found");//NOI18N
273: }
274:
275: return project;
276: }
277:
278: private Element getRootElement(Document doc) throws IOException {
279: Element docEl = doc.getDocumentElement();
280:
281: if (!docEl.getTagName().equals(ROOT_ELEMENT)) { // NOI18N
282: String message = NbBundle.getMessage(
283: UserLibrarySupport.class, "ERR_WrongRootElement",
284: docEl.getTagName());// NOI18N
285: throw new IOException(message);
286: }
287:
288: return docEl;
289: }
290: }
|