KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.netbeans.modules.xml.schema.model.Schema;
23 import org.netbeans.modules.xml.schema.model.SchemaComponent;
24 import org.netbeans.modules.xml.schema.model.impl.SchemaComponentImpl;
25 import org.netbeans.modules.xml.schema.model.impl.SchemaModelImpl;
26 import org.w3c.dom.Document JavaDoc;
27 import org.w3c.dom.Node JavaDoc;
28 import org.w3c.dom.Element JavaDoc;
29
30 /**
31  *
32  * @author ajit
33  */

34 public class FindSchemaComponentFromDOM extends DeepSchemaVisitor {
35
36     /** Creates a new instance of XMLModelMapperVisitor */
37     public FindSchemaComponentFromDOM() {
38     }
39     
40     public static <T extends SchemaComponent> T find(Class JavaDoc<T> type, SchemaComponent root, String JavaDoc xpath) {
41         SchemaComponent ret = new FindSchemaComponentFromDOM().findComponent(root, xpath);
42         return type.cast(ret);
43     }
44     
45     public SchemaComponent findComponent(SchemaComponent root, Element JavaDoc xmlNode) {
46         assert root instanceof Schema;
47         assert xmlNode != null;
48         
49         this.xmlNode = xmlNode;
50         result = null;
51         root.accept(this);
52         return result;
53     }
54     
55     public SchemaComponent findComponent(SchemaComponent root, String JavaDoc xpath) {
56         Document JavaDoc doc = getDocument(root);
57         if (doc == null) {
58             return null;
59         }
60         
61         Node JavaDoc result = ((SchemaModelImpl)root.getModel()).getAccess().findNode(doc, xpath);
62         if (result instanceof Element JavaDoc) {
63             return findComponent(root, (Element JavaDoc) result);
64         } else {
65             return null;
66         }
67     }
68
69     private Document JavaDoc getDocument(SchemaComponent root) {
70         return (Document JavaDoc)root.getModel().getDocument();
71     }
72
73     private Element JavaDoc getElement(SchemaComponent c) {
74         return (Element JavaDoc) c.getPeer();
75     }
76     
77     public String JavaDoc getXPathForComponent(SchemaComponent root, SchemaComponent target) {
78         Document JavaDoc doc = getDocument(root);
79         Element JavaDoc element = getElement(target);
80         if (doc == null || element == null) {
81             return null;
82         }
83         return ((SchemaModelImpl)root.getModel()).getAccess().getXPath(doc, element);
84     }
85
86     protected void visitChildren(SchemaComponent component) {
87         if(result != null) return;
88         if (component.referencesSameNode(xmlNode)) {
89             result = component;
90         } else {
91             super.visitChildren(component);
92         }
93     }
94     
95     private SchemaComponent result;
96     private Element JavaDoc xmlNode;
97
98 }
99
Popular Tags