1 19 package org.netbeans.modules.xml.tools.generator; 20 21 import java.io.*; 22 import java.util.*; 23 import java.awt.BorderLayout ; 24 import javax.swing.*; 25 import java.lang.reflect.Modifier ; 26 27 import org.openide.*; 28 import org.openide.nodes.*; 29 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 JAVA_EXT = "java"; 44 private static final String DOM_PACKAGE = "org.w3c.dom."; private static final String DOM_DOCUMENT = DOM_PACKAGE + "Document"; private static final String DOM_ELEMENT = DOM_PACKAGE + "Element"; private static final String DOM_NAMED_NODE_MAP = DOM_PACKAGE + "NamedNodeMap"; 49 private static final String VARIABLE_DOCUMENT = "document"; private static final String VARIABLE_ELEMENT = "element"; private static final String VARIABLE_ATTRS = "attrs"; 53 private static final String METHOD_SCAN_DOCUMENT = "visitDocument"; private static final String METHOD_SCAN_ELEMENT = "visitElement"; 56 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 ("null"); this.DO = DO; 70 this.dtd = dtd; 71 } 72 73 public void generate () { 75 } 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"); } 131 dtd = (TreeDTDRoot)result; 132 } 133 return dtd; 134 } 135 136 149 150 154 180 184 275 279 private String 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 tagName = ((TreeElementDecl)it.next()).getName(); 290 elements.add(tagName); 291 } 292 293 } else { 294 addElements(type, elements); 295 } 296 297 StringBuffer sb2 = new StringBuffer (); 298 sb2.append ("org.w3c.dom.NodeList nodes = element.getChildNodes();\n"); sb2.append ("for (int i = 0; i < nodes.getLength(); i++) {\n"); sb2.append ("org.w3c.dom.Node node = nodes.item (i);\n"); sb2.append ("switch (node.getNodeType()) {\n"); sb2.append ("case org.w3c.dom.Node.CDATA_SECTION_NODE:\n"); sb2.append ("// ((org.w3c.dom.CDATASection)node).getData();\nbreak;\n"); sb2.append ("case org.w3c.dom.Node.ELEMENT_NODE:\n"); sb2.append ("org.w3c.dom.Element nodeElement = (org.w3c.dom.Element)node;\n"); it = dtd.getElementDeclarations().iterator(); 307 while (it.hasNext()) { 308 String tagName = ((TreeElementDecl)it.next()).getName(); 309 if (elements.contains(tagName) == false) continue; 310 sb2.append ("if (nodeElement.getTagName().equals (\"").append (tagName).append ("\")) {\n"); sb2.append (METHOD_SCAN_ELEMENT).append ("_").append (GenerateSupportUtils.getJavaName (tagName)).append (" (nodeElement);\n}\n"); } 313 sb2.append ("break;\n"); sb2.append ("case org.w3c.dom.Node.PROCESSING_INSTRUCTION_NODE:\n"); sb2.append ("// ((org.w3c.dom.ProcessingInstruction)node).getTarget();\n"); sb2.append ("// ((org.w3c.dom.ProcessingInstruction)node).getData();\n"); sb2.append ("break;\n"); if (type.allowText()) { 319 sb2.append ("case org.w3c.dom.Node.TEXT_NODE:\n"); sb2.append ("// ((org.w3c.dom.Text)node).getData();\n"); sb2.append ("break;\n"); } 323 sb2.append ("}\n}\n"); 325 return sb2.toString(); 326 } 327 328 329 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 findRootTagName () { 348 return null; 349 } 358 } 359 | Popular Tags |