KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileFilter JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.util.logging.Logger JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.Set JavaDoc;
33 import org.netbeans.modules.projectimport.j2seimport.AbstractProject;
34 import org.netbeans.modules.projectimport.j2seimport.LoggerFactory;
35 import org.openide.ErrorManager;
36 import org.openide.filesystems.FileUtil;
37 import org.openide.util.NbBundle;
38 import org.openide.xml.XMLUtil;
39 import org.w3c.dom.Document JavaDoc;
40 import org.w3c.dom.Element JavaDoc;
41 import org.xml.sax.InputSource JavaDoc;
42 import org.xml.sax.SAXException JavaDoc;
43
44 /**
45  *
46  * @author Radek Matous
47  */

48 public final class UserLibrarySupport {
49     private static final String JavaDoc ROOT_ELEMENT = "library";//NOI18N
50
private static final String JavaDoc FULLNAME_ELEMENT = "fullname";//NOI18N
51
private static final String JavaDoc CLASS_ELEMENT = "class";//NOI18N
52
private static final String JavaDoc PATH_ELEMENT = "path";//NOI18N
53
private static final String JavaDoc REQUIRED_LIB = "required";//NOI18N
54

55     private static File JavaDoc installDirLib;// = new File();
56
private static File JavaDoc userHomeLib;// = new File();
57

58     private File JavaDoc library;
59     private String JavaDoc libraryName;
60     
61     private static final Logger JavaDoc logger =
62             LoggerFactory.getDefault().createLogger(UserLibrarySupport.class);
63     
64     public static AbstractProject.UserLibrary getInstance(String JavaDoc libraryName, File JavaDoc projectDir) {
65         File JavaDoc[] folders = new File JavaDoc[] {projectDir, getUserHomeLib(),getInstallDirLib()};
66         Set JavaDoc checkCyclicDeps = new HashSet JavaDoc();
67         UserLibrarySupport uSupport = UserLibrarySupport.getInstance(libraryName, folders);
68         return (uSupport != null) ? uSupport.getLibrary(folders, checkCyclicDeps) : null;
69     }
70     
71     public static File JavaDoc getUserHomeLib() {
72         if (userHomeLib == null) {
73             String JavaDoc home = System.getProperty("user.home", "");//NOI18N
74

75             if (home.length() > 0) {
76                 userHomeLib = new File JavaDoc(home, ".jbuilder2006");//NOI18N
77
if (!userHomeLib.exists()) {
78                     userHomeLib = new File JavaDoc(home, ".jbuilder2005");//NOI18N
79
if (!userHomeLib.exists()) {
80                         logger.finest("Not valid user.home.lib: " + userHomeLib);//NOI18N
81
userHomeLib = null;
82                     }
83                 }
84             } else {
85                 logger.finest("Not valid user.home: ");//NOI18N
86
}
87         }
88         
89         return userHomeLib;
90     }
91     
92     public static File JavaDoc getInstallDirLib() {
93         return installDirLib;
94     }
95     
96     
97     
98     public static void setUserHomeLib(final File JavaDoc uHomeDirLib) {
99         userHomeLib = uHomeDirLib;
100     }
101     
102     
103     public static void setInstallDirLib(final File JavaDoc iDirLib) {
104         installDirLib = iDirLib;
105     }
106     
107     private static UserLibrarySupport getInstance(String JavaDoc libraryName, File JavaDoc[] folders) {
108         final String JavaDoc fileName = libraryName.trim()+".library";//NOI18N
109
for (int i = 0; i < folders.length; i++) {
110             if (folders[i] == null) continue;
111             File JavaDoc library = new File JavaDoc(folders[i], fileName);
112             if (library.exists()) {
113                 return new UserLibrarySupport(libraryName, library);
114             }
115         }
116         
117         for (int i = 0; i < folders.length; i++) {
118             if (folders[i] == null) continue;
119             final File JavaDoc[] allChildren = folders[i].listFiles(new FileFilter JavaDoc() {
120                 public boolean accept(File JavaDoc f) {
121                     return f.isFile() && f.getName().endsWith(".library");//NOI18N
122
}
123             });
124             if (allChildren == null) continue;
125             for (int j = 0; j < allChildren.length; j++) {
126                 UserLibrarySupport result = resolveLibrary(libraryName, allChildren[j], folders, new HashSet JavaDoc());
127                 if (result != null) {
128                     return result;
129                 }
130             }
131         }
132         
133         logger.finest("library: "+libraryName + " doesn't exists");//NOI18N
134
return null;
135     }
136
137     private static UserLibrarySupport resolveLibrary(final String JavaDoc libraryName, final File JavaDoc libFile, final File JavaDoc[] folders,final Set JavaDoc checkCyclicDeps) {
138         UserLibrarySupport instance = new UserLibrarySupport(libraryName, libFile);
139         AbstractProject.UserLibrary ul = instance.getLibrary(folders, checkCyclicDeps);
140         return ul != null ? instance : null;
141     }
142     
143     /** Creates a new instance of JBLibraries */
144     private UserLibrarySupport(String JavaDoc libraryName, File JavaDoc library) {
145         this.libraryName = libraryName;
146         this.library = library;
147     }
148     
149     private AbstractProject.UserLibrary getLibrary(File JavaDoc[] folders, Set JavaDoc checkCyclicDeps) {
150         try {
151             return buildLibrary(folders, checkCyclicDeps);
152         } catch (IOException JavaDoc iex) {
153             ErrorManager.getDefault().notify(iex);
154         } catch (SAXException JavaDoc sax) {
155             ErrorManager.getDefault().notify(sax);
156         }
157         
158         return null;
159     }
160     
161     
162     private AbstractProject.UserLibrary buildLibrary(File JavaDoc[] folders, Set JavaDoc checkCyclicDeps) throws IOException JavaDoc, SAXException JavaDoc {
163         AbstractProject.UserLibrary retval = new AbstractProject.UserLibrary(libraryName);
164         boolean isthere = checkCyclicDeps.add(libraryName);
165         assert isthere : libraryName;
166         InputStream JavaDoc jprIs = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(library));
167         try {
168             Document JavaDoc doc = XMLUtil.parse(new InputSource JavaDoc(jprIs), false, false, null, null);
169             Element JavaDoc docEl = getRootElement(doc);
170             
171             String JavaDoc fullName = getFullName(docEl);
172             if (!fullName.equals(libraryName)) {
173                 return null;
174             }
175
176             List JavaDoc/*<Element>*/ reqElems = Util.findSubElements(docEl);
177             for (int i = 0; i < reqElems.size(); i++) {
178                 Element JavaDoc elem = (Element JavaDoc)reqElems.get(i);
179                 String JavaDoc classElem = getClassElement(elem);
180                 if (classElem != null) {
181                     resolvePath(folders, retval, elem);
182                 } else {
183                     String JavaDoc requiredLibrary = getRequiredLibrary(elem);
184                     if (requiredLibrary != null) {
185                         if (checkCyclicDeps.contains(requiredLibrary)) {
186                             AbstractProject.UserLibrary uL = new AbstractProject.UserLibrary(requiredLibrary, false);
187                             retval.addDependency(uL);
188                         } else {
189                             UserLibrarySupport uS = UserLibrarySupport.getInstance(requiredLibrary, folders);
190                             if (uS != null) {
191                                 AbstractProject.UserLibrary uL = uS.getLibrary(folders, checkCyclicDeps);
192                                 if (uL != null) {
193                                     retval.addDependency(uL);
194                                 }
195                             }
196                         }
197                     }
198                 }
199             }
200             
201             //Element classElem = Util.findElement(docEl, CLASS_ELEMENT,null);
202

203         } catch (Exception JavaDoc ex) {
204             System.out.println("libraryName: " + libraryName);
205             return null;
206         } finally {
207             if (jprIs != null) {
208                 jprIs.close();
209             }
210         }
211         
212         return retval;
213     }
214
215     private void resolvePath(final File JavaDoc[] folders, final AbstractProject.UserLibrary retval, final Element JavaDoc classElem) throws IllegalArgumentException JavaDoc {
216         List JavaDoc/*<Element>*/ pathElems = (classElem != null) ? Util.findSubElements(classElem) : Collections.EMPTY_LIST;
217         for (int i = 0; i < pathElems.size(); i++) {
218             String JavaDoc path = getPath((Element JavaDoc)pathElems.get(i));
219             if (path != null) {
220                 AbstractProject.Library lEntry = createLibraryEntry(path);
221                 if (lEntry != null) {
222                     retval.addLibrary(lEntry);
223                 }
224             }
225         }
226     }
227     
228     private Element JavaDoc getRootElement(Document JavaDoc doc) throws IOException JavaDoc {
229         Element JavaDoc docEl = doc.getDocumentElement();
230         
231         if (!docEl.getTagName().equals(ROOT_ELEMENT)) { // NOI18N
232
String JavaDoc message = NbBundle.getMessage(UserLibrarySupport.class,"ERR_WrongRootElement",docEl.getTagName());// NOI18N
233
throw new IOException JavaDoc(message);
234         }
235         
236         return docEl;
237     }
238     
239     private AbstractProject.Library createLibraryEntry(String JavaDoc encodedPath) {
240         String JavaDoc decodedPath = encodedPath.replaceAll("^\\[", "");//NOI18N
241
decodedPath = decodedPath.replaceAll("]", "");//NOI18N
242
decodedPath = decodedPath.replaceAll("\\%\\|", ":");//NOI18N
243
File JavaDoc f = new File JavaDoc(decodedPath);
244         if (!f.exists()) {
245             f = new File JavaDoc(library.getParentFile(), decodedPath);
246         }
247         f = FileUtil.normalizeFile(f);
248         if (!f.exists()) {
249             logger.finest(encodedPath+ " converted into file: " + f.getAbsolutePath() );//NOI18N
250
}
251         return (f.exists()) ? new AbstractProject.Library(f) : null;
252     }
253     
254     private String JavaDoc getFullName(Element JavaDoc docEl) {
255         String JavaDoc fullName = null;
256         
257         if (docEl != null) {
258             Element JavaDoc fullNameElement = Util.findElement(docEl, FULLNAME_ELEMENT,null);
259             fullName = (fullNameElement != null) ? Util.findText(fullNameElement) : null;
260         }
261         
262         return fullName;
263     }
264     
265     
266     private String JavaDoc getPath(Element JavaDoc pathElem) {
267         return getElement(pathElem, PATH_ELEMENT);
268     }
269
270     private String JavaDoc getRequiredLibrary(Element JavaDoc pathElem) {
271         return getElement(pathElem, REQUIRED_LIB);
272     }
273     
274     private String JavaDoc getClassElement(Element JavaDoc pathElem) {
275         return getElement(pathElem, CLASS_ELEMENT);
276     }
277     
278     
279     private String JavaDoc getElement(final Element JavaDoc pathElem, String JavaDoc name) {
280         String JavaDoc path = null;
281         
282         if (pathElem != null && pathElem.getNodeName().equals(name)) {
283             path = Util.findText(pathElem);
284             
285         }
286         
287         return path;
288     }
289
290     
291 }
292
Popular Tags