use of javax.xml.stream.events.StartDocument in project wso2-synapse by wso2.
the class SimpleXMLEventFactoryTest method testCreateStartDocument.
@Test
public void testCreateStartDocument() throws XMLStreamException {
StartDocument event = factory.createStartDocument();
verify(event, XMLStreamConstants.START_DOCUMENT, "<?xml version=\"1.0\"?>");
}
use of javax.xml.stream.events.StartDocument in project wso2-synapse by wso2.
the class SimpleXMLEventWriter method add.
@Override
public void add(XMLEvent event) throws XMLStreamException {
switch(event.getEventType()) {
case XMLStreamConstants.ATTRIBUTE:
Attribute attribute = (Attribute) event;
QName attrName = attribute.getName();
delegate.writeAttribute(attrName.getPrefix(), attrName.getNamespaceURI(), attrName.getLocalPart(), attribute.getValue());
break;
case XMLStreamConstants.END_DOCUMENT:
delegate.writeEndDocument();
break;
case XMLStreamConstants.END_ELEMENT:
delegate.writeEndElement();
break;
case XMLStreamConstants.NAMESPACE:
Namespace namespace = (Namespace) event;
delegate.writeNamespace(namespace.getPrefix(), namespace.getNamespaceURI());
break;
case XMLStreamConstants.START_DOCUMENT:
StartDocument startDocument = (StartDocument) event;
if (startDocument.encodingSet()) {
// encoding defined?
delegate.writeStartDocument(startDocument.getCharacterEncodingScheme(), startDocument.getVersion());
} else {
delegate.writeStartDocument(startDocument.getVersion());
}
break;
case XMLStreamConstants.START_ELEMENT:
StartElement startElement = event.asStartElement();
QName elemName = startElement.getName();
delegate.writeStartElement(elemName.getPrefix(), elemName.getLocalPart(), elemName.getNamespaceURI());
Iterator<?> namespaces = startElement.getNamespaces();
while (namespaces.hasNext()) {
add((Namespace) namespaces.next());
}
Iterator<?> attributes = startElement.getAttributes();
while (attributes.hasNext()) {
add((Attribute) attributes.next());
}
break;
case XMLStreamConstants.CHARACTERS:
case XMLStreamConstants.CDATA:
Characters characters = event.asCharacters();
if (characters.isCData()) {
delegate.writeCData(characters.getData());
} else {
delegate.writeCharacters(characters.getData());
}
break;
case XMLStreamConstants.COMMENT:
delegate.writeComment(((Comment) event).getText());
break;
case XMLStreamConstants.DTD:
delegate.writeDTD(((DTD) event).getDocumentTypeDeclaration());
break;
case XMLStreamConstants.ENTITY_REFERENCE:
delegate.writeEntityRef(((EntityReference) event).getName());
break;
case XMLStreamConstants.PROCESSING_INSTRUCTION:
ProcessingInstruction processingInstruction = (ProcessingInstruction) event;
delegate.writeProcessingInstruction(processingInstruction.getTarget(), processingInstruction.getData());
break;
case XMLStreamConstants.SPACE:
break;
default:
throw new XMLStreamException("Cannot write event " + event);
}
}
use of javax.xml.stream.events.StartDocument in project Payara by payara.
the class GenericSniffer method readDeploymentConfig.
private String readDeploymentConfig(final InputStream is) throws IOException {
String encoding = null;
XMLEventReader rdr = null;
try {
is.mark(Integer.MAX_VALUE);
rdr = xmlInputFactory.createXMLEventReader(new InputStreamReader(is));
while (rdr.hasNext()) {
final XMLEvent ev = rdr.nextEvent();
if (ev.isStartDocument()) {
final StartDocument sd = (StartDocument) ev;
encoding = sd.getCharacterEncodingScheme();
break;
}
}
} catch (XMLStreamException e) {
if (rdr != null) {
try {
rdr.close();
} catch (XMLStreamException inner) {
throw new IOException(e);
}
}
throw new IOException(e);
}
if (encoding == null) {
encoding = "UTF-8";
}
is.reset();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int bytesRead;
final byte[] buffer = new byte[1024];
while ((bytesRead = is.read(buffer)) != -1) {
baos.write(buffer, 0, bytesRead);
}
try {
rdr.close();
} catch (XMLStreamException ex) {
throw new IOException(ex);
}
is.close();
return new String(baos.toByteArray(), encoding);
}
use of javax.xml.stream.events.StartDocument in project spring-framework by spring-projects.
the class StaxEventXMLReader method handleStartDocument.
private void handleStartDocument(final XMLEvent event) throws SAXException {
if (event.isStartDocument()) {
StartDocument startDocument = (StartDocument) event;
String xmlVersion = startDocument.getVersion();
if (StringUtils.hasLength(xmlVersion)) {
this.xmlVersion = xmlVersion;
}
if (startDocument.encodingSet()) {
this.encoding = startDocument.getCharacterEncodingScheme();
}
}
ContentHandler contentHandler = getContentHandler();
if (contentHandler != null) {
final Location location = event.getLocation();
contentHandler.setDocumentLocator(new Locator2() {
@Override
public int getColumnNumber() {
return (location != null ? location.getColumnNumber() : -1);
}
@Override
public int getLineNumber() {
return (location != null ? location.getLineNumber() : -1);
}
@Override
@Nullable
public String getPublicId() {
return (location != null ? location.getPublicId() : null);
}
@Override
@Nullable
public String getSystemId() {
return (location != null ? location.getSystemId() : null);
}
@Override
public String getXMLVersion() {
return xmlVersion;
}
@Override
@Nullable
public String getEncoding() {
return encoding;
}
});
contentHandler.startDocument();
}
}
Aggregations