001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.xml.xdm.visitor;
043:
044: import java.util.ArrayList;
045: import java.util.HashMap;
046: import java.util.List;
047: import java.util.Map;
048: import org.netbeans.modules.xml.xdm.nodes.Attribute;
049: import org.netbeans.modules.xml.xdm.nodes.Element;
050: import org.netbeans.modules.xml.xdm.nodes.Node;
051: import org.netbeans.modules.xml.xdm.nodes.Document;
052: import org.w3c.dom.NamedNodeMap;
053:
054: /**
055: *
056: * @author ajit
057: */
058: public class FindNamespaceVisitor extends ChildVisitor {
059:
060: /** Creates a new instance of FindNamespaceVisitor */
061: public FindNamespaceVisitor(Document root) {
062: this .root = root;
063: }
064:
065: public String findNamespace(Node target) {
066: if (!(target instanceof Element)
067: && !(target instanceof Attribute))
068: return null;
069: return getNamespaceMap().get(target.getId());
070: }
071:
072: public Map<Integer, String> getNamespaceMap() {
073: if (namespaceMap.isEmpty()) {
074: nodeCtr = 0;
075: visit(root);
076: }
077: return namespaceMap;
078: }
079:
080: protected void visitNode(Node node) {
081: Map<String, String> namespaces = new HashMap<String, String>();
082: if ((node instanceof Element) || (node instanceof Attribute)) {
083: nodeCtr++;
084: boolean found = false;
085: String prefix = node.getPrefix();
086: if (prefix == null) {
087: if (node instanceof Attribute)
088: return;
089: prefix = "";
090: }
091: if (node instanceof Element && node.hasAttributes()) {
092: NamedNodeMap attrMap = node.getAttributes();
093: for (int i = 0; i < attrMap.getLength(); i++) {
094: Attribute attribute = (Attribute) attrMap.item(i);
095: if (Element.XMLNS.equals(attribute.getPrefix())
096: || Element.XMLNS
097: .equals(attribute.getName())) {
098: String key = attribute.getPrefix() == null ? ""
099: : attribute.getLocalName();
100: String value = attribute.getValue();
101: namespaces.put(key, value);
102: if (key.equals(prefix)) {
103: namespaceMap.put(node.getId(), value);
104: found = true;
105: }
106: }
107: }
108: }
109: if (!found) {
110: for (Map<String, String> map : ancestorNamespaceMaps) {
111: if (map == null)
112: continue;
113: if (map.containsKey(prefix)) {
114: namespaceMap.put(node.getId(), map.get(prefix));
115: break;
116: }
117: }
118: }
119: }
120: if (!namespaces.isEmpty())
121: ancestorNamespaceMaps.add(0, namespaces);
122: super .visitNode(node);
123: ancestorNamespaceMaps.remove(namespaces);
124: }
125:
126: private Map<Integer, String> namespaceMap = new HashMap<Integer, String>();
127: private Document root = null;
128: private List<Map<String, String>> ancestorNamespaceMaps = new ArrayList<Map<String, String>>();
129: int nodeCtr;
130: }
|