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: package org.netbeans.tax;
042:
043: import java.util.Map;
044: import java.util.HashMap;
045: import java.util.Iterator;
046:
047: /**
048: * Context allows to determive namespace URI by its context prefix.
049: * <p>
050: * Default namespace prefix is "".
051: * <p>
052: *
053: * @author Libor Kramolis
054: * @version 0.1
055: */
056: public class TreeNamespaceContext {
057:
058: /**
059: * Namespace context is defined by element nesting
060: * so we seach for parent context by quering parent element namespace context.
061: */
062: private TreeElement element;
063:
064: /**
065: * It hold only namespaces defined at peer element.
066: * It avoids problems with the following scenario:
067: * <pre>
068: * <ns1:root xmlns:ns1="original ns1">
069: * <ns2:child xmlns:ns2="original ns2">
070: * <ns1:kid xmlns:ns1="context redefined ns1 binding">
071: * </pre>
072: */
073: private static Map definedNS = new HashMap();
074:
075: static {
076: TreeNamespace namespace;
077:
078: namespace = TreeNamespace.XML_NAMESPACE;
079: definedNS.put(namespace.getPrefix(), namespace);
080:
081: namespace = TreeNamespace.XMLNS_NAMESPACE;
082: definedNS.put(namespace.getPrefix(), namespace);
083: }
084:
085: //
086: // init
087: //
088:
089: /**
090: * Creates new TreeNamespaceContext.
091: * Only TreeElement can do so.
092: */
093: protected TreeNamespaceContext(TreeElement element) {
094: this .element = element;
095: }
096:
097: /**
098: * Traverse over parents and parse their attributes until given prefix is resolved.
099: * @param prefix namespace prefix ("" default)
100: * @return null it is not defined
101: */
102: public String getURI(String prefix) {
103:
104: // well known prefixes are in this map
105:
106: TreeNamespace ns = (TreeNamespace) definedNS.get(prefix);
107: if (ns != null) {
108: return ns.getURI();
109: }
110:
111: // look for attributes that defines namespaces
112: // take cate to default namespace definition attribute
113:
114: TreeNamedObjectMap attrs = element.getAttributes();
115: if (attrs != null) {
116: Iterator it = attrs.iterator();
117: while (it.hasNext()) {
118: TreeAttribute next = (TreeAttribute) it.next();
119: TreeName name = next.getTreeName();
120: if ("xmlns".equals(name.getPrefix())) { // NOI18N
121: if (prefix.equals(name.getName())) {
122: return next.getValue();
123: }
124: } else if ("xmlns".equals(name.getQualifiedName())) { // NOI18N
125: return next.getValue();
126: }
127: }
128: }
129:
130: // try my parent
131:
132: TreeParentNode parentNode = element.getParentNode();
133: if (parentNode instanceof TreeElement) {
134: TreeElement parentElement = (TreeElement) parentNode;
135: if (parentElement != null) {
136: return parentElement.getNamespaceContext().getURI(
137: prefix);
138: }
139: }
140: return null;
141: }
142:
143: }
|