1 11 package org.eclipse.jdt.internal.ui.jarpackager; 12 13 import java.io.BufferedOutputStream ; 14 import java.io.ByteArrayOutputStream ; 15 import java.io.IOException ; 16 import java.io.OutputStream ; 17 import java.io.UnsupportedEncodingException ; 18 19 import javax.xml.parsers.DocumentBuilder ; 20 import javax.xml.parsers.DocumentBuilderFactory ; 21 import javax.xml.parsers.ParserConfigurationException ; 22 import javax.xml.transform.OutputKeys ; 23 import javax.xml.transform.Transformer ; 24 import javax.xml.transform.TransformerException ; 25 import javax.xml.transform.TransformerFactory ; 26 import javax.xml.transform.dom.DOMSource ; 27 import javax.xml.transform.stream.StreamResult ; 28 29 import org.eclipse.core.runtime.Assert; 30 import org.eclipse.core.runtime.CoreException; 31 import org.eclipse.core.runtime.IStatus; 32 import org.eclipse.core.runtime.Status; 33 34 import org.eclipse.core.resources.IProject; 35 import org.eclipse.core.resources.IResource; 36 37 38 import org.eclipse.ltk.core.refactoring.RefactoringCore; 39 import org.eclipse.ltk.core.refactoring.RefactoringDescriptor; 40 import org.eclipse.ltk.core.refactoring.RefactoringDescriptorProxy; 41 import org.eclipse.ltk.core.refactoring.history.IRefactoringHistoryService; 42 43 import org.eclipse.jdt.core.IJavaElement; 44 import org.eclipse.jdt.core.IPackageFragment; 45 46 import org.eclipse.jdt.ui.jarpackager.IJarDescriptionWriter; 47 import org.eclipse.jdt.ui.jarpackager.JarPackageData; 48 49 import org.eclipse.jdt.internal.ui.IJavaStatusConstants; 50 import org.eclipse.jdt.internal.ui.JavaPlugin; 51 52 import org.w3c.dom.DOMException ; 53 import org.w3c.dom.Document ; 54 import org.w3c.dom.Element ; 55 56 59 public class JarPackageWriter extends Object implements IJarDescriptionWriter { 60 61 private final OutputStream fOutputStream; 62 private final String fEncoding; 63 64 68 public JarPackageWriter(OutputStream outputStream, String encoding) { 69 Assert.isNotNull(outputStream); 70 fOutputStream= new BufferedOutputStream (outputStream); 71 fEncoding= encoding; 72 } 73 74 public void write(JarPackageData jarPackage) throws CoreException { 75 try { 76 writeXML(jarPackage); 77 } catch (IOException ex) { 78 String message= (ex.getLocalizedMessage() != null ? ex.getLocalizedMessage() : ""); throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IJavaStatusConstants.INTERNAL_ERROR, message, ex)); 80 } 81 } 82 83 89 public void writeXML(JarPackageData jarPackage) throws IOException { 90 Assert.isNotNull(jarPackage); 91 DocumentBuilder docBuilder= null; 92 DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance(); 93 factory.setValidating(false); 94 try { 95 docBuilder= factory.newDocumentBuilder(); 96 } catch (ParserConfigurationException ex) { 97 throw new IOException (JarPackagerMessages.JarWriter_error_couldNotGetXmlBuilder); 98 } 99 Document document= docBuilder.newDocument(); 100 101 Element xmlJarDesc= document.createElement(JarPackagerUtil.DESCRIPTION_EXTENSION); 103 document.appendChild(xmlJarDesc); 104 xmlWriteJarLocation(jarPackage, document, xmlJarDesc); 105 xmlWriteOptions(jarPackage, document, xmlJarDesc); 106 xmlWriteRefactoring(jarPackage, document, xmlJarDesc); 107 xmlWriteSelectedProjects(jarPackage, document, xmlJarDesc); 108 if (jarPackage.areGeneratedFilesExported()) 109 xmlWriteManifest(jarPackage, document, xmlJarDesc); 110 xmlWriteSelectedElements(jarPackage, document, xmlJarDesc); 111 112 try { 113 Transformer transformer=TransformerFactory.newInstance().newTransformer(); 115 transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, fEncoding); 117 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","4"); DOMSource source = new DOMSource (document); 120 StreamResult result = new StreamResult (fOutputStream); 121 transformer.transform(source, result); 122 } catch (TransformerException e) { 123 throw new IOException (JarPackagerMessages.JarWriter_error_couldNotTransformToXML); 124 } 125 } 126 127 private void xmlWriteJarLocation(JarPackageData jarPackage, Document document, Element xmlJarDesc) throws DOMException { 128 Element jar= document.createElement(JarPackagerUtil.JAR_EXTENSION); 129 xmlJarDesc.appendChild(jar); 130 jar.setAttribute("path", jarPackage.getJarLocation().toPortableString()); } 132 133 private void xmlWriteOptions(JarPackageData jarPackage, Document document, Element xmlJarDesc) throws DOMException { 134 Element options= document.createElement("options"); xmlJarDesc.appendChild(options); 136 options.setAttribute("overwrite", "" + jarPackage.allowOverwrite()); options.setAttribute("compress", "" + jarPackage.isCompressed()); options.setAttribute("exportErrors", "" + jarPackage.areErrorsExported()); options.setAttribute("exportWarnings", "" + jarPackage.exportWarnings()); options.setAttribute("saveDescription", "" + jarPackage.isDescriptionSaved()); options.setAttribute("descriptionLocation", jarPackage.getDescriptionLocation().toPortableString()); options.setAttribute("useSourceFolders", "" + jarPackage.useSourceFolderHierarchy()); options.setAttribute("buildIfNeeded", "" + jarPackage.isBuildingIfNeeded()); options.setAttribute("includeDirectoryEntries", "" + jarPackage.areDirectoryEntriesIncluded()); options.setAttribute("storeRefactorings", "" + jarPackage.isRefactoringAware()); } 147 148 private void xmlWriteRefactoring(JarPackageData jarPackage, Document document, Element xmlJarDesc) throws DOMException { 149 Element refactoring= document.createElement("storedRefactorings"); xmlJarDesc.appendChild(refactoring); 151 refactoring.setAttribute("structuralOnly", "" + jarPackage.isExportStructuralOnly()); refactoring.setAttribute("deprecationInfo", "" + jarPackage.isDeprecationAware()); final IProject[] projects= jarPackage.getRefactoringProjects(); 154 if (projects != null && projects.length > 0) { 155 for (int index= 0; index < projects.length; index++) 156 refactoring.setAttribute("project" + (index + 1), projects[index].getName()); } 158 final RefactoringDescriptorProxy[] proxies= jarPackage.getRefactoringDescriptors(); 159 if (proxies != null && proxies.length > 0) { 160 int count= 1; 161 IRefactoringHistoryService service= RefactoringCore.getHistoryService(); 162 try { 163 service.connect(); 164 for (int index= 0; index < proxies.length; index++, count++) { 165 try { 166 final ByteArrayOutputStream stream= new ByteArrayOutputStream (); 167 service.writeRefactoringDescriptors(new RefactoringDescriptorProxy[] { proxies[index]}, stream, RefactoringDescriptor.NONE, true, null); 168 refactoring.setAttribute("refactoring" + count, stream.toString("UTF-8")); } catch (CoreException exception) { 170 JavaPlugin.log(exception); 171 } catch (UnsupportedEncodingException exception) { 172 Assert.isTrue(false); 173 } 174 } 175 } finally { 176 service.disconnect(); 177 } 178 } 179 } 180 181 private void xmlWriteManifest(JarPackageData jarPackage, Document document, Element xmlJarDesc) throws DOMException { 182 Element manifest= document.createElement("manifest"); xmlJarDesc.appendChild(manifest); 184 manifest.setAttribute("manifestVersion", jarPackage.getManifestVersion()); manifest.setAttribute("usesManifest", "" + jarPackage.usesManifest()); manifest.setAttribute("reuseManifest", "" + jarPackage.isManifestReused()); manifest.setAttribute("saveManifest", "" + jarPackage.isManifestSaved()); manifest.setAttribute("generateManifest", "" + jarPackage.isManifestGenerated()); manifest.setAttribute("manifestLocation", jarPackage.getManifestLocation().toPortableString()); if (jarPackage.getManifestMainClass() != null) 191 manifest.setAttribute("mainClassHandleIdentifier", jarPackage.getManifestMainClass().getHandleIdentifier()); xmlWriteSealingInfo(jarPackage, document, manifest); 193 } 194 195 private void xmlWriteSealingInfo(JarPackageData jarPackage, Document document, Element manifest) throws DOMException { 196 Element sealing= document.createElement("sealing"); manifest.appendChild(sealing); 198 sealing.setAttribute("sealJar", "" + jarPackage.isJarSealed()); Element packagesToSeal= document.createElement("packagesToSeal"); sealing.appendChild(packagesToSeal); 201 add(jarPackage.getPackagesToSeal(), packagesToSeal, document); 202 Element packagesToUnSeal= document.createElement("packagesToUnSeal"); sealing.appendChild(packagesToUnSeal); 204 add(jarPackage.getPackagesToUnseal(), packagesToUnSeal, document); 205 } 206 207 private void xmlWriteSelectedElements(JarPackageData jarPackage, Document document, Element xmlJarDesc) throws DOMException { 208 Element selectedElements= document.createElement("selectedElements"); xmlJarDesc.appendChild(selectedElements); 210 selectedElements.setAttribute("exportClassFiles", "" + jarPackage.areClassFilesExported()); selectedElements.setAttribute("exportOutputFolder", "" + jarPackage.areOutputFoldersExported()); selectedElements.setAttribute("exportJavaFiles", "" + jarPackage.areJavaFilesExported()); Object [] elements= jarPackage.getElements(); 214 for (int i= 0; i < elements.length; i++) { 215 Object element= elements[i]; 216 if (element instanceof IResource) 217 add((IResource)element, selectedElements, document); 218 else if (element instanceof IJavaElement) 219 add((IJavaElement)element, selectedElements, document); 220 } 222 } 223 224 private void xmlWriteSelectedProjects(JarPackageData jarPackage, Document document, Element xmlJarDesc) throws DOMException { 225 Element selectedElements= document.createElement("selectedProjects"); xmlJarDesc.appendChild(selectedElements); 227 Object [] elements= jarPackage.getRefactoringProjects(); 228 for (int index= 0; index < elements.length; index++) { 229 Object element= elements[index]; 230 if (element instanceof IResource) 231 add((IResource)element, selectedElements, document); 232 } 233 } 234 235 241 public void close() throws CoreException { 242 if (fOutputStream != null) { 243 try { 244 fOutputStream.close(); 245 } catch (IOException ex) { 246 String message= (ex.getLocalizedMessage() != null ? ex.getLocalizedMessage() : ""); throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IJavaStatusConstants.INTERNAL_ERROR, message, ex)); 248 } 249 } 250 } 251 252 private void add(IResource resource, Element parent, Document document) { 253 if (resource.getType() == IResource.PROJECT) { 254 Element element= document.createElement("project"); parent.appendChild(element); 256 element.setAttribute("name", resource.getName()); } else if (resource.getType() == IResource.FILE) { 258 Element element= document.createElement("file"); parent.appendChild(element); 260 element.setAttribute("path", resource.getFullPath().toString()); } else if (resource.getType() == IResource.FOLDER) { 262 Element element= document.createElement("folder"); parent.appendChild(element); 264 element.setAttribute("path", resource.getFullPath().toString()); } 266 } 267 268 private void add(IJavaElement javaElement, Element parent, Document document) { 269 Element element= document.createElement("javaElement"); parent.appendChild(element); 271 element.setAttribute("handleIdentifier", javaElement.getHandleIdentifier()); } 273 274 private void add(IPackageFragment[] packages, Element parent, Document document) { 275 for (int i= 0; i < packages.length; i++) { 276 Element pkg= document.createElement("package"); parent.appendChild(pkg); 278 pkg.setAttribute("handleIdentifier", packages[i].getHandleIdentifier()); } 280 } 281 282 285 public IStatus getStatus() { 286 return new Status(IStatus.OK, JavaPlugin.getPluginId(), 0, "", null); } 288 } 289 | Popular Tags |