Skip to contentMethod: handleDTD()
1: /*
2: * Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Distribution License v. 1.0, which is available at
6: * http://www.eclipse.org/org/documents/edl-v10.php.
7: *
8: * SPDX-License-Identifier: BSD-3-Clause
9: */
10:
11: package org.jvnet.staxex.util;
12:
13: import java.io.IOException;
14:
15: import jakarta.xml.bind.attachment.AttachmentMarshaller;
16: import javax.xml.stream.XMLStreamConstants;
17: import javax.xml.stream.XMLStreamException;
18: import javax.xml.stream.XMLStreamReader;
19: import javax.xml.stream.XMLStreamWriter;
20: import javax.xml.XMLConstants;
21:
22: import org.jvnet.staxex.Base64Data;
23: import org.jvnet.staxex.XMLStreamReaderEx;
24: import org.jvnet.staxex.XMLStreamWriterEx;
25:
26: /**
27: * Reads a sub-tree from {@link XMLStreamReader} and writes to {@link XMLStreamWriter}
28: * as-is.
29: *
30: * <p>
31: * This class can be sub-classed to implement a simple transformation logic.
32: *
33: * @author Kohsuke Kawaguchi
34: * @author Ryan Shoemaker
35: */
36: public class XMLStreamReaderToXMLStreamWriter {
37:
38: static public class Breakpoint {
39: protected XMLStreamReader reader;
40: protected XMLStreamWriter writer;
41:
42: public Breakpoint(XMLStreamReader r, XMLStreamWriter w) { reader = r; writer = w; }
43:
44: public XMLStreamReader reader() { return reader; }
45: public XMLStreamWriter writer() { return writer; }
46: public boolean proceedBeforeStartElement() { return true; }
47: public boolean proceedAfterStartElement() { return true; }
48: }
49:
50: private static final int BUF_SIZE = 4096;
51:
52: protected XMLStreamReader in;
53: protected XMLStreamWriter out;
54:
55: private char[] buf;
56:
57: boolean optimizeBase64Data = false;
58:
59: AttachmentMarshaller mtomAttachmentMarshaller;
60:
61: /**
62: * Reads one subtree and writes it out.
63: *
64: * <p>
65: * The {@link XMLStreamWriter} never receives a start/end document event.
66: * Those need to be written separately by the caller.
67: */
68: public void bridge(XMLStreamReader in, XMLStreamWriter out) throws XMLStreamException {
69: bridge(in, out, null);
70: }
71:
72: public void bridge(Breakpoint breakPoint) throws XMLStreamException {
73: bridge(breakPoint.reader(), breakPoint.writer(), breakPoint);
74: }
75:
76: private void bridge(XMLStreamReader in, XMLStreamWriter out, Breakpoint breakPoint) throws XMLStreamException {
77: assert in!=null && out!=null;
78: this.in = in;
79: this.out = out;
80:
81: optimizeBase64Data = (in instanceof XMLStreamReaderEx);
82:
83: if (out instanceof XMLStreamWriterEx && out instanceof MtomStreamWriter) {
84: mtomAttachmentMarshaller = ((MtomStreamWriter) out).getAttachmentMarshaller();
85: }
86: // remembers the nest level of elements to know when we are done.
87: int depth=0;
88:
89: buf = new char[BUF_SIZE];
90:
91: // if the parser is at the start tag, proceed to the first element
92: int event = getEventType();
93:
94: if( event!=XMLStreamConstants.START_ELEMENT)
95: throw new IllegalStateException("The current event is not START_ELEMENT\n but " + event);
96:
97: do {
98: // These are all of the events listed in the javadoc for
99: // XMLEvent.
100: // The spec only really describes 11 of them.
101: switch (event) {
102: case XMLStreamConstants.START_ELEMENT :
103: if (breakPoint != null && !breakPoint.proceedBeforeStartElement()) return;
104: depth++;
105: handleStartElement();
106: if (breakPoint != null && !breakPoint.proceedAfterStartElement()) return;
107: break;
108: case XMLStreamConstants.END_ELEMENT :
109: handleEndElement();
110: depth--;
111: if(depth==0)
112: return;
113: break;
114: case XMLStreamConstants.CHARACTERS :
115: handleCharacters();
116: break;
117: case XMLStreamConstants.ENTITY_REFERENCE :
118: handleEntityReference();
119: break;
120: case XMLStreamConstants.PROCESSING_INSTRUCTION :
121: handlePI();
122: break;
123: case XMLStreamConstants.COMMENT :
124: handleComment();
125: break;
126: case XMLStreamConstants.DTD :
127: handleDTD();
128: break;
129: case XMLStreamConstants.CDATA :
130: handleCDATA();
131: break;
132: case XMLStreamConstants.SPACE :
133: handleSpace();
134: break;
135: case XMLStreamConstants.END_DOCUMENT:
136: throw new XMLStreamException("Malformed XML at depth="+depth+", Reached EOF. Event="+event);
137: default :
138: throw new XMLStreamException("Cannot process event: " + event);
139: }
140:
141: event=getNextEvent();
142: } while (depth!=0);
143: }
144:
145: protected void handlePI() throws XMLStreamException {
146: out.writeProcessingInstruction(
147: in.getPITarget(),
148: in.getPIData());
149: }
150:
151:
152: protected void handleCharacters() throws XMLStreamException {
153:
154: CharSequence c = null;
155:
156: if (optimizeBase64Data) {
157: c = ((XMLStreamReaderEx)in).getPCDATA();
158: }
159:
160: if ((c != null) && (c instanceof Base64Data)) {
161: if (mtomAttachmentMarshaller != null) {
162: Base64Data b64d = (Base64Data) c;
163: ((XMLStreamWriterEx)out).writeBinary(b64d.getDataHandler());
164: } else {
165: try {
166: ((Base64Data)c).writeTo(out);
167: } catch (IOException e) {
168: throw new XMLStreamException(e);
169: }
170: }
171: } else {
172: for (int start=0,read=buf.length; read == buf.length; start+=buf.length) {
173: read = in.getTextCharacters(start, buf, 0, buf.length);
174: out.writeCharacters(buf, 0, read);
175: }
176: }
177: }
178:
179: protected void handleEndElement() throws XMLStreamException {
180: out.writeEndElement();
181: }
182:
183: protected void handleStartElement() throws XMLStreamException {
184: String nsUri = in.getNamespaceURI();
185: if(nsUri==null)
186: out.writeStartElement(in.getLocalName());
187: else
188: out.writeStartElement(
189: fixNull(in.getPrefix()),
190: in.getLocalName(),
191: nsUri
192: );
193:
194: // start namespace bindings
195: int nsCount = in.getNamespaceCount();
196: for (int i = 0; i < nsCount; i++) {
197: out.writeNamespace(
198: fixNull(in.getNamespacePrefix(i)), //StAX reader will return null for default NS
199: fixNull(in.getNamespaceURI(i))); // zephyr doesn't like null, I don't know what is correct, so just fix null to "" for now
200: }
201:
202: // write attributes
203: int attCount = in.getAttributeCount();
204: for (int i = 0; i < attCount; i++) {
205: handleAttribute(i);
206: }
207: }
208:
209: /**
210: * Writes out the {@code i}-th attribute of the current element.
211: *
212: * <p>
213: * Used from {@link #handleStartElement()}.
214: */
215: protected void handleAttribute(int i) throws XMLStreamException {
216: String nsUri = in.getAttributeNamespace(i);
217: String prefix = in.getAttributePrefix(i);
218: if (fixNull(nsUri).equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) {
219: //Its a namespace decl, ignore as it is already written.
220: return;
221: }
222:
223: if(nsUri==null || prefix == null || prefix.equals("")) {
224: out.writeAttribute(
225: in.getAttributeLocalName(i),
226: in.getAttributeValue(i)
227: );
228: } else {
229: out.writeAttribute(
230: prefix,
231: nsUri,
232: in.getAttributeLocalName(i),
233: in.getAttributeValue(i)
234: );
235: }
236: }
237:
238: protected void handleDTD() throws XMLStreamException {
239: out.writeDTD(in.getText());
240: }
241:
242: protected void handleComment() throws XMLStreamException {
243: out.writeComment(in.getText());
244: }
245:
246: protected void handleEntityReference() throws XMLStreamException {
247: out.writeEntityRef(in.getText());
248: }
249:
250: protected void handleSpace() throws XMLStreamException {
251: handleCharacters();
252: }
253:
254: protected void handleCDATA() throws XMLStreamException {
255: out.writeCData(in.getText());
256: }
257:
258: private static String fixNull(String s) {
259: if(s==null) return "";
260: else return s;
261: }
262:
263: private int getEventType() throws XMLStreamException {
264: int event = in.getEventType();
265: // if the parser is at the start tag, proceed to the first element
266: //Note - need to do this every time because we could be using a composite reader
267: if(event == XMLStreamConstants.START_DOCUMENT) {
268: // nextTag doesn't correctly handle DTDs
269: while( !in.isStartElement() ) {
270: event = in.next();
271: if (event == XMLStreamConstants.COMMENT)
272: handleComment();
273: }
274: }
275: return event;
276: }
277:
278: private int getNextEvent() throws XMLStreamException {
279: in.next();
280: return getEventType();
281: }
282: }