KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > tools > generator > GenerateDOMScannerSupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xml.tools.generator;
20
21 import java.io.*;
22 import java.util.*;
23 import java.awt.BorderLayout JavaDoc;
24 import javax.swing.*;
25 import java.lang.reflect.Modifier JavaDoc;
26
27 import org.openide.*;
28 import org.openide.nodes.*;
29 //import org.openide.src.*;
30
import org.openide.filesystems.*;
31 import org.openide.loaders.*;
32 import org.openide.util.*;
33
34 import org.netbeans.tax.*;
35 import org.netbeans.tax.decl.*;
36 import org.netbeans.modules.xml.core.DTDDataObject;
37 import org.netbeans.modules.xml.core.lib.GuiUtil;
38 import org.netbeans.modules.xml.tax.cookies.TreeEditorCookie;
39
40 public class GenerateDOMScannerSupport implements XMLGenerateCookie {
41     
42     private static final String JavaDoc JAVA_EXT = "java"; // NOI18N
43

44     private static final String JavaDoc DOM_PACKAGE = "org.w3c.dom."; // NOI18N
45
private static final String JavaDoc DOM_DOCUMENT = DOM_PACKAGE + "Document"; // NOI18N
46
private static final String JavaDoc DOM_ELEMENT = DOM_PACKAGE + "Element"; // NOI18N
47
private static final String JavaDoc DOM_NAMED_NODE_MAP = DOM_PACKAGE + "NamedNodeMap"; // NOI18N
48

49     private static final String JavaDoc VARIABLE_DOCUMENT = "document"; // NOI18N
50
private static final String JavaDoc VARIABLE_ELEMENT = "element"; // NOI18N
51
private static final String JavaDoc VARIABLE_ATTRS = "attrs"; // NOI18N
52

53     private static final String JavaDoc METHOD_SCAN_DOCUMENT = "visitDocument"; // NOI18N
54
private static final String JavaDoc METHOD_SCAN_ELEMENT = "visitElement"; // NOI18N
55

56     //TODO: Retouche
57
// private static final Type Type_STRING = Type.createFromClass (String.class);
58

59
60     private DataObject DO;
61     private TreeDTDRoot dtd;
62
63     public GenerateDOMScannerSupport (DTDDataObject DO) {
64     this (DO, null);
65     }
66
67     public GenerateDOMScannerSupport (DataObject DO, TreeDTDRoot dtd) {
68         if (DO == null) throw new IllegalArgumentException JavaDoc("null"); // NOI18N
69
this.DO = DO;
70         this.dtd = dtd;
71     }
72
73     //TODO: Retouche
74
public void generate () {
75 // try {
76
//
77
// if (getDTD() == null)
78
// return;
79
//
80
// FileObject primFile = DO.getPrimaryFile();
81
//
82
// String rawName = primFile.getName();
83
// String name = rawName.substring(0,1).toUpperCase() + rawName.substring(1) + Util.THIS.getString("NAME_SUFFIX_Scanner");
84
//
85
// FileObject folder = primFile.getParent();
86
// String packageName = Util.findJavaPackage(folder);
87
//
88
// FileObject generFile = (new SelectFileDialog (folder, name, JAVA_EXT)).getFileObject();
89
// name = generFile.getName();
90
//
91
// // write to file
92
// FileLock lock = null;
93
// PrintStream printer = null;
94
// try {
95
// GuiUtil.setStatusText(Util.THIS.getString("MSG_DOM_1"));
96
// lock = generFile.lock ();
97
// printer = new PrintStream (generFile.getOutputStream (lock));
98
// printer.println (prepareDOMScanner (name, packageName, primFile));
99
// } finally {
100
// GuiUtil.setStatusText(""); // NOI18N
101
// if (printer != null)
102
// printer.close();
103
// if (lock != null)
104
// lock.releaseLock();
105
// }
106
// GuiUtil.performDefaultAction (generFile);
107
//
108
// } catch (UserCancelException e) {
109
// } catch (SourceException e) {
110
// // should not occure
111
// ErrorManager.getDefault().notify(e);
112
// } catch (TreeException e) {
113
// // can not get tree representaion
114
// GuiUtil.notifyError(Util.THIS.getString("MSG_DOM_ERR_1"));
115
// } catch (IOException e) {
116
// // can not get tree representaion or write
117
// GuiUtil.notifyError(Util.THIS.getString("MSG_DOM_ERR_2"));
118
// }
119
}
120
121     private TreeDTDRoot getDTD () throws IOException, TreeException {
122     if (dtd == null) {
123         TreeDocumentRoot result;
124
125         TreeEditorCookie cake = (TreeEditorCookie) ((DTDDataObject)DO).getCookie(TreeEditorCookie.class);
126         if (cake != null) {
127             result = cake.openDocumentRoot();
128         } else {
129             throw new TreeException("DTDDataObject:INTERNAL ERROR"); // NOI18N
130
}
131         dtd = (TreeDTDRoot)result;
132     }
133         return dtd;
134     }
135         
136 // private String prepareDOMScanner (String name, String packageName, FileObject primFile) throws IOException, SourceException, TreeException {
137
// getDTD();
138
//
139
// String header = GenerateSupportUtils.getJavaFileHeader (name, primFile);
140
// String packageLine = (packageName != null && packageName.length() != 0 ? "package " + packageName + ";\n" : ""); // NOI18N
141
// ClassElement clazz = prepareDOMScannerClass (name);
142
//
143
// StringBuffer buf = new StringBuffer();
144
// buf.append (header).append ("\n").append (packageLine).append (clazz.toString()); // NOI18N
145
//
146
// return buf.toString();
147
// }
148

149     
150     /*
151      * Generate top level class content.
152      *
153      */

154 // private ClassElement prepareDOMScannerClass (String name) throws SourceException {
155
// ClassElement clazz = new ClassElement ();
156
// JavaDoc javadoc = clazz.getJavaDoc();
157
// javadoc.setRawText ("\n"+ // NOI18N
158
// " This is a scanner of DOM tree.\n"+ // NOI18N
159
// "\n"+ // NOI18N
160
// " Example:\n"+ // NOI18N
161
// " <pre>\n"+ // NOI18N
162
// " javax.xml.parsers.DocumentBuilderFactory builderFactory = javax.xml.parsers.DocumentBuilderFactory.newInstance();\n"+ // NOI18N
163
// " javax.xml.parsers.DocumentBuilder builder = builderFactory.newDocumentBuilder();\n"+ // NOI18N
164
// " org.w3c.dom.Document document = builder.parse (new org.xml.sax.InputSource (???));\n"+ // NOI18N
165
// " <font color=\"blue\">"+name+" scanner = new "+name+" (document);</font>\n"+ // NOI18N
166
// " <font color=\"blue\">scanner."+METHOD_SCAN_DOCUMENT+"();</font>\n"+ // NOI18N
167
// " </pre>\n"+ // NOI18N
168
// "\n"+ // NOI18N
169
// " @see org.w3c.dom.Document\n"+ // NOI18N
170
// " @see org.w3c.dom.Element\n"+ // NOI18N
171
// " @see org.w3c.dom.NamedNodeMap\n"); // NOI18N
172
// clazz.setModifiers (Modifier.PUBLIC);
173
// clazz.setName (Identifier.create (name));
174
//
175
// dtd2java (clazz, findRootTagName());
176
//
177
// return (clazz);
178
// }
179

180     /*
181      * Generate scanner methods.
182      *
183      */

184 // private void dtd2java (ClassElement clazz, String tempRootName) throws SourceException {
185
// Iterator it;
186
// FieldElement field;
187
// ConstructorElement constructor;
188
// MethodElement method;
189
// Type docType = Type.parse (DOM_DOCUMENT);
190
// Type elemType = Type.parse (DOM_ELEMENT);
191
// StringBuffer sb;
192
// JavaDoc javadoc;
193
//
194
// // document field
195
// field = new FieldElement();
196
// field.setType (docType);
197
// field.setName (Identifier.create (VARIABLE_DOCUMENT));
198
// javadoc = field.getJavaDoc();
199
// javadoc.setRawText ("org.w3c.dom.Document document"); // NOI18N
200
// clazz.addField (field);
201
//
202
// // constructor
203
// constructor = new ConstructorElement();
204
// constructor.setModifiers (Modifier.PUBLIC);
205
// constructor.setName (clazz.getName());
206
// constructor.setParameters
207
// (new MethodParameter [] { new MethodParameter (VARIABLE_DOCUMENT, docType, false) });
208
// sb = new StringBuffer ("\n"); // NOI18N
209
// sb.append ("this.").append (VARIABLE_DOCUMENT).append (" = ").append (VARIABLE_DOCUMENT).append (";\n"); // NOI18N
210
// constructor.setBody (sb.toString());
211
// javadoc = constructor.getJavaDoc();
212
// javadoc.setRawText ("Create new " + clazz.getName() + " with org.w3c.dom.Document."); // NOI18N
213
// clazz.addConstructor (constructor);
214
//
215
// // scanDocument method
216
//
217
// method = new MethodElement();
218
// method.setModifiers (Modifier.PUBLIC);
219
// method.setReturn (Type.VOID);
220
// method.setName (Identifier.create (METHOD_SCAN_DOCUMENT));
221
// sb = new StringBuffer ("\n"); // NOI18N
222
// sb.append (DOM_ELEMENT).append (" ").append (VARIABLE_ELEMENT).append (" = "). // NOI18N
223
// append (VARIABLE_DOCUMENT).append (".getDocumentElement();\n"); // NOI18N
224
//
225
// // no root element is obvious, go over all declated elements.
226
//
227
// it = dtd.getElementDeclarations().iterator();
228
// while (it.hasNext()) {
229
// String tagName = ((TreeElementDecl)it.next()).getName();
230
// sb.append ("if ((").append (VARIABLE_ELEMENT).append (" != null) && "). // NOI18N
231
// append (VARIABLE_ELEMENT).append (".getTagName().equals (\"").append (tagName).append ("\")) {\n"); // NOI18N
232
// sb.append (METHOD_SCAN_ELEMENT).append ("_").append (GenerateSupportUtils.getJavaName (tagName)).append (" (").append (VARIABLE_ELEMENT). // NOI18N
233
// append (");\n}\n"); // NOI18N
234
// }
235
// method.setBody (sb.toString());
236
// javadoc = method.getJavaDoc();
237
// javadoc.setRawText ("Scan through org.w3c.dom.Document " + VARIABLE_DOCUMENT + "."); // NOI18N
238
// clazz.addMethod (method);
239
//
240
// // set of scan_ methods
241
//
242
// it = dtd.getElementDeclarations().iterator();
243
// while (it.hasNext()) {
244
// TreeElementDecl next = (TreeElementDecl) it.next();
245
// String tagName = next.getName();
246
// method = new MethodElement();
247
// method.setReturn (Type.VOID);
248
// method.setName (Identifier.create (GenerateSupportUtils.getJavaName (METHOD_SCAN_ELEMENT + "_" + tagName))); // NOI18N
249
// method.setParameters
250
// (new MethodParameter [] { new MethodParameter (VARIABLE_ELEMENT, elemType, false) });
251
// sb = new StringBuffer ();
252
// sb.append (" // <").append (tagName).append (">\n// element.getValue();\n"); // NOI18N
253
// Iterator it2;
254
// if ((it2 = dtd.getAttributeDeclarations (tagName).iterator()).hasNext()) {
255
// sb.append (DOM_NAMED_NODE_MAP).append (" ").append (VARIABLE_ATTRS).append (" = "). // NOI18N
256
// append (VARIABLE_ELEMENT).append (".getAttributes();\n"); // NOI18N
257
// sb.append ("for (int i = 0; i < ").append (VARIABLE_ATTRS).append (".getLength(); i++) {\n"); // NOI18N
258
// sb.append ("org.w3c.dom.Attr attr = (org.w3c.dom.Attr)attrs.item(i);\n"); // NOI18N
259
// while (it2.hasNext()) {
260
// TreeAttlistDeclAttributeDef attr = (TreeAttlistDeclAttributeDef)it2.next();
261
// sb.append ("if (attr.getName().equals (\"").append (attr.getName()).append ("\")) { // <"). // NOI18N
262
// append (tagName).append (" ").append (attr.getName()).append ("=\"???\">\n"); // NOI18N
263
// sb.append ("// attr.getValue();\n}\n"); // NOI18N
264
// }
265
// sb.append ("}\n"); // NOI18N
266
// }
267
// sb.append (generateElementScanner(next));
268
// method.setBody (sb.toString());
269
// javadoc = method.getJavaDoc();
270
// javadoc.setRawText ("Scan through org.w3c.dom.Element named " + tagName + "."); // NOI18N
271
// clazz.addMethod (method);
272
// }
273
// }
274

275     /*
276      * Generate fragment of code that goes over element content model
277      * (and calls nested scanners/visitors).
278      */

279     private String JavaDoc generateElementScanner(TreeElementDecl element) {
280         
281         Iterator it;
282         Set elements = new HashSet();
283         
284         TreeElementDecl.ContentType type = element.getContentType();
285         
286         if (type instanceof ANYType) {
287             it = dtd.getElementDeclarations().iterator();
288             while (it.hasNext()) {
289                 String JavaDoc tagName = ((TreeElementDecl)it.next()).getName();
290                 elements.add(tagName);
291             }
292             
293         } else {
294             addElements(type, elements);
295         }
296         
297         StringBuffer JavaDoc sb2 = new StringBuffer JavaDoc();
298         sb2.append ("org.w3c.dom.NodeList nodes = element.getChildNodes();\n"); // NOI18N
299
sb2.append ("for (int i = 0; i < nodes.getLength(); i++) {\n"); // NOI18N
300
sb2.append ("org.w3c.dom.Node node = nodes.item (i);\n"); // NOI18N
301
sb2.append ("switch (node.getNodeType()) {\n"); // NOI18N
302
sb2.append ("case org.w3c.dom.Node.CDATA_SECTION_NODE:\n"); // NOI18N
303
sb2.append ("// ((org.w3c.dom.CDATASection)node).getData();\nbreak;\n"); // NOI18N
304
sb2.append ("case org.w3c.dom.Node.ELEMENT_NODE:\n"); // NOI18N
305
sb2.append ("org.w3c.dom.Element nodeElement = (org.w3c.dom.Element)node;\n"); // NOI18N
306
it = dtd.getElementDeclarations().iterator();
307         while (it.hasNext()) {
308             String JavaDoc tagName = ((TreeElementDecl)it.next()).getName();
309             if (elements.contains(tagName) == false) continue;
310             sb2.append ("if (nodeElement.getTagName().equals (\"").append (tagName).append ("\")) {\n"); // NOI18N
311
sb2.append (METHOD_SCAN_ELEMENT).append ("_").append (GenerateSupportUtils.getJavaName (tagName)).append (" (nodeElement);\n}\n"); // NOI18N
312
}
313         sb2.append ("break;\n"); // NOI18N
314
sb2.append ("case org.w3c.dom.Node.PROCESSING_INSTRUCTION_NODE:\n"); // NOI18N
315
sb2.append ("// ((org.w3c.dom.ProcessingInstruction)node).getTarget();\n"); // NOI18N
316
sb2.append ("// ((org.w3c.dom.ProcessingInstruction)node).getData();\n"); // NOI18N
317
sb2.append ("break;\n"); // NOI18N
318
if (type.allowText()) {
319             sb2.append ("case org.w3c.dom.Node.TEXT_NODE:\n"); // NOI18N
320
sb2.append ("// ((org.w3c.dom.Text)node).getData();\n"); // NOI18N
321
sb2.append ("break;\n"); // NOI18N
322
}
323         sb2.append ("}\n}\n"); // NOI18N
324

325         return sb2.toString();
326     }
327
328     
329     /*
330      * Recursive descend looking for all declared children of type
331      * Takes into account just ChildrenType and NameType.
332      */

333     private void addElements(TreeElementDecl.ContentType type, Set elements) {
334         
335         if (type instanceof ChildrenType) {
336             for (Iterator it = ((ChildrenType)type).getTypes().iterator(); it.hasNext(); ) {
337                 TreeElementDecl.ContentType next = (TreeElementDecl.ContentType) it.next();
338                 if (next instanceof ChildrenType) {
339                     addElements(next, elements);
340                 } else if ( next instanceof NameType) {
341                     elements.add(((NameType)next).getName());
342                 }
343             }
344         }
345     }
346     
347     private String JavaDoc findRootTagName () {
348         return null;
349         // SelectTagNamePanel panel = new SelectTagNamePanel (dtd);
350
// DialogDescriptor dd = new DialogDescriptor
351
// (panel, Util.THIS.getString ("PROP_rootElementNameTitle"), true, // NOI18N
352
// new Object[] { DialogDescriptor.OK_OPTION },
353
// DialogDescriptor.OK_OPTION,
354
// DialogDescriptor.BOTTOM_ALIGN, null, null);
355
// TopManager.getDefault().createDialog (dd).show();
356
// return panel.getRootName();
357
}
358 }
359
Popular Tags