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