KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > model > visitor > FindWSDLComponent


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 /*
21  * XMLModelMapperVisitor.java
22  *
23  * Created on October 28, 2005, 3:17 PM
24  *
25  * To change this template, choose Tools | Template Manager
26  * and open the template in the editor.
27  */

28
29 package org.netbeans.modules.xml.wsdl.model.visitor;
30
31 import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
32 import org.w3c.dom.Document JavaDoc;
33 import org.w3c.dom.Element JavaDoc;
34 import org.w3c.dom.Node JavaDoc;
35
36 /**
37  *
38  * @author ajit
39  */

40 public class FindWSDLComponent extends ChildVisitor {
41     
42     /** Creates a new instance of XMLModelMapperVisitor */
43     public FindWSDLComponent() {
44     }
45     
46     public static <T extends WSDLComponent> T findComponent(Class JavaDoc<T> type, WSDLComponent root, String JavaDoc xpath) {
47         WSDLComponent ret = new FindWSDLComponent().findComponent(root, xpath);
48         if (ret == null) {
49             return null;
50         } else {
51             return type.cast(ret);
52         }
53     }
54     
55     public WSDLComponent findComponent(WSDLComponent root, Element JavaDoc xmlNode) {
56         assert xmlNode != null;
57         
58         this.xmlNode = xmlNode;
59         result = null;
60         root.accept(this);
61         return result;
62     }
63     
64     public WSDLComponent findComponent(WSDLComponent root, String JavaDoc xpath) {
65         Document JavaDoc doc = (Document JavaDoc) root.getModel().getDocument();
66         if (doc == null) {
67             return null;
68         }
69         
70         Node JavaDoc result = root.getModel().getAccess().findNode(doc, xpath);
71         if (result instanceof Element JavaDoc) {
72             return findComponent(root, (Element JavaDoc) result);
73         } else {
74             return null;
75         }
76     }
77
78     protected void visitComponent(WSDLComponent component) {
79         if (result != null) return;
80         if (component.referencesSameNode(xmlNode)) {
81             result = component;
82             return;
83         } else {
84             super.visitComponent(component);
85         }
86     }
87     
88     private WSDLComponent result;
89     private Element JavaDoc xmlNode;
90 }
91
Popular Tags