1 11 package org.eclipse.jdt.internal.ui.jarpackager; 12 13 import java.io.BufferedInputStream ; 14 import java.io.ByteArrayInputStream ; 15 import java.io.IOException ; 16 import java.io.InputStream ; 17 import java.util.ArrayList ; 18 import java.util.HashSet ; 19 import java.util.List ; 20 import java.util.Set ; 21 22 import javax.xml.parsers.DocumentBuilder ; 23 import javax.xml.parsers.DocumentBuilderFactory ; 24 import javax.xml.parsers.ParserConfigurationException ; 25 26 import org.eclipse.core.resources.IFile; 27 import org.eclipse.core.resources.IFolder; 28 import org.eclipse.core.resources.IProject; 29 import org.eclipse.core.resources.ResourcesPlugin; 30 31 import org.eclipse.core.runtime.Assert; 32 import org.eclipse.core.runtime.CoreException; 33 import org.eclipse.core.runtime.IPath; 34 import org.eclipse.core.runtime.IStatus; 35 import org.eclipse.core.runtime.MultiStatus; 36 import org.eclipse.core.runtime.Path; 37 import org.eclipse.core.runtime.Status; 38 39 40 import org.eclipse.ltk.core.refactoring.RefactoringCore; 41 import org.eclipse.ltk.core.refactoring.RefactoringDescriptor; 42 import org.eclipse.ltk.core.refactoring.RefactoringDescriptorProxy; 43 import org.eclipse.ltk.core.refactoring.history.IRefactoringHistoryService; 44 import org.eclipse.ltk.core.refactoring.history.RefactoringHistory; 45 46 import org.w3c.dom.Element ; 47 import org.w3c.dom.Node ; 48 import org.w3c.dom.NodeList ; 49 import org.xml.sax.InputSource ; 50 import org.xml.sax.SAXException ; 51 52 import org.eclipse.jdt.core.IJavaElement; 53 import org.eclipse.jdt.core.IPackageFragment; 54 import org.eclipse.jdt.core.IType; 55 import org.eclipse.jdt.core.JavaCore; 56 57 import org.eclipse.jdt.internal.corext.util.Messages; 58 59 import org.eclipse.jdt.ui.jarpackager.IJarDescriptionReader; 60 import org.eclipse.jdt.ui.jarpackager.JarPackageData; 61 62 import org.eclipse.jdt.internal.ui.JavaPlugin; 63 import org.eclipse.jdt.internal.ui.IJavaStatusConstants; 64 65 68 public class JarPackageReader extends Object implements IJarDescriptionReader { 69 70 protected InputStream fInputStream; 71 72 private MultiStatus fWarnings; 73 74 78 public JarPackageReader(InputStream inputStream) { 79 Assert.isNotNull(inputStream); 80 fInputStream= new BufferedInputStream (inputStream); 81 fWarnings= new MultiStatus(JavaPlugin.getPluginId(), 0, JarPackagerMessages.JarPackageReader_jarPackageReaderWarnings, null); 82 } 83 84 public void read(JarPackageData jarPackage) throws CoreException { 85 try { 86 readXML(jarPackage); 87 } catch (IOException ex) { 88 String message= (ex.getLocalizedMessage() != null ? ex.getLocalizedMessage() : ""); throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IJavaStatusConstants.INTERNAL_ERROR, message, ex)); 90 } catch (SAXException ex) { 91 String message= (ex.getLocalizedMessage() != null ? ex.getLocalizedMessage() : ""); throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IJavaStatusConstants.INTERNAL_ERROR, message, ex)); 93 } 94 } 95 96 97 103 public void close() throws CoreException { 104 if (fInputStream != null) 105 try { 106 fInputStream.close(); 107 } catch (IOException ex) { 108 String message= (ex.getLocalizedMessage() != null ? ex.getLocalizedMessage() : ""); throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IJavaStatusConstants.INTERNAL_ERROR, message, ex)); 110 } 111 } 112 113 public JarPackageData readXML(JarPackageData jarPackage) throws IOException , SAXException { 114 DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance(); 115 factory.setValidating(false); 116 DocumentBuilder parser= null; 117 118 try { 119 parser= factory.newDocumentBuilder(); 120 } catch (ParserConfigurationException ex) { 121 throw new IOException (ex.getLocalizedMessage()); 122 } finally { 123 } 125 Element xmlJarDesc= parser.parse(new InputSource (fInputStream)).getDocumentElement(); 126 if (!xmlJarDesc.getNodeName().equals(JarPackagerUtil.DESCRIPTION_EXTENSION)) { 127 throw new IOException (JarPackagerMessages.JarPackageReader_error_badFormat); 128 } 129 NodeList topLevelElements= xmlJarDesc.getChildNodes(); 130 for (int i= 0; i < topLevelElements.getLength(); i++) { 131 Node node= topLevelElements.item(i); 132 if (node.getNodeType() != Node.ELEMENT_NODE) 133 continue; 134 Element element= (Element )node; 135 xmlReadJarLocation(jarPackage, element); 136 xmlReadOptions(jarPackage, element); 137 xmlReadRefactoring(jarPackage, element); 138 xmlReadSelectedProjects(jarPackage, element); 139 if (jarPackage.areGeneratedFilesExported()) 140 xmlReadManifest(jarPackage, element); 141 xmlReadSelectedElements(jarPackage, element); 142 } 143 return jarPackage; 144 } 145 146 private void xmlReadJarLocation(JarPackageData jarPackage, Element element) { 147 if (element.getNodeName().equals(JarPackagerUtil.JAR_EXTENSION)) 148 jarPackage.setJarLocation(Path.fromPortableString(element.getAttribute("path"))); } 150 151 private void xmlReadOptions(JarPackageData jarPackage, Element element) throws java.io.IOException { 152 if (element.getNodeName().equals("options")) { jarPackage.setOverwrite(getBooleanAttribute(element, "overwrite")); jarPackage.setCompress(getBooleanAttribute(element, "compress")); jarPackage.setExportErrors(getBooleanAttribute(element, "exportErrors")); jarPackage.setExportWarnings(getBooleanAttribute(element, "exportWarnings")); jarPackage.setSaveDescription(getBooleanAttribute(element, "saveDescription")); jarPackage.setUseSourceFolderHierarchy(getBooleanAttribute(element, "useSourceFolders", false)); jarPackage.setDescriptionLocation(Path.fromPortableString(element.getAttribute("descriptionLocation"))); jarPackage.setBuildIfNeeded(getBooleanAttribute(element, "buildIfNeeded", jarPackage.isBuildingIfNeeded())); jarPackage.setIncludeDirectoryEntries(getBooleanAttribute(element, "includeDirectoryEntries", false)); jarPackage.setRefactoringAware(getBooleanAttribute(element, "storeRefactorings", false)); } 164 } 165 166 private void xmlReadRefactoring(JarPackageData jarPackage, Element element) throws java.io.IOException { 167 if (element.getNodeName().equals("storedRefactorings")) { jarPackage.setExportStructuralOnly(getBooleanAttribute(element, "structuralOnly", jarPackage.isExportStructuralOnly())); jarPackage.setDeprecationAware(getBooleanAttribute(element, "deprecationInfo", jarPackage.isDeprecationAware())); List elements= new ArrayList (); 171 int count= 1; 172 String value= element.getAttribute("project" + count); while (value != null && !"".equals(value)) { final IProject project= ResourcesPlugin.getWorkspace().getRoot().getProject(value); 175 if (project.exists()) 176 elements.add(project); 177 count++; 178 value= element.getAttribute("project" + count); } 180 jarPackage.setRefactoringProjects((IProject[]) elements.toArray(new IProject[elements.size()])); 181 elements.clear(); 182 count= 1; 183 IRefactoringHistoryService service= RefactoringCore.getHistoryService(); 184 try { 185 service.connect(); 186 value= element.getAttribute("refactoring" + count); while (value != null && !"".equals(value)) { final ByteArrayInputStream stream= new ByteArrayInputStream (value.getBytes("UTF-8")); try { 190 final RefactoringHistory history= service.readRefactoringHistory(stream, RefactoringDescriptor.NONE); 191 if (history != null) { 192 final RefactoringDescriptorProxy[] descriptors= history.getDescriptors(); 193 if (descriptors.length > 0) { 194 for (int index= 0; index < descriptors.length; index++) { 195 elements.add(descriptors[index]); 196 } 197 } 198 } 199 } catch (CoreException exception) { 200 JavaPlugin.log(exception); 201 } 202 count++; 203 value= element.getAttribute("refactoring" + count); } 205 } finally { 206 service.disconnect(); 207 } 208 jarPackage.setRefactoringDescriptors((RefactoringDescriptorProxy[]) elements.toArray(new RefactoringDescriptorProxy[elements.size()])); 209 } 210 } 211 212 private void xmlReadManifest(JarPackageData jarPackage, Element element) throws java.io.IOException { 213 if (element.getNodeName().equals("manifest")) { jarPackage.setManifestVersion(element.getAttribute("manifestVersion")); jarPackage.setUsesManifest(getBooleanAttribute(element, "usesManifest")); jarPackage.setReuseManifest(getBooleanAttribute(element, "reuseManifest")); jarPackage.setSaveManifest(getBooleanAttribute(element,"saveManifest")); jarPackage.setGenerateManifest(getBooleanAttribute(element, "generateManifest")); jarPackage.setManifestLocation(Path.fromPortableString(element.getAttribute("manifestLocation"))); jarPackage.setManifestMainClass(getMainClass(element)); 221 xmlReadSealingInfo(jarPackage, element); 222 } 223 } 224 225 private void xmlReadSealingInfo(JarPackageData jarPackage, Element element) throws java.io.IOException { 226 231 NodeList sealingElementContainer= element.getChildNodes(); 232 for (int j= 0; j < sealingElementContainer.getLength(); j++) { 233 Node sealingNode= sealingElementContainer.item(j); 234 if (sealingNode.getNodeType() == Node.ELEMENT_NODE 235 && sealingNode.getNodeName().equals("sealing")) { Element sealingElement= (Element )sealingNode; 238 jarPackage.setSealJar(getBooleanAttribute(sealingElement, "sealJar")); jarPackage.setPackagesToSeal(getPackages(sealingElement.getElementsByTagName("packagesToSeal"))); jarPackage.setPackagesToUnseal(getPackages(sealingElement.getElementsByTagName("packagesToUnSeal"))); } 242 } 243 } 244 245 private void xmlReadSelectedElements(JarPackageData jarPackage, Element element) throws java.io.IOException { 246 if (element.getNodeName().equals("selectedElements")) { jarPackage.setExportClassFiles(getBooleanAttribute(element, "exportClassFiles")); jarPackage.setExportOutputFolders(getBooleanAttribute(element, "exportOutputFolder", false)); jarPackage.setExportJavaFiles(getBooleanAttribute(element, "exportJavaFiles")); NodeList selectedElements= element.getChildNodes(); 251 Set elementsToExport= new HashSet (selectedElements.getLength()); 252 for (int j= 0; j < selectedElements.getLength(); j++) { 253 Node selectedNode= selectedElements.item(j); 254 if (selectedNode.getNodeType() != Node.ELEMENT_NODE) 255 continue; 256 Element selectedElement= (Element )selectedNode; 257 if (selectedElement.getNodeName().equals("file")) addFile(elementsToExport, selectedElement); 259 else if (selectedElement.getNodeName().equals("folder")) addFolder(elementsToExport,selectedElement); 261 else if (selectedElement.getNodeName().equals("project")) addProject(elementsToExport ,selectedElement); 263 else if (selectedElement.getNodeName().equals("javaElement")) addJavaElement(elementsToExport, selectedElement); 265 } 267 jarPackage.setElements(elementsToExport.toArray()); 268 } 269 } 270 271 private void xmlReadSelectedProjects(JarPackageData jarPackage, Element element) throws java.io.IOException { 272 if (element.getNodeName().equals("selectedProjects")) { NodeList selectedElements= element.getChildNodes(); 274 Set selectedProjects= new HashSet (selectedElements.getLength()); 275 for (int index= 0; index < selectedElements.getLength(); index++) { 276 Node node= selectedElements.item(index); 277 if (node.getNodeType() != Node.ELEMENT_NODE) 278 continue; 279 Element selectedElement= (Element )node; 280 if (selectedElement.getNodeName().equals("project")) addProject(selectedProjects ,selectedElement); 282 } 283 jarPackage.setRefactoringProjects((IProject[]) selectedProjects.toArray(new IProject[selectedProjects.size()])); 284 } 285 } 286 287 protected boolean getBooleanAttribute(Element element, String name, boolean defaultValue) throws IOException { 288 if (element.hasAttribute(name)) 289 return getBooleanAttribute(element, name); 290 else 291 return defaultValue; 292 } 293 294 protected boolean getBooleanAttribute(Element element, String name) throws IOException { 295 String value= element.getAttribute(name); 296 if (value != null && value.equalsIgnoreCase("true")) return true; 298 if (value != null && value.equalsIgnoreCase("false")) return false; 300 throw new IOException (JarPackagerMessages.JarPackageReader_error_illegalValueForBooleanAttribute); 301 } 302 303 private void addFile(Set selectedElements, Element element) throws IOException { 304 IPath path= getPath(element); 305 if (path != null) { 306 IFile file= JavaPlugin.getWorkspace().getRoot().getFile(path); 307 if (file != null) 308 selectedElements.add(file); 309 } 310 } 311 312 private void addFolder(Set selectedElements, Element element) throws IOException { 313 IPath path= getPath(element); 314 if (path != null) { 315 IFolder folder= JavaPlugin.getWorkspace().getRoot().getFolder(path); 316 if (folder != null) 317 selectedElements.add(folder); 318 } 319 } 320 321 private void addProject(Set selectedElements, Element element) throws IOException { 322 String name= element.getAttribute("name"); if (name.length() == 0) 324 throw new IOException (JarPackagerMessages.JarPackageReader_error_tagNameNotFound); 325 IProject project= JavaPlugin.getWorkspace().getRoot().getProject(name); 326 if (project != null) 327 selectedElements.add(project); 328 } 329 330 private IPath getPath(Element element) throws IOException { 331 String pathString= element.getAttribute("path"); if (pathString.length() == 0) 333 throw new IOException (JarPackagerMessages.JarPackageReader_error_tagPathNotFound); 334 return Path.fromPortableString(element.getAttribute("path")); } 336 337 private void addJavaElement(Set selectedElements, Element element) throws IOException { 338 String handleId= element.getAttribute("handleIdentifier"); if (handleId.length() == 0) 340 throw new IOException (JarPackagerMessages.JarPackageReader_error_tagHandleIdentifierNotFoundOrEmpty); 341 IJavaElement je= JavaCore.create(handleId); 342 if (je == null) 343 addWarning(JarPackagerMessages.JarPackageReader_warning_javaElementDoesNotExist, null); 344 else 345 selectedElements.add(je); 346 } 347 348 private IPackageFragment[] getPackages(NodeList list) throws IOException { 349 if (list.getLength() > 1) 350 throw new IOException (Messages.format(JarPackagerMessages.JarPackageReader_error_duplicateTag, list.item(0).getNodeName())); 351 if (list.getLength() == 0) 352 return null; NodeList packageNodes= list.item(0).getChildNodes(); 354 List packages= new ArrayList (packageNodes.getLength()); 355 for (int i= 0; i < packageNodes.getLength(); i++) { 356 Node packageNode= packageNodes.item(i); 357 if (packageNode.getNodeType() == Node.ELEMENT_NODE && packageNode.getNodeName().equals("package")) { String handleId= ((Element )packageNode).getAttribute("handleIdentifier"); if (handleId.equals("")) throw new IOException (JarPackagerMessages.JarPackageReader_error_tagHandleIdentifierNotFoundOrEmpty); 361 IJavaElement je= JavaCore.create(handleId); 362 if (je != null && je.getElementType() == IJavaElement.PACKAGE_FRAGMENT) 363 packages.add(je); 364 else 365 addWarning(JarPackagerMessages.JarPackageReader_warning_javaElementDoesNotExist, null); 366 } 367 } 368 return (IPackageFragment[])packages.toArray(new IPackageFragment[packages.size()]); 369 } 370 371 private IType getMainClass(Element element) { 372 String handleId= element.getAttribute("mainClassHandleIdentifier"); if (handleId.equals("")) return null; IJavaElement je= JavaCore.create(handleId); 376 if (je != null && je.getElementType() == IJavaElement.TYPE) 377 return (IType)je; 378 addWarning(JarPackagerMessages.JarPackageReader_warning_mainClassDoesNotExist, null); 379 return null; 380 } 381 382 390 public IStatus getStatus() { 391 if (fWarnings.getChildren().length == 0) 392 return new Status(IStatus.OK, JavaPlugin.getPluginId(), 0, "", null); else 394 return fWarnings; 395 } 396 397 403 protected void addWarning(String message, Throwable error) { 404 fWarnings.add(new Status(IStatus.WARNING, JavaPlugin.getPluginId(), 0, message, error)); 405 } 406 } 407 | Popular Tags |