KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > jarpackager > JarPackageWriter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.jarpackager;
12
13 import java.io.BufferedOutputStream JavaDoc;
14 import java.io.ByteArrayOutputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.OutputStream JavaDoc;
17 import java.io.UnsupportedEncodingException JavaDoc;
18
19 import javax.xml.parsers.DocumentBuilder JavaDoc;
20 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
21 import javax.xml.parsers.ParserConfigurationException JavaDoc;
22 import javax.xml.transform.OutputKeys JavaDoc;
23 import javax.xml.transform.Transformer JavaDoc;
24 import javax.xml.transform.TransformerException JavaDoc;
25 import javax.xml.transform.TransformerFactory JavaDoc;
26 import javax.xml.transform.dom.DOMSource JavaDoc;
27 import javax.xml.transform.stream.StreamResult JavaDoc;
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 JavaDoc;
53 import org.w3c.dom.Document JavaDoc;
54 import org.w3c.dom.Element JavaDoc;
55
56 /**
57  * Writes a JarPackage to an underlying OutputStream
58  */

59 public class JarPackageWriter extends Object JavaDoc implements IJarDescriptionWriter {
60     
61     private final OutputStream JavaDoc fOutputStream;
62     private final String JavaDoc fEncoding;
63     
64     /**
65      * Create a JarPackageWriter on the given output stream.
66      * It is the clients responsibility to close the output stream.
67      */

68     public JarPackageWriter(OutputStream JavaDoc outputStream, String JavaDoc encoding) {
69         Assert.isNotNull(outputStream);
70         fOutputStream= new BufferedOutputStream JavaDoc(outputStream);
71         fEncoding= encoding;
72     }
73     
74     public void write(JarPackageData jarPackage) throws CoreException {
75         try {
76             writeXML(jarPackage);
77         } catch (IOException JavaDoc ex) {
78             String JavaDoc message= (ex.getLocalizedMessage() != null ? ex.getLocalizedMessage() : ""); //$NON-NLS-1$
79
throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IJavaStatusConstants.INTERNAL_ERROR, message, ex));
80         }
81     }
82
83     /**
84      * Writes a XML representation of the JAR specification
85      * to to the underlying stream.
86      *
87      * @exception IOException if writing to the underlying stream fails
88      */

89     public void writeXML(JarPackageData jarPackage) throws IOException JavaDoc {
90         Assert.isNotNull(jarPackage);
91         DocumentBuilder JavaDoc docBuilder= null;
92         DocumentBuilderFactory JavaDoc factory= DocumentBuilderFactory.newInstance();
93         factory.setValidating(false);
94         try {
95             docBuilder= factory.newDocumentBuilder();
96         } catch (ParserConfigurationException JavaDoc ex) {
97             throw new IOException JavaDoc(JarPackagerMessages.JarWriter_error_couldNotGetXmlBuilder);
98         }
99         Document JavaDoc document= docBuilder.newDocument();
100         
101         // Create the document
102
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             // Write the document to the stream
114
Transformer JavaDoc transformer=TransformerFactory.newInstance().newTransformer();
115             transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
116
transformer.setOutputProperty(OutputKeys.ENCODING, fEncoding);
117             transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
118
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","4"); //$NON-NLS-1$ //$NON-NLS-2$
119
DOMSource JavaDoc source = new DOMSource JavaDoc(document);
120             StreamResult JavaDoc result = new StreamResult JavaDoc(fOutputStream);
121             transformer.transform(source, result);
122         } catch (TransformerException JavaDoc e) {
123             throw new IOException JavaDoc(JarPackagerMessages.JarWriter_error_couldNotTransformToXML);
124         }
125     }
126
127     private void xmlWriteJarLocation(JarPackageData jarPackage, Document JavaDoc document, Element xmlJarDesc) throws DOMException JavaDoc {
128         Element jar= document.createElement(JarPackagerUtil.JAR_EXTENSION);
129         xmlJarDesc.appendChild(jar);
130         jar.setAttribute("path", jarPackage.getJarLocation().toPortableString()); //$NON-NLS-1$
131
}
132
133     private void xmlWriteOptions(JarPackageData jarPackage, Document JavaDoc document, Element xmlJarDesc) throws DOMException JavaDoc {
134         Element options= document.createElement("options"); //$NON-NLS-1$
135
xmlJarDesc.appendChild(options);
136         options.setAttribute("overwrite", "" + jarPackage.allowOverwrite()); //$NON-NLS-2$ //$NON-NLS-1$
137
options.setAttribute("compress", "" + jarPackage.isCompressed()); //$NON-NLS-2$ //$NON-NLS-1$
138
options.setAttribute("exportErrors", "" + jarPackage.areErrorsExported()); //$NON-NLS-2$ //$NON-NLS-1$
139
options.setAttribute("exportWarnings", "" + jarPackage.exportWarnings()); //$NON-NLS-2$ //$NON-NLS-1$
140
options.setAttribute("saveDescription", "" + jarPackage.isDescriptionSaved()); //$NON-NLS-2$ //$NON-NLS-1$
141
options.setAttribute("descriptionLocation", jarPackage.getDescriptionLocation().toPortableString()); //$NON-NLS-1$
142
options.setAttribute("useSourceFolders", "" + jarPackage.useSourceFolderHierarchy()); //$NON-NLS-2$ //$NON-NLS-1$
143
options.setAttribute("buildIfNeeded", "" + jarPackage.isBuildingIfNeeded()); //$NON-NLS-2$ //$NON-NLS-1$
144
options.setAttribute("includeDirectoryEntries", "" + jarPackage.areDirectoryEntriesIncluded()); //$NON-NLS-1$//$NON-NLS-2$
145
options.setAttribute("storeRefactorings", "" + jarPackage.isRefactoringAware()); //$NON-NLS-1$//$NON-NLS-2$
146
}
147
148     private void xmlWriteRefactoring(JarPackageData jarPackage, Document JavaDoc document, Element xmlJarDesc) throws DOMException JavaDoc {
149         Element refactoring= document.createElement("storedRefactorings"); //$NON-NLS-1$
150
xmlJarDesc.appendChild(refactoring);
151         refactoring.setAttribute("structuralOnly", "" + jarPackage.isExportStructuralOnly()); //$NON-NLS-1$ //$NON-NLS-2$
152
refactoring.setAttribute("deprecationInfo", "" + jarPackage.isDeprecationAware()); //$NON-NLS-1$ //$NON-NLS-2$
153
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()); //$NON-NLS-1$
157
}
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 JavaDoc stream= new ByteArrayOutputStream JavaDoc();
167                         service.writeRefactoringDescriptors(new RefactoringDescriptorProxy[] { proxies[index]}, stream, RefactoringDescriptor.NONE, true, null);
168                         refactoring.setAttribute("refactoring" + count, stream.toString("UTF-8")); //$NON-NLS-1$ //$NON-NLS-2$
169
} catch (CoreException exception) {
170                         JavaPlugin.log(exception);
171                     } catch (UnsupportedEncodingException JavaDoc exception) {
172                         Assert.isTrue(false);
173                     }
174                 }
175             } finally {
176                 service.disconnect();
177             }
178         }
179     }
180
181     private void xmlWriteManifest(JarPackageData jarPackage, Document JavaDoc document, Element xmlJarDesc) throws DOMException JavaDoc {
182         Element manifest= document.createElement("manifest"); //$NON-NLS-1$
183
xmlJarDesc.appendChild(manifest);
184         manifest.setAttribute("manifestVersion", jarPackage.getManifestVersion()); //$NON-NLS-1$
185
manifest.setAttribute("usesManifest", "" + jarPackage.usesManifest()); //$NON-NLS-2$ //$NON-NLS-1$
186
manifest.setAttribute("reuseManifest", "" + jarPackage.isManifestReused()); //$NON-NLS-2$ //$NON-NLS-1$
187
manifest.setAttribute("saveManifest", "" + jarPackage.isManifestSaved()); //$NON-NLS-2$ //$NON-NLS-1$
188
manifest.setAttribute("generateManifest", "" + jarPackage.isManifestGenerated()); //$NON-NLS-2$ //$NON-NLS-1$
189
manifest.setAttribute("manifestLocation", jarPackage.getManifestLocation().toPortableString()); //$NON-NLS-1$
190
if (jarPackage.getManifestMainClass() != null)
191             manifest.setAttribute("mainClassHandleIdentifier", jarPackage.getManifestMainClass().getHandleIdentifier()); //$NON-NLS-1$
192
xmlWriteSealingInfo(jarPackage, document, manifest);
193     }
194
195     private void xmlWriteSealingInfo(JarPackageData jarPackage, Document JavaDoc document, Element manifest) throws DOMException JavaDoc {
196         Element sealing= document.createElement("sealing"); //$NON-NLS-1$
197
manifest.appendChild(sealing);
198         sealing.setAttribute("sealJar", "" + jarPackage.isJarSealed()); //$NON-NLS-2$ //$NON-NLS-1$
199
Element packagesToSeal= document.createElement("packagesToSeal"); //$NON-NLS-1$
200
sealing.appendChild(packagesToSeal);
201         add(jarPackage.getPackagesToSeal(), packagesToSeal, document);
202         Element packagesToUnSeal= document.createElement("packagesToUnSeal"); //$NON-NLS-1$
203
sealing.appendChild(packagesToUnSeal);
204         add(jarPackage.getPackagesToUnseal(), packagesToUnSeal, document);
205     }
206
207     private void xmlWriteSelectedElements(JarPackageData jarPackage, Document JavaDoc document, Element xmlJarDesc) throws DOMException JavaDoc {
208         Element selectedElements= document.createElement("selectedElements"); //$NON-NLS-1$
209
xmlJarDesc.appendChild(selectedElements);
210         selectedElements.setAttribute("exportClassFiles", "" + jarPackage.areClassFilesExported()); //$NON-NLS-2$ //$NON-NLS-1$
211
selectedElements.setAttribute("exportOutputFolder", "" + jarPackage.areOutputFoldersExported()); //$NON-NLS-2$ //$NON-NLS-1$
212
selectedElements.setAttribute("exportJavaFiles", "" + jarPackage.areJavaFilesExported()); //$NON-NLS-2$ //$NON-NLS-1$
213
Object JavaDoc[] elements= jarPackage.getElements();
214         for (int i= 0; i < elements.length; i++) {
215             Object JavaDoc 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             // Note: Other file types are not handled by this writer
221
}
222     }
223
224     private void xmlWriteSelectedProjects(JarPackageData jarPackage, Document JavaDoc document, Element xmlJarDesc) throws DOMException JavaDoc {
225         Element selectedElements= document.createElement("selectedProjects"); //$NON-NLS-1$
226
xmlJarDesc.appendChild(selectedElements);
227         Object JavaDoc[] elements= jarPackage.getRefactoringProjects();
228         for (int index= 0; index < elements.length; index++) {
229             Object JavaDoc element= elements[index];
230             if (element instanceof IResource)
231                 add((IResource)element, selectedElements, document);
232         }
233     }
234     
235     /**
236      * Closes this stream.
237      * It is the client's responsibility to close the stream.
238      *
239      * @throws CoreException
240      */

241     public void close() throws CoreException {
242         if (fOutputStream != null) {
243             try {
244                 fOutputStream.close();
245             } catch (IOException JavaDoc ex) {
246                 String JavaDoc message= (ex.getLocalizedMessage() != null ? ex.getLocalizedMessage() : ""); //$NON-NLS-1$
247
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 JavaDoc document) {
253         if (resource.getType() == IResource.PROJECT) {
254             Element element= document.createElement("project"); //$NON-NLS-1$
255
parent.appendChild(element);
256             element.setAttribute("name", resource.getName()); //$NON-NLS-1$
257
} else if (resource.getType() == IResource.FILE) {
258             Element element= document.createElement("file"); //$NON-NLS-1$
259
parent.appendChild(element);
260             element.setAttribute("path", resource.getFullPath().toString()); //$NON-NLS-1$
261
} else if (resource.getType() == IResource.FOLDER) {
262             Element element= document.createElement("folder"); //$NON-NLS-1$
263
parent.appendChild(element);
264             element.setAttribute("path", resource.getFullPath().toString()); //$NON-NLS-1$
265
}
266     }
267     
268     private void add(IJavaElement javaElement, Element parent, Document JavaDoc document) {
269         Element element= document.createElement("javaElement"); //$NON-NLS-1$
270
parent.appendChild(element);
271         element.setAttribute("handleIdentifier", javaElement.getHandleIdentifier()); //$NON-NLS-1$
272
}
273
274     private void add(IPackageFragment[] packages, Element parent, Document JavaDoc document) {
275         for (int i= 0; i < packages.length; i++) {
276             Element pkg= document.createElement("package"); //$NON-NLS-1$
277
parent.appendChild(pkg);
278             pkg.setAttribute("handleIdentifier", packages[i].getHandleIdentifier()); //$NON-NLS-1$
279
}
280     }
281
282     /*
283      * This writer always returns OK
284      */

285     public IStatus getStatus() {
286         return new Status(IStatus.OK, JavaPlugin.getPluginId(), 0, "", null); //$NON-NLS-1$
287
}
288 }
289
Popular Tags