src/share/jaxws_classes/com/sun/xml/internal/stream/buffer/stax/NamespaceContexHelper.java

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/stream/buffer/stax/NamespaceContexHelper.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,286 @@
     1.4 +/*
     1.5 + * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.stream.buffer.stax;
    1.30 +
    1.31 +import java.util.ArrayList;
    1.32 +import java.util.Collections;
    1.33 +import java.util.Iterator;
    1.34 +import java.util.List;
    1.35 +import com.sun.xml.internal.org.jvnet.staxex.NamespaceContextEx;
    1.36 +
    1.37 +/**
    1.38 + * A helper class for managing the declaration of namespaces.
    1.39 + * <p>
    1.40 + * A namespace is declared on a namespace context.
    1.41 + * Namespace contexts are pushed on and popped off the namespace context stack.
    1.42 + * <p>
    1.43 + * A declared namespace will be in scope iff the context that it was declared on
    1.44 + * has not been popped off the stack.
    1.45 + * <p>
    1.46 + * When instantiated the namespace stack consists of the root namespace context,
    1.47 + * which contains, by default, the "xml" and "xmlns" declarations.
    1.48 + * Namespaces may be declarations may be declared on the root context.
    1.49 + * The root context cannot be popped but can be reset to contain just the
    1.50 + * "xml" and "xmlns" declarations.
    1.51 + * <p>
    1.52 + * Implementation note: determining the prefix from a namespace URI
    1.53 + * (or vice versa) is efficient when there are few namespace
    1.54 + * declarations i.e. what is considered to be the case for namespace
    1.55 + * declarations in 'average' XML documents. The look up of a namespace URI
    1.56 + * given a prefix is performed in O(n) time. The look up of a prefix given
    1.57 + * a namespace URI is performed in O(2n) time.
    1.58 + * <p>
    1.59 + * The implementation does not scale when there are many namespace
    1.60 + * declarations. TODO: Use a hash map when there are many namespace
    1.61 + * declarations.
    1.62 + *
    1.63 + * @author Paul.Sandoz@Sun.Com
    1.64 + */
    1.65 +final public class NamespaceContexHelper implements NamespaceContextEx {
    1.66 +    private static int DEFAULT_SIZE = 8;
    1.67 +
    1.68 +    // The prefixes of the namespace declarations
    1.69 +    private String[] prefixes = new String[DEFAULT_SIZE];
    1.70 +    // The URIs of the namespace declarations
    1.71 +    private String[] namespaceURIs = new String[DEFAULT_SIZE];
    1.72 +    // Current position to store the next namespace declaration
    1.73 +    private int namespacePosition;
    1.74 +
    1.75 +    // The namespace contexts
    1.76 +    private int[] contexts = new int[DEFAULT_SIZE];
    1.77 +    // Current position to store the next namespace context
    1.78 +    private int contextPosition;
    1.79 +
    1.80 +    /**
    1.81 +     * Create a new NamespaceContexHelper.
    1.82 +     *
    1.83 +     */
    1.84 +    public NamespaceContexHelper() {
    1.85 +        // The default namespace declarations that are always in scope
    1.86 +        prefixes[0] = "xml";
    1.87 +        namespaceURIs[0] = "http://www.w3.org/XML/1998/namespace";
    1.88 +        prefixes[1] = "xmlns";
    1.89 +        namespaceURIs[1] = "http://www.w3.org/2000/xmlns/";
    1.90 +
    1.91 +        namespacePosition = 2;
    1.92 +    }
    1.93 +
    1.94 +
    1.95 +    // NamespaceContext interface
    1.96 +
    1.97 +    public String getNamespaceURI(String prefix) {
    1.98 +        if (prefix == null) throw new IllegalArgumentException();
    1.99 +
   1.100 +        prefix = prefix.intern();
   1.101 +
   1.102 +        for (int i = namespacePosition - 1; i >= 0; i--) {
   1.103 +            final String declaredPrefix = prefixes[i];
   1.104 +            if (declaredPrefix == prefix) {
   1.105 +                return namespaceURIs[i];
   1.106 +            }
   1.107 +        }
   1.108 +
   1.109 +        return "";
   1.110 +    }
   1.111 +
   1.112 +    public String getPrefix(String namespaceURI) {
   1.113 +        if (namespaceURI == null) throw new IllegalArgumentException();
   1.114 +
   1.115 +        for (int i = namespacePosition - 1; i >= 0; i--) {
   1.116 +            final String declaredNamespaceURI = namespaceURIs[i];
   1.117 +            if (declaredNamespaceURI == namespaceURI || declaredNamespaceURI.equals(namespaceURI)) {
   1.118 +                final String declaredPrefix = prefixes[i];
   1.119 +
   1.120 +                // Check if prefix is out of scope
   1.121 +                for (++i; i < namespacePosition; i++)
   1.122 +                    if (declaredPrefix == prefixes[i])
   1.123 +                        return null;
   1.124 +
   1.125 +                return declaredPrefix;
   1.126 +            }
   1.127 +        }
   1.128 +
   1.129 +        return null;
   1.130 +    }
   1.131 +
   1.132 +    public Iterator getPrefixes(String namespaceURI) {
   1.133 +        if (namespaceURI == null) throw new IllegalArgumentException();
   1.134 +
   1.135 +        List<String> l = new ArrayList<String>();
   1.136 +
   1.137 +        NAMESPACE_LOOP: for (int i = namespacePosition - 1; i >= 0; i--) {
   1.138 +            final String declaredNamespaceURI = namespaceURIs[i];
   1.139 +            if (declaredNamespaceURI == namespaceURI || declaredNamespaceURI.equals(namespaceURI)) {
   1.140 +                final String declaredPrefix = prefixes[i];
   1.141 +
   1.142 +                // Check if prefix is out of scope
   1.143 +                for (int j = i + 1; j < namespacePosition; j++)
   1.144 +                    if (declaredPrefix == prefixes[j])
   1.145 +                        continue NAMESPACE_LOOP;
   1.146 +
   1.147 +                l.add(declaredPrefix);
   1.148 +            }
   1.149 +        }
   1.150 +
   1.151 +        return l.iterator();
   1.152 +    }
   1.153 +
   1.154 +    // NamespaceContextEx interface
   1.155 +
   1.156 +    public Iterator<NamespaceContextEx.Binding> iterator() {
   1.157 +        if (namespacePosition == 2)
   1.158 +            return Collections.EMPTY_LIST.iterator();
   1.159 +
   1.160 +        final List<NamespaceContextEx.Binding> namespaces =
   1.161 +                new ArrayList<NamespaceContextEx.Binding>(namespacePosition);
   1.162 +
   1.163 +        NAMESPACE_LOOP: for (int i = namespacePosition - 1; i >= 2; i--) {
   1.164 +            final String declaredPrefix = prefixes[i];
   1.165 +
   1.166 +            // Check if prefix is out of scope
   1.167 +            for (int j = i + 1; j < namespacePosition; j++) {
   1.168 +                if (declaredPrefix == prefixes[j])
   1.169 +                    continue NAMESPACE_LOOP;
   1.170 +
   1.171 +                namespaces.add(new NamespaceBindingImpl(i));
   1.172 +            }
   1.173 +        }
   1.174 +
   1.175 +        return namespaces.iterator();
   1.176 +    }
   1.177 +
   1.178 +    final private class NamespaceBindingImpl implements NamespaceContextEx.Binding {
   1.179 +        int index;
   1.180 +
   1.181 +        NamespaceBindingImpl(int index) {
   1.182 +            this.index = index;
   1.183 +        }
   1.184 +
   1.185 +        public String getPrefix() {
   1.186 +            return prefixes[index];
   1.187 +        }
   1.188 +
   1.189 +        public String getNamespaceURI() {
   1.190 +            return namespaceURIs[index];
   1.191 +        }
   1.192 +    }
   1.193 +
   1.194 +    /**
   1.195 +     * Declare a default namespace.
   1.196 +     * <p>
   1.197 +     * @param namespaceURI the namespace URI to declare, may be null.
   1.198 +     */
   1.199 +    public void declareDefaultNamespace(String namespaceURI) {
   1.200 +        declareNamespace("", namespaceURI);
   1.201 +    }
   1.202 +
   1.203 +    /**
   1.204 +     * Declare a namespace.
   1.205 +     * <p>
   1.206 +     * The namespace will be declared on the current namespace context.
   1.207 +     * <p>
   1.208 +     * The namespace can be removed by popping the current namespace
   1.209 +     * context, or, if the declaration occured in the root context, by
   1.210 +     * reseting the namespace context.
   1.211 +     * <p>
   1.212 +     * A default namespace can be declared by passing <code>""</code> as
   1.213 +     * the value of the prefix parameter.
   1.214 +     * A namespace may be undeclared by passing <code>null</code> as the
   1.215 +     * value of the namespaceURI parameter.
   1.216 +     * <p>
   1.217 +     * @param prefix the namespace prefix to declare, may not be null.
   1.218 +     * @param namespaceURI the namespace URI to declare, may be null.
   1.219 +     * @throws IllegalArgumentException, if the prefix is null.
   1.220 +     */
   1.221 +    public void declareNamespace(String prefix, String namespaceURI) {
   1.222 +        if (prefix == null) throw new IllegalArgumentException();
   1.223 +
   1.224 +        prefix = prefix.intern();
   1.225 +        // Ignore the "xml" or "xmlns" declarations
   1.226 +        if (prefix == "xml" || prefix == "xmlns")
   1.227 +            return;
   1.228 +
   1.229 +        // Check for undeclaration
   1.230 +        if (namespaceURI != null)
   1.231 +            namespaceURI = namespaceURI.intern();
   1.232 +
   1.233 +        if (namespacePosition == namespaceURIs.length)
   1.234 +            resizeNamespaces();
   1.235 +
   1.236 +        // Add new declaration
   1.237 +        prefixes[namespacePosition] = prefix;
   1.238 +        namespaceURIs[namespacePosition++] = namespaceURI;
   1.239 +    }
   1.240 +
   1.241 +    private void resizeNamespaces() {
   1.242 +        final int newLength = namespaceURIs.length * 3 / 2 + 1;
   1.243 +
   1.244 +        String[] newPrefixes = new String[newLength];
   1.245 +        System.arraycopy(prefixes, 0, newPrefixes, 0, prefixes.length);
   1.246 +        prefixes = newPrefixes;
   1.247 +
   1.248 +        String[] newNamespaceURIs = new String[newLength];
   1.249 +        System.arraycopy(namespaceURIs, 0, newNamespaceURIs, 0, namespaceURIs.length);
   1.250 +        namespaceURIs = newNamespaceURIs;
   1.251 +    }
   1.252 +
   1.253 +    /**
   1.254 +     * Push a namespace context on the stack.
   1.255 +     */
   1.256 +    public void pushContext() {
   1.257 +        if (contextPosition == contexts.length)
   1.258 +            resizeContexts();
   1.259 +
   1.260 +        contexts[contextPosition++] = namespacePosition;
   1.261 +    }
   1.262 +
   1.263 +    private void resizeContexts() {
   1.264 +        int[] newContexts = new int[contexts.length * 3 / 2 + 1];
   1.265 +        System.arraycopy(contexts, 0, newContexts, 0, contexts.length);
   1.266 +        contexts = newContexts;
   1.267 +    }
   1.268 +
   1.269 +    /**
   1.270 +     * Pop the namespace context off the stack.
   1.271 +     * <p>
   1.272 +     * Namespaces declared within the context (to be popped)
   1.273 +     * will be removed and no longer be in scope.
   1.274 +     */
   1.275 +    public void popContext() {
   1.276 +        if (contextPosition > 0) {
   1.277 +            namespacePosition = contexts[--contextPosition];
   1.278 +        }
   1.279 +    }
   1.280 +
   1.281 +    /**
   1.282 +     * Reset namespace contexts.
   1.283 +     * <p>
   1.284 +     * Pop all namespace contexts and reset the root context.
   1.285 +     */
   1.286 +    public void resetContexts() {
   1.287 +        namespacePosition = 2;
   1.288 +    }
   1.289 +}

mercurial