View Javadoc

1   package org.apache.maven.archetype.common.util;
2   
3   /*
4    * Copyright (C) 2000-2004 Jason Hunter & Brett McLaughlin.
5    * All rights reserved.
6    *
7    * Redistribution and use in source and binary forms, with or without
8    * modification, are permitted provided that the following conditions
9    * are met:
10   *
11   * 1. Redistributions of source code must retain the above copyright
12   *    notice, this list of conditions, and the following disclaimer.
13   *
14   * 2. Redistributions in binary form must reproduce the above copyright
15   *    notice, this list of conditions, and the disclaimer that follows 
16   *    these conditions in the documentation and/or other materials 
17   *    provided with the distribution.
18   *
19   * 3. The name "JDOM" must not be used to endorse or promote products
20   *    derived from this software without prior written permission.  For
21   *    written permission, please contact <request_AT_jdom_DOT_org>.
22   *
23   * 4. Products derived from this software may not be called "JDOM", nor
24   *    may "JDOM" appear in their name, without prior written permission
25   *    from the JDOM Project Management <request_AT_jdom_DOT_org>.
26   *
27   * In addition, we request (but do not require) that you include in the 
28   * end-user documentation provided with the redistribution and/or in the 
29   * software itself an acknowledgement equivalent to the following:
30   *     "This product includes software developed by the
31   *      JDOM Project (http://www.jdom.org/)."
32   * Alternatively, the acknowledgment may be graphical using the logos 
33   * available at http://www.jdom.org/images/logos.
34   *
35   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38   * DISCLAIMED.  IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
39   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46   * SUCH DAMAGE.
47   *
48   * This software consists of voluntary contributions made by many 
49   * individuals on behalf of the JDOM Project and was originally 
50   * created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
51   * Brett McLaughlin <brett_AT_jdom_DOT_org>.  For more information
52   * on the JDOM Project, please see <http://www.jdom.org/>.
53   */
54  
55  import org.jdom.Namespace;
56  
57  import java.util.Stack;
58  
59  /**
60   * A non-public utility class used by both <code>{@link XMLOutputter}</code> and
61   * <code>{@link SAXOutputter}</code> to manage namespaces in a JDOM Document
62   * during output.
63   *
64   * @author Elliotte Rusty Harolde
65   * @author Fred Trimble
66   * @author Brett McLaughlin
67   * @version $Revision: 1.13 $, $Date: 2004/02/06 09:28:32 $
68   */
69  class NamespaceStack
70  {
71  
72      @SuppressWarnings( "unused" )
73      private static final String CVS_ID =
74          "@(#) $RCSfile: NamespaceStack.java,v $ $Revision: 1.13 $ $Date: 2004/02/06 09:28:32 $ $Name: jdom_1_0 $";
75  
76      /** The prefixes available */
77      private Stack<String> prefixes;
78  
79      /** The URIs available */
80      private Stack<String> uris;
81  
82      /** This creates the needed storage. */
83      NamespaceStack()
84      {
85          prefixes = new Stack<String>();
86          uris = new Stack<String>();
87      }
88  
89      /**
90       * This will add a new <code>{@link Namespace}</code>
91       * to those currently available.
92       *
93       * @param ns <code>Namespace</code> to add.
94       */
95      public void push( Namespace ns )
96      {
97          prefixes.push( ns.getPrefix() );
98          uris.push( ns.getURI() );
99      }
100 
101     /**
102      * This will remove the topmost (most recently added)
103      * <code>{@link Namespace}</code>, and return its prefix.
104      *
105      * @return <code>String</code> - the popped namespace prefix.
106      */
107     public String pop()
108     {
109         String prefix = (String) prefixes.pop();
110         uris.pop();
111 
112         return prefix;
113     }
114 
115     /**
116      * This returns the number of available namespaces.
117      *
118      * @return <code>int</code> - size of the namespace stack.
119      */
120     public int size()
121     {
122         return prefixes.size();
123     }
124 
125     /**
126      * Given a prefix, this will return the namespace URI most
127      * rencently (topmost) associated with that prefix.
128      *
129      * @param prefix <code>String</code> namespace prefix.
130      * @return <code>String</code> - the namespace URI for that prefix.
131      */
132     public String getURI( String prefix )
133     {
134         int index = prefixes.lastIndexOf( prefix );
135         if ( index == -1 )
136         {
137             return null;
138         }
139         String uri = (String) uris.elementAt( index );
140         return uri;
141     }
142 
143     /**
144      * This will print out the size and current stack, from the
145      * most recently added <code>{@link Namespace}</code> to
146      * the "oldest," all to <code>System.out</code>.
147      */
148     public String toString()
149     {
150         StringBuffer buf = new StringBuffer();
151         String sep = System.getProperty( "line.separator" );
152         buf.append( "Stack: " + prefixes.size() + sep );
153         for ( int i = 0; i < prefixes.size(); i++ )
154         {
155             buf.append( prefixes.elementAt( i ) + "&" + uris.elementAt( i ) + sep );
156         }
157         return buf.toString();
158     }
159 }
160