KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > axi > visitor > PrintAXITreeVisitor


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 /**
28  *
29  * @author Samaresh (Samaresh.Panda@Sun.Com)
30  */

31 public class PrintAXITreeVisitor extends DeepAXITreeVisitor {
32             
33     /**
34      * Creates a new instance of PrintAXITreeVisitor
35      */

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 JavaDoc buffer = new StringBuffer JavaDoc();
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 JavaDoc getAttributes(Element element) {
74         StringBuffer JavaDoc attrs = new StringBuffer JavaDoc();
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 JavaDoc getTab() {
85         String JavaDoc tabStr = "++++";
86         
87         if(depth == 0) {
88             return null;
89         }
90         
91         StringBuffer JavaDoc tab = new StringBuffer JavaDoc();
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