1 11 package org.eclipse.pde.ant; 12 13 import java.io.*; 14 import java.net.*; 15 import java.util.*; 16 import java.util.jar.*; 17 18 import javax.xml.parsers.*; 19 20 import org.apache.tools.ant.*; 21 import org.eclipse.core.runtime.*; 22 import org.eclipse.osgi.util.*; 23 import org.eclipse.pde.core.plugin.*; 24 import org.eclipse.pde.internal.*; 25 import org.eclipse.pde.internal.builders.*; 26 import org.eclipse.pde.internal.core.*; 27 import org.eclipse.pde.internal.core.plugin.*; 28 import org.eclipse.pde.internal.core.schema.*; 29 import org.osgi.framework.*; 30 31 public class ConvertSchemaToHTML extends Task { 32 33 private SAXParser fParser; 34 private SchemaTransformer fTransformer = new SchemaTransformer(); 35 private String manifest; 36 private String destination; 37 private URL cssURL; 38 39 public void execute() throws BuildException { 40 if (!validateDestination()) 41 return; 42 43 IPluginModelBase model = readManifestFile(); 44 if (model == null) 45 return; 46 47 String pluginID = model.getPluginBase().getId(); 48 if (pluginID == null) { 49 pluginID = getPluginID(); 50 } 51 52 IPluginExtensionPoint[] extPoints = model.getPluginBase().getExtensionPoints(); 53 for (int i = 0; i < extPoints.length; i++) { 54 String schemaLocation = extPoints[i].getSchema(); 55 PrintWriter out = null; 56 57 if (schemaLocation == null || schemaLocation.equals("")) continue; 59 Schema schema=null; 60 try { 61 if (fParser == null) { 62 SAXParserFactory factory = SAXParserFactory.newInstance(); 63 fParser = factory.newSAXParser(); 64 } 65 File schemaFile = new File(model.getInstallLocation(), schemaLocation); 66 XMLDefaultHandler handler = new XMLDefaultHandler(); 67 fParser.parse(schemaFile, handler); 68 69 URL url = schemaFile.toURL(); SchemaDescriptor desc = new SchemaDescriptor(extPoints[i].getFullId(), url); 71 schema = (Schema)desc.getSchema(false); 72 73 File directory = 74 new Path(destination).isAbsolute() 75 ? new File(destination) 76 : new File(getProject().getBaseDir(), destination); 77 if (!directory.exists() || !directory.isDirectory()) 78 if (!directory.mkdirs()) { 79 schema.dispose(); 80 return; 81 } 82 83 File file = 84 new File( 85 directory, 86 (pluginID + "." + extPoints[i].getId()).replace('.', '_') + ".html"); 88 out = new PrintWriter(new FileWriter(file), true); 89 fTransformer.transform(schema, out, cssURL, SchemaTransformer.BUILD); 90 } catch (Exception e) { 91 if (e.getMessage() != null) 92 System.out.println(e.getMessage()); 93 } finally { 94 if (out != null) 95 out.close(); 96 if (schema!=null) 97 schema.dispose(); 98 } 99 } 100 } 101 102 private String getPluginID() { 103 File file = 104 new Path(manifest).isAbsolute() 105 ? new File(manifest) 106 : new File(getProject().getBaseDir(), manifest); 107 File OSGiFile = new File(file.getParentFile(), "META-INF/MANIFEST.MF"); 109 if (OSGiFile.exists()) { 110 try { 111 Manifest OSGiManifest = new Manifest(new FileInputStream(OSGiFile)); 112 Dictionary headers = manifestToProperties(OSGiManifest.getMainAttributes()); 113 String value = headers.get(Constants.BUNDLE_SYMBOLICNAME).toString(); 114 if (value == null) 115 return null; 116 ManifestElement[] elements = ManifestElement.parseHeader(Constants.BUNDLE_SYMBOLICNAME, value); 117 if (elements.length > 0) 118 return elements[0].getValue(); 119 } catch (Exception e1) { 120 System.out.print(e1.getMessage()); 121 } 122 } 123 return null; 124 } 125 126 public void setManifest(String manifest) { 127 this.manifest = manifest; 128 } 129 130 public void setDestination(String destination) { 131 this.destination = destination; 132 } 133 134 public URL getCSSURL(){ 135 return cssURL; 136 } 137 138 public void setCSSURL(String url){ 139 try { 140 cssURL = new URL(url); 141 } catch (MalformedURLException e) { 142 PDE.logException(e); 143 } 144 } 145 146 public void setCSSURL(URL url){ 147 cssURL = url; 148 } 149 150 private IPluginModelBase readManifestFile() { 151 if (manifest == null) { 152 System.out.println( 153 NLS.bind(PDEMessages.Builders_Convert_missingAttribute, "manifest")); return null; 155 } 156 157 File file = 158 new Path(manifest).isAbsolute() 159 ? new File(manifest) 160 : new File(getProject().getBaseDir(), manifest); 161 InputStream stream = null; 162 try { 163 stream = new FileInputStream(file); 164 } catch (Exception e) { 165 if (e.getMessage() != null) 166 System.out.println(e.getMessage()); 167 return null; 168 } 169 170 ExternalPluginModelBase model = null; 171 try { 172 if (file.getName().toLowerCase(Locale.ENGLISH).equals("fragment.xml")) model = new ExternalFragmentModel(); 174 else if (file.getName().toLowerCase(Locale.ENGLISH).equals("plugin.xml")) model = new ExternalPluginModel(); 176 else { 177 System.out.println( 178 NLS.bind(PDEMessages.Builders_Convert_illegalValue, "manifest")); return null; 180 } 181 182 String parentPath = file.getParentFile().getAbsolutePath(); 183 model.setInstallLocation(parentPath); 184 model.load(stream, false); 185 stream.close(); 186 } catch (Exception e) { 187 if (e.getMessage() != null) 188 System.out.println(e.getMessage()); 189 } 190 191 return model; 192 } 193 194 private Properties manifestToProperties(Attributes d) { 195 Iterator iter = d.keySet().iterator(); 196 Properties result = new Properties(); 197 while (iter.hasNext()) { 198 Attributes.Name key = (Attributes.Name) iter.next(); 199 result.put(key.toString(), d.get(key)); 200 } 201 return result; 202 } 203 204 205 private boolean validateDestination() { 206 boolean valid = true; 207 if (destination == null) { 208 System.out.println( 209 NLS.bind(PDEMessages.Builders_Convert_missingAttribute, 210 "destination")); valid = false; 212 } else if (!new Path(destination).isValidPath(destination)) { 213 System.out.println( 214 NLS.bind(PDEMessages.Builders_Convert_illegalValue, "destination")); valid = false; 216 } 217 return valid; 218 } 219 220 } 221 | Popular Tags |