Source Code Cross Referenced for NamespaceSupport.java in  » 6.0-JDK-Core » xml » org » xml » sax » helpers » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » xml » org.xml.sax.helpers 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001        /*
002         * Copyright 2000-2005 Sun Microsystems, Inc.  All Rights Reserved.
003         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004         *
005         * This code is free software; you can redistribute it and/or modify it
006         * under the terms of the GNU General Public License version 2 only, as
007         * published by the Free Software Foundation.  Sun designates this
008         * particular file as subject to the "Classpath" exception as provided
009         * by Sun in the LICENSE file that accompanied this code.
010         *
011         * This code is distributed in the hope that it will be useful, but WITHOUT
012         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014         * version 2 for more details (a copy is included in the LICENSE file that
015         * accompanied this code).
016         *
017         * You should have received a copy of the GNU General Public License version
018         * 2 along with this work; if not, write to the Free Software Foundation,
019         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020         *
021         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022         * CA 95054 USA or visit www.sun.com if you need additional information or
023         * have any questions.
024         */
025
026        // NamespaceSupport.java - generic Namespace support for SAX.
027        // http://www.saxproject.org
028        // Written by David Megginson
029        // This class is in the Public Domain.  NO WARRANTY!
030        // $Id: NamespaceSupport.java,v 1.5 2004/11/03 22:53:09 jsuttor Exp $
031        package org.xml.sax.helpers;
032
033        import java.util.EmptyStackException;
034        import java.util.Enumeration;
035        import java.util.Hashtable;
036        import java.util.Vector;
037
038        /**
039         * Encapsulate Namespace logic for use by applications using SAX,
040         * or internally by SAX drivers.
041         *
042         * <blockquote>
043         * <em>This module, both source code and documentation, is in the
044         * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
045         * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
046         * for further information.
047         * </blockquote>
048         *
049         * <p>This class encapsulates the logic of Namespace processing: it
050         * tracks the declarations currently in force for each context and
051         * automatically processes qualified XML names into their Namespace
052         * parts; it can also be used in reverse for generating XML qnames
053         * from Namespaces.</p>
054         *
055         * <p>Namespace support objects are reusable, but the reset method
056         * must be invoked between each session.</p>
057         *
058         * <p>Here is a simple session:</p>
059         *
060         * <pre>
061         * String parts[] = new String[3];
062         * NamespaceSupport support = new NamespaceSupport();
063         *
064         * support.pushContext();
065         * support.declarePrefix("", "http://www.w3.org/1999/xhtml");
066         * support.declarePrefix("dc", "http://www.purl.org/dc#");
067         *
068         * parts = support.processName("p", parts, false);
069         * System.out.println("Namespace URI: " + parts[0]);
070         * System.out.println("Local name: " + parts[1]);
071         * System.out.println("Raw name: " + parts[2]);
072         *
073         * parts = support.processName("dc:title", parts, false);
074         * System.out.println("Namespace URI: " + parts[0]);
075         * System.out.println("Local name: " + parts[1]);
076         * System.out.println("Raw name: " + parts[2]);
077         *
078         * support.popContext();
079         * </pre>
080         *
081         * <p>Note that this class is optimized for the use case where most
082         * elements do not contain Namespace declarations: if the same
083         * prefix/URI mapping is repeated for each context (for example), this
084         * class will be somewhat less efficient.</p>
085         *
086         * <p>Although SAX drivers (parsers) may choose to use this class to
087         * implement namespace handling, they are not required to do so.
088         * Applications must track namespace information themselves if they
089         * want to use namespace information.
090         *
091         * @since SAX 2.0
092         * @author David Megginson
093         * @version 2.0.1 (sax2r2)
094         */
095        public class NamespaceSupport {
096
097            ////////////////////////////////////////////////////////////////////
098            // Constants.
099            ////////////////////////////////////////////////////////////////////
100
101            /**
102             * The XML Namespace URI as a constant.
103             * The value is <code>http://www.w3.org/XML/1998/namespace</code>
104             * as defined in the "Namespaces in XML" * recommendation.
105             *
106             * <p>This is the Namespace URI that is automatically mapped
107             * to the "xml" prefix.</p>
108             */
109            public final static String XMLNS = "http://www.w3.org/XML/1998/namespace";
110
111            /**
112             * The namespace declaration URI as a constant.
113             * The value is <code>http://www.w3.org/xmlns/2000/</code>, as defined
114             * in a backwards-incompatible erratum to the "Namespaces in XML"
115             * recommendation.  Because that erratum postdated SAX2, SAX2 defaults 
116             * to the original recommendation, and does not normally use this URI.
117             * 
118             *
119             * <p>This is the Namespace URI that is optionally applied to
120             * <em>xmlns</em> and <em>xmlns:*</em> attributes, which are used to
121             * declare namespaces.  </p>
122             *
123             * @since SAX 2.1alpha
124             * @see #setNamespaceDeclUris
125             * @see #isNamespaceDeclUris
126             */
127            public final static String NSDECL = "http://www.w3.org/xmlns/2000/";
128
129            /**
130             * An empty enumeration.
131             */
132            private final static Enumeration EMPTY_ENUMERATION = new Vector()
133                    .elements();
134
135            ////////////////////////////////////////////////////////////////////
136            // Constructor.
137            ////////////////////////////////////////////////////////////////////
138
139            /**
140             * Create a new Namespace support object.
141             */
142            public NamespaceSupport() {
143                reset();
144            }
145
146            ////////////////////////////////////////////////////////////////////
147            // Context management.
148            ////////////////////////////////////////////////////////////////////
149
150            /**
151             * Reset this Namespace support object for reuse.
152             *
153             * <p>It is necessary to invoke this method before reusing the
154             * Namespace support object for a new session.  If namespace
155             * declaration URIs are to be supported, that flag must also
156             * be set to a non-default value.
157             * </p>
158             *
159             * @see #setNamespaceDeclUris
160             */
161            public void reset() {
162                contexts = new Context[32];
163                namespaceDeclUris = false;
164                contextPos = 0;
165                contexts[contextPos] = currentContext = new Context();
166                currentContext.declarePrefix("xml", XMLNS);
167            }
168
169            /**
170             * Start a new Namespace context.
171             * The new context will automatically inherit
172             * the declarations of its parent context, but it will also keep
173             * track of which declarations were made within this context.
174             *
175             * <p>Event callback code should start a new context once per element.
176             * This means being ready to call this in either of two places.
177             * For elements that don't include namespace declarations, the
178             * <em>ContentHandler.startElement()</em> callback is the right place.
179             * For elements with such a declaration, it'd done in the first
180             * <em>ContentHandler.startPrefixMapping()</em> callback.
181             * A boolean flag can be used to
182             * track whether a context has been started yet.  When either of
183             * those methods is called, it checks the flag to see if a new context
184             * needs to be started.  If so, it starts the context and sets the
185             * flag.  After <em>ContentHandler.startElement()</em>
186             * does that, it always clears the flag.
187             *
188             * <p>Normally, SAX drivers would push a new context at the beginning
189             * of each XML element.  Then they perform a first pass over the
190             * attributes to process all namespace declarations, making
191             * <em>ContentHandler.startPrefixMapping()</em> callbacks.
192             * Then a second pass is made, to determine the namespace-qualified
193             * names for all attributes and for the element name.
194             * Finally all the information for the
195             * <em>ContentHandler.startElement()</em> callback is available,
196             * so it can then be made.
197             *
198             * <p>The Namespace support object always starts with a base context
199             * already in force: in this context, only the "xml" prefix is
200             * declared.</p>
201             *
202             * @see org.xml.sax.ContentHandler
203             * @see #popContext
204             */
205            public void pushContext() {
206                int max = contexts.length;
207
208                contextPos++;
209
210                // Extend the array if necessary
211                if (contextPos >= max) {
212                    Context newContexts[] = new Context[max * 2];
213                    System.arraycopy(contexts, 0, newContexts, 0, max);
214                    max *= 2;
215                    contexts = newContexts;
216                }
217
218                // Allocate the context if necessary.
219                currentContext = contexts[contextPos];
220                if (currentContext == null) {
221                    contexts[contextPos] = currentContext = new Context();
222                }
223
224                // Set the parent, if any.
225                if (contextPos > 0) {
226                    currentContext.setParent(contexts[contextPos - 1]);
227                }
228            }
229
230            /**
231             * Revert to the previous Namespace context.
232             *
233             * <p>Normally, you should pop the context at the end of each
234             * XML element.  After popping the context, all Namespace prefix
235             * mappings that were previously in force are restored.</p>
236             *
237             * <p>You must not attempt to declare additional Namespace
238             * prefixes after popping a context, unless you push another
239             * context first.</p>
240             *
241             * @see #pushContext
242             */
243            public void popContext() {
244                contexts[contextPos].clear();
245                contextPos--;
246                if (contextPos < 0) {
247                    throw new EmptyStackException();
248                }
249                currentContext = contexts[contextPos];
250            }
251
252            ////////////////////////////////////////////////////////////////////
253            // Operations within a context.
254            ////////////////////////////////////////////////////////////////////
255
256            /**
257             * Declare a Namespace prefix.  All prefixes must be declared
258             * before they are referenced.  For example, a SAX driver (parser)
259             * would scan an element's attributes
260             * in two passes:  first for namespace declarations,
261             * then a second pass using {@link #processName processName()} to
262             * interpret prefixes against (potentially redefined) prefixes.
263             *
264             * <p>This method declares a prefix in the current Namespace
265             * context; the prefix will remain in force until this context
266             * is popped, unless it is shadowed in a descendant context.</p>
267             *
268             * <p>To declare the default element Namespace, use the empty string as
269             * the prefix.</p>
270             *
271             * <p>Note that there is an asymmetry in this library: {@link
272             * #getPrefix getPrefix} will not return the "" prefix,
273             * even if you have declared a default element namespace.
274             * To check for a default namespace,
275             * you have to look it up explicitly using {@link #getURI getURI}.
276             * This asymmetry exists to make it easier to look up prefixes
277             * for attribute names, where the default prefix is not allowed.</p>
278             *
279             * @param prefix The prefix to declare, or the empty string to
280             *	indicate the default element namespace.  This may never have
281             *	the value "xml" or "xmlns".
282             * @param uri The Namespace URI to associate with the prefix.
283             * @return true if the prefix was legal, false otherwise
284             *
285             * @see #processName
286             * @see #getURI
287             * @see #getPrefix
288             */
289            public boolean declarePrefix(String prefix, String uri) {
290                if (prefix.equals("xml") || prefix.equals("xmlns")) {
291                    return false;
292                } else {
293                    currentContext.declarePrefix(prefix, uri);
294                    return true;
295                }
296            }
297
298            /**
299             * Process a raw XML qualified name, after all declarations in the
300             * current context have been handled by {@link #declarePrefix
301             * declarePrefix()}.
302             *
303             * <p>This method processes a raw XML qualified name in the
304             * current context by removing the prefix and looking it up among
305             * the prefixes currently declared.  The return value will be the
306             * array supplied by the caller, filled in as follows:</p>
307             *
308             * <dl>
309             * <dt>parts[0]</dt>
310             * <dd>The Namespace URI, or an empty string if none is
311             *  in use.</dd>
312             * <dt>parts[1]</dt>
313             * <dd>The local name (without prefix).</dd>
314             * <dt>parts[2]</dt>
315             * <dd>The original raw name.</dd>
316             * </dl>
317             *
318             * <p>All of the strings in the array will be internalized.  If
319             * the raw name has a prefix that has not been declared, then
320             * the return value will be null.</p>
321             *
322             * <p>Note that attribute names are processed differently than
323             * element names: an unprefixed element name will receive the
324             * default Namespace (if any), while an unprefixed attribute name
325             * will not.</p>
326             *
327             * @param qName The XML qualified name to be processed.
328             * @param parts An array supplied by the caller, capable of
329             *        holding at least three members.
330             * @param isAttribute A flag indicating whether this is an
331             *        attribute name (true) or an element name (false).
332             * @return The supplied array holding three internalized strings 
333             *        representing the Namespace URI (or empty string), the
334             *        local name, and the XML qualified name; or null if there
335             *        is an undeclared prefix.
336             * @see #declarePrefix
337             * @see java.lang.String#intern */
338            public String[] processName(String qName, String parts[],
339                    boolean isAttribute) {
340                String myParts[] = currentContext.processName(qName,
341                        isAttribute);
342                if (myParts == null) {
343                    return null;
344                } else {
345                    parts[0] = myParts[0];
346                    parts[1] = myParts[1];
347                    parts[2] = myParts[2];
348                    return parts;
349                }
350            }
351
352            /**
353             * Look up a prefix and get the currently-mapped Namespace URI.
354             *
355             * <p>This method looks up the prefix in the current context.
356             * Use the empty string ("") for the default Namespace.</p>
357             *
358             * @param prefix The prefix to look up.
359             * @return The associated Namespace URI, or null if the prefix
360             *         is undeclared in this context.
361             * @see #getPrefix
362             * @see #getPrefixes
363             */
364            public String getURI(String prefix) {
365                return currentContext.getURI(prefix);
366            }
367
368            /**
369             * Return an enumeration of all prefixes whose declarations are
370             * active in the current context.
371             * This includes declarations from parent contexts that have
372             * not been overridden.
373             *
374             * <p><strong>Note:</strong> if there is a default prefix, it will not be
375             * returned in this enumeration; check for the default prefix
376             * using the {@link #getURI getURI} with an argument of "".</p>
377             *
378             * @return An enumeration of prefixes (never empty).
379             * @see #getDeclaredPrefixes
380             * @see #getURI
381             */
382            public Enumeration getPrefixes() {
383                return currentContext.getPrefixes();
384            }
385
386            /**
387             * Return one of the prefixes mapped to a Namespace URI.
388             *
389             * <p>If more than one prefix is currently mapped to the same
390             * URI, this method will make an arbitrary selection; if you
391             * want all of the prefixes, use the {@link #getPrefixes}
392             * method instead.</p>
393             *
394             * <p><strong>Note:</strong> this will never return the empty (default) prefix;
395             * to check for a default prefix, use the {@link #getURI getURI}
396             * method with an argument of "".</p>
397             *
398             * @param uri the namespace URI
399             * @return one of the prefixes currently mapped to the URI supplied,
400             *         or null if none is mapped or if the URI is assigned to
401             *         the default namespace
402             * @see #getPrefixes(java.lang.String)
403             * @see #getURI
404             */
405            public String getPrefix(String uri) {
406                return currentContext.getPrefix(uri);
407            }
408
409            /**
410             * Return an enumeration of all prefixes for a given URI whose
411             * declarations are active in the current context.
412             * This includes declarations from parent contexts that have
413             * not been overridden.
414             *
415             * <p>This method returns prefixes mapped to a specific Namespace
416             * URI.  The xml: prefix will be included.  If you want only one
417             * prefix that's mapped to the Namespace URI, and you don't care 
418             * which one you get, use the {@link #getPrefix getPrefix}
419             *  method instead.</p>
420             *
421             * <p><strong>Note:</strong> the empty (default) prefix is <em>never</em> included
422             * in this enumeration; to check for the presence of a default
423             * Namespace, use the {@link #getURI getURI} method with an
424             * argument of "".</p>
425             *
426             * @param uri The Namespace URI.
427             * @return An enumeration of prefixes (never empty).
428             * @see #getPrefix
429             * @see #getDeclaredPrefixes
430             * @see #getURI
431             */
432            public Enumeration getPrefixes(String uri) {
433                Vector prefixes = new Vector();
434                Enumeration allPrefixes = getPrefixes();
435                while (allPrefixes.hasMoreElements()) {
436                    String prefix = (String) allPrefixes.nextElement();
437                    if (uri.equals(getURI(prefix))) {
438                        prefixes.addElement(prefix);
439                    }
440                }
441                return prefixes.elements();
442            }
443
444            /**
445             * Return an enumeration of all prefixes declared in this context.
446             *
447             * <p>The empty (default) prefix will be included in this 
448             * enumeration; note that this behaviour differs from that of
449             * {@link #getPrefix} and {@link #getPrefixes}.</p>
450             *
451             * @return An enumeration of all prefixes declared in this
452             *         context.
453             * @see #getPrefixes
454             * @see #getURI
455             */
456            public Enumeration getDeclaredPrefixes() {
457                return currentContext.getDeclaredPrefixes();
458            }
459
460            /**
461             * Controls whether namespace declaration attributes are placed
462             * into the {@link #NSDECL NSDECL} namespace
463             * by {@link #processName processName()}.  This may only be
464             * changed before any contexts have been pushed.
465             *
466             * @since SAX 2.1alpha
467             *
468             * @exception IllegalStateException when attempting to set this
469             *	after any context has been pushed.
470             */
471            public void setNamespaceDeclUris(boolean value) {
472                if (contextPos != 0)
473                    throw new IllegalStateException();
474                if (value == namespaceDeclUris)
475                    return;
476                namespaceDeclUris = value;
477                if (value)
478                    currentContext.declarePrefix("xmlns", NSDECL);
479                else {
480                    contexts[contextPos] = currentContext = new Context();
481                    currentContext.declarePrefix("xml", XMLNS);
482                }
483            }
484
485            /**
486             * Returns true if namespace declaration attributes are placed into
487             * a namespace.  This behavior is not the default.
488             *
489             * @since SAX 2.1alpha
490             */
491            public boolean isNamespaceDeclUris() {
492                return namespaceDeclUris;
493            }
494
495            ////////////////////////////////////////////////////////////////////
496            // Internal state.
497            ////////////////////////////////////////////////////////////////////
498
499            private Context contexts[];
500            private Context currentContext;
501            private int contextPos;
502            private boolean namespaceDeclUris;
503
504            ////////////////////////////////////////////////////////////////////
505            // Internal classes.
506            ////////////////////////////////////////////////////////////////////
507
508            /**
509             * Internal class for a single Namespace context.
510             *
511             * <p>This module caches and reuses Namespace contexts,
512             * so the number allocated
513             * will be equal to the element depth of the document, not to the total
514             * number of elements (i.e. 5-10 rather than tens of thousands).
515             * Also, data structures used to represent contexts are shared when
516             * possible (child contexts without declarations) to further reduce
517             * the amount of memory that's consumed.
518             * </p>
519             */
520            final class Context {
521
522                /**
523                 * Create the root-level Namespace context.
524                 */
525                Context() {
526                    copyTables();
527                }
528
529                /**
530                 * (Re)set the parent of this Namespace context.
531                 * The context must either have been freshly constructed,
532                 * or must have been cleared.
533                 *
534                 * @param context The parent Namespace context object.
535                 */
536                void setParent(Context parent) {
537                    this .parent = parent;
538                    declarations = null;
539                    prefixTable = parent.prefixTable;
540                    uriTable = parent.uriTable;
541                    elementNameTable = parent.elementNameTable;
542                    attributeNameTable = parent.attributeNameTable;
543                    defaultNS = parent.defaultNS;
544                    declSeen = false;
545                }
546
547                /**
548                 * Makes associated state become collectible,
549                 * invalidating this context.
550                 * {@link #setParent} must be called before
551                 * this context may be used again.
552                 */
553                void clear() {
554                    parent = null;
555                    prefixTable = null;
556                    uriTable = null;
557                    elementNameTable = null;
558                    attributeNameTable = null;
559                    defaultNS = null;
560                }
561
562                /**
563                 * Declare a Namespace prefix for this context.
564                 *
565                 * @param prefix The prefix to declare.
566                 * @param uri The associated Namespace URI.
567                 * @see org.xml.sax.helpers.NamespaceSupport#declarePrefix
568                 */
569                void declarePrefix(String prefix, String uri) {
570                    // Lazy processing...
571                    //	    if (!declsOK)
572                    //		throw new IllegalStateException (
573                    //		    "can't declare any more prefixes in this context");
574                    if (!declSeen) {
575                        copyTables();
576                    }
577                    if (declarations == null) {
578                        declarations = new Vector();
579                    }
580
581                    prefix = prefix.intern();
582                    uri = uri.intern();
583                    if ("".equals(prefix)) {
584                        if ("".equals(uri)) {
585                            defaultNS = null;
586                        } else {
587                            defaultNS = uri;
588                        }
589                    } else {
590                        prefixTable.put(prefix, uri);
591                        uriTable.put(uri, prefix); // may wipe out another prefix
592                    }
593                    declarations.addElement(prefix);
594                }
595
596                /**
597                 * Process an XML qualified name in this context.
598                 *
599                 * @param qName The XML qualified name.
600                 * @param isAttribute true if this is an attribute name.
601                 * @return An array of three strings containing the
602                 *         URI part (or empty string), the local part,
603                 *         and the raw name, all internalized, or null
604                 *         if there is an undeclared prefix.
605                 * @see org.xml.sax.helpers.NamespaceSupport#processName
606                 */
607                String[] processName(String qName, boolean isAttribute) {
608                    String name[];
609                    Hashtable table;
610
611                    // Select the appropriate table.
612                    if (isAttribute) {
613                        table = attributeNameTable;
614                    } else {
615                        table = elementNameTable;
616                    }
617
618                    // Start by looking in the cache, and
619                    // return immediately if the name
620                    // is already known in this content
621                    name = (String[]) table.get(qName);
622                    if (name != null) {
623                        return name;
624                    }
625
626                    // We haven't seen this name in this
627                    // context before.  Maybe in the parent
628                    // context, but we can't assume prefix
629                    // bindings are the same.
630                    name = new String[3];
631                    name[2] = qName.intern();
632                    int index = qName.indexOf(':');
633
634                    // No prefix.
635                    if (index == -1) {
636                        if (isAttribute) {
637                            if (qName == "xmlns" && namespaceDeclUris)
638                                name[0] = NSDECL;
639                            else
640                                name[0] = "";
641                        } else if (defaultNS == null) {
642                            name[0] = "";
643                        } else {
644                            name[0] = defaultNS;
645                        }
646                        name[1] = name[2];
647                    }
648
649                    // Prefix
650                    else {
651                        String prefix = qName.substring(0, index);
652                        String local = qName.substring(index + 1);
653                        String uri;
654                        if ("".equals(prefix)) {
655                            uri = defaultNS;
656                        } else {
657                            uri = (String) prefixTable.get(prefix);
658                        }
659                        if (uri == null
660                                || (!isAttribute && "xmlns".equals(prefix))) {
661                            return null;
662                        }
663                        name[0] = uri;
664                        name[1] = local.intern();
665                    }
666
667                    // Save in the cache for future use.
668                    // (Could be shared with parent context...)
669                    table.put(name[2], name);
670                    return name;
671                }
672
673                /**
674                 * Look up the URI associated with a prefix in this context.
675                 *
676                 * @param prefix The prefix to look up.
677                 * @return The associated Namespace URI, or null if none is
678                 *         declared.	
679                 * @see org.xml.sax.helpers.NamespaceSupport#getURI
680                 */
681                String getURI(String prefix) {
682                    if ("".equals(prefix)) {
683                        return defaultNS;
684                    } else if (prefixTable == null) {
685                        return null;
686                    } else {
687                        return (String) prefixTable.get(prefix);
688                    }
689                }
690
691                /**
692                 * Look up one of the prefixes associated with a URI in this context.
693                 *
694                 * <p>Since many prefixes may be mapped to the same URI,
695                 * the return value may be unreliable.</p>
696                 *
697                 * @param uri The URI to look up.
698                 * @return The associated prefix, or null if none is declared.
699                 * @see org.xml.sax.helpers.NamespaceSupport#getPrefix
700                 */
701                String getPrefix(String uri) {
702                    if (uriTable == null) {
703                        return null;
704                    } else {
705                        return (String) uriTable.get(uri);
706                    }
707                }
708
709                /**
710                 * Return an enumeration of prefixes declared in this context.
711                 *
712                 * @return An enumeration of prefixes (possibly empty).
713                 * @see org.xml.sax.helpers.NamespaceSupport#getDeclaredPrefixes
714                 */
715                Enumeration getDeclaredPrefixes() {
716                    if (declarations == null) {
717                        return EMPTY_ENUMERATION;
718                    } else {
719                        return declarations.elements();
720                    }
721                }
722
723                /**
724                 * Return an enumeration of all prefixes currently in force.
725                 *
726                 * <p>The default prefix, if in force, is <em>not</em>
727                 * returned, and will have to be checked for separately.</p>
728                 *
729                 * @return An enumeration of prefixes (never empty).
730                 * @see org.xml.sax.helpers.NamespaceSupport#getPrefixes
731                 */
732                Enumeration getPrefixes() {
733                    if (prefixTable == null) {
734                        return EMPTY_ENUMERATION;
735                    } else {
736                        return prefixTable.keys();
737                    }
738                }
739
740                ////////////////////////////////////////////////////////////////
741                // Internal methods.
742                ////////////////////////////////////////////////////////////////
743
744                /**
745                 * Copy on write for the internal tables in this context.
746                 *
747                 * <p>This class is optimized for the normal case where most
748                 * elements do not contain Namespace declarations.</p>
749                 */
750                private void copyTables() {
751                    if (prefixTable != null) {
752                        prefixTable = (Hashtable) prefixTable.clone();
753                    } else {
754                        prefixTable = new Hashtable();
755                    }
756                    if (uriTable != null) {
757                        uriTable = (Hashtable) uriTable.clone();
758                    } else {
759                        uriTable = new Hashtable();
760                    }
761                    elementNameTable = new Hashtable();
762                    attributeNameTable = new Hashtable();
763                    declSeen = true;
764                }
765
766                ////////////////////////////////////////////////////////////////
767                // Protected state.
768                ////////////////////////////////////////////////////////////////
769
770                Hashtable prefixTable;
771                Hashtable uriTable;
772                Hashtable elementNameTable;
773                Hashtable attributeNameTable;
774                String defaultNS = null;
775
776                ////////////////////////////////////////////////////////////////
777                // Internal state.
778                ////////////////////////////////////////////////////////////////
779
780                private Vector declarations = null;
781                private boolean declSeen = false;
782                private Context parent = null;
783            }
784        }
785
786        // end of NamespaceSupport.java
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.