1 11 package org.eclipse.pde.internal.core.ant; 12 13 import java.io.BufferedInputStream ; 14 import java.io.File ; 15 import java.io.FileInputStream ; 16 import java.io.FileWriter ; 17 import java.io.InputStream ; 18 import java.io.PrintWriter ; 19 import java.net.MalformedURLException ; 20 import java.net.URL ; 21 import java.util.Locale ; 22 import java.util.Map ; 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 manifest; 48 private String destination; 49 private URL 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 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 schemaLocation = extPoints[i].getSchema(); 67 PrintWriter out = null; 68 69 if (schemaLocation == null || schemaLocation.equals("")) continue; 71 Schema schema=null; 72 SAXParserWrapper parser = null; 73 try { 74 parser = new SAXParserWrapper(); 75 File schemaFile = new File (model.getInstallLocation(), schemaLocation); 76 XMLDefaultHandler handler = new XMLDefaultHandler(); 77 parser.parse(schemaFile, handler); 78 79 URL url = schemaFile.toURL(); 80 SchemaDescriptor desc = new SchemaDescriptor(extPoints[i].getFullId(), url); 81 schema = (Schema)desc.getSchema(false); 82 83 File directory = 84 new Path(destination).isAbsolute() 85 ? new File (destination) 86 : new File (getProject().getBaseDir(), destination); 87 if (!directory.exists() || !directory.isDirectory()) 88 if (!directory.mkdirs()) { 89 schema.dispose(); 90 return; 91 } 92 93 String id = extPoints[i].getId(); 94 if (id.indexOf('.') == -1) 95 id = pluginID + "." + id; File file = new File (directory, id.replace('.', '_') + ".html"); out = new PrintWriter (new FileWriter (file), true); 98 fTransformer.transform(schema, out, cssURL, SchemaTransformer.BUILD); 99 } catch (Exception 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 getPluginID() { 112 File file = 113 new Path(manifest).isAbsolute() 114 ? new File (manifest) 115 : new File (getProject().getBaseDir(), manifest); 116 File OSGiFile = new File (file.getParentFile(), "META-INF/MANIFEST.MF"); 118 if (OSGiFile.exists()) { 119 try { 120 Map headers = ManifestElement.parseBundleManifest(new FileInputStream (OSGiFile), new HeaderMap()); 121 String 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 e1) { 128 System.out.print(e1.getMessage()); 129 } 130 } 131 return null; 132 } 133 134 public void setManifest(String manifest) { 135 this.manifest = manifest; 136 } 137 138 public void setDestination(String destination) { 139 this.destination = destination; 140 } 141 142 public URL getCSSURL(){ 143 return cssURL; 144 } 145 146 public void setCSSURL(String url){ 147 try { 148 cssURL = new URL (url); 149 } catch (MalformedURLException e) { 150 PDECore.logException(e); 151 } 152 } 153 154 public void setCSSURL(URL 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")); return null; 163 } 164 165 File file = 166 new Path(manifest).isAbsolute() 167 ? new File (manifest) 168 : new File (getProject().getBaseDir(), manifest); 169 InputStream stream = null; 170 try { 171 stream = new BufferedInputStream (new FileInputStream (file)); 172 } catch (Exception 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")) model = new ExternalFragmentModel(); 182 else if (file.getName().toLowerCase(Locale.ENGLISH).equals("plugin.xml")) model = new ExternalPluginModel(); 184 else { 185 System.out.println( 186 NLS.bind(PDECoreMessages.Builders_Convert_illegalValue, "manifest")); return null; 188 } 189 190 String parentPath = file.getParentFile().getAbsolutePath(); 191 model.setInstallLocation(parentPath); 192 model.load(stream, false); 193 stream.close(); 194 } catch (Exception 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")); valid = false; 209 } else if (!new Path(destination).isValidPath(destination)) { 210 System.out.println( 211 NLS.bind(PDECoreMessages.Builders_Convert_illegalValue, "destination")); valid = false; 213 } 214 return valid; 215 } 216 217 } 218 | Popular Tags |