Sun, 18 Jun 2017 23:18:45 +0100
8172297: In java 8, the marshalling with JAX-WS does not escape carriage return
Reviewed-by: lancea
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.xml.internal.bind.v2.runtime.output; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.io.IOException; |
aoqi@0 | 29 | import java.io.OutputStream; |
aoqi@0 | 30 | import java.util.Arrays; |
aoqi@0 | 31 | import java.util.Collections; |
aoqi@0 | 32 | |
aoqi@0 | 33 | import com.sun.xml.internal.bind.api.JAXBRIContext; |
aoqi@0 | 34 | import com.sun.xml.internal.bind.v2.runtime.Name; |
aoqi@0 | 35 | import com.sun.istack.internal.FinalArrayList; |
aoqi@0 | 36 | import com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler; |
aoqi@0 | 37 | |
aoqi@0 | 38 | /** |
aoqi@0 | 39 | * {@link XmlOutput} that generates canonical XML. |
aoqi@0 | 40 | * |
aoqi@0 | 41 | * @see com.sun.xml.internal.bind.api.C14nSupport_ArchitectureDocument |
aoqi@0 | 42 | * @author Kohsuke Kawaguchi |
aoqi@0 | 43 | */ |
aoqi@0 | 44 | public class C14nXmlOutput extends UTF8XmlOutput { |
aoqi@0 | 45 | public C14nXmlOutput(OutputStream out, Encoded[] localNames, boolean namedAttributesAreOrdered, CharacterEscapeHandler escapeHandler) { |
aoqi@0 | 46 | super(out, localNames, escapeHandler); |
aoqi@0 | 47 | this.namedAttributesAreOrdered = namedAttributesAreOrdered; |
aoqi@0 | 48 | |
aoqi@0 | 49 | for( int i=0; i<staticAttributes.length; i++ ) |
aoqi@0 | 50 | staticAttributes[i] = new StaticAttribute(); |
aoqi@0 | 51 | } |
aoqi@0 | 52 | |
aoqi@0 | 53 | /** |
aoqi@0 | 54 | * Hosts statically known attributes. |
aoqi@0 | 55 | * |
aoqi@0 | 56 | * {@link StaticAttribute} instances are reused. |
aoqi@0 | 57 | */ |
aoqi@0 | 58 | private StaticAttribute[] staticAttributes = new StaticAttribute[8]; |
aoqi@0 | 59 | private int len = 0; |
aoqi@0 | 60 | |
aoqi@0 | 61 | /** |
aoqi@0 | 62 | * Used to sort namespace declarations. Reused. |
aoqi@0 | 63 | */ |
aoqi@0 | 64 | private int[] nsBuf = new int[8]; |
aoqi@0 | 65 | |
aoqi@0 | 66 | /** |
aoqi@0 | 67 | * Hosts other attributes whose name are not statically known |
aoqi@0 | 68 | * (AKA attribute wildcard.) |
aoqi@0 | 69 | * |
aoqi@0 | 70 | * As long as this map is empty, there's no need for sorting. |
aoqi@0 | 71 | * see {@link com.sun.xml.internal.bind.api.C14nSupport_ArchitectureDocument} for more details. |
aoqi@0 | 72 | */ |
aoqi@0 | 73 | private final FinalArrayList<DynamicAttribute> otherAttributes = new FinalArrayList<DynamicAttribute>(); |
aoqi@0 | 74 | |
aoqi@0 | 75 | /** |
aoqi@0 | 76 | * True if {@link JAXBRIContext} is created with c14n support on, |
aoqi@0 | 77 | * in which case all named attributes are sorted by the marshaller |
aoqi@0 | 78 | * and we won't have to do it here. |
aoqi@0 | 79 | */ |
aoqi@0 | 80 | private final boolean namedAttributesAreOrdered; |
aoqi@0 | 81 | |
aoqi@0 | 82 | final class StaticAttribute implements Comparable<StaticAttribute> { |
aoqi@0 | 83 | Name name; |
aoqi@0 | 84 | String value; |
aoqi@0 | 85 | |
aoqi@0 | 86 | public void set(Name name, String value) { |
aoqi@0 | 87 | this.name = name; |
aoqi@0 | 88 | this.value = value; |
aoqi@0 | 89 | } |
aoqi@0 | 90 | |
aoqi@0 | 91 | void write() throws IOException { |
aoqi@0 | 92 | C14nXmlOutput.super.attribute(name,value); |
aoqi@0 | 93 | } |
aoqi@0 | 94 | |
aoqi@0 | 95 | DynamicAttribute toDynamicAttribute() { |
aoqi@0 | 96 | int nsUriIndex = name.nsUriIndex; |
aoqi@0 | 97 | int prefix; |
aoqi@0 | 98 | if(nsUriIndex==-1) |
aoqi@0 | 99 | prefix = -1; |
aoqi@0 | 100 | else |
aoqi@0 | 101 | prefix = nsUriIndex2prefixIndex[nsUriIndex]; |
aoqi@0 | 102 | return new DynamicAttribute( |
aoqi@0 | 103 | prefix, name.localName, value ); |
aoqi@0 | 104 | } |
aoqi@0 | 105 | |
aoqi@0 | 106 | public int compareTo(StaticAttribute that) { |
aoqi@0 | 107 | return this.name.compareTo(that.name); |
aoqi@0 | 108 | } |
aoqi@0 | 109 | |
aoqi@0 | 110 | } |
aoqi@0 | 111 | |
aoqi@0 | 112 | final class DynamicAttribute implements Comparable<DynamicAttribute> { |
aoqi@0 | 113 | final int prefix; |
aoqi@0 | 114 | final String localName; |
aoqi@0 | 115 | final String value; |
aoqi@0 | 116 | |
aoqi@0 | 117 | public DynamicAttribute(int prefix, String localName, String value) { |
aoqi@0 | 118 | this.prefix = prefix; |
aoqi@0 | 119 | this.localName = localName; |
aoqi@0 | 120 | this.value = value; |
aoqi@0 | 121 | } |
aoqi@0 | 122 | |
aoqi@0 | 123 | private String getURI() { |
aoqi@0 | 124 | if(prefix==-1) return ""; |
aoqi@0 | 125 | else return nsContext.getNamespaceURI(prefix); |
aoqi@0 | 126 | } |
aoqi@0 | 127 | |
aoqi@0 | 128 | public int compareTo(DynamicAttribute that) { |
aoqi@0 | 129 | int r = this.getURI().compareTo(that.getURI()); |
aoqi@0 | 130 | if(r!=0) return r; |
aoqi@0 | 131 | return this.localName.compareTo(that.localName); |
aoqi@0 | 132 | } |
aoqi@0 | 133 | } |
aoqi@0 | 134 | |
aoqi@0 | 135 | @Override |
aoqi@0 | 136 | public void attribute(Name name, String value) throws IOException { |
aoqi@0 | 137 | if(staticAttributes.length==len) { |
aoqi@0 | 138 | // reallocate |
aoqi@0 | 139 | int newLen = len*2; |
aoqi@0 | 140 | StaticAttribute[] newbuf = new StaticAttribute[newLen]; |
aoqi@0 | 141 | System.arraycopy(staticAttributes,0,newbuf,0,len); |
aoqi@0 | 142 | for(int i=len;i<newLen;i++) |
aoqi@0 | 143 | staticAttributes[i] = new StaticAttribute(); |
aoqi@0 | 144 | staticAttributes = newbuf; |
aoqi@0 | 145 | } |
aoqi@0 | 146 | |
aoqi@0 | 147 | staticAttributes[len++].set(name,value); |
aoqi@0 | 148 | } |
aoqi@0 | 149 | |
aoqi@0 | 150 | @Override |
aoqi@0 | 151 | public void attribute(int prefix, String localName, String value) throws IOException { |
aoqi@0 | 152 | otherAttributes.add(new DynamicAttribute(prefix,localName,value)); |
aoqi@0 | 153 | } |
aoqi@0 | 154 | |
aoqi@0 | 155 | @Override |
aoqi@0 | 156 | public void endStartTag() throws IOException { |
aoqi@0 | 157 | if(otherAttributes.isEmpty()) { |
aoqi@0 | 158 | if(len!=0) { |
aoqi@0 | 159 | // sort is expensive even for size 0 array, |
aoqi@0 | 160 | // so it's worth checking len==0 |
aoqi@0 | 161 | if(!namedAttributesAreOrdered) |
aoqi@0 | 162 | Arrays.sort(staticAttributes,0,len); |
aoqi@0 | 163 | // this is the common case |
aoqi@0 | 164 | for( int i=0; i<len; i++ ) |
aoqi@0 | 165 | staticAttributes[i].write(); |
aoqi@0 | 166 | len = 0; |
aoqi@0 | 167 | } |
aoqi@0 | 168 | } else { |
aoqi@0 | 169 | // this is the exceptional case |
aoqi@0 | 170 | |
aoqi@0 | 171 | // sort all the attributes, not just the other attributes |
aoqi@0 | 172 | for( int i=0; i<len; i++ ) |
aoqi@0 | 173 | otherAttributes.add(staticAttributes[i].toDynamicAttribute()); |
aoqi@0 | 174 | len = 0; |
aoqi@0 | 175 | Collections.sort(otherAttributes); |
aoqi@0 | 176 | |
aoqi@0 | 177 | // write them all |
aoqi@0 | 178 | int size = otherAttributes.size(); |
aoqi@0 | 179 | for( int i=0; i<size; i++ ) { |
aoqi@0 | 180 | DynamicAttribute a = otherAttributes.get(i); |
aoqi@0 | 181 | super.attribute(a.prefix,a.localName,a.value); |
aoqi@0 | 182 | } |
aoqi@0 | 183 | otherAttributes.clear(); |
aoqi@0 | 184 | } |
aoqi@0 | 185 | super.endStartTag(); |
aoqi@0 | 186 | } |
aoqi@0 | 187 | |
aoqi@0 | 188 | /** |
aoqi@0 | 189 | * Write namespace declarations after sorting them. |
aoqi@0 | 190 | */ |
aoqi@0 | 191 | @Override |
aoqi@0 | 192 | protected void writeNsDecls(int base) throws IOException { |
aoqi@0 | 193 | int count = nsContext.getCurrent().count(); |
aoqi@0 | 194 | |
aoqi@0 | 195 | if(count==0) |
aoqi@0 | 196 | return; // quickly reject the most common case |
aoqi@0 | 197 | |
aoqi@0 | 198 | if(count>nsBuf.length) |
aoqi@0 | 199 | nsBuf = new int[count]; |
aoqi@0 | 200 | |
aoqi@0 | 201 | for( int i=count-1; i>=0; i-- ) |
aoqi@0 | 202 | nsBuf[i] = base+i; |
aoqi@0 | 203 | |
aoqi@0 | 204 | // do a bubble sort. Hopefully # of ns decls are small enough to justify bubble sort. |
aoqi@0 | 205 | // faster algorithm is more compliated to implement |
aoqi@0 | 206 | for( int i=0; i<count; i++ ) { |
aoqi@0 | 207 | for( int j=i+1; j<count; j++ ) { |
aoqi@0 | 208 | String p = nsContext.getPrefix(nsBuf[i]); |
aoqi@0 | 209 | String q = nsContext.getPrefix(nsBuf[j]); |
aoqi@0 | 210 | if( p.compareTo(q) > 0 ) { |
aoqi@0 | 211 | // swap |
aoqi@0 | 212 | int t = nsBuf[j]; |
aoqi@0 | 213 | nsBuf[j] = nsBuf[i]; |
aoqi@0 | 214 | nsBuf[i] = t; |
aoqi@0 | 215 | } |
aoqi@0 | 216 | } |
aoqi@0 | 217 | } |
aoqi@0 | 218 | |
aoqi@0 | 219 | // write them out |
aoqi@0 | 220 | for( int i=0; i<count; i++ ) |
aoqi@0 | 221 | writeNsDecl(nsBuf[i]); |
aoqi@0 | 222 | } |
aoqi@0 | 223 | } |