1 12 package org.eclipse.jdt.apt.core.internal.util; 13 14 import java.io.File ; 15 import java.io.IOException ; 16 import java.io.StringReader ; 17 import java.util.Iterator ; 18 import java.util.LinkedHashMap ; 19 import java.util.Map ; 20 21 import javax.xml.parsers.DocumentBuilder ; 22 import javax.xml.parsers.DocumentBuilderFactory ; 23 import javax.xml.parsers.ParserConfigurationException ; 24 25 import org.eclipse.core.resources.IFile; 26 import org.eclipse.core.resources.IProject; 27 import org.eclipse.core.resources.IResource; 28 import org.eclipse.core.runtime.CoreException; 29 import org.eclipse.core.runtime.IPath; 30 import org.eclipse.core.runtime.IStatus; 31 import org.eclipse.core.runtime.Path; 32 import org.eclipse.core.runtime.Status; 33 import org.eclipse.jdt.apt.core.internal.AptPlugin; 34 import org.eclipse.jdt.apt.core.internal.ExtJarFactoryContainer; 35 import org.eclipse.jdt.apt.core.internal.FactoryPluginManager; 36 import org.eclipse.jdt.apt.core.internal.VarJarFactoryContainer; 37 import org.eclipse.jdt.apt.core.internal.WkspJarFactoryContainer; 38 import org.eclipse.jdt.apt.core.internal.util.FactoryContainer.FactoryType; 39 import org.eclipse.jdt.apt.core.util.IFactoryPath; 40 import org.eclipse.jdt.core.IJavaProject; 41 import org.w3c.dom.Element ; 42 import org.w3c.dom.Node ; 43 import org.w3c.dom.NodeList ; 44 import org.xml.sax.InputSource ; 45 import org.xml.sax.SAXException ; 46 47 50 public final class FactoryPathUtil { 51 52 private static final String FACTORYPATH_TAG = "factorypath"; private static final String FACTORYPATH_ENTRY_TAG = "factorypathentry"; private static final String KIND = "kind"; private static final String ID = "id"; private static final String ENABLED = "enabled"; private static final String RUN_IN_BATCH_MODE = "runInBatchMode"; 59 private static final String FACTORYPATH_FILE = ".factorypath"; 61 private static final String INDENT = " "; 64 private FactoryPathUtil() {} 66 67 75 public static boolean isFactoryPathFile(IResource res) { 76 if (res == null || res.getType() != IResource.FILE || res.getProject() == null) { 77 return false; 78 } 79 IPath path = res.getProjectRelativePath(); 80 if (path.segmentCount() != 1) { 81 return false; 82 } 83 return FACTORYPATH_FILE.equals(path.lastSegment()); 84 } 85 86 90 public static Map <FactoryContainer, FactoryPath.Attributes> readFactoryPathFile(IJavaProject jproj) 91 throws CoreException 92 { 93 String data = null; 94 try { 95 if (jproj == null) { 97 File file = getFileForWorkspace(); 98 if (!file.exists()) { 99 return null; 100 } 101 data = FileSystemUtil.getContentsOfFile(file); 102 } 103 else { 104 IFile ifile = getIFileForProject(jproj); 105 if (!ifile.exists()) { 106 return null; 107 } 108 data = FileSystemUtil.getContentsOfIFile(ifile); 109 } 110 } 111 catch (IOException e) { 112 throw new CoreException(new Status(IStatus.ERROR, AptPlugin.PLUGIN_ID, -1, Messages.FactoryPathUtil_status_ioException, e)); 113 } 114 115 return FactoryPathUtil.decodeFactoryPath(data); 116 } 117 118 123 public static void saveFactoryPathFile(IJavaProject jproj, Map <FactoryContainer, FactoryPath.Attributes> containers) 124 throws CoreException 125 { 126 IFile projFile; 127 File wkspFile; 128 if (jproj != null) { 129 projFile = getIFileForProject(jproj); 130 wkspFile = null; 131 } 132 else { 133 wkspFile = getFileForWorkspace(); 134 projFile = null; 135 } 136 137 try { 138 if (containers != null) { 139 String data = FactoryPathUtil.encodeFactoryPath(containers); 140 if (jproj == null) { 142 FileSystemUtil.writeStringToFile(wkspFile, data); 143 } 144 else { 145 FileSystemUtil.writeStringToIFile(projFile, data); 146 } 147 } 148 else { if (jproj != null) { 150 projFile.delete(true, null); 151 } 152 else { 153 wkspFile.delete(); 154 } 155 } 156 } 157 catch (IOException e) { 158 throw new CoreException(new Status(IStatus.ERROR, AptPlugin.PLUGIN_ID, -1, Messages.FactoryPathUtil_status_ioException, e)); 159 } 160 } 161 162 166 public static String encodeFactoryPath(Map <FactoryContainer, FactoryPath.Attributes> factories) { 167 StringBuilder sb = new StringBuilder (); 168 sb.append("<").append(FACTORYPATH_TAG).append(">\n"); for (Map.Entry <FactoryContainer, FactoryPath.Attributes> entry : factories.entrySet()) { 170 FactoryContainer container = entry.getKey(); 171 FactoryPath.Attributes attr = entry.getValue(); 172 sb.append(INDENT); 173 sb.append("<"); sb.append(FACTORYPATH_ENTRY_TAG).append(" "); sb.append(KIND).append("=\"").append(container.getType()).append("\" "); sb.append(ID).append("=\"").append(container.getId()).append("\" "); sb.append(ENABLED).append("=\"").append(attr.isEnabled()).append("\" "); sb.append(RUN_IN_BATCH_MODE).append("=\"").append(attr.runInBatchMode()).append("\"/>\n"); } 180 sb.append("</").append(FACTORYPATH_TAG).append(">\n"); 182 return sb.toString(); 183 } 184 185 190 public static FactoryContainer newExtJarFactoryContainer(File jar) { 191 return new ExtJarFactoryContainer(jar); 192 } 193 194 199 public static FactoryContainer newWkspJarFactoryContainer(IPath jar) { 200 return new WkspJarFactoryContainer(jar); 201 } 202 203 209 public static FactoryContainer newVarJarFactoryContainer(IPath jar) { 210 return new VarJarFactoryContainer(jar); 211 } 212 213 public static Map <FactoryContainer, FactoryPath.Attributes> decodeFactoryPath(final String xmlFactoryPath) 214 throws CoreException 215 { 216 Map <FactoryContainer, FactoryPath.Attributes> result = new LinkedHashMap <FactoryContainer, FactoryPath.Attributes>(); 217 StringReader reader = new StringReader (xmlFactoryPath); 218 Element fpElement = null; 219 220 try { 221 DocumentBuilder parser = 222 DocumentBuilderFactory.newInstance().newDocumentBuilder(); 223 fpElement = parser.parse(new InputSource (reader)).getDocumentElement(); 224 225 } 226 catch (IOException e) { 227 throw new CoreException(new Status(IStatus.ERROR, AptPlugin.PLUGIN_ID, -1, Messages.FactoryPathUtil_status_ioException, e)); 228 } 229 catch (SAXException e) { 230 throw new CoreException(new Status(IStatus.ERROR, AptPlugin.PLUGIN_ID, -1, Messages.FactoryPathUtil_status_couldNotParse, e)); 231 } 232 catch (ParserConfigurationException e) { 233 throw new CoreException(new Status(IStatus.ERROR, AptPlugin.PLUGIN_ID, -1, Messages.FactoryPathUtil_status_parserConfigError, e)); 234 } 235 finally { 236 reader.close(); 237 } 238 239 if (!fpElement.getNodeName().equalsIgnoreCase(FACTORYPATH_TAG)) { 240 IOException e = new IOException ("Incorrect file format. File must begin with " + FACTORYPATH_TAG); throw new CoreException(new Status(IStatus.ERROR, AptPlugin.PLUGIN_ID, -1, Messages.FactoryPathUtil_status_ioException, e)); 242 } 243 NodeList nodes = fpElement.getElementsByTagName(FACTORYPATH_ENTRY_TAG); 244 for (int i=0; i < nodes.getLength(); i++) { 245 Node node = nodes.item(i); 246 if (node.getNodeType() == Node.ELEMENT_NODE) { 247 Element element = (Element )node; 248 String kindString = element.getAttribute(KIND); 249 if ("JAR".equals(kindString)) { kindString = "EXTJAR"; } 253 String idString = element.getAttribute(ID); 254 String enabledString = element.getAttribute(ENABLED); 255 String runInAptModeString = element.getAttribute(RUN_IN_BATCH_MODE); 256 FactoryType kind = FactoryType.valueOf(kindString); 257 FactoryContainer container = null; 258 switch (kind) { 259 260 case WKSPJAR : 261 container = newWkspJarFactoryContainer(new Path(idString)); 262 break; 263 264 case EXTJAR : 265 container = newExtJarFactoryContainer(new File (idString)); 266 break; 267 268 case VARJAR : 269 container = newVarJarFactoryContainer(new Path(idString)); 270 break; 271 272 case PLUGIN : 273 container = FactoryPluginManager.getPluginFactoryContainer(idString); 274 break; 275 276 default : 277 throw new IllegalStateException ("Unrecognized kind: " + kind + ". Original string: " + kindString); } 279 280 if (null != container) { 281 FactoryPath.Attributes a = new FactoryPath.Attributes( 282 Boolean.parseBoolean(enabledString), Boolean.parseBoolean(runInAptModeString)); 283 result.put(container, a); 284 } 285 } 286 } 287 288 return result; 289 } 290 291 296 private static File getFileForWorkspace() { 297 return AptPlugin.getPlugin().getStateLocation().append(FACTORYPATH_FILE).toFile(); 298 } 299 300 306 private static IFile getIFileForProject(IJavaProject jproj) { 307 IProject proj = jproj.getProject(); 308 return proj.getFile(FACTORYPATH_FILE); 309 } 310 311 317 public static boolean doesFactoryPathFileExist(IJavaProject jproj) { 318 if (jproj == null) { 319 File wkspFile = getFileForWorkspace(); 320 return wkspFile.exists(); 321 } 322 else { 323 IFile projFile = getIFileForProject(jproj); 324 return projFile.exists(); 325 } 326 } 327 328 337 private static synchronized Map <FactoryContainer, FactoryPath.Attributes> calculatePath(IJavaProject jproj) { 338 Map <FactoryContainer, FactoryPath.Attributes> map = null; 339 boolean foundPerProjFile = false; 340 if (jproj != null) { 341 try { 342 map = FactoryPathUtil.readFactoryPathFile(jproj); 343 foundPerProjFile = (map != null); 344 } 345 catch (CoreException ce) { 346 AptPlugin.log(ce, "Could not get factory containers for project: " + jproj); } 348 } 349 if (map == null) { 351 try { 352 map = FactoryPathUtil.readFactoryPathFile(null); 353 } 354 catch (CoreException ce) { 355 AptPlugin.log(ce, "Could not get factory containers for project: " + jproj); } 357 } 358 if (map == null) { 360 map = new LinkedHashMap <FactoryContainer, FactoryPath.Attributes>(); 361 } 362 boolean disableNewPlugins = (jproj != null) && foundPerProjFile; 363 updatePluginContainers(map, disableNewPlugins); 364 return map; 365 } 366 367 381 private static void updatePluginContainers( 382 Map <FactoryContainer, FactoryPath.Attributes> path, boolean disableNewPlugins) { 383 384 Map <FactoryContainer, FactoryPath.Attributes> pluginContainers = FactoryPluginManager.getAllPluginFactoryContainers(); 386 387 for (Iterator <FactoryContainer> i = path.keySet().iterator(); i.hasNext(); ) { 389 FactoryContainer fc = i.next(); 390 if (fc.getType() == FactoryContainer.FactoryType.PLUGIN && !pluginContainers.containsKey(fc)) { 391 i.remove(); 392 } 393 } 394 395 for (Map.Entry <FactoryContainer, FactoryPath.Attributes> entry : pluginContainers.entrySet()) { 398 if (!path.containsKey(entry.getKey())) { 399 FactoryPath.Attributes newAttr; 400 FactoryPath.Attributes oldAttr = entry.getValue(); 401 if (disableNewPlugins) { 402 newAttr = new FactoryPath.Attributes(false, oldAttr.runInBatchMode()); 403 } else { 404 newAttr = oldAttr; 405 } 406 path.put(entry.getKey(), newAttr); 407 } 408 } 409 } 410 411 417 public static IFactoryPath getDefaultFactoryPath(IJavaProject jproj) { 418 FactoryPath fp = new FactoryPath(); 419 if (jproj != null) { 420 fp.setContainers(calculatePath(null)); 421 } 422 else { 423 fp.setContainers(FactoryPluginManager.getAllPluginFactoryContainers()); 424 } 425 return fp; 426 } 427 428 public static FactoryPath getFactoryPath(IJavaProject jproj) { 429 Map <FactoryContainer, FactoryPath.Attributes> map = calculatePath(jproj); 430 FactoryPath fp = new FactoryPath(); 431 fp.setContainers(map); 432 return fp; 433 } 434 435 public static void setFactoryPath(IJavaProject jproj, FactoryPath path) 436 throws CoreException { 437 Map <FactoryContainer, FactoryPath.Attributes> map = (path != null) ? 438 path.getAllContainers() : null; 439 saveFactoryPathFile(jproj, map); 440 } 441 442 443 } 444 | Popular Tags |