|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
package jdk.internal.util.xml.impl; |
|
|
|
import java.io.OutputStream; |
|
import java.io.UnsupportedEncodingException; |
|
import java.nio.charset.Charset; |
|
import java.nio.charset.IllegalCharsetNameException; |
|
import java.nio.charset.UnsupportedCharsetException; |
|
import jdk.internal.util.xml.XMLStreamException; |
|
import jdk.internal.util.xml.XMLStreamWriter; |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public class XMLStreamWriterImpl implements XMLStreamWriter { |
|
//Document state |
|
|
|
static final int STATE_XML_DECL = 1; |
|
static final int STATE_PROLOG = 2; |
|
static final int STATE_DTD_DECL = 3; |
|
static final int STATE_ELEMENT = 4; |
|
|
|
static final int ELEMENT_STARTTAG_OPEN = 10; |
|
static final int ELEMENT_STARTTAG_CLOSE = 11; |
|
static final int ELEMENT_ENDTAG_OPEN = 12; |
|
static final int ELEMENT_ENDTAG_CLOSE = 13; |
|
public static final char CLOSE_START_TAG = '>'; |
|
public static final char OPEN_START_TAG = '<'; |
|
public static final String OPEN_END_TAG = "</"; |
|
public static final char CLOSE_END_TAG = '>'; |
|
public static final String START_CDATA = "<![CDATA["; |
|
public static final String END_CDATA = "]]>"; |
|
public static final String CLOSE_EMPTY_ELEMENT = "/>"; |
|
public static final String ENCODING_PREFIX = "&#x"; |
|
public static final char SPACE = ' '; |
|
public static final char AMPERSAND = '&'; |
|
public static final char DOUBLEQUOT = '"'; |
|
public static final char SEMICOLON = ';'; |
|
|
|
private int _state = 0; |
|
private Element _currentEle; |
|
private XMLWriter _writer; |
|
private Charset _charset; |
|
|
|
|
|
|
|
*/ |
|
boolean _escapeCharacters = true; |
|
|
|
private boolean _doIndent = true; |
|
|
|
private char[] _lineSep = System.lineSeparator().toCharArray(); |
|
|
|
public XMLStreamWriterImpl(OutputStream os) throws XMLStreamException { |
|
this(os, XMLStreamWriter.DEFAULT_CHARSET); |
|
} |
|
|
|
public XMLStreamWriterImpl(OutputStream os, Charset cs) |
|
throws XMLStreamException |
|
{ |
|
if (cs == null) { |
|
_charset = XMLStreamWriter.DEFAULT_CHARSET; |
|
} else { |
|
try { |
|
_charset = checkCharset(cs); |
|
} catch (UnsupportedEncodingException e) { |
|
throw new XMLStreamException(e); |
|
} |
|
} |
|
|
|
_writer = new XMLWriter(os, null, _charset); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void writeStartDocument() throws XMLStreamException { |
|
writeStartDocument(_charset.name(), XMLStreamWriter.DEFAULT_XML_VERSION); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void writeStartDocument(String version) throws XMLStreamException { |
|
writeStartDocument(_charset.name(), version, null); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void writeStartDocument(String encoding, String version) throws XMLStreamException { |
|
writeStartDocument(encoding, version, null); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void writeStartDocument(String encoding, String version, String standalone) |
|
throws XMLStreamException |
|
{ |
|
if (_state > 0) { |
|
throw new XMLStreamException("XML declaration must be as the first line in the XML document."); |
|
} |
|
_state = STATE_XML_DECL; |
|
String enc = encoding; |
|
if (enc == null) { |
|
enc = _charset.name(); |
|
} else { |
|
|
|
try { |
|
getCharset(encoding); |
|
} catch (UnsupportedEncodingException e) { |
|
throw new XMLStreamException(e); |
|
} |
|
} |
|
|
|
if (version == null) { |
|
version = XMLStreamWriter.DEFAULT_XML_VERSION; |
|
} |
|
|
|
_writer.write("<?xml version=\""); |
|
_writer.write(version); |
|
_writer.write(DOUBLEQUOT); |
|
|
|
if (enc != null) { |
|
_writer.write(" encoding=\""); |
|
_writer.write(enc); |
|
_writer.write(DOUBLEQUOT); |
|
} |
|
|
|
if (standalone != null) { |
|
_writer.write(" standalone=\""); |
|
_writer.write(standalone); |
|
_writer.write(DOUBLEQUOT); |
|
} |
|
_writer.write("?>"); |
|
writeLineSeparator(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void writeDTD(String dtd) throws XMLStreamException { |
|
if (_currentEle != null && _currentEle.getState() == ELEMENT_STARTTAG_OPEN) { |
|
closeStartTag(); |
|
} |
|
_writer.write(dtd); |
|
writeLineSeparator(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void writeStartElement(String localName) throws XMLStreamException { |
|
if (localName == null || localName.length() == 0) { |
|
throw new XMLStreamException("Local Name cannot be null or empty"); |
|
} |
|
|
|
_state = STATE_ELEMENT; |
|
if (_currentEle != null && _currentEle.getState() == ELEMENT_STARTTAG_OPEN) { |
|
closeStartTag(); |
|
} |
|
|
|
_currentEle = new Element(_currentEle, localName, false); |
|
openStartTag(); |
|
|
|
_writer.write(localName); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void writeEmptyElement(String localName) throws XMLStreamException { |
|
if (_currentEle != null && _currentEle.getState() == ELEMENT_STARTTAG_OPEN) { |
|
closeStartTag(); |
|
} |
|
|
|
_currentEle = new Element(_currentEle, localName, true); |
|
|
|
openStartTag(); |
|
_writer.write(localName); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void writeAttribute(String localName, String value) throws XMLStreamException { |
|
if (_currentEle.getState() != ELEMENT_STARTTAG_OPEN) { |
|
throw new XMLStreamException( |
|
"Attribute not associated with any element"); |
|
} |
|
|
|
_writer.write(SPACE); |
|
_writer.write(localName); |
|
_writer.write("=\""); |
|
writeXMLContent( |
|
value, |
|
true, |
|
true); |
|
_writer.write(DOUBLEQUOT); |
|
} |
|
|
|
public void writeEndDocument() throws XMLStreamException { |
|
if (_currentEle != null && _currentEle.getState() == ELEMENT_STARTTAG_OPEN) { |
|
closeStartTag(); |
|
} |
|
|
|
|
|
|
|
*/ |
|
while (_currentEle != null) { |
|
|
|
if (!_currentEle.isEmpty()) { |
|
_writer.write(OPEN_END_TAG); |
|
_writer.write(_currentEle.getLocalName()); |
|
_writer.write(CLOSE_END_TAG); |
|
} |
|
|
|
_currentEle = _currentEle.getParent(); |
|
} |
|
} |
|
|
|
public void writeEndElement() throws XMLStreamException { |
|
if (_currentEle != null && _currentEle.getState() == ELEMENT_STARTTAG_OPEN) { |
|
closeStartTag(); |
|
} |
|
|
|
if (_currentEle == null) { |
|
throw new XMLStreamException("No element was found to write"); |
|
} |
|
|
|
if (_currentEle.isEmpty()) { |
|
return; |
|
} |
|
|
|
_writer.write(OPEN_END_TAG); |
|
_writer.write(_currentEle.getLocalName()); |
|
_writer.write(CLOSE_END_TAG); |
|
writeLineSeparator(); |
|
|
|
_currentEle = _currentEle.getParent(); |
|
} |
|
|
|
public void writeCData(String cdata) throws XMLStreamException { |
|
if (cdata == null) { |
|
throw new XMLStreamException("cdata cannot be null"); |
|
} |
|
|
|
if (_currentEle != null && _currentEle.getState() == ELEMENT_STARTTAG_OPEN) { |
|
closeStartTag(); |
|
} |
|
|
|
_writer.write(START_CDATA); |
|
_writer.write(cdata); |
|
_writer.write(END_CDATA); |
|
} |
|
|
|
public void writeCharacters(String data) throws XMLStreamException { |
|
if (_currentEle != null && _currentEle.getState() == ELEMENT_STARTTAG_OPEN) { |
|
closeStartTag(); |
|
} |
|
|
|
writeXMLContent(data); |
|
} |
|
|
|
public void writeCharacters(char[] data, int start, int len) |
|
throws XMLStreamException { |
|
if (_currentEle != null && _currentEle.getState() == ELEMENT_STARTTAG_OPEN) { |
|
closeStartTag(); |
|
} |
|
|
|
writeXMLContent(data, start, len, _escapeCharacters); |
|
} |
|
|
|
|
|
|
|
*/ |
|
public void close() throws XMLStreamException { |
|
if (_writer != null) { |
|
_writer.close(); |
|
} |
|
_writer = null; |
|
_currentEle = null; |
|
_state = 0; |
|
} |
|
|
|
|
|
|
|
*/ |
|
public void flush() throws XMLStreamException { |
|
if (_writer != null) { |
|
_writer.flush(); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public void setDoIndent(boolean doIndent) { |
|
_doIndent = doIndent; |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
private void writeXMLContent(char[] content, int start, int length, boolean escapeChars) |
|
throws XMLStreamException |
|
{ |
|
if (!escapeChars) { |
|
_writer.write(content, start, length); |
|
return; |
|
} |
|
|
|
|
|
int startWritePos = start; |
|
|
|
final int end = start + length; |
|
|
|
for (int index = start; index < end; index++) { |
|
char ch = content[index]; |
|
|
|
if (!_writer.canEncode(ch)) { |
|
_writer.write(content, startWritePos, index - startWritePos); |
|
|
|
|
|
_writer.write(ENCODING_PREFIX); |
|
_writer.write(Integer.toHexString(ch)); |
|
_writer.write(SEMICOLON); |
|
startWritePos = index + 1; |
|
continue; |
|
} |
|
|
|
switch (ch) { |
|
case OPEN_START_TAG: |
|
_writer.write(content, startWritePos, index - startWritePos); |
|
_writer.write("<"); |
|
startWritePos = index + 1; |
|
|
|
break; |
|
|
|
case AMPERSAND: |
|
_writer.write(content, startWritePos, index - startWritePos); |
|
_writer.write("&"); |
|
startWritePos = index + 1; |
|
|
|
break; |
|
|
|
case CLOSE_START_TAG: |
|
_writer.write(content, startWritePos, index - startWritePos); |
|
_writer.write(">"); |
|
startWritePos = index + 1; |
|
|
|
break; |
|
} |
|
} |
|
|
|
|
|
_writer.write(content, startWritePos, end - startWritePos); |
|
} |
|
|
|
private void writeXMLContent(String content) throws XMLStreamException { |
|
if ((content != null) && (content.length() > 0)) { |
|
writeXMLContent(content, |
|
_escapeCharacters, |
|
false); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
private void writeXMLContent( |
|
String content, |
|
boolean escapeChars, |
|
boolean escapeDoubleQuotes) |
|
throws XMLStreamException |
|
{ |
|
|
|
if (!escapeChars) { |
|
_writer.write(content); |
|
|
|
return; |
|
} |
|
|
|
|
|
int startWritePos = 0; |
|
|
|
final int end = content.length(); |
|
|
|
for (int index = 0; index < end; index++) { |
|
char ch = content.charAt(index); |
|
|
|
if (!_writer.canEncode(ch)) { |
|
_writer.write(content, startWritePos, index - startWritePos); |
|
|
|
|
|
_writer.write(ENCODING_PREFIX); |
|
_writer.write(Integer.toHexString(ch)); |
|
_writer.write(SEMICOLON); |
|
startWritePos = index + 1; |
|
continue; |
|
} |
|
|
|
switch (ch) { |
|
case OPEN_START_TAG: |
|
_writer.write(content, startWritePos, index - startWritePos); |
|
_writer.write("<"); |
|
startWritePos = index + 1; |
|
|
|
break; |
|
|
|
case AMPERSAND: |
|
_writer.write(content, startWritePos, index - startWritePos); |
|
_writer.write("&"); |
|
startWritePos = index + 1; |
|
|
|
break; |
|
|
|
case CLOSE_START_TAG: |
|
_writer.write(content, startWritePos, index - startWritePos); |
|
_writer.write(">"); |
|
startWritePos = index + 1; |
|
|
|
break; |
|
|
|
case DOUBLEQUOT: |
|
_writer.write(content, startWritePos, index - startWritePos); |
|
if (escapeDoubleQuotes) { |
|
_writer.write("""); |
|
} else { |
|
_writer.write(DOUBLEQUOT); |
|
} |
|
startWritePos = index + 1; |
|
|
|
break; |
|
} |
|
} |
|
|
|
|
|
_writer.write(content, startWritePos, end - startWritePos); |
|
} |
|
|
|
|
|
|
|
*/ |
|
private void openStartTag() throws XMLStreamException { |
|
_currentEle.setState(ELEMENT_STARTTAG_OPEN); |
|
_writer.write(OPEN_START_TAG); |
|
} |
|
|
|
|
|
|
|
*/ |
|
private void closeStartTag() throws XMLStreamException { |
|
if (_currentEle.isEmpty()) { |
|
_writer.write(CLOSE_EMPTY_ELEMENT); |
|
} else { |
|
_writer.write(CLOSE_START_TAG); |
|
|
|
} |
|
|
|
if (_currentEle.getParent() == null) { |
|
writeLineSeparator(); |
|
} |
|
|
|
_currentEle.setState(ELEMENT_STARTTAG_CLOSE); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
private void writeLineSeparator() throws XMLStreamException { |
|
if (_doIndent) { |
|
_writer.write(_lineSep, 0, _lineSep.length); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private Charset getCharset(String encoding) throws UnsupportedEncodingException { |
|
if (encoding.equalsIgnoreCase("UTF-32")) { |
|
throw new UnsupportedEncodingException("The basic XMLWriter does " |
|
+ "not support " + encoding); |
|
} |
|
|
|
Charset cs; |
|
try { |
|
cs = Charset.forName(encoding); |
|
} catch (IllegalCharsetNameException | UnsupportedCharsetException ex) { |
|
throw new UnsupportedEncodingException(encoding); |
|
} |
|
return cs; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private Charset checkCharset(Charset charset) throws UnsupportedEncodingException { |
|
if (charset.name().equalsIgnoreCase("UTF-32")) { |
|
throw new UnsupportedEncodingException("The basic XMLWriter does " |
|
+ "not support " + charset.name()); |
|
} |
|
return charset; |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
protected class Element { |
|
|
|
|
|
|
|
*/ |
|
protected Element _parent; |
|
|
|
|
|
*/ |
|
protected short _Depth; |
|
|
|
|
|
*/ |
|
boolean _isEmptyElement = false; |
|
String _localpart; |
|
int _state; |
|
|
|
|
|
|
|
*/ |
|
public Element() { |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public Element(Element parent, String localpart, boolean isEmpty) { |
|
_parent = parent; |
|
_localpart = localpart; |
|
_isEmptyElement = isEmpty; |
|
} |
|
|
|
public Element getParent() { |
|
return _parent; |
|
} |
|
|
|
public String getLocalName() { |
|
return _localpart; |
|
} |
|
|
|
|
|
|
|
*/ |
|
public int getState() { |
|
return _state; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void setState(int state) { |
|
_state = state; |
|
} |
|
|
|
public boolean isEmpty() { |
|
return _isEmptyElement; |
|
} |
|
} |
|
} |