1 11 package org.eclipse.pde.internal.builders; 12 13 import java.io.*; 14 import java.net.*; 15 import java.util.Locale ; 16 17 import org.eclipse.core.runtime.*; 18 import org.eclipse.pde.internal.core.ischema.*; 19 import org.eclipse.pde.internal.core.schema.*; 20 import org.osgi.framework.*; 21 22 public class SchemaTransformer { 23 24 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"; 29 public static final byte TEMP = 0x00; 30 public static final byte BUILD = 0x01; 31 32 private byte fCssPurpose; 33 private PrintWriter fWriter; 34 private ISchema fSchema; 35 private URL fCssURL; 36 37 public void transform(ISchema schema, PrintWriter out) { 38 transform(schema, out, null, TEMP); 39 } 40 41 public void transform(ISchema schema, PrintWriter out, URL cssURL, byte cssPurpose) { 42 fSchema = schema; 43 fWriter = out; 44 fCssPurpose = cssPurpose; 45 setCssURL(cssURL); 46 printHTMLContent(); 47 } 48 49 private void setCssURL(URL cssURL) { 50 try { 51 if (cssURL != null) 52 fCssURL = Platform.resolve(cssURL); 53 } catch (IOException e) { 54 } 55 if (fCssURL == null && fCssPurpose != BUILD) 56 fCssURL = getResourceURL(getProductPlugin(), PLATFORM_CSS); 57 } 58 59 private String getCssURL() { 60 return (fCssURL != null) ? fCssURL.toString() : "../../" + PLATFORM_CSS; } 62 63 private String getSchemaCssURL() { 64 if (fCssPurpose == BUILD) 65 return "../../" + SCHEMA_CSS; return getResourceURL(PLATFORM_PLUGIN_DOC, SCHEMA_CSS).toString(); 67 } 68 69 private void printHTMLContent() { 70 fWriter.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">"); fWriter.println("<HTML>"); printHeader(); 73 printBody(); 74 fWriter.println("</HTML>"); } 76 77 private void printHeader() { 78 fWriter.print("<HEAD>"); fWriter.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">"); fWriter.println("<title>" + fSchema.getName() + "</title>"); printStyles(); 82 fWriter.println("</HEAD>"); } 84 85 private void printStyles() { 86 fWriter.println("<style>@import url(\"" + getCssURL() + "\");</style>"); fWriter.println("<style>@import url(\"" + getSchemaCssURL() + "\");</style>"); } 89 90 private URL getResourceURL(String bundleID, String resourcePath) { 91 try { 92 Bundle bundle = Platform.getBundle(bundleID); 93 if (bundle != null) { 94 URL entry = bundle.getEntry(resourcePath); 95 if (entry != null) 96 return Platform.resolve(entry); 97 } 98 } catch (IOException e) { 99 } 100 return null; 101 } 102 103 private void printBody() { 104 fWriter.println("<BODY>"); fWriter.println("<H1><CENTER>" + fSchema.getName() + "</CENTER></H1>"); fWriter.println("<p></p>"); fWriter.print("<h6 class=CaptionFigColumn id=header>Identifier: </h6>"); fWriter.print(fSchema.getQualifiedPointId()); 109 fWriter.println("<p></p>"); transformSection("Since:", IDocumentSection.SINCE); transformDescription(); 112 fWriter.println("<p><h6 class=CaptionFigColumn id=header>Configuration Markup:</h6></p>"); transformMarkup(); 114 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); 120 fWriter.println("</p>"); fWriter.println("</BODY>"); } 123 124 private void transformSection(String title, String sectionId) { 125 IDocumentSection section = findSection(fSchema.getDocumentSections(), sectionId); 126 if (section == null) 127 return; 128 String description = section.getDescription(); 129 if (description == null || description.trim().length() == 0) 130 return; 131 if (title != null) 132 fWriter.print("<h6 class=CaptionFigColumn id=header>" + title + " </h6>"); transformText(description); 134 fWriter.println(); 135 fWriter.println("<p></p>"); fWriter.println(); 137 } 138 139 private DocumentSection findSection(IDocumentSection[] sections, String sectionId) { 140 for (int i = 0; i < sections.length; i++) { 141 if (sections[i].getSectionId().equals(sectionId)) { 142 return (DocumentSection) sections[i]; 143 } 144 } 145 return null; 146 } 147 148 private void transformText(String text) { 149 if (text == null) 150 return; 151 boolean preformatted = false; 152 boolean inTag = false; 153 boolean inCstring = false; 154 155 for (int i = 0; i < text.length(); i++) { 156 char c = text.charAt(i); 157 if (c == '<') { 158 if (isPreStart(text, i)) { 159 fWriter.print("<pre>"); i += 4; 161 preformatted = true; 162 continue; 163 } 164 if (isPreEnd(text, i)) { 165 fWriter.print("</pre>"); i += 5; 167 preformatted = false; 168 inTag = false; 169 inCstring = false; 170 continue; 171 } 172 } 173 if (preformatted) { 174 switch (c) { 175 case '<' : 176 inTag = true; 177 fWriter.print("<p class=code id=tag>"); fWriter.print("<"); break; 180 case '>' : 181 fWriter.print(">"); fWriter.print("</p>"); inTag = false; 184 inCstring = false; 185 break; 186 case '&' : 187 fWriter.print("&"); break; 189 case '\'' : 190 fWriter.print("'"); break; 192 case '\"' : 193 if (inTag) { 194 if (inCstring) { 195 fWriter.print("""); fWriter.print("</p>"); fWriter.print("<p class=code id=tag>"); inCstring = false; 199 } else { 200 inCstring = true; 201 fWriter.print("<p class=code id=cstring>"); fWriter.print("""); } 204 } else { 205 fWriter.print("\""); } 207 break; 208 default : 209 fWriter.print(c); 210 } 211 } else 212 fWriter.print(c); 213 } 214 } 215 216 private void transformDescription() { 217 fWriter.println("<p>"); fWriter.print("<h6 class=CaptionFigColumn id=header>Description: </h6>"); transformText(fSchema.getDescription()); 220 ISchemaInclude[] includes = fSchema.getIncludes(); 221 for (int i = 0; i < includes.length; i++) { 222 ISchema ischema = includes[i].getIncludedSchema(); 223 if (ischema != null) { 224 fWriter.println("<p>"); transformText(ischema.getDescription()); 226 } 227 } 228 fWriter.println("</p>"); } 230 231 private void transformMarkup() { 232 ISchemaElement[] elements = fSchema.getResolvedElements(); 233 for (int i = 0; i < elements.length; i++) { 234 transformElement(elements[i]); 235 } 236 } 237 238 private void transformElement(ISchemaElement element) { 239 String name = element.getName(); 240 String dtd = element.getDTDRepresentation(true); 241 String nameLink = "<a name=\"e." + name + "\">" + name + "</a>"; 243 fWriter.print( 244 "<p class=code id=dtd><!ELEMENT " + nameLink 246 + " " + dtd); 248 fWriter.println("></p>"); 250 ISchemaAttribute[] attributes = element.getAttributes(); 251 252 if (attributes.length > 0) { 253 fWriter.println( 254 "<p class=code id=dtd><!ATTLIST " + name 256 + "</p>"); int maxWidth = calculateMaxAttributeWidth(element.getAttributes()); 258 for (int i = 0; i < attributes.length; i++) { 259 appendAttlist(attributes[i], maxWidth); 260 } 261 fWriter.println("></p>"); 263 } 264 fWriter.println("<p></p>"); 266 String description = element.getDescription(); 268 269 if (description != null && description.trim().length() > 0) { 270 fWriter.println("<p class=ConfigMarkup id=elementDesc>"); transformText(description); 272 fWriter.println("</p>"); } 274 if (attributes.length == 0){ 276 fWriter.println("<br><br>"); return; 278 } else if (description != null && description.trim().length() > 0){ 279 fWriter.println("<br>"); } 281 282 fWriter.println("<ul class=ConfigMarkup id=attlistDesc>"); for (int i = 0; i < attributes.length; i++) { 284 ISchemaAttribute att = attributes[i]; 285 if (name.equals("extension")) { if (att.getDescription() == null 287 || att.getDescription().trim().length() == 0) { 288 continue; 289 } 290 } 291 fWriter.print("<li><b>" + att.getName() + "</b> - "); transformText(att.getDescription()); 293 fWriter.println("</li>"); } 295 fWriter.println("</ul>"); fWriter.print("<br>"); } 299 300 private void appendAttlist(ISchemaAttribute att, int maxWidth) { 301 fWriter.print("<p class=code id=dtdAttlist>"); fWriter.print(att.getName()); 304 int delta = maxWidth - att.getName().length(); 306 for (int i = 0; i < delta + 1; i++) { 307 fWriter.print(" "); } 309 ISchemaSimpleType type = att.getType(); 311 ISchemaRestriction restriction = null; 312 boolean choices = false; 313 if (type != null) 314 restriction = type.getRestriction(); 315 String typeName = 316 type != null ? type.getName().toLowerCase(Locale.ENGLISH) : "string"; if (typeName.equals("boolean")) { fWriter.print("(true | false) "); choices = true; 320 } else if (restriction != null) { 321 appendRestriction(restriction); 322 choices = true; 323 } else { 324 fWriter.print("CDATA "); } 326 327 if (att.getUse() == ISchemaAttribute.REQUIRED) { 329 if (!choices) 330 fWriter.print("#REQUIRED"); } else if (att.getUse() == ISchemaAttribute.DEFAULT) { 332 fWriter.print("\"" + att.getValue() + "\""); } else if (!choices) 334 fWriter.print("#IMPLIED"); } 336 337 private void appendRestriction(ISchemaRestriction restriction) { 338 if (restriction instanceof ChoiceRestriction) { 339 String [] choices = ((ChoiceRestriction) restriction).getChoicesAsStrings(); 340 fWriter.print("("); for (int i = 0; i < choices.length; i++) { 342 if (i > 0) 343 fWriter.print("|"); fWriter.print(choices[i]); 345 } 346 fWriter.print(") "); } 348 } 349 350 private boolean isPreEnd(String text, int loc) { 351 if (loc + 5 >= text.length()) 352 return false; 353 return (text.substring(loc, loc + 6).toLowerCase(Locale.ENGLISH).equals("</pre>")); } 355 356 private boolean isPreStart(String text, int loc) { 357 if (loc + 4 >= text.length()) 358 return false; 359 return (text.substring(loc, loc + 5).toLowerCase(Locale.ENGLISH).equals("<pre>")); } 361 362 private int calculateMaxAttributeWidth(ISchemaAttribute[] attributes) { 363 int width = 0; 364 for (int i = 0; i < attributes.length; i++) { 365 width = Math.max(width, attributes[i].getName().length()); 366 } 367 return width; 368 } 369 370 private String getProductPlugin() { 371 IProduct product = Platform.getProduct(); 372 if (product != null) { 373 Bundle plugin = product.getDefiningBundle(); 374 if (plugin != null) { 375 return plugin.getSymbolicName(); 376 } 377 } 378 return PLATFORM_PLUGIN; 379 } 380 } 381 | Popular Tags |