src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/output/C14nXmlOutput.java

changeset 0
373ffda63c9a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/output/C14nXmlOutput.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,223 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2011, 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.bind.v2.runtime.output;
    1.30 +
    1.31 +import java.io.IOException;
    1.32 +import java.io.OutputStream;
    1.33 +import java.util.Arrays;
    1.34 +import java.util.Collections;
    1.35 +
    1.36 +import com.sun.xml.internal.bind.api.JAXBRIContext;
    1.37 +import com.sun.xml.internal.bind.v2.runtime.Name;
    1.38 +import com.sun.istack.internal.FinalArrayList;
    1.39 +import com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler;
    1.40 +
    1.41 +/**
    1.42 + * {@link XmlOutput} that generates canonical XML.
    1.43 + *
    1.44 + * @see com.sun.xml.internal.bind.api.C14nSupport_ArchitectureDocument
    1.45 + * @author Kohsuke Kawaguchi
    1.46 + */
    1.47 +public class C14nXmlOutput extends UTF8XmlOutput {
    1.48 +    public C14nXmlOutput(OutputStream out, Encoded[] localNames, boolean namedAttributesAreOrdered, CharacterEscapeHandler escapeHandler) {
    1.49 +        super(out, localNames, escapeHandler);
    1.50 +        this.namedAttributesAreOrdered = namedAttributesAreOrdered;
    1.51 +
    1.52 +        for( int i=0; i<staticAttributes.length; i++ )
    1.53 +            staticAttributes[i] = new StaticAttribute();
    1.54 +    }
    1.55 +
    1.56 +    /**
    1.57 +     * Hosts statically known attributes.
    1.58 +     *
    1.59 +     * {@link StaticAttribute} instances are reused.
    1.60 +     */
    1.61 +    private StaticAttribute[] staticAttributes = new StaticAttribute[8];
    1.62 +    private int len = 0;
    1.63 +
    1.64 +    /**
    1.65 +     * Used to sort namespace declarations. Reused.
    1.66 +     */
    1.67 +    private int[] nsBuf = new int[8];
    1.68 +
    1.69 +    /**
    1.70 +     * Hosts other attributes whose name are not statically known
    1.71 +     * (AKA attribute wildcard.)
    1.72 +     *
    1.73 +     * As long as this map is empty, there's no need for sorting.
    1.74 +     * see {@link com.sun.xml.internal.bind.api.C14nSupport_ArchitectureDocument} for more details.
    1.75 +     */
    1.76 +    private final FinalArrayList<DynamicAttribute> otherAttributes = new FinalArrayList<DynamicAttribute>();
    1.77 +
    1.78 +    /**
    1.79 +     * True if {@link JAXBRIContext} is created with c14n support on,
    1.80 +     * in which case all named attributes are sorted by the marshaller
    1.81 +     * and we won't have to do it here.
    1.82 +     */
    1.83 +    private final boolean namedAttributesAreOrdered;
    1.84 +
    1.85 +    final class StaticAttribute implements Comparable<StaticAttribute> {
    1.86 +        Name name;
    1.87 +        String value;
    1.88 +
    1.89 +        public void set(Name name, String value) {
    1.90 +            this.name = name;
    1.91 +            this.value = value;
    1.92 +        }
    1.93 +
    1.94 +        void write() throws IOException {
    1.95 +            C14nXmlOutput.super.attribute(name,value);
    1.96 +        }
    1.97 +
    1.98 +        DynamicAttribute toDynamicAttribute() {
    1.99 +            int nsUriIndex = name.nsUriIndex;
   1.100 +            int prefix;
   1.101 +            if(nsUriIndex==-1)
   1.102 +                prefix = -1;
   1.103 +            else
   1.104 +                prefix = nsUriIndex2prefixIndex[nsUriIndex];
   1.105 +            return new DynamicAttribute(
   1.106 +                prefix, name.localName, value );
   1.107 +        }
   1.108 +
   1.109 +        public int compareTo(StaticAttribute that) {
   1.110 +            return this.name.compareTo(that.name);
   1.111 +        }
   1.112 +
   1.113 +    }
   1.114 +
   1.115 +    final class DynamicAttribute implements Comparable<DynamicAttribute> {
   1.116 +        final int prefix;
   1.117 +        final String localName;
   1.118 +        final String value;
   1.119 +
   1.120 +        public DynamicAttribute(int prefix, String localName, String value) {
   1.121 +            this.prefix = prefix;
   1.122 +            this.localName = localName;
   1.123 +            this.value = value;
   1.124 +        }
   1.125 +
   1.126 +        private String getURI() {
   1.127 +            if(prefix==-1)  return "";
   1.128 +            else            return nsContext.getNamespaceURI(prefix);
   1.129 +        }
   1.130 +
   1.131 +        public int compareTo(DynamicAttribute that) {
   1.132 +            int r = this.getURI().compareTo(that.getURI());
   1.133 +            if(r!=0)    return r;
   1.134 +            return this.localName.compareTo(that.localName);
   1.135 +        }
   1.136 +    }
   1.137 +
   1.138 +    @Override
   1.139 +    public void attribute(Name name, String value) throws IOException {
   1.140 +        if(staticAttributes.length==len) {
   1.141 +            // reallocate
   1.142 +            int newLen = len*2;
   1.143 +            StaticAttribute[] newbuf = new StaticAttribute[newLen];
   1.144 +            System.arraycopy(staticAttributes,0,newbuf,0,len);
   1.145 +            for(int i=len;i<newLen;i++)
   1.146 +                staticAttributes[i] = new StaticAttribute();
   1.147 +            staticAttributes = newbuf;
   1.148 +        }
   1.149 +
   1.150 +        staticAttributes[len++].set(name,value);
   1.151 +    }
   1.152 +
   1.153 +    @Override
   1.154 +    public void attribute(int prefix, String localName, String value) throws IOException {
   1.155 +        otherAttributes.add(new DynamicAttribute(prefix,localName,value));
   1.156 +    }
   1.157 +
   1.158 +    @Override
   1.159 +    public void endStartTag() throws IOException {
   1.160 +        if(otherAttributes.isEmpty()) {
   1.161 +            if(len!=0) {
   1.162 +                // sort is expensive even for size 0 array,
   1.163 +                // so it's worth checking len==0
   1.164 +                if(!namedAttributesAreOrdered)
   1.165 +                    Arrays.sort(staticAttributes,0,len);
   1.166 +                // this is the common case
   1.167 +                for( int i=0; i<len; i++ )
   1.168 +                    staticAttributes[i].write();
   1.169 +                len = 0;
   1.170 +            }
   1.171 +        } else {
   1.172 +            // this is the exceptional case
   1.173 +
   1.174 +            // sort all the attributes, not just the other attributes
   1.175 +            for( int i=0; i<len; i++ )
   1.176 +                otherAttributes.add(staticAttributes[i].toDynamicAttribute());
   1.177 +            len = 0;
   1.178 +            Collections.sort(otherAttributes);
   1.179 +
   1.180 +            // write them all
   1.181 +            int size = otherAttributes.size();
   1.182 +            for( int i=0; i<size; i++ ) {
   1.183 +                DynamicAttribute a = otherAttributes.get(i);
   1.184 +                super.attribute(a.prefix,a.localName,a.value);
   1.185 +            }
   1.186 +            otherAttributes.clear();
   1.187 +        }
   1.188 +        super.endStartTag();
   1.189 +    }
   1.190 +
   1.191 +    /**
   1.192 +     * Write namespace declarations after sorting them.
   1.193 +     */
   1.194 +    @Override
   1.195 +    protected void writeNsDecls(int base) throws IOException {
   1.196 +        int count = nsContext.getCurrent().count();
   1.197 +
   1.198 +        if(count==0)
   1.199 +            return; // quickly reject the most common case
   1.200 +
   1.201 +        if(count>nsBuf.length)
   1.202 +            nsBuf = new int[count];
   1.203 +
   1.204 +        for( int i=count-1; i>=0; i-- )
   1.205 +            nsBuf[i] = base+i;
   1.206 +
   1.207 +        // do a bubble sort. Hopefully # of ns decls are small enough to justify bubble sort.
   1.208 +        // faster algorithm is more compliated to implement
   1.209 +        for( int i=0; i<count; i++ ) {
   1.210 +            for( int j=i+1; j<count; j++ ) {
   1.211 +                String p = nsContext.getPrefix(nsBuf[i]);
   1.212 +                String q = nsContext.getPrefix(nsBuf[j]);
   1.213 +                if( p.compareTo(q) > 0 ) {
   1.214 +                    // swap
   1.215 +                    int t = nsBuf[j];
   1.216 +                    nsBuf[j] = nsBuf[i];
   1.217 +                    nsBuf[i] = t;
   1.218 +                }
   1.219 +            }
   1.220 +        }
   1.221 +
   1.222 +        // write them out
   1.223 +        for( int i=0; i<count; i++ )
   1.224 +            writeNsDecl(nsBuf[i]);
   1.225 +    }
   1.226 +}

mercurial