1 11 package org.eclipse.pde.internal.core.builders; 12 13 import java.io.IOException ; 14 import java.io.PrintWriter ; 15 import java.net.URL ; 16 import java.util.Locale ; 17 18 import org.eclipse.core.runtime.FileLocator; 19 import org.eclipse.core.runtime.IProduct; 20 import org.eclipse.core.runtime.Platform; 21 import org.eclipse.pde.internal.core.PDECore; 22 import org.eclipse.pde.internal.core.ischema.IDocumentSection; 23 import org.eclipse.pde.internal.core.ischema.ISchema; 24 import org.eclipse.pde.internal.core.ischema.ISchemaAttribute; 25 import org.eclipse.pde.internal.core.ischema.ISchemaElement; 26 import org.eclipse.pde.internal.core.ischema.ISchemaInclude; 27 import org.eclipse.pde.internal.core.ischema.ISchemaRestriction; 28 import org.eclipse.pde.internal.core.ischema.ISchemaSimpleType; 29 import org.eclipse.pde.internal.core.schema.ChoiceRestriction; 30 import org.eclipse.pde.internal.core.schema.DocumentSection; 31 import org.eclipse.pde.internal.core.schema.SchemaRootElement; 32 import org.osgi.framework.Bundle; 33 34 public class SchemaTransformer { 35 36 private static final String PLATFORM_PLUGIN = "org.eclipse.platform"; private static final String PLATFORM_PLUGIN_DOC = "org.eclipse.platform.doc.isv"; private static final String SCHEMA_CSS = "schema.css"; private static final String PLATFORM_CSS = "book.css"; 41 public static final byte TEMP = 0x00; 42 public static final byte BUILD = 0x01; 43 44 private byte fCssPurpose; 45 private PrintWriter fWriter; 46 private ISchema fSchema; 47 private URL fCssURL; 48 49 public void transform(ISchema schema, PrintWriter out) { 50 transform(schema, out, null, TEMP); 51 } 52 53 public void transform(ISchema schema, PrintWriter out, URL cssURL, byte cssPurpose) { 54 fSchema = schema; 55 fWriter = out; 56 fCssPurpose = cssPurpose; 57 setCssURL(cssURL); 58 printHTMLContent(); 59 } 60 61 private void setCssURL(URL cssURL) { 62 try { 63 if (cssURL != null) 64 fCssURL = FileLocator.resolve(cssURL); 65 } catch (IOException e) { 66 } 67 if (fCssURL == null && fCssPurpose != BUILD) 68 fCssURL = getResourceURL(getProductPlugin(), PLATFORM_CSS); 69 if (fCssURL == null && fCssPurpose != BUILD) 70 fCssURL = getResourceURL(PDECore.PLUGIN_ID, PLATFORM_CSS); 71 } 72 73 private String getCssURL() { 74 return (fCssURL != null) ? fCssURL.toString() : "../../" + PLATFORM_CSS; } 76 77 private String getSchemaCssURL() { 78 if (fCssPurpose == BUILD) 79 return "../../" + SCHEMA_CSS; URL url = getResourceURL(PLATFORM_PLUGIN_DOC, SCHEMA_CSS); 81 if (url == null) { 82 url = getResourceURL(PDECore.PLUGIN_ID, SCHEMA_CSS); 84 } 85 return url.toString(); 86 } 87 88 private void printHTMLContent() { 89 fWriter.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">"); fWriter.println("<HTML>"); printHeader(); 92 printBody(); 93 fWriter.println("</HTML>"); } 95 96 private void printHeader() { 97 fWriter.print("<HEAD>"); fWriter.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">"); fWriter.println("<title>" + fSchema.getName() + "</title>"); printStyles(); 101 fWriter.println("</HEAD>"); } 103 104 private void printStyles() { 105 fWriter.println("<style>@import url(\"" + getCssURL() + "\");</style>"); fWriter.println("<style>@import url(\"" + getSchemaCssURL() + "\");</style>"); } 108 109 private URL getResourceURL(String bundleID, String resourcePath) { 110 try { 111 Bundle bundle = Platform.getBundle(bundleID); 112 if (bundle != null) { 113 URL entry = bundle.getEntry(resourcePath); 114 if (entry != null) 115 return FileLocator.toFileURL(entry); 116 } 117 } catch (IOException e) { 118 } 119 return null; 120 } 121 122 private void printBody() { 123 fWriter.println("<BODY>"); fWriter.println("<H1><CENTER>" + fSchema.getName() + "</CENTER></H1>"); if (fSchema.isDeperecated()) { 126 fWriter.print("<div style=\"border: 1px solid #990000; padding: 5px; text-align: center; color: red;\">"); fWriter.print("This extension point is deprecated"); String suggestion = fSchema.getDeprecatedSuggestion(); 129 if (suggestion != null) 130 fWriter.print(", use <i>" + suggestion + "</i> as a replacement."); fWriter.println("</div>"); } 133 fWriter.println("<p></p>"); fWriter.print("<h6 class=CaptionFigColumn id=header>Identifier: </h6>"); fWriter.print(fSchema.getQualifiedPointId()); 136 fWriter.println("<p></p>"); transformSection("Since:", IDocumentSection.SINCE); transformDescription(); 139 fWriter.println("<p><h6 class=CaptionFigColumn id=header>Configuration Markup:</h6></p>"); transformMarkup(); 141 transformSection("Examples:", IDocumentSection.EXAMPLES); transformSection("API Information:", IDocumentSection.API_INFO); transformSection("Supplied Implementation:", IDocumentSection.IMPLEMENTATION); fWriter.println("<br>"); fWriter.println("<p class=note id=copyright>"); transformSection(null, IDocumentSection.COPYRIGHT); 147 fWriter.println("</p>"); fWriter.println("</BODY>"); } 150 151 private void transformSection(String title, String sectionId) { 152 IDocumentSection section = findSection(fSchema.getDocumentSections(), sectionId); 153 if (section == null) 154 return; 155 String description = section.getDescription(); 156 if (description == null || description.trim().length() == 0) 157 return; 158 if (title != null) 159 fWriter.print("<h6 class=CaptionFigColumn id=header>" + title + " </h6>"); transformText(description); 161 fWriter.println(); 162 fWriter.println("<p></p>"); fWriter.println(); 164 } 165 166 private DocumentSection findSection(IDocumentSection[] sections, String sectionId) { 167 for (int i = 0; i < sections.length; i++) { 168 if (sections[i].getSectionId().equals(sectionId)) { 169 return (DocumentSection) sections[i]; 170 } 171 } 172 return null; 173 } 174 175 private void transformText(String text) { 176 if (text == null) 177 return; 178 boolean preformatted = false; 179 boolean inTag = false; 180 boolean inCstring = false; 181 182 for (int i = 0; i < text.length(); i++) { 183 char c = text.charAt(i); 184 if (c == '<') { 185 if (isPreStart(text, i)) { 186 fWriter.print("<pre>"); i += 4; 188 preformatted = true; 189 continue; 190 } 191 if (isPreEnd(text, i)) { 192 fWriter.print("</pre>"); i += 5; 194 preformatted = false; 195 inTag = false; 196 inCstring = false; 197 continue; 198 } 199 } 200 if (preformatted) { 201 switch (c) { 202 case '<' : 203 inTag = true; 204 fWriter.print("<p class=code id=tag>"); fWriter.print("<"); break; 207 case '>' : 208 fWriter.print(">"); fWriter.print("</p>"); inTag = false; 211 inCstring = false; 212 break; 213 case '&' : 214 fWriter.print("&"); break; 216 case '\'' : 217 fWriter.print("'"); break; 219 case '\"' : 220 if (inTag) { 221 if (inCstring) { 222 fWriter.print("""); fWriter.print("</p>"); fWriter.print("<p class=code id=tag>"); inCstring = false; 226 } else { 227 inCstring = true; 228 fWriter.print("<p class=code id=cstring>"); fWriter.print("""); } 231 } else { 232 fWriter.print("\""); } 234 break; 235 default : 236 fWriter.print(c); 237 } 238 } else 239 fWriter.print(c); 240 } 241 } 242 243 private void transformDescription() { 244 fWriter.println("<p>"); fWriter.print("<h6 class=CaptionFigColumn id=header>Description: </h6>"); transformText(fSchema.getDescription()); 247 ISchemaInclude[] includes = fSchema.getIncludes(); 248 for (int i = 0; i < includes.length; i++) { 249 ISchema ischema = includes[i].getIncludedSchema(); 250 if (ischema != null) { 251 fWriter.println("<p>"); transformText(ischema.getDescription()); 253 } 254 } 255 fWriter.println("</p>"); } 257 258 private void transformMarkup() { 259 ISchemaElement[] elements = fSchema.getResolvedElements(); 260 for (int i = 0; i < elements.length; i++) { 261 transformElement(elements[i]); 262 } 263 } 264 265 private void transformElement(ISchemaElement element) { 266 String name = element.getName(); 267 String dtd = element.getDTDRepresentation(true); 268 String nameLink = "<a name=\"e." + name + "\">" + name + "</a>"; 270 if (element.isDeprecated() && !(element instanceof SchemaRootElement)) 271 fWriter.print("<div style=\"color: red; font-style: italic;\">The <b>" + name + "</b> element is deprecated</div> "); 273 fWriter.print( 274 "<p class=code id=dtd><!ELEMENT " + nameLink 276 + " " + dtd); 278 fWriter.println("></p>"); 280 ISchemaAttribute[] attributes = element.getAttributes(); 281 282 if (attributes.length > 0) { 283 fWriter.println( 284 "<p class=code id=dtd><!ATTLIST " + name 286 + "</p>"); int maxWidth = calculateMaxAttributeWidth(element.getAttributes()); 288 for (int i = 0; i < attributes.length; i++) { 289 appendAttlist(attributes[i], maxWidth); 290 } 291 fWriter.println("></p>"); 293 } 294 fWriter.println("<p></p>"); 296 String description = element.getDescription(); 298 299 if (description != null && description.trim().length() > 0) { 300 fWriter.println("<p class=ConfigMarkup id=elementDesc>"); transformText(description); 302 fWriter.println("</p>"); } 304 if (attributes.length == 0){ 306 fWriter.println("<br><br>"); return; 308 } else if (description != null && description.trim().length() > 0){ 309 fWriter.println("<br>"); } 311 312 fWriter.println("<ul class=ConfigMarkup id=attlistDesc>"); for (int i = 0; i < attributes.length; i++) { 314 ISchemaAttribute att = attributes[i]; 315 if (name.equals("extension")) { if (att.getDescription() == null 317 || att.getDescription().trim().length() == 0) { 318 continue; 319 } 320 } 321 fWriter.print("<li>"); if (att.isDeprecated()) 323 fWriter.print("<i style=\"color: red;\">Deprecated</i> "); fWriter.print("<b>" + att.getName() + "</b> - "); transformText(att.getDescription()); 326 fWriter.println("</li>"); } 328 fWriter.println("</ul>"); fWriter.print("<br>"); } 332 333 private void appendAttlist(ISchemaAttribute att, int maxWidth) { 334 fWriter.print("<p class=code id=dtdAttlist>"); fWriter.print(att.getName()); 337 int delta = maxWidth - att.getName().length(); 339 for (int i = 0; i < delta + 1; i++) { 340 fWriter.print(" "); } 342 ISchemaSimpleType type = att.getType(); 344 ISchemaRestriction restriction = null; 345 boolean choices = false; 346 if (type != null) 347 restriction = type.getRestriction(); 348 String typeName = 349 type != null ? type.getName().toLowerCase(Locale.ENGLISH) : "string"; if (typeName.equals("boolean")) { fWriter.print("(true | false) "); choices = true; 353 } else if (restriction != null) { 354 appendRestriction(restriction); 355 choices = true; 356 } else { 357 fWriter.print("CDATA "); } 359 360 if (att.getUse() == ISchemaAttribute.REQUIRED) { 362 if (!choices) 363 fWriter.print("#REQUIRED"); } else if (att.getUse() == ISchemaAttribute.DEFAULT) { 365 fWriter.print("\"" + att.getValue() + "\""); } else if (!choices) 367 fWriter.print("#IMPLIED"); } 369 370 private void appendRestriction(ISchemaRestriction restriction) { 371 if (restriction instanceof ChoiceRestriction) { 372 String [] choices = ((ChoiceRestriction) restriction).getChoicesAsStrings(); 373 fWriter.print("("); for (int i = 0; i < choices.length; i++) { 375 if (i > 0) 376 fWriter.print("|"); fWriter.print(choices[i]); 378 } 379 fWriter.print(") "); } 381 } 382 383 private boolean isPreEnd(String text, int loc) { 384 if (loc + 5 >= text.length()) 385 return false; 386 return (text.substring(loc, loc + 6).toLowerCase(Locale.ENGLISH).equals("</pre>")); } 388 389 private boolean isPreStart(String text, int loc) { 390 if (loc + 4 >= text.length()) 391 return false; 392 return (text.substring(loc, loc + 5).toLowerCase(Locale.ENGLISH).equals("<pre>")); } 394 395 private int calculateMaxAttributeWidth(ISchemaAttribute[] attributes) { 396 int width = 0; 397 for (int i = 0; i < attributes.length; i++) { 398 width = Math.max(width, attributes[i].getName().length()); 399 } 400 return width; 401 } 402 403 private String getProductPlugin() { 404 IProduct product = Platform.getProduct(); 405 if (product != null) { 406 Bundle plugin = product.getDefiningBundle(); 407 if (plugin != null) { 408 return plugin.getSymbolicName(); 409 } 410 } 411 return PLATFORM_PLUGIN; 412 } 413 } 414 | Popular Tags |