KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xdm > visitor > FindNamespaceVisitor


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.xdm.visitor;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import org.netbeans.modules.xml.xdm.nodes.Attribute;
27 import org.netbeans.modules.xml.xdm.nodes.Element;
28 import org.netbeans.modules.xml.xdm.nodes.Node;
29 import org.netbeans.modules.xml.xdm.nodes.Document;
30 import org.w3c.dom.NamedNodeMap JavaDoc;
31
32 /**
33  *
34  * @author ajit
35  */

36 public class FindNamespaceVisitor extends ChildVisitor {
37     
38     /** Creates a new instance of FindNamespaceVisitor */
39     public FindNamespaceVisitor(Document root) {
40         this.root = root;
41     }
42     
43     public String JavaDoc findNamespace(Node target) {
44         if(!(target instanceof Element) && !(target instanceof Attribute)) return null;
45         return getNamespaceMap().get(target.getId());
46     }
47     
48     public Map JavaDoc<Integer JavaDoc,String JavaDoc> getNamespaceMap() {
49         if(namespaceMap.isEmpty()) {
50             nodeCtr = 0;
51             visit(root);
52         }
53         return namespaceMap;
54     }
55     
56     protected void visitNode(Node node) {
57         Map JavaDoc<String JavaDoc,String JavaDoc> namespaces = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
58         if((node instanceof Element) || (node instanceof Attribute)) {
59             nodeCtr++;
60             boolean found = false;
61             String JavaDoc prefix = node.getPrefix();
62             if(prefix == null) {
63                 if(node instanceof Attribute) return;
64                 prefix = "";
65             }
66             if(node instanceof Element && node.hasAttributes()) {
67                 NamedNodeMap JavaDoc attrMap = node.getAttributes();
68                 for (int i=0;i<attrMap.getLength();i++) {
69                     Attribute attribute = (Attribute)attrMap.item(i);
70                     if(Element.XMLNS.equals(attribute.getPrefix()) || Element.XMLNS.equals(attribute.getName())) {
71                         String JavaDoc key = attribute.getPrefix()==null?"":attribute.getLocalName();
72                         String JavaDoc value = attribute.getValue();
73                         namespaces.put(key,value);
74                         if(key.equals(prefix)) {
75                             namespaceMap.put(node.getId(),value);
76                             found = true;
77                         }
78                     }
79                 }
80             }
81             if(!found) {
82                 for(Map JavaDoc<String JavaDoc,String JavaDoc> map:ancestorNamespaceMaps) {
83                     if(map.containsKey(prefix)) {
84                         namespaceMap.put(node.getId(),map.get(prefix));
85                         break;
86                     }
87                 }
88             }
89         }
90         if(!namespaces.isEmpty())
91             ancestorNamespaceMaps.add(0,namespaces);
92         super.visitNode(node);
93         ancestorNamespaceMaps.remove(namespaces);
94     }
95
96     private Map JavaDoc<Integer JavaDoc,String JavaDoc> namespaceMap = new HashMap JavaDoc<Integer JavaDoc,String JavaDoc>();
97     private Document root = null;
98     private List JavaDoc<Map JavaDoc<String JavaDoc,String JavaDoc>> ancestorNamespaceMaps = new ArrayList JavaDoc<Map JavaDoc<String JavaDoc,String JavaDoc>>();
99     int nodeCtr;
100 }
101
Popular Tags