< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.bind.v2.runtime.output;
  27 
  28 import java.io.IOException;
  29 import java.io.OutputStream;
  30 import java.util.Arrays;
  31 import java.util.Collections;
  32 
  33 import com.sun.xml.internal.bind.api.JAXBRIContext;
  34 import com.sun.xml.internal.bind.v2.runtime.Name;
  35 import com.sun.istack.internal.FinalArrayList;
  36 import com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler;
  37 
  38 /**
  39  * {@link XmlOutput} that generates canonical XML.
  40  *
  41  * @see com.sun.xml.internal.bind.api.C14nSupport_ArchitectureDocument
  42  * @author Kohsuke Kawaguchi
  43  */
  44 public class C14nXmlOutput extends UTF8XmlOutput {
  45     public C14nXmlOutput(OutputStream out, Encoded[] localNames, boolean namedAttributesAreOrdered, CharacterEscapeHandler escapeHandler) {
  46         super(out, localNames, escapeHandler);
  47         this.namedAttributesAreOrdered = namedAttributesAreOrdered;
  48 
  49         for( int i=0; i<staticAttributes.length; i++ )
  50             staticAttributes[i] = new StaticAttribute();
  51     }
  52 
  53     /**
  54      * Hosts statically known attributes.
  55      *
  56      * {@link StaticAttribute} instances are reused.
  57      */
  58     private StaticAttribute[] staticAttributes = new StaticAttribute[8];
  59     private int len = 0;
  60 
  61     /**
  62      * Used to sort namespace declarations. Reused.
  63      */
  64     private int[] nsBuf = new int[8];
  65 
  66     /**
  67      * Hosts other attributes whose name are not statically known
  68      * (AKA attribute wildcard.)
  69      *
  70      * As long as this map is empty, there's no need for sorting.
  71      * see {@link com.sun.xml.internal.bind.api.C14nSupport_ArchitectureDocument} for more details.
  72      */
  73     private final FinalArrayList<DynamicAttribute> otherAttributes = new FinalArrayList<DynamicAttribute>();
  74 
  75     /**
  76      * True if {@link JAXBRIContext} is created with c14n support on,
  77      * in which case all named attributes are sorted by the marshaller
  78      * and we won't have to do it here.
  79      */
  80     private final boolean namedAttributesAreOrdered;
  81 
  82     final class StaticAttribute implements Comparable<StaticAttribute> {
  83         Name name;
  84         String value;
  85 
  86         public void set(Name name, String value) {
  87             this.name = name;
  88             this.value = value;
  89         }
  90 
  91         void write() throws IOException {


   1 /*
   2  * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.bind.v2.runtime.output;
  27 
  28 import java.io.IOException;
  29 import java.io.OutputStream;
  30 import java.util.Arrays;
  31 import java.util.Collections;
  32 
  33 import com.sun.xml.internal.bind.api.JAXBRIContext;
  34 import com.sun.xml.internal.bind.v2.runtime.Name;
  35 import com.sun.istack.internal.FinalArrayList;
  36 import com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler;
  37 
  38 /**
  39  * {@link XmlOutput} that generates canonical XML.
  40  *

  41  * @author Kohsuke Kawaguchi
  42  */
  43 public class C14nXmlOutput extends UTF8XmlOutput {
  44     public C14nXmlOutput(OutputStream out, Encoded[] localNames, boolean namedAttributesAreOrdered, CharacterEscapeHandler escapeHandler) {
  45         super(out, localNames, escapeHandler);
  46         this.namedAttributesAreOrdered = namedAttributesAreOrdered;
  47 
  48         for( int i=0; i<staticAttributes.length; i++ )
  49             staticAttributes[i] = new StaticAttribute();
  50     }
  51 
  52     /**
  53      * Hosts statically known attributes.
  54      *
  55      * {@link StaticAttribute} instances are reused.
  56      */
  57     private StaticAttribute[] staticAttributes = new StaticAttribute[8];
  58     private int len = 0;
  59 
  60     /**
  61      * Used to sort namespace declarations. Reused.
  62      */
  63     private int[] nsBuf = new int[8];
  64 
  65     /**
  66      * Hosts other attributes whose name are not statically known
  67      * (AKA attribute wildcard.)
  68      *
  69      * As long as this map is empty, there's no need for sorting.

  70      */
  71     private final FinalArrayList<DynamicAttribute> otherAttributes = new FinalArrayList<DynamicAttribute>();
  72 
  73     /**
  74      * True if {@link JAXBRIContext} is created with c14n support on,
  75      * in which case all named attributes are sorted by the marshaller
  76      * and we won't have to do it here.
  77      */
  78     private final boolean namedAttributesAreOrdered;
  79 
  80     final class StaticAttribute implements Comparable<StaticAttribute> {
  81         Name name;
  82         String value;
  83 
  84         public void set(Name name, String value) {
  85             this.name = name;
  86             this.value = value;
  87         }
  88 
  89         void write() throws IOException {


< prev index next >