KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > commons > xml > namespace > AbstractNamespaceResolver


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.commons.xml.namespace;
20
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.logging.*;
25 import java.util.logging.Level JavaDoc;
26
27 import org.w3c.dom.Node JavaDoc;
28
29 /**
30  * This class provides methods to support NamespaceResolver implementations. It stores
31  * Namespace URI/Prefix mappings, checks for clashes and generates new safe prefixes for
32  * Namespaces that do not already have a prefix associated to them.
33  *
34  * @author Matthew Large
35  * @version $Revision: 1.3 $
36  */

37 public abstract class AbstractNamespaceResolver implements NamespaceResolver {
38
39     /**
40      * Prefix to Namespace mapping
41      */

42     private Map JavaDoc m_namespaces = new HashMap JavaDoc(11);
43     
44     /**
45      * Number to append to the next namespace prefix that is created, e.g. ns1, ns2 etc
46      */

47     private int m_nNextPrefixAppend = 0;
48     
49     /**
50      * Logger for this class
51      */

52     private static final Logger m_logger = Logger.getLogger(AbstractNamespaceResolver.class.getName());
53     
54     /**
55      * Adds the default namespace URI-prefix mappings.
56      *
57      * @see NamespaceType
58      */

59     protected void addDefaultNamespaces() {
60         try {
61             this.addNamespace(NamespaceType.COL_RDF);
62             this.addNamespace(NamespaceType.DAV);
63             this.addNamespace(NamespaceType.LOM_CLS);
64             this.addNamespace(NamespaceType.LOM_XML);
65             this.addNamespace(NamespaceType.RDF);
66             this.addNamespace(NamespaceType.RDF_SCHEMA);
67             this.addNamespace(NamespaceType.XML);
68             this.addNamespace(NamespaceType.XML_SCHEMA);
69             this.addNamespace(NamespaceType.XML_SCHEMA_INSTANCE);
70             this.addNamespace(NamespaceType.OHRM);
71             this.addNamespace(NamespaceType.XSLFO);
72             this.addNamespace(NamespaceType.XSLT);
73         } catch (NamespaceClashException e) {
74             m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
75         }
76     }
77     
78     /**
79      * Adds a namespace URI-prefix mapping from a {@link NamespaceType}
80      * object.
81      *
82      * @param namespace Namespace information to add
83      * @throws NamespaceClashException Thrown if this prefix has already been used
84      */

85     private void addNamespace(NamespaceType namespace) throws NamespaceClashException {
86         this.addNamespace(namespace.getURI(), namespace.getPrefix());
87     }
88
89     /* (non-Javadoc)
90      * @see org.openharmonise.commons.xml.namespace.NamespaceResolver#addNamespace(java.lang.String, java.lang.String)
91      */

92     public void addNamespace(String JavaDoc sURI, String JavaDoc sPrefix)
93         throws NamespaceClashException {
94             
95         boolean bFound = false;
96         
97         synchronized (this.m_namespaces) {
98             Iterator JavaDoc itor = this.m_namespaces.values().iterator();
99             while( itor.hasNext() ) {
100                 Namespace ns = (Namespace)itor.next();
101                 if( ns.getPrefix().equals(sPrefix) ) {
102                     bFound=true;
103                 }
104             }
105         }
106             
107         if( bFound ) {
108             throw new NamespaceClashException();
109         } else {
110             m_namespaces.put(sPrefix, new Namespace(sURI, sPrefix));
111         }
112
113     }
114
115     /* (non-Javadoc)
116      * @see org.openharmonise.commons.xml.namespace.NamespaceResolver#getNamespaceByPrefix(java.lang.String)
117      */

118     public String JavaDoc getNamespaceByPrefix(String JavaDoc sPrefix) {
119         String JavaDoc sReturn = null;
120         
121         Namespace ns = (Namespace)m_namespaces.get(sPrefix);
122         if( ns!=null ) {
123             sReturn = ns.getURI();
124         }
125         
126         return sReturn;
127     }
128
129     /* (non-Javadoc)
130      * @see org.openharmonise.commons.xml.namespace.NamespaceResolver#getPrefixByNamespace(java.lang.String)
131      */

132     public String JavaDoc getPrefixByNamespace(String JavaDoc sURI) {
133         String JavaDoc sPrefix=null;
134         
135         synchronized (this.m_namespaces) {
136             Iterator JavaDoc itor = this.m_namespaces.values().iterator();
137             while( itor.hasNext() ) {
138                 Namespace ns = (Namespace)itor.next();
139                 if( ns.getURI().equals(sURI) ) {
140                     sPrefix=ns.getPrefix();
141                 }
142             }
143         }
144         
145         if( sPrefix==null ) {
146             sPrefix = this.createPrefix(sURI);
147             try {
148                 this.addNamespace(sURI, sPrefix);
149             } catch (NamespaceClashException e) {
150                 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
151             }
152         }
153         
154         return sPrefix;
155     }
156
157     /* (non-Javadoc)
158      * @see org.openharmonise.commons.xml.namespace.NamespaceResolver#getPrefixByNode(org.w3c.dom.Node)
159      */

160     public String JavaDoc getPrefixByNode(Node JavaDoc node) throws NamespaceClashException {
161         String JavaDoc sPrefix = null;
162         
163         String JavaDoc sURI = node.getNamespaceURI();
164         String JavaDoc sNodePrefix = node.getPrefix();
165         
166         if( sNodePrefix!=null && this.m_namespaces.containsKey(sNodePrefix) ) {
167             Namespace ns = (Namespace)this.m_namespaces.get(sNodePrefix);
168             if( sURI==null || (sURI!=null && sURI.equals(ns.m_sURI )) ) {
169                 sPrefix=ns.getPrefix();
170             } else {
171                 throw new NamespaceClashException("Node has a namespace prefix that is already in the resolver associated to a different namespace URI than the one associates to the node.");
172             }
173         } else if( sNodePrefix!=null && !this.m_namespaces.containsKey(sNodePrefix) ) {
174             if( sURI!=null ) {
175                 this.addNamespace(sURI, sNodePrefix);
176                 sPrefix=sNodePrefix;
177             } else {
178                 throw new NamespaceClashException("Node has prefix but no namespace URI and resolver does not have a namespace URI for that prefix.");
179             }
180         } else if( sNodePrefix==null && sURI!=null ) {
181             sPrefix = this.getPrefixByNamespace(sURI);
182         }
183         
184         return sPrefix;
185     }
186
187     /* (non-Javadoc)
188      * @see org.openharmonise.commons.xml.namespace.NamespaceResolver#removeNamespace(java.lang.String)
189      */

190     public void removeNamespace(String JavaDoc sURI) {
191         String JavaDoc sPrefix=null;
192         
193         synchronized (this.m_namespaces) {
194             Iterator JavaDoc itor = this.m_namespaces.values().iterator();
195             while( itor.hasNext() ) {
196                 Namespace ns = (Namespace)itor.next();
197                 if( ns.getURI().equals(sURI) ) {
198                     sPrefix=ns.getPrefix();
199                 }
200             }
201         }
202         
203         if( sPrefix!=null ) {
204             this.m_namespaces.remove(sPrefix);
205         }
206     }
207     
208     /**
209      * Creates a prefix for a namespace URI, e.g. ns1, ns2 etc.
210      *
211      * @param sURI Namespace URI
212      * @return Namespace prefix
213      */

214     private String JavaDoc createPrefix(String JavaDoc sURI) {
215         String JavaDoc sPrefix = "ns" + this.m_nNextPrefixAppend;
216         this.m_nNextPrefixAppend++;
217         
218         while( this.m_namespaces.containsKey(sPrefix) ) {
219             sPrefix = "ns" + this.m_nNextPrefixAppend;
220             this.m_nNextPrefixAppend++;
221         }
222         
223         return sPrefix;
224     }
225     
226     /**
227      * Holder for Namespace information, URI and prefix.
228      *
229      * @author Matthew Large
230      * @version $Revision: 1.3 $
231      *
232      */

233     private class Namespace {
234         
235         /**
236          * Namespace URI.
237          */

238         private String JavaDoc m_sURI;
239         
240         /**
241          * Namespace prefix.
242          */

243         private String JavaDoc m_sPrefix;
244         
245         /**
246          * Constructs new instance.
247          *
248          * @param sURI Namespace URI
249          * @param sPrefix Namespace prefix
250          */

251         public Namespace(String JavaDoc sURI, String JavaDoc sPrefix) {
252             this.m_sURI = sURI;
253             this.m_sPrefix = sPrefix;
254         }
255         
256         /**
257          * Returns the Namespace URI.
258          *
259          * @return Namespace URI
260          */

261         public String JavaDoc getURI() {
262             return this.m_sURI;
263         }
264         
265         /**
266          * Returns the Namespace prefix.
267          *
268          * @return Namespace prefix
269          */

270         public String JavaDoc getPrefix() {
271             return this.m_sPrefix;
272         }
273         
274         /* (non-Javadoc)
275          * @see java.lang.Object#toString()
276          */

277         public String JavaDoc toString() {
278             return m_sPrefix;
279         }
280     }
281
282 }
283
Popular Tags