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.FileFilter ; 25 import java.io.FileInputStream ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.util.logging.Logger ; 29 import java.util.List ; 30 import java.util.Collections ; 31 import java.util.HashSet ; 32 import java.util.Set ; 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 ; 40 import org.w3c.dom.Element ; 41 import org.xml.sax.InputSource ; 42 import org.xml.sax.SAXException ; 43 44 48 public final class UserLibrarySupport { 49 private static final String ROOT_ELEMENT = "library"; private static final String FULLNAME_ELEMENT = "fullname"; private static final String CLASS_ELEMENT = "class"; private static final String PATH_ELEMENT = "path"; private static final String REQUIRED_LIB = "required"; 55 private static File installDirLib; private static File userHomeLib; 58 private File library; 59 private String libraryName; 60 61 private static final Logger logger = 62 LoggerFactory.getDefault().createLogger(UserLibrarySupport.class); 63 64 public static AbstractProject.UserLibrary getInstance(String libraryName, File projectDir) { 65 File [] folders = new File [] {projectDir, getUserHomeLib(),getInstallDirLib()}; 66 Set checkCyclicDeps = new HashSet (); 67 UserLibrarySupport uSupport = UserLibrarySupport.getInstance(libraryName, folders); 68 return (uSupport != null) ? uSupport.getLibrary(folders, checkCyclicDeps) : null; 69 } 70 71 public static File getUserHomeLib() { 72 if (userHomeLib == null) { 73 String home = System.getProperty("user.home", ""); 75 if (home.length() > 0) { 76 userHomeLib = new File (home, ".jbuilder2006"); if (!userHomeLib.exists()) { 78 userHomeLib = new File (home, ".jbuilder2005"); if (!userHomeLib.exists()) { 80 logger.finest("Not valid user.home.lib: " + userHomeLib); userHomeLib = null; 82 } 83 } 84 } else { 85 logger.finest("Not valid user.home: "); } 87 } 88 89 return userHomeLib; 90 } 91 92 public static File getInstallDirLib() { 93 return installDirLib; 94 } 95 96 97 98 public static void setUserHomeLib(final File uHomeDirLib) { 99 userHomeLib = uHomeDirLib; 100 } 101 102 103 public static void setInstallDirLib(final File iDirLib) { 104 installDirLib = iDirLib; 105 } 106 107 private static UserLibrarySupport getInstance(String libraryName, File [] folders) { 108 final String fileName = libraryName.trim()+".library"; for (int i = 0; i < folders.length; i++) { 110 if (folders[i] == null) continue; 111 File library = new File (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 [] allChildren = folders[i].listFiles(new FileFilter () { 120 public boolean accept(File f) { 121 return f.isFile() && f.getName().endsWith(".library"); } 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 ()); 127 if (result != null) { 128 return result; 129 } 130 } 131 } 132 133 logger.finest("library: "+libraryName + " doesn't exists"); return null; 135 } 136 137 private static UserLibrarySupport resolveLibrary(final String libraryName, final File libFile, final File [] folders,final Set 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 144 private UserLibrarySupport(String libraryName, File library) { 145 this.libraryName = libraryName; 146 this.library = library; 147 } 148 149 private AbstractProject.UserLibrary getLibrary(File [] folders, Set checkCyclicDeps) { 150 try { 151 return buildLibrary(folders, checkCyclicDeps); 152 } catch (IOException iex) { 153 ErrorManager.getDefault().notify(iex); 154 } catch (SAXException sax) { 155 ErrorManager.getDefault().notify(sax); 156 } 157 158 return null; 159 } 160 161 162 private AbstractProject.UserLibrary buildLibrary(File [] folders, Set checkCyclicDeps) throws IOException , SAXException { 163 AbstractProject.UserLibrary retval = new AbstractProject.UserLibrary(libraryName); 164 boolean isthere = checkCyclicDeps.add(libraryName); 165 assert isthere : libraryName; 166 InputStream jprIs = new BufferedInputStream (new FileInputStream (library)); 167 try { 168 Document doc = XMLUtil.parse(new InputSource (jprIs), false, false, null, null); 169 Element docEl = getRootElement(doc); 170 171 String fullName = getFullName(docEl); 172 if (!fullName.equals(libraryName)) { 173 return null; 174 } 175 176 List reqElems = Util.findSubElements(docEl); 177 for (int i = 0; i < reqElems.size(); i++) { 178 Element elem = (Element )reqElems.get(i); 179 String classElem = getClassElement(elem); 180 if (classElem != null) { 181 resolvePath(folders, retval, elem); 182 } else { 183 String 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 203 } catch (Exception 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 [] folders, final AbstractProject.UserLibrary retval, final Element classElem) throws IllegalArgumentException { 216 List pathElems = (classElem != null) ? Util.findSubElements(classElem) : Collections.EMPTY_LIST; 217 for (int i = 0; i < pathElems.size(); i++) { 218 String path = getPath((Element )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 getRootElement(Document doc) throws IOException { 229 Element docEl = doc.getDocumentElement(); 230 231 if (!docEl.getTagName().equals(ROOT_ELEMENT)) { String message = NbBundle.getMessage(UserLibrarySupport.class,"ERR_WrongRootElement",docEl.getTagName()); throw new IOException (message); 234 } 235 236 return docEl; 237 } 238 239 private AbstractProject.Library createLibraryEntry(String encodedPath) { 240 String decodedPath = encodedPath.replaceAll("^\\[", ""); decodedPath = decodedPath.replaceAll("]", ""); decodedPath = decodedPath.replaceAll("\\%\\|", ":"); File f = new File (decodedPath); 244 if (!f.exists()) { 245 f = new File (library.getParentFile(), decodedPath); 246 } 247 f = FileUtil.normalizeFile(f); 248 if (!f.exists()) { 249 logger.finest(encodedPath+ " converted into file: " + f.getAbsolutePath() ); } 251 return (f.exists()) ? new AbstractProject.Library(f) : null; 252 } 253 254 private String getFullName(Element docEl) { 255 String fullName = null; 256 257 if (docEl != null) { 258 Element 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 getPath(Element pathElem) { 267 return getElement(pathElem, PATH_ELEMENT); 268 } 269 270 private String getRequiredLibrary(Element pathElem) { 271 return getElement(pathElem, REQUIRED_LIB); 272 } 273 274 private String getClassElement(Element pathElem) { 275 return getElement(pathElem, CLASS_ELEMENT); 276 } 277 278 279 private String getElement(final Element pathElem, String name) { 280 String 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 |