KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > ant > ConvertSchemaToHTML


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.pde.internal.core.ant;
12
13 import java.io.BufferedInputStream JavaDoc;
14 import java.io.File JavaDoc;
15 import java.io.FileInputStream JavaDoc;
16 import java.io.FileWriter JavaDoc;
17 import java.io.InputStream JavaDoc;
18 import java.io.PrintWriter JavaDoc;
19 import java.net.MalformedURLException JavaDoc;
20 import java.net.URL JavaDoc;
21 import java.util.Locale JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.apache.tools.ant.BuildException;
25 import org.apache.tools.ant.Task;
26 import org.eclipse.core.runtime.Path;
27 import org.eclipse.osgi.util.ManifestElement;
28 import org.eclipse.osgi.util.NLS;
29 import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
30 import org.eclipse.pde.core.plugin.IPluginModelBase;
31 import org.eclipse.pde.internal.core.PDECore;
32 import org.eclipse.pde.internal.core.PDECoreMessages;
33 import org.eclipse.pde.internal.core.XMLDefaultHandler;
34 import org.eclipse.pde.internal.core.builders.SchemaTransformer;
35 import org.eclipse.pde.internal.core.plugin.ExternalFragmentModel;
36 import org.eclipse.pde.internal.core.plugin.ExternalPluginModel;
37 import org.eclipse.pde.internal.core.plugin.ExternalPluginModelBase;
38 import org.eclipse.pde.internal.core.schema.Schema;
39 import org.eclipse.pde.internal.core.schema.SchemaDescriptor;
40 import org.eclipse.pde.internal.core.util.HeaderMap;
41 import org.eclipse.pde.internal.core.util.SAXParserWrapper;
42 import org.osgi.framework.Constants;
43
44 public class ConvertSchemaToHTML extends Task {
45
46     private SchemaTransformer fTransformer = new SchemaTransformer();
47     private String JavaDoc manifest;
48     private String JavaDoc destination;
49     private URL JavaDoc cssURL;
50     
51     public void execute() throws BuildException {
52         if (!validateDestination())
53             return;
54
55         IPluginModelBase model = readManifestFile();
56         if (model == null)
57             return;
58         
59         String JavaDoc pluginID = model.getPluginBase().getId();
60         if (pluginID == null) {
61             pluginID = getPluginID();
62         }
63
64         IPluginExtensionPoint[] extPoints = model.getPluginBase().getExtensionPoints();
65         for (int i = 0; i < extPoints.length; i++) {
66             String JavaDoc schemaLocation = extPoints[i].getSchema();
67             PrintWriter JavaDoc out = null;
68
69             if (schemaLocation == null || schemaLocation.equals("")) //$NON-NLS-1$
70
continue;
71             Schema schema=null;
72             SAXParserWrapper parser = null;
73             try {
74                 parser = new SAXParserWrapper();
75                 File JavaDoc schemaFile = new File JavaDoc(model.getInstallLocation(), schemaLocation);
76                 XMLDefaultHandler handler = new XMLDefaultHandler();
77                 parser.parse(schemaFile, handler);
78
79                 URL JavaDoc url = schemaFile.toURL();
80                 SchemaDescriptor desc = new SchemaDescriptor(extPoints[i].getFullId(), url);
81                 schema = (Schema)desc.getSchema(false);
82                     
83                 File JavaDoc directory =
84                     new Path(destination).isAbsolute()
85                         ? new File JavaDoc(destination)
86                         : new File JavaDoc(getProject().getBaseDir(), destination);
87                 if (!directory.exists() || !directory.isDirectory())
88                     if (!directory.mkdirs()) {
89                         schema.dispose();
90                         return;
91                     }
92
93                 String JavaDoc id = extPoints[i].getId();
94                 if (id.indexOf('.') == -1)
95                     id = pluginID + "." + id; //$NON-NLS-1$
96
File JavaDoc file = new File JavaDoc(directory, id.replace('.', '_') + ".html"); //$NON-NLS-1$ //$NON-NLS-2$
97
out = new PrintWriter JavaDoc(new FileWriter JavaDoc(file), true);
98                 fTransformer.transform(schema, out, cssURL, SchemaTransformer.BUILD);
99             } catch (Exception JavaDoc e) {
100                 if (e.getMessage() != null)
101                     System.out.println(e.getMessage());
102             } finally {
103                 if (out != null)
104                     out.close();
105                 if (schema!=null)
106                     schema.dispose();
107             }
108         }
109     }
110     
111     private String JavaDoc getPluginID() {
112         File JavaDoc file =
113             new Path(manifest).isAbsolute()
114                 ? new File JavaDoc(manifest)
115                 : new File JavaDoc(getProject().getBaseDir(), manifest);
116         File JavaDoc OSGiFile = new File JavaDoc(file.getParentFile(), "META-INF/MANIFEST.MF"); //$NON-NLS-1$
117

118         if (OSGiFile.exists()) {
119             try {
120                 Map JavaDoc headers = ManifestElement.parseBundleManifest(new FileInputStream JavaDoc(OSGiFile), new HeaderMap());
121                 String JavaDoc value = headers.get(Constants.BUNDLE_SYMBOLICNAME).toString();
122                 if (value == null)
123                     return null;
124                     ManifestElement[] elements = ManifestElement.parseHeader(Constants.BUNDLE_SYMBOLICNAME, value);
125                     if (elements.length > 0)
126                         return elements[0].getValue();
127             } catch (Exception JavaDoc e1) {
128                 System.out.print(e1.getMessage());
129             }
130         }
131         return null;
132     }
133
134     public void setManifest(String JavaDoc manifest) {
135         this.manifest = manifest;
136     }
137
138     public void setDestination(String JavaDoc destination) {
139         this.destination = destination;
140     }
141     
142     public URL JavaDoc getCSSURL(){
143         return cssURL;
144     }
145     
146     public void setCSSURL(String JavaDoc url){
147         try {
148             cssURL = new URL JavaDoc(url);
149         } catch (MalformedURLException JavaDoc e) {
150             PDECore.logException(e);
151         }
152     }
153     
154     public void setCSSURL(URL JavaDoc url){
155         cssURL = url;
156     }
157
158     private IPluginModelBase readManifestFile() {
159         if (manifest == null) {
160             System.out.println(
161                 NLS.bind(PDECoreMessages.Builders_Convert_missingAttribute, "manifest")); //$NON-NLS-1$
162
return null;
163         }
164         
165         File JavaDoc file =
166             new Path(manifest).isAbsolute()
167                 ? new File JavaDoc(manifest)
168                 : new File JavaDoc(getProject().getBaseDir(), manifest);
169         InputStream JavaDoc stream = null;
170         try {
171             stream = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(file));
172         } catch (Exception JavaDoc e) {
173             if (e.getMessage() != null)
174                 System.out.println(e.getMessage());
175             return null;
176         }
177
178         ExternalPluginModelBase model = null;
179         try {
180             if (file.getName().toLowerCase(Locale.ENGLISH).equals("fragment.xml")) //$NON-NLS-1$
181
model = new ExternalFragmentModel();
182             else if (file.getName().toLowerCase(Locale.ENGLISH).equals("plugin.xml")) //$NON-NLS-1$
183
model = new ExternalPluginModel();
184             else {
185                 System.out.println(
186                         NLS.bind(PDECoreMessages.Builders_Convert_illegalValue, "manifest")); //$NON-NLS-1$
187
return null;
188             }
189
190             String JavaDoc parentPath = file.getParentFile().getAbsolutePath();
191             model.setInstallLocation(parentPath);
192             model.load(stream, false);
193             stream.close();
194         } catch (Exception JavaDoc e) {
195             if (e.getMessage() != null)
196                 System.out.println(e.getMessage());
197         }
198         
199         return model;
200     }
201     
202     private boolean validateDestination() {
203         boolean valid = true;
204         if (destination == null) {
205             System.out.println(
206                     NLS.bind(PDECoreMessages.Builders_Convert_missingAttribute,
207                     "destination")); //$NON-NLS-1$
208
valid = false;
209         } else if (!new Path(destination).isValidPath(destination)) {
210             System.out.println(
211                     NLS.bind(PDECoreMessages.Builders_Convert_illegalValue, "destination")); //$NON-NLS-1$
212
valid = false;
213         }
214         return valid;
215     }
216     
217 }
218
Popular Tags