1 19 package org.netbeans.modules.xml.axi.visitor; 20 21 import org.netbeans.modules.xml.axi.AXIComponent; 22 import org.netbeans.modules.xml.axi.AbstractAttribute; 23 import org.netbeans.modules.xml.axi.AnyElement; 24 import org.netbeans.modules.xml.axi.Element; 25 import org.netbeans.modules.xml.axi.Compositor; 26 27 31 public class PrintAXITreeVisitor extends DeepAXITreeVisitor { 32 33 36 public PrintAXITreeVisitor() { 37 super(); 38 } 39 40 protected void visitChildren(AXIComponent component) { 41 if(PRINT_TO_CONSOLE) 42 printModel(component); 43 44 depth++; 45 super.visitChildren(component); 46 depth--; 47 } 48 49 private void printModel(AXIComponent component) { 50 StringBuffer buffer = new StringBuffer (); 51 if(component instanceof Compositor) { 52 Compositor compositor = (Compositor)component; 53 buffer.append((getTab() == null) ? compositor : getTab() + compositor); 54 buffer.append("<min=" + compositor.getMinOccurs() + ":max=" + compositor.getMaxOccurs() + ">"); 55 } 56 if(component instanceof Element) { 57 Element element = (Element)component; 58 buffer.append((getTab() == null) ? element.getName() : getTab() + element.getName()); 59 if(element.getAttributes().size() != 0) { 60 buffer.append("<" + getAttributes(element) + ">"); 61 } 62 buffer.append("<min=" + element.getMinOccurs() + ":max=" + element.getMaxOccurs() + ">"); 63 } 64 if(component instanceof AnyElement) { 65 AnyElement element = (AnyElement)component; 66 buffer.append((getTab() == null) ? element : getTab() + element); 67 } 68 69 System.out.println(buffer.toString()); 70 } 71 72 73 private String getAttributes(Element element) { 74 StringBuffer attrs = new StringBuffer (); 75 for(AbstractAttribute attr : element.getAttributes()) { 76 attrs.append(attr+":"); 77 } 78 if(attrs.length() > 0) 79 return attrs.toString().substring(0, attrs.length()-1); 80 else 81 return attrs.toString(); 82 } 83 84 private String getTab() { 85 String tabStr = "++++"; 86 87 if(depth == 0) { 88 return null; 89 } 90 91 StringBuffer tab = new StringBuffer (); 92 for(int i=0; i<depth ; i++) { 93 tab.append(tabStr); 94 } 95 return tab.toString(); 96 } 97 98 private int depth = 0; 99 private static boolean PRINT_TO_CONSOLE= false; 100 } 101 | Popular Tags |