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
61 /** local names encoded in UTF-8. All entries are pre-filled. */
62 private final Encoded[] localNames;
63
64 /** Temporary buffer used to encode text. */
65 /*
66 * TODO
67 * The textBuffer could write directly to the _octetBuffer
68 * when encoding a string if Encoder is modified.
69 * This will avoid an additional memory copy.
70 */
71 private final Encoded textBuffer = new Encoded();
72
73 /** Buffer of octets for writing. */
74 // TODO: Obtain buffer size from property on the JAXB context
75 protected final byte[] octetBuffer = new byte[1024];
76
77 /** Index in buffer to write to. */
78 protected int octetBufferIndex;
79
80 /**
81 * Set to true to indicate that we need to write '>'
82 * to close a start tag. Deferring the write of this char
83 * allows us to write "/>" for empty elements.
84 */
85 protected boolean closeStartTagPending = false;
86
87 /**
88 * @see MarshallerImpl#header
89 */
90 private String header;
91
92 private CharacterEscapeHandler escapeHandler = null;
93
94 /**
95 *
96 * @param localNames
97 * local names encoded in UTF-8.
98 */
99 public UTF8XmlOutput(OutputStream out, Encoded[] localNames, CharacterEscapeHandler escapeHandler) {
100 this.out = out;
101 this.localNames = localNames;
102 for( int i=0; i<prefixes.length; i++ )
103 prefixes[i] = new Encoded();
112 public void startDocument(XMLSerializer serializer, boolean fragment, int[] nsUriIndex2prefixIndex, NamespaceContextImpl nsContext) throws IOException, SAXException, XMLStreamException {
113 super.startDocument(serializer, fragment,nsUriIndex2prefixIndex,nsContext);
114
115 octetBufferIndex = 0;
116 if(!fragment) {
117 write(XML_DECL);
118 }
119 if(header!=null) {
120 textBuffer.set(header);
121 textBuffer.write(this);
122 }
123 }
124
125 @Override
126 public void endDocument(boolean fragment) throws IOException, SAXException, XMLStreamException {
127 flushBuffer();
128 super.endDocument(fragment);
129 }
130
131 /**
132 * Writes '>' to close the start tag, if necessary.
133 */
134 protected final void closeStartTag() throws IOException {
135 if(closeStartTagPending) {
136 write('>');
137 closeStartTagPending = false;
138 }
139 }
140
141 public void beginStartTag(int prefix, String localName) throws IOException {
142 closeStartTag();
143 int base= pushNsDecls();
144 write('<');
145 writeName(prefix,localName);
146 writeNsDecls(base);
147 }
148
149 @Override
150 public void beginStartTag(Name name) throws IOException {
151 closeStartTag();
152 int base = pushNsDecls();
|
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
61 /** local names encoded in UTF-8. All entries are pre-filled. */
62 private final Encoded[] localNames;
63
64 /** Temporary buffer used to encode text. */
65 /*
66 * TODO
67 * The textBuffer could write directly to the _octetBuffer
68 * when encoding a string if Encoder is modified.
69 * This will avoid an additional memory copy.
70 */
71 private final Encoded textBuffer = new Encoded();
72
73 /** Buffer of octets for writing. */
74 // TODO: Obtain buffer size from property on the JAXB context
75 protected final byte[] octetBuffer = new byte[1024];
76
77 /** Index in buffer to write to. */
78 protected int octetBufferIndex;
79
80 /**
81 * Set to true to indicate that we need to write {@code '>'}
82 * to close a start tag. Deferring the write of this char
83 * allows us to write {@code "/>"} for empty elements.
84 */
85 protected boolean closeStartTagPending = false;
86
87 /**
88 * @see MarshallerImpl#header
89 */
90 private String header;
91
92 private CharacterEscapeHandler escapeHandler = null;
93
94 /**
95 *
96 * @param localNames
97 * local names encoded in UTF-8.
98 */
99 public UTF8XmlOutput(OutputStream out, Encoded[] localNames, CharacterEscapeHandler escapeHandler) {
100 this.out = out;
101 this.localNames = localNames;
102 for( int i=0; i<prefixes.length; i++ )
103 prefixes[i] = new Encoded();
112 public void startDocument(XMLSerializer serializer, boolean fragment, int[] nsUriIndex2prefixIndex, NamespaceContextImpl nsContext) throws IOException, SAXException, XMLStreamException {
113 super.startDocument(serializer, fragment,nsUriIndex2prefixIndex,nsContext);
114
115 octetBufferIndex = 0;
116 if(!fragment) {
117 write(XML_DECL);
118 }
119 if(header!=null) {
120 textBuffer.set(header);
121 textBuffer.write(this);
122 }
123 }
124
125 @Override
126 public void endDocument(boolean fragment) throws IOException, SAXException, XMLStreamException {
127 flushBuffer();
128 super.endDocument(fragment);
129 }
130
131 /**
132 * Writes {@code '>'} to close the start tag, if necessary.
133 */
134 protected final void closeStartTag() throws IOException {
135 if(closeStartTagPending) {
136 write('>');
137 closeStartTagPending = false;
138 }
139 }
140
141 public void beginStartTag(int prefix, String localName) throws IOException {
142 closeStartTag();
143 int base= pushNsDecls();
144 write('<');
145 writeName(prefix,localName);
146 writeNsDecls(base);
147 }
148
149 @Override
150 public void beginStartTag(Name name) throws IOException {
151 closeStartTag();
152 int base = pushNsDecls();
|