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.FileFilter;
047: import java.io.FileInputStream;
048: import java.io.IOException;
049: import java.io.InputStream;
050: import java.util.logging.Logger;
051: import java.util.List;
052: import java.util.Collections;
053: import java.util.HashSet;
054: import java.util.Set;
055: import org.netbeans.modules.projectimport.j2seimport.AbstractProject;
056: import org.netbeans.modules.projectimport.j2seimport.LoggerFactory;
057: import org.openide.ErrorManager;
058: import org.openide.filesystems.FileUtil;
059: import org.openide.util.NbBundle;
060: import org.openide.xml.XMLUtil;
061: import org.w3c.dom.Document;
062: import org.w3c.dom.Element;
063: import org.xml.sax.InputSource;
064: import org.xml.sax.SAXException;
065:
066: /**
067: *
068: * @author Radek Matous
069: */
070: public final class UserLibrarySupport {
071: private static final String ROOT_ELEMENT = "library";//NOI18N
072: private static final String FULLNAME_ELEMENT = "fullname";//NOI18N
073: private static final String CLASS_ELEMENT = "class";//NOI18N
074: private static final String PATH_ELEMENT = "path";//NOI18N
075: private static final String REQUIRED_LIB = "required";//NOI18N
076:
077: private static File installDirLib;// = new File();
078: private static File userHomeLib;// = new File();
079:
080: private File library;
081: private String libraryName;
082:
083: private static final Logger logger = LoggerFactory.getDefault()
084: .createLogger(UserLibrarySupport.class);
085:
086: public static AbstractProject.UserLibrary getInstance(
087: String libraryName, File projectDir) {
088: File[] folders = new File[] { projectDir, getUserHomeLib(),
089: getInstallDirLib() };
090: Set checkCyclicDeps = new HashSet();
091: UserLibrarySupport uSupport = UserLibrarySupport.getInstance(
092: libraryName, folders);
093: return (uSupport != null) ? uSupport.getLibrary(folders,
094: checkCyclicDeps) : null;
095: }
096:
097: public static File getUserHomeLib() {
098: if (userHomeLib == null) {
099: String home = System.getProperty("user.home", "");//NOI18N
100:
101: if (home.length() > 0) {
102: userHomeLib = new File(home, ".jbuilder2006");//NOI18N
103: if (!userHomeLib.exists()) {
104: userHomeLib = new File(home, ".jbuilder2005");//NOI18N
105: if (!userHomeLib.exists()) {
106: logger.finest("Not valid user.home.lib: "
107: + userHomeLib);//NOI18N
108: userHomeLib = null;
109: }
110: }
111: } else {
112: logger.finest("Not valid user.home: ");//NOI18N
113: }
114: }
115:
116: return userHomeLib;
117: }
118:
119: public static File getInstallDirLib() {
120: return installDirLib;
121: }
122:
123: public static void setUserHomeLib(final File uHomeDirLib) {
124: userHomeLib = uHomeDirLib;
125: }
126:
127: public static void setInstallDirLib(final File iDirLib) {
128: installDirLib = iDirLib;
129: }
130:
131: private static UserLibrarySupport getInstance(String libraryName,
132: File[] folders) {
133: final String fileName = libraryName.trim() + ".library";//NOI18N
134: for (int i = 0; i < folders.length; i++) {
135: if (folders[i] == null)
136: continue;
137: File library = new File(folders[i], fileName);
138: if (library.exists()) {
139: return new UserLibrarySupport(libraryName, library);
140: }
141: }
142:
143: for (int i = 0; i < folders.length; i++) {
144: if (folders[i] == null)
145: continue;
146: final File[] allChildren = folders[i]
147: .listFiles(new FileFilter() {
148: public boolean accept(File f) {
149: return f.isFile()
150: && f.getName().endsWith(".library");//NOI18N
151: }
152: });
153: if (allChildren == null)
154: continue;
155: for (int j = 0; j < allChildren.length; j++) {
156: UserLibrarySupport result = resolveLibrary(libraryName,
157: allChildren[j], folders, new HashSet());
158: if (result != null) {
159: return result;
160: }
161: }
162: }
163:
164: logger.finest("library: " + libraryName + " doesn't exists");//NOI18N
165: return null;
166: }
167:
168: private static UserLibrarySupport resolveLibrary(
169: final String libraryName, final File libFile,
170: final File[] folders, final Set checkCyclicDeps) {
171: UserLibrarySupport instance = new UserLibrarySupport(
172: libraryName, libFile);
173: AbstractProject.UserLibrary ul = instance.getLibrary(folders,
174: checkCyclicDeps);
175: return ul != null ? instance : null;
176: }
177:
178: /** Creates a new instance of JBLibraries */
179: private UserLibrarySupport(String libraryName, File library) {
180: this .libraryName = libraryName;
181: this .library = library;
182: }
183:
184: private AbstractProject.UserLibrary getLibrary(File[] folders,
185: Set checkCyclicDeps) {
186: try {
187: return buildLibrary(folders, checkCyclicDeps);
188: } catch (IOException iex) {
189: ErrorManager.getDefault().notify(iex);
190: } catch (SAXException sax) {
191: ErrorManager.getDefault().notify(sax);
192: }
193:
194: return null;
195: }
196:
197: private AbstractProject.UserLibrary buildLibrary(File[] folders,
198: Set checkCyclicDeps) throws IOException, SAXException {
199: AbstractProject.UserLibrary retval = new AbstractProject.UserLibrary(
200: libraryName);
201: boolean isthere = checkCyclicDeps.add(libraryName);
202: assert isthere : libraryName;
203: InputStream jprIs = new BufferedInputStream(
204: new FileInputStream(library));
205: try {
206: Document doc = XMLUtil.parse(new InputSource(jprIs), false,
207: false, null, null);
208: Element docEl = getRootElement(doc);
209:
210: String fullName = getFullName(docEl);
211: if (!fullName.equals(libraryName)) {
212: return null;
213: }
214:
215: List/*<Element>*/reqElems = Util.findSubElements(docEl);
216: for (int i = 0; i < reqElems.size(); i++) {
217: Element elem = (Element) reqElems.get(i);
218: String classElem = getClassElement(elem);
219: if (classElem != null) {
220: resolvePath(folders, retval, elem);
221: } else {
222: String requiredLibrary = getRequiredLibrary(elem);
223: if (requiredLibrary != null) {
224: if (checkCyclicDeps.contains(requiredLibrary)) {
225: AbstractProject.UserLibrary uL = new AbstractProject.UserLibrary(
226: requiredLibrary, false);
227: retval.addDependency(uL);
228: } else {
229: UserLibrarySupport uS = UserLibrarySupport
230: .getInstance(requiredLibrary,
231: folders);
232: if (uS != null) {
233: AbstractProject.UserLibrary uL = uS
234: .getLibrary(folders,
235: checkCyclicDeps);
236: if (uL != null) {
237: retval.addDependency(uL);
238: }
239: }
240: }
241: }
242: }
243: }
244:
245: //Element classElem = Util.findElement(docEl, CLASS_ELEMENT,null);
246:
247: } catch (Exception ex) {
248: System.out.println("libraryName: " + libraryName);
249: return null;
250: } finally {
251: if (jprIs != null) {
252: jprIs.close();
253: }
254: }
255:
256: return retval;
257: }
258:
259: private void resolvePath(final File[] folders,
260: final AbstractProject.UserLibrary retval,
261: final Element classElem) throws IllegalArgumentException {
262: List/*<Element>*/pathElems = (classElem != null) ? Util
263: .findSubElements(classElem) : Collections.EMPTY_LIST;
264: for (int i = 0; i < pathElems.size(); i++) {
265: String path = getPath((Element) pathElems.get(i));
266: if (path != null) {
267: AbstractProject.Library lEntry = createLibraryEntry(path);
268: if (lEntry != null) {
269: retval.addLibrary(lEntry);
270: }
271: }
272: }
273: }
274:
275: private Element getRootElement(Document doc) throws IOException {
276: Element docEl = doc.getDocumentElement();
277:
278: if (!docEl.getTagName().equals(ROOT_ELEMENT)) { // NOI18N
279: String message = NbBundle.getMessage(
280: UserLibrarySupport.class, "ERR_WrongRootElement",
281: docEl.getTagName());// NOI18N
282: throw new IOException(message);
283: }
284:
285: return docEl;
286: }
287:
288: private AbstractProject.Library createLibraryEntry(
289: String encodedPath) {
290: String decodedPath = encodedPath.replaceAll("^\\[", "");//NOI18N
291: decodedPath = decodedPath.replaceAll("]", "");//NOI18N
292: decodedPath = decodedPath.replaceAll("\\%\\|", ":");//NOI18N
293: File f = new File(decodedPath);
294: if (!f.exists()) {
295: f = new File(library.getParentFile(), decodedPath);
296: }
297: f = FileUtil.normalizeFile(f);
298: if (!f.exists()) {
299: logger.finest(encodedPath + " converted into file: "
300: + f.getAbsolutePath());//NOI18N
301: }
302: return (f.exists()) ? new AbstractProject.Library(f) : null;
303: }
304:
305: private String getFullName(Element docEl) {
306: String fullName = null;
307:
308: if (docEl != null) {
309: Element fullNameElement = Util.findElement(docEl,
310: FULLNAME_ELEMENT, null);
311: fullName = (fullNameElement != null) ? Util
312: .findText(fullNameElement) : null;
313: }
314:
315: return fullName;
316: }
317:
318: private String getPath(Element pathElem) {
319: return getElement(pathElem, PATH_ELEMENT);
320: }
321:
322: private String getRequiredLibrary(Element pathElem) {
323: return getElement(pathElem, REQUIRED_LIB);
324: }
325:
326: private String getClassElement(Element pathElem) {
327: return getElement(pathElem, CLASS_ELEMENT);
328: }
329:
330: private String getElement(final Element pathElem, String name) {
331: String path = null;
332:
333: if (pathElem != null && pathElem.getNodeName().equals(name)) {
334: path = Util.findText(pathElem);
335:
336: }
337:
338: return path;
339: }
340:
341: }
|