KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > model > visitor > FindGlobalReferenceVisitor


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

19
20 package org.netbeans.modules.xml.schema.model.visitor;
21
22 import java.util.Collection JavaDoc;
23 import java.util.List JavaDoc;
24 import org.netbeans.modules.xml.schema.model.SchemaComponent;
25 import org.netbeans.modules.xml.schema.model.GlobalAttribute;
26 import org.netbeans.modules.xml.schema.model.GlobalAttributeGroup;
27 import org.netbeans.modules.xml.schema.model.GlobalComplexType;
28 import org.netbeans.modules.xml.schema.model.GlobalElement;
29 import org.netbeans.modules.xml.schema.model.GlobalSimpleType;
30 import org.netbeans.modules.xml.schema.model.GlobalGroup;
31 import org.netbeans.modules.xml.schema.model.Notation;
32 import org.netbeans.modules.xml.schema.model.Schema;
33 import org.netbeans.modules.xml.xam.NamedReferenceable;
34
35 /**
36  *
37  * @author rico
38  */

39 public class FindGlobalReferenceVisitor <T extends NamedReferenceable> extends DefaultSchemaVisitor{
40     private Class JavaDoc<T> elementType;
41     private String JavaDoc localName;
42     private Schema schema;
43     private T refType;
44     private boolean found;
45     
46     public T find(Class JavaDoc<T> elementType, String JavaDoc localName, Schema schema){
47         if (elementType == null || localName == null || schema == null) {
48             throw new IllegalArgumentException JavaDoc("elementType == null");
49         }
50
51         this.elementType = elementType;
52         this.localName = localName;
53         this.schema = schema;
54         found = false;
55         schema.accept(this);
56         return refType;
57     }
58     
59     public void visit(Schema schema) {
60         List JavaDoc<SchemaComponent> ch = schema.getChildren();
61         for (SchemaComponent c : ch) {
62             c.accept(this);
63             if(found) return;
64         }
65     }
66     
67     public void visit(GlobalAttributeGroup e) {
68         findReference(GlobalAttributeGroup.class, e);
69     }
70     
71     public void visit(GlobalGroup e) {
72         findReference(GlobalGroup.class, e);
73     }
74     
75     public void visit(GlobalAttribute e) {
76         findReference(GlobalAttribute.class, e);
77     }
78     
79     public void visit(GlobalElement e) {
80         findReference(GlobalElement.class, e);
81     }
82     
83     public void visit(GlobalSimpleType e) {
84         findReference(GlobalSimpleType.class, e);
85     }
86     
87     public void visit(GlobalComplexType e) {
88         findReference(GlobalComplexType.class, e);
89     }
90     
91     public void visit(Notation e) {
92         findReference(Notation.class, e);
93     }
94     
95     private void findReference(Class JavaDoc<? extends NamedReferenceable> refClass,
96     NamedReferenceable n) {
97     if(elementType.isAssignableFrom(refClass)){
98         if(localName.equals(n.getName())){
99         refType = elementType.cast(n);
100         found = true;
101         }
102     }
103     }
104     
105 }
106
Popular Tags