KickJava   Java API By Example, From Geeks To Geeks.

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


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
20 package org.netbeans.modules.xml.axi.visitor;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import org.netbeans.modules.xml.axi.AXIComponent;
25 import org.netbeans.modules.xml.axi.AXIDocument;
26 import org.netbeans.modules.xml.axi.AXIModel;
27 import org.netbeans.modules.xml.axi.Element;
28 import org.netbeans.modules.xml.axi.impl.SchemaGeneratorUtil;
29 import org.netbeans.modules.xml.schema.model.SchemaModel;
30
31 /**
32  * Deep non-cyclic visitor for AXI
33  *
34  * @author Ayub Khan
35  */

36 public class AXINonCyclicVisitor extends DeepAXITreeVisitor {
37     
38     List JavaDoc<AXIComponent> path = null;
39     
40     private AXIModel am;
41     
42     private SchemaModel sm;
43     
44     /**
45      * Creates a new instance of AXINonCyclicVisitor
46      */

47     public AXINonCyclicVisitor(AXIModel am) {
48         this.am = am;
49         this.sm = am.getSchemaModel();
50         path = new ArrayList JavaDoc<AXIComponent>();
51     }
52     
53     public void expand(AXIDocument root) {
54         path.clear();
55         if(root != null) {
56             for(AXIComponent c : root.getChildren()) {//deep visit
57
c.accept(this);
58             }
59         }
60     }
61     
62     //Deep visit to populate AXI model, visit only the least referenced
63
//global element, that references most global elements
64
public void expand(List JavaDoc<Element> elements) {
65         path.clear();
66         for(Element e : elements) {//deep visit
67
e.accept(this);
68         }
69     }
70     
71     public void visit(Element e) {
72         if(!canVisit(e)) //skip recursion
73
return;
74         visitChildren(e);
75     }
76     
77     public boolean canVisit(Element e) {
78         Element orig = getOriginalElement(e);
79         if(!SchemaGeneratorUtil.fromSameSchemaModel(orig, sm) ||
80                 path.size() > 0 && path.contains(orig)) //skip recursion
81
return false;
82         return true;
83     }
84     
85     public void visitChildren(Element e) {
86         Element orig = getOriginalElement(e);
87         path.add(orig);
88         try {
89             super.visit(e);//now visit children
90
} finally {
91             path.remove(path.size()-1);
92         }
93     }
94     
95     private Element getOriginalElement(final Element e) {
96         Element orig = e;
97         if(orig.isReference()) {
98             orig = SchemaGeneratorUtil.findOriginalElement(e);
99         }
100         return orig;
101     }
102 }
103
Popular Tags