KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.builders;
12
13 import java.io.*;
14 import java.net.*;
15 import java.util.Locale JavaDoc;
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 JavaDoc PLATFORM_PLUGIN = "org.eclipse.platform"; //$NON-NLS-1$
25
private static final String JavaDoc PLATFORM_PLUGIN_DOC = "org.eclipse.platform.doc.isv"; //$NON-NLS-1$
26
private static final String JavaDoc SCHEMA_CSS = "schema.css"; //$NON-NLS-1$
27
private static final String JavaDoc PLATFORM_CSS = "book.css"; //$NON-NLS-1$
28

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 JavaDoc getCssURL() {
60         return (fCssURL != null) ? fCssURL.toString() : "../../" + PLATFORM_CSS; //$NON-NLS-1$
61
}
62     
63     private String JavaDoc getSchemaCssURL() {
64         if (fCssPurpose == BUILD)
65             return "../../" + SCHEMA_CSS; //$NON-NLS-1$
66
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\">"); //$NON-NLS-1$
71
fWriter.println("<HTML>"); //$NON-NLS-1$
72
printHeader();
73         printBody();
74         fWriter.println("</HTML>"); //$NON-NLS-1$
75
}
76     
77     private void printHeader() {
78         fWriter.print("<HEAD>"); //$NON-NLS-1$
79
fWriter.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">"); //$NON-NLS-1$
80
fWriter.println("<title>" + fSchema.getName() + "</title>"); //$NON-NLS-1$ //$NON-NLS-2$
81
printStyles();
82         fWriter.println("</HEAD>"); //$NON-NLS-1$
83
}
84
85     private void printStyles() {
86         fWriter.println("<style>@import url(\"" + getCssURL() + "\");</style>"); //$NON-NLS-1$ //$NON-NLS-2$
87
fWriter.println("<style>@import url(\"" + getSchemaCssURL() + "\");</style>"); //$NON-NLS-1$ //$NON-NLS-2$
88
}
89     
90     private URL getResourceURL(String JavaDoc bundleID, String JavaDoc 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>"); //$NON-NLS-1$
105
fWriter.println("<H1><CENTER>" + fSchema.getName() + "</CENTER></H1>"); //$NON-NLS-1$ //$NON-NLS-2$
106
fWriter.println("<p></p>"); //$NON-NLS-1$
107
fWriter.print("<h6 class=CaptionFigColumn id=header>Identifier: </h6>"); //$NON-NLS-1$
108
fWriter.print(fSchema.getQualifiedPointId());
109         fWriter.println("<p></p>"); //$NON-NLS-1$
110
transformSection("Since:", IDocumentSection.SINCE); //$NON-NLS-1$
111
transformDescription();
112         fWriter.println("<p><h6 class=CaptionFigColumn id=header>Configuration Markup:</h6></p>"); //$NON-NLS-1$
113
transformMarkup();
114         transformSection("Examples:", IDocumentSection.EXAMPLES); //$NON-NLS-1$
115
transformSection("API Information:", IDocumentSection.API_INFO); //$NON-NLS-1$
116
transformSection("Supplied Implementation:", IDocumentSection.IMPLEMENTATION); //$NON-NLS-1$
117
fWriter.println("<br>"); //$NON-NLS-1$
118
fWriter.println("<p class=note id=copyright>"); //$NON-NLS-1$
119
transformSection(null, IDocumentSection.COPYRIGHT);
120         fWriter.println("</p>"); //$NON-NLS-1$
121
fWriter.println("</BODY>"); //$NON-NLS-1$
122
}
123     
124     private void transformSection(String JavaDoc title, String JavaDoc sectionId) {
125         IDocumentSection section = findSection(fSchema.getDocumentSections(), sectionId);
126         if (section == null)
127             return;
128         String JavaDoc 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>"); //$NON-NLS-1$ //$NON-NLS-2$
133
transformText(description);
134         fWriter.println();
135         fWriter.println("<p></p>"); //$NON-NLS-1$
136
fWriter.println();
137     }
138
139     private DocumentSection findSection(IDocumentSection[] sections, String JavaDoc 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 JavaDoc 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>"); //$NON-NLS-1$
160
i += 4;
161                     preformatted = true;
162                     continue;
163                 }
164                 if (isPreEnd(text, i)) {
165                     fWriter.print("</pre>"); //$NON-NLS-1$
166
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>"); //$NON-NLS-1$
178
fWriter.print("&lt;"); //$NON-NLS-1$
179
break;
180                     case '>' :
181                         fWriter.print("&gt;"); //$NON-NLS-1$
182
fWriter.print("</p>"); //$NON-NLS-1$
183
inTag = false;
184                         inCstring = false;
185                         break;
186                     case '&' :
187                         fWriter.print("&amp;"); //$NON-NLS-1$
188
break;
189                     case '\'' :
190                         fWriter.print("&apos;"); //$NON-NLS-1$
191
break;
192                     case '\"' :
193                         if (inTag) {
194                             if (inCstring) {
195                                 fWriter.print("&quot;"); //$NON-NLS-1$
196
fWriter.print("</p>"); //$NON-NLS-1$
197
fWriter.print("<p class=code id=tag>"); //$NON-NLS-1$
198
inCstring = false;
199                             } else {
200                                 inCstring = true;
201                                 fWriter.print("<p class=code id=cstring>"); //$NON-NLS-1$
202
fWriter.print("&quot;"); //$NON-NLS-1$
203
}
204                         } else {
205                             fWriter.print("\""); //$NON-NLS-1$
206
}
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>"); //$NON-NLS-1$
218
fWriter.print("<h6 class=CaptionFigColumn id=header>Description: </h6>"); //$NON-NLS-1$
219
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>"); //$NON-NLS-1$
225
transformText(ischema.getDescription());
226             }
227         }
228         fWriter.println("</p>"); //$NON-NLS-1$
229
}
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 JavaDoc name = element.getName();
240         String JavaDoc dtd = element.getDTDRepresentation(true);
241         String JavaDoc nameLink = "<a name=\"e." + name + "\">" + name + "</a>"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
242

243         fWriter.print(
244             "<p class=code id=dtd>&lt;!ELEMENT " //$NON-NLS-1$
245
+ nameLink
246                 + " " //$NON-NLS-1$
247
+ dtd);
248         fWriter.println("&gt;</p>"); //$NON-NLS-1$
249

250         ISchemaAttribute[] attributes = element.getAttributes();
251
252         if (attributes.length > 0) {
253             fWriter.println(
254                 "<p class=code id=dtd>&lt;!ATTLIST " //$NON-NLS-1$
255
+ name
256                     + "</p>"); //$NON-NLS-1$
257
int maxWidth = calculateMaxAttributeWidth(element.getAttributes());
258             for (int i = 0; i < attributes.length; i++) {
259                 appendAttlist(attributes[i], maxWidth);
260             }
261             fWriter.println("&gt;</p>"); //$NON-NLS-1$
262

263         }
264         fWriter.println("<p></p>"); //$NON-NLS-1$
265

266         // inserted desc here for element
267
String JavaDoc description = element.getDescription();
268
269         if (description != null && description.trim().length() > 0) {
270             fWriter.println("<p class=ConfigMarkup id=elementDesc>"); //$NON-NLS-1$
271
transformText(description);
272             fWriter.println("</p>"); //$NON-NLS-1$
273
}
274         // end of inserted desc for element
275
if (attributes.length == 0){
276             fWriter.println("<br><br>"); //$NON-NLS-1$
277
return;
278         } else if (description != null && description.trim().length() > 0){
279             fWriter.println("<br>"); //$NON-NLS-1$
280
}
281         
282         fWriter.println("<ul class=ConfigMarkup id=attlistDesc>"); //$NON-NLS-1$
283
for (int i = 0; i < attributes.length; i++) {
284             ISchemaAttribute att = attributes[i];
285             if (name.equals("extension")) { //$NON-NLS-1$
286
if (att.getDescription() == null
287                     || att.getDescription().trim().length() == 0) {
288                     continue;
289                 }
290             }
291             fWriter.print("<li><b>" + att.getName() + "</b> - "); //$NON-NLS-1$ //$NON-NLS-2$
292
transformText(att.getDescription());
293             fWriter.println("</li>"); //$NON-NLS-1$
294
}
295         fWriter.println("</ul>"); //$NON-NLS-1$
296
// adding spaces for new shifted view
297
fWriter.print("<br>"); //$NON-NLS-1$
298
}
299     
300     private void appendAttlist(ISchemaAttribute att, int maxWidth) {
301         fWriter.print("<p class=code id=dtdAttlist>"); //$NON-NLS-1$
302
// add name
303
fWriter.print(att.getName());
304         // fill spaces to align data type
305
int delta = maxWidth - att.getName().length();
306         for (int i = 0; i < delta + 1; i++) {
307             fWriter.print("&nbsp;"); //$NON-NLS-1$
308
}
309         // add data type
310
ISchemaSimpleType type = att.getType();
311         ISchemaRestriction restriction = null;
312         boolean choices = false;
313         if (type != null)
314             restriction = type.getRestriction();
315         String JavaDoc typeName =
316             type != null ? type.getName().toLowerCase(Locale.ENGLISH) : "string"; //$NON-NLS-1$
317
if (typeName.equals("boolean")) { //$NON-NLS-1$
318
fWriter.print("(true | false) "); //$NON-NLS-1$
319
choices = true;
320         } else if (restriction != null) {
321             appendRestriction(restriction);
322             choices = true;
323         } else {
324             fWriter.print("CDATA "); //$NON-NLS-1$
325
}
326
327         // add use
328
if (att.getUse() == ISchemaAttribute.REQUIRED) {
329             if (!choices)
330                 fWriter.print("#REQUIRED"); //$NON-NLS-1$
331
} else if (att.getUse() == ISchemaAttribute.DEFAULT) {
332             fWriter.print("\"" + att.getValue() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
333
} else if (!choices)
334             fWriter.print("#IMPLIED"); //$NON-NLS-1$
335
}
336     
337     private void appendRestriction(ISchemaRestriction restriction) {
338         if (restriction instanceof ChoiceRestriction) {
339             String JavaDoc[] choices = ((ChoiceRestriction) restriction).getChoicesAsStrings();
340             fWriter.print("("); //$NON-NLS-1$
341
for (int i = 0; i < choices.length; i++) {
342                 if (i > 0)
343                     fWriter.print("|"); //$NON-NLS-1$
344
fWriter.print(choices[i]);
345             }
346             fWriter.print(") "); //$NON-NLS-1$
347
}
348     }
349     
350     private boolean isPreEnd(String JavaDoc text, int loc) {
351         if (loc + 5 >= text.length())
352             return false;
353         return (text.substring(loc, loc + 6).toLowerCase(Locale.ENGLISH).equals("</pre>")); //$NON-NLS-1$
354
}
355     
356     private boolean isPreStart(String JavaDoc text, int loc) {
357         if (loc + 4 >= text.length())
358             return false;
359         return (text.substring(loc, loc + 5).toLowerCase(Locale.ENGLISH).equals("<pre>")); //$NON-NLS-1$
360
}
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 JavaDoc 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