KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > tax > TreeNamespaceContext


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 package org.netbeans.tax;
20
21 import java.util.Map JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 /**
26  * Context allows to determive namespace URI by its context prefix.
27  * <p>
28  * Default namespace prefix is "".
29  * <p>
30  *
31  * @author Libor Kramolis
32  * @version 0.1
33  */

34 public class TreeNamespaceContext {
35
36     /**
37      * Namespace context is defined by element nesting
38      * so we seach for parent context by quering parent element namespace context.
39      */

40     private TreeElement element;
41
42     /**
43      * It hold only namespaces defined at peer element.
44      * It avoids problems with the following scenario:
45      * <pre>
46      * &lt;ns1:root xmlns:ns1="original ns1">
47      * &lt;ns2:child xmlns:ns2="original ns2">
48      * &lt;ns1:kid xmlns:ns1="context redefined ns1 binding">
49      * </pre>
50      */

51     private static Map JavaDoc definedNS = new HashMap JavaDoc ();
52     
53     static {
54         TreeNamespace namespace;
55         
56         namespace = TreeNamespace.XML_NAMESPACE;
57         definedNS.put (namespace.getPrefix (), namespace);
58         
59         namespace = TreeNamespace.XMLNS_NAMESPACE;
60         definedNS.put (namespace.getPrefix (), namespace);
61     }
62     
63     
64     //
65
// init
66
//
67

68     /**
69      * Creates new TreeNamespaceContext.
70      * Only TreeElement can do so.
71      */

72     protected TreeNamespaceContext (TreeElement element) {
73         this.element = element;
74     }
75     
76     
77     /**
78      * Traverse over parents and parse their attributes until given prefix is resolved.
79      * @param prefix namespace prefix ("" default)
80      * @return null it is not defined
81      */

82     public String JavaDoc getURI (String JavaDoc prefix) {
83         
84         // well known prefixes are in this map
85

86         TreeNamespace ns = (TreeNamespace)definedNS.get (prefix);
87         if (ns != null) {
88             return ns.getURI ();
89         }
90         
91         // look for attributes that defines namespaces
92
// take cate to default namespace definition attribute
93

94         TreeNamedObjectMap attrs = element.getAttributes ();
95         if (attrs != null) {
96             Iterator JavaDoc it = attrs.iterator ();
97             while (it.hasNext ()) {
98                 TreeAttribute next = (TreeAttribute) it.next ();
99                 TreeName name = next.getTreeName ();
100                 if ("xmlns".equals (name.getPrefix ())) { // NOI18N
101
if (prefix.equals (name.getName ())) {
102                         return next.getValue ();
103                     }
104                 } else if ("xmlns".equals (name.getQualifiedName ())) { // NOI18N
105
return next.getValue ();
106                 }
107             }
108         }
109         
110         // try my parent
111

112         TreeParentNode parentNode = element.getParentNode ();
113         if ( parentNode instanceof TreeElement ) {
114             TreeElement parentElement = (TreeElement)parentNode;
115             if (parentElement != null) {
116                 return parentElement.getNamespaceContext ().getURI (prefix);
117             }
118         }
119         return null;
120     }
121     
122 }
123
Popular Tags