KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > builders > SchemaTransformer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.builders;
12
13 import java.io.IOException JavaDoc;
14 import java.io.PrintWriter JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.Locale JavaDoc;
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 JavaDoc PLATFORM_PLUGIN = "org.eclipse.platform"; //$NON-NLS-1$
37
private static final String JavaDoc PLATFORM_PLUGIN_DOC = "org.eclipse.platform.doc.isv"; //$NON-NLS-1$
38
private static final String JavaDoc SCHEMA_CSS = "schema.css"; //$NON-NLS-1$
39
private static final String JavaDoc PLATFORM_CSS = "book.css"; //$NON-NLS-1$
40

41     public static final byte TEMP = 0x00;
42     public static final byte BUILD = 0x01;
43     
44     private byte fCssPurpose;
45     private PrintWriter JavaDoc fWriter;
46     private ISchema fSchema;
47     private URL JavaDoc fCssURL;
48
49     public void transform(ISchema schema, PrintWriter JavaDoc out) {
50         transform(schema, out, null, TEMP);
51     }
52         
53     public void transform(ISchema schema, PrintWriter JavaDoc out, URL JavaDoc cssURL, byte cssPurpose) {
54         fSchema = schema;
55         fWriter = out;
56         fCssPurpose = cssPurpose;
57         setCssURL(cssURL);
58         printHTMLContent();
59     }
60     
61     private void setCssURL(URL JavaDoc cssURL) {
62         try {
63             if (cssURL != null)
64                 fCssURL = FileLocator.resolve(cssURL);
65         } catch (IOException JavaDoc 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 JavaDoc getCssURL() {
74         return (fCssURL != null) ? fCssURL.toString() : "../../" + PLATFORM_CSS; //$NON-NLS-1$
75
}
76     
77     private String JavaDoc getSchemaCssURL() {
78         if (fCssPurpose == BUILD)
79             return "../../" + SCHEMA_CSS; //$NON-NLS-1$
80
URL JavaDoc url = getResourceURL(PLATFORM_PLUGIN_DOC, SCHEMA_CSS);
81         if (url == null) {
82             // this CSS file is last resort and is always there.
83
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\">"); //$NON-NLS-1$
90
fWriter.println("<HTML>"); //$NON-NLS-1$
91
printHeader();
92         printBody();
93         fWriter.println("</HTML>"); //$NON-NLS-1$
94
}
95     
96     private void printHeader() {
97         fWriter.print("<HEAD>"); //$NON-NLS-1$
98
fWriter.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">"); //$NON-NLS-1$
99
fWriter.println("<title>" + fSchema.getName() + "</title>"); //$NON-NLS-1$ //$NON-NLS-2$
100
printStyles();
101         fWriter.println("</HEAD>"); //$NON-NLS-1$
102
}
103
104     private void printStyles() {
105         fWriter.println("<style>@import url(\"" + getCssURL() + "\");</style>"); //$NON-NLS-1$ //$NON-NLS-2$
106
fWriter.println("<style>@import url(\"" + getSchemaCssURL() + "\");</style>"); //$NON-NLS-1$ //$NON-NLS-2$
107
}
108     
109     private URL JavaDoc getResourceURL(String JavaDoc bundleID, String JavaDoc resourcePath) {
110         try {
111             Bundle bundle = Platform.getBundle(bundleID);
112             if (bundle != null) {
113                 URL JavaDoc entry = bundle.getEntry(resourcePath);
114                 if (entry != null)
115                     return FileLocator.toFileURL(entry);
116             }
117         } catch (IOException JavaDoc e) {
118         }
119         return null;
120     }
121     
122     private void printBody() {
123         fWriter.println("<BODY>"); //$NON-NLS-1$
124
fWriter.println("<H1><CENTER>" + fSchema.getName() + "</CENTER></H1>"); //$NON-NLS-1$ //$NON-NLS-2$
125
if (fSchema.isDeperecated()) {
126             fWriter.print("<div style=\"border: 1px solid #990000; padding: 5px; text-align: center; color: red;\">"); //$NON-NLS-1$
127
fWriter.print("This extension point is deprecated"); //$NON-NLS-1$
128
String JavaDoc suggestion = fSchema.getDeprecatedSuggestion();
129             if (suggestion != null)
130                 fWriter.print(", use <i>" + suggestion + "</i> as a replacement."); //$NON-NLS-1$ //$NON-NLS-2$
131
fWriter.println("</div>"); //$NON-NLS-1$
132
}
133         fWriter.println("<p></p>"); //$NON-NLS-1$
134
fWriter.print("<h6 class=CaptionFigColumn id=header>Identifier: </h6>"); //$NON-NLS-1$
135
fWriter.print(fSchema.getQualifiedPointId());
136         fWriter.println("<p></p>"); //$NON-NLS-1$
137
transformSection("Since:", IDocumentSection.SINCE); //$NON-NLS-1$
138
transformDescription();
139         fWriter.println("<p><h6 class=CaptionFigColumn id=header>Configuration Markup:</h6></p>"); //$NON-NLS-1$
140
transformMarkup();
141         transformSection("Examples:", IDocumentSection.EXAMPLES); //$NON-NLS-1$
142
transformSection("API Information:", IDocumentSection.API_INFO); //$NON-NLS-1$
143
transformSection("Supplied Implementation:", IDocumentSection.IMPLEMENTATION); //$NON-NLS-1$
144
fWriter.println("<br>"); //$NON-NLS-1$
145
fWriter.println("<p class=note id=copyright>"); //$NON-NLS-1$
146
transformSection(null, IDocumentSection.COPYRIGHT);
147         fWriter.println("</p>"); //$NON-NLS-1$
148
fWriter.println("</BODY>"); //$NON-NLS-1$
149
}
150     
151     private void transformSection(String JavaDoc title, String JavaDoc sectionId) {
152         IDocumentSection section = findSection(fSchema.getDocumentSections(), sectionId);
153         if (section == null)
154             return;
155         String JavaDoc 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>"); //$NON-NLS-1$ //$NON-NLS-2$
160
transformText(description);
161         fWriter.println();
162         fWriter.println("<p></p>"); //$NON-NLS-1$
163
fWriter.println();
164     }
165
166     private DocumentSection findSection(IDocumentSection[] sections, String JavaDoc 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 JavaDoc 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>"); //$NON-NLS-1$
187
i += 4;
188                     preformatted = true;
189                     continue;
190                 }
191                 if (isPreEnd(text, i)) {
192                     fWriter.print("</pre>"); //$NON-NLS-1$
193
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>"); //$NON-NLS-1$
205
fWriter.print("&lt;"); //$NON-NLS-1$
206
break;
207                     case '>' :
208                         fWriter.print("&gt;"); //$NON-NLS-1$
209
fWriter.print("</p>"); //$NON-NLS-1$
210
inTag = false;
211                         inCstring = false;
212                         break;
213                     case '&' :
214                         fWriter.print("&amp;"); //$NON-NLS-1$
215
break;
216                     case '\'' :
217                         fWriter.print("&apos;"); //$NON-NLS-1$
218
break;
219                     case '\"' :
220                         if (inTag) {
221                             if (inCstring) {
222                                 fWriter.print("&quot;"); //$NON-NLS-1$
223
fWriter.print("</p>"); //$NON-NLS-1$
224
fWriter.print("<p class=code id=tag>"); //$NON-NLS-1$
225
inCstring = false;
226                             } else {
227                                 inCstring = true;
228                                 fWriter.print("<p class=code id=cstring>"); //$NON-NLS-1$
229
fWriter.print("&quot;"); //$NON-NLS-1$
230
}
231                         } else {
232                             fWriter.print("\""); //$NON-NLS-1$
233
}
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>"); //$NON-NLS-1$
245
fWriter.print("<h6 class=CaptionFigColumn id=header>Description: </h6>"); //$NON-NLS-1$
246
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>"); //$NON-NLS-1$
252
transformText(ischema.getDescription());
253             }
254         }
255         fWriter.println("</p>"); //$NON-NLS-1$
256
}
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 JavaDoc name = element.getName();
267         String JavaDoc dtd = element.getDTDRepresentation(true);
268         String JavaDoc nameLink = "<a name=\"e." + name + "\">" + name + "</a>"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
269

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> "); //$NON-NLS-1$ //$NON-NLS-2$
272

273         fWriter.print(
274             "<p class=code id=dtd>&lt;!ELEMENT " //$NON-NLS-1$
275
+ nameLink
276                 + " " //$NON-NLS-1$
277
+ dtd);
278         fWriter.println("&gt;</p>"); //$NON-NLS-1$
279

280         ISchemaAttribute[] attributes = element.getAttributes();
281
282         if (attributes.length > 0) {
283             fWriter.println(
284                 "<p class=code id=dtd>&lt;!ATTLIST " //$NON-NLS-1$
285
+ name
286                     + "</p>"); //$NON-NLS-1$
287
int maxWidth = calculateMaxAttributeWidth(element.getAttributes());
288             for (int i = 0; i < attributes.length; i++) {
289                 appendAttlist(attributes[i], maxWidth);
290             }
291             fWriter.println("&gt;</p>"); //$NON-NLS-1$
292

293         }
294         fWriter.println("<p></p>"); //$NON-NLS-1$
295

296         // inserted desc here for element
297
String JavaDoc description = element.getDescription();
298
299         if (description != null && description.trim().length() > 0) {
300             fWriter.println("<p class=ConfigMarkup id=elementDesc>"); //$NON-NLS-1$
301
transformText(description);
302             fWriter.println("</p>"); //$NON-NLS-1$
303
}
304         // end of inserted desc for element
305
if (attributes.length == 0){
306             fWriter.println("<br><br>"); //$NON-NLS-1$
307
return;
308         } else if (description != null && description.trim().length() > 0){
309             fWriter.println("<br>"); //$NON-NLS-1$
310
}
311         
312         fWriter.println("<ul class=ConfigMarkup id=attlistDesc>"); //$NON-NLS-1$
313
for (int i = 0; i < attributes.length; i++) {
314             ISchemaAttribute att = attributes[i];
315             if (name.equals("extension")) { //$NON-NLS-1$
316
if (att.getDescription() == null
317                     || att.getDescription().trim().length() == 0) {
318                     continue;
319                 }
320             }
321             fWriter.print("<li>"); //$NON-NLS-1$
322
if (att.isDeprecated())
323                 fWriter.print("<i style=\"color: red;\">Deprecated</i> "); //$NON-NLS-1$
324
fWriter.print("<b>" + att.getName() + "</b> - "); //$NON-NLS-1$ //$NON-NLS-2$
325
transformText(att.getDescription());
326             fWriter.println("</li>"); //$NON-NLS-1$
327
}
328         fWriter.println("</ul>"); //$NON-NLS-1$
329
// adding spaces for new shifted view
330
fWriter.print("<br>"); //$NON-NLS-1$
331
}
332     
333     private void appendAttlist(ISchemaAttribute att, int maxWidth) {
334         fWriter.print("<p class=code id=dtdAttlist>"); //$NON-NLS-1$
335
// add name
336
fWriter.print(att.getName());
337         // fill spaces to align data type
338
int delta = maxWidth - att.getName().length();
339         for (int i = 0; i < delta + 1; i++) {
340             fWriter.print("&nbsp;"); //$NON-NLS-1$
341
}
342         // add data type
343
ISchemaSimpleType type = att.getType();
344         ISchemaRestriction restriction = null;
345         boolean choices = false;
346         if (type != null)
347             restriction = type.getRestriction();
348         String JavaDoc typeName =
349             type != null ? type.getName().toLowerCase(Locale.ENGLISH) : "string"; //$NON-NLS-1$
350
if (typeName.equals("boolean")) { //$NON-NLS-1$
351
fWriter.print("(true | false) "); //$NON-NLS-1$
352
choices = true;
353         } else if (restriction != null) {
354             appendRestriction(restriction);
355             choices = true;
356         } else {
357             fWriter.print("CDATA "); //$NON-NLS-1$
358
}
359
360         // add use
361
if (att.getUse() == ISchemaAttribute.REQUIRED) {
362             if (!choices)
363                 fWriter.print("#REQUIRED"); //$NON-NLS-1$
364
} else if (att.getUse() == ISchemaAttribute.DEFAULT) {
365             fWriter.print("\"" + att.getValue() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
366
} else if (!choices)
367             fWriter.print("#IMPLIED"); //$NON-NLS-1$
368
}
369     
370     private void appendRestriction(ISchemaRestriction restriction) {
371         if (restriction instanceof ChoiceRestriction) {
372             String JavaDoc[] choices = ((ChoiceRestriction) restriction).getChoicesAsStrings();
373             fWriter.print("("); //$NON-NLS-1$
374
for (int i = 0; i < choices.length; i++) {
375                 if (i > 0)
376                     fWriter.print("|"); //$NON-NLS-1$
377
fWriter.print(choices[i]);
378             }
379             fWriter.print(") "); //$NON-NLS-1$
380
}
381     }
382     
383     private boolean isPreEnd(String JavaDoc text, int loc) {
384         if (loc + 5 >= text.length())
385             return false;
386         return (text.substring(loc, loc + 6).toLowerCase(Locale.ENGLISH).equals("</pre>")); //$NON-NLS-1$
387
}
388     
389     private boolean isPreStart(String JavaDoc text, int loc) {
390         if (loc + 4 >= text.length())
391             return false;
392         return (text.substring(loc, loc + 5).toLowerCase(Locale.ENGLISH).equals("<pre>")); //$NON-NLS-1$
393
}
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 JavaDoc 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