KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Stack JavaDoc;
22 import org.netbeans.modules.xml.axi.AXIComponent;
23 import org.netbeans.modules.xml.axi.AXIComponent.ComponentType;
24 import org.netbeans.modules.xml.axi.AXIDocument;
25 import org.netbeans.modules.xml.axi.AnyAttribute;
26 import org.netbeans.modules.xml.axi.AnyElement;
27 import org.netbeans.modules.xml.axi.Attribute;
28 import org.netbeans.modules.xml.axi.Compositor;
29 import org.netbeans.modules.xml.axi.Element;
30 import org.netbeans.modules.xml.axi.ContentModel;
31 import org.netbeans.modules.xml.axi.impl.ElementRef;
32
33 /**
34  *
35  * @author Samaresh (Samaresh.Panda@Sun.Com)
36  */

37 public class DeepAXITreeVisitor extends DefaultVisitor {
38         
39     Stack JavaDoc<AXIComponent> pathToRoot = new Stack JavaDoc<AXIComponent>();
40     
41     /**
42      * Creates a new instance of DeepAXITreeVisitor
43      */

44     public DeepAXITreeVisitor() {
45         super();
46     }
47     
48     public void visit(AXIDocument root) {
49         visitChildren(root);
50     }
51     
52     public void visit(Element element) {
53         visitChildren(element);
54     }
55     
56     public void visit(AnyElement element) {
57         visitChildren(element);
58     }
59     
60     public void visit(Attribute attribute) {
61         visitChildren(attribute);
62     }
63         
64     public void visit(AnyAttribute attribute) {
65         visitChildren(attribute);
66     }
67     
68     public void visit(Compositor compositor) {
69         visitChildren(compositor);
70     }
71             
72     public void visit(ContentModel element) {
73         visitChildren(element);
74     }
75         
76     protected void visitChildren(AXIComponent component) {
77         if( !canVisit(component) )
78             return;
79                 
80         pathToRoot.push(component.getOriginal());
81         for(AXIComponent child: component.getChildren()) {
82             child.accept(this);
83         }
84         pathToRoot.pop();
85     }
86         
87     private boolean canVisit(AXIComponent component) {
88         if(pathToRoot.contains(component))
89             return false;
90         
91         if(component.getComponentType() == ComponentType.PROXY)
92             return canVisit(component.getOriginal());
93
94         if(component.getComponentType() == ComponentType.REFERENCE &&
95            component instanceof ElementRef) {
96             ElementRef ref = (ElementRef)component;
97             Element e = ref.getReferent();
98             if(pathToRoot.contains(e))
99                 return false;
100         }
101         
102         return true;
103     }
104     
105 }
106
Popular Tags