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.jdom2.Namespace;
56  
57  import java.util.Stack;
58  
59  /**
60   * A non-public utility class used by <code>{@link XMLOutputter}</code>
61   * to manage namespaces in a JDOM Document during output.
62   *
63   * @author Elliotte Rusty Harolde
64   * @author Fred Trimble
65   * @author Brett McLaughlin
66   */
67  class NamespaceStack
68  {
69      /** The prefixes available */
70      private Stack<String> prefixes;
71  
72      /** The URIs available */
73      private Stack<String> uris;
74  
75      /** This creates the needed storage. */
76      NamespaceStack()
77      {
78          prefixes = new Stack<>();
79          uris = new Stack<>();
80      }
81  
82      /**
83       * This will add a new <code>{@link Namespace}</code>
84       * to those currently available.
85       *
86       * @param ns <code>Namespace</code> to add.
87       */
88      public void push( Namespace ns )
89      {
90          prefixes.push( ns.getPrefix() );
91          uris.push( ns.getURI() );
92      }
93  
94      /**
95       * This will remove the topmost (most recently added)
96       * <code>{@link Namespace}</code>, and return its prefix.
97       *
98       * @return <code>String</code> - the popped namespace prefix.
99       */
100     public String pop()
101     {
102         String prefix = prefixes.pop();
103         uris.pop();
104 
105         return prefix;
106     }
107 
108     /**
109      * This returns the number of available namespaces.
110      *
111      * @return <code>int</code> - size of the namespace stack.
112      */
113     public int size()
114     {
115         return prefixes.size();
116     }
117 
118     /**
119      * Given a prefix, this will return the namespace URI most
120      * rencently (topmost) associated with that prefix.
121      *
122      * @param prefix <code>String</code> namespace prefix.
123      * @return <code>String</code> - the namespace URI for that prefix.
124      */
125     public String getURI( String prefix )
126     {
127         int index = prefixes.lastIndexOf( prefix );
128         if ( index == -1 )
129         {
130             return null;
131         }
132         String uri = uris.elementAt( index );
133         return uri;
134     }
135 
136     /**
137      * This will print out the size and current stack, from the
138      * most recently added <code>{@link Namespace}</code> to
139      * the "oldest," all to <code>System.out</code>.
140      */
141     @Override
142     public String toString()
143     {
144         StringBuilder buf = new StringBuilder();
145         String sep = System.lineSeparator();
146         buf.append( "Stack: " + prefixes.size() + sep );
147         for ( int i = 0; i < prefixes.size(); i++ )
148         {
149             buf.append( prefixes.elementAt( i ) + "&" + uris.elementAt( i ) + sep );
150         }
151         return buf.toString();
152     }
153 }
154