本文整理汇总了Java中javax.xml.transform.stax.StAXResult类的典型用法代码示例。如果您正苦于以下问题:Java StAXResult类的具体用法?Java StAXResult怎么用?Java StAXResult使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
StAXResult类属于javax.xml.transform.stax包,在下文中一共展示了StAXResult类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createResult
import javax.xml.transform.stax.StAXResult; //导入依赖的package包/类
/**
* Retrieves a new Result for setting the XML value designated by this
* SQLXML instance.
*
* @param resultClass The class of the result, or null.
* @throws java.sql.SQLException if there is an error processing the XML
* value or the state is not writable
* @return for setting the XML value designated by this SQLXML instance.
*/
protected <T extends Result>T createResult(
Class<T> resultClass) throws SQLException {
checkWritable();
setWritable(false);
setReadable(true);
if (JAXBResult.class.isAssignableFrom(resultClass)) {
// Must go first presently, since JAXBResult extends SAXResult
// (purely as an implmentation detail) and it's not possible
// to instantiate a valid JAXBResult with a Zero-Args
// constructor(or any subclass thereof, due to the finality of
// its private UnmarshallerHandler)
// FALL THROUGH... will throw an exception
} else if ((resultClass == null)
|| StreamResult.class.isAssignableFrom(resultClass)) {
return createStreamResult(resultClass);
} else if (DOMResult.class.isAssignableFrom(resultClass)) {
return createDOMResult(resultClass);
} else if (SAXResult.class.isAssignableFrom(resultClass)) {
return createSAXResult(resultClass);
} else if (StAXResult.class.isAssignableFrom(resultClass)) {
return createStAXResult(resultClass);
}
throw JDBCUtil.invalidArgument("resultClass: " + resultClass);
}
示例2: createXMLStreamWriter
import javax.xml.transform.stax.StAXResult; //导入依赖的package包/类
public javax.xml.stream.XMLStreamWriter createXMLStreamWriter(javax.xml.transform.Result result) throws javax.xml.stream.XMLStreamException {
if (result instanceof StreamResult) {
return createXMLStreamWriter((StreamResult) result, null);
} else if (result instanceof DOMResult) {
return new XMLDOMWriterImpl((DOMResult) result);
} else if (result instanceof StAXResult) {
if (((StAXResult) result).getXMLStreamWriter() != null) {
return ((StAXResult) result).getXMLStreamWriter();
} else {
throw new java.lang.UnsupportedOperationException("Result of type " + result + " is not supported");
}
} else {
if (result.getSystemId() !=null) {
//this is not correct impl of SAXResult. Keep it for now for compatibility
return createXMLStreamWriter(new StreamResult(result.getSystemId()));
} else {
throw new java.lang.UnsupportedOperationException("Result of type " + result + " is not supported. " +
"Supported result types are: DOMResult, StAXResult and StreamResult.");
}
}
}
示例3: testStreamWriterWithStAXResultNStreamWriter
import javax.xml.transform.stax.StAXResult; //导入依赖的package包/类
@Test
public void testStreamWriterWithStAXResultNStreamWriter() {
final String EXPECTED_OUTPUT = "<?xml version=\"1.0\"?><root></root>";
try {
XMLOutputFactory ofac = XMLOutputFactory.newInstance();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
XMLStreamWriter writer = ofac.createXMLStreamWriter(buffer);
StAXResult res = new StAXResult(writer);
writer = ofac.createXMLStreamWriter(res);
writer.writeStartDocument("1.0");
writer.writeStartElement("root");
writer.writeEndElement();
writer.writeEndDocument();
writer.close();
Assert.assertEquals(buffer.toString(), EXPECTED_OUTPUT);
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.toString());
}
}
示例4: testValidateStAX
import javax.xml.transform.stax.StAXResult; //导入依赖的package包/类
@Test
public void testValidateStAX() {
File resultFile = null;
try {
resultFile = new File("stax.result");
if (resultFile.exists()) {
resultFile.delete();
}
Result xmlResult = new javax.xml.transform.stax.StAXResult(XMLOutputFactory.newInstance().createXMLStreamWriter(new FileWriter(resultFile)));
Source xmlSource = new javax.xml.transform.stax.StAXSource(getXMLEventReader("toys.xml"));
validate("toys.xsd", xmlSource, xmlResult);
((StAXResult) xmlResult).getXMLStreamWriter().close();
Assert.assertTrue(resultFile.exists(), "result file is not created");
} catch (Exception ex) {
ex.printStackTrace();
Assert.fail("Exception : " + ex.getMessage());
} finally {
if (resultFile != null && resultFile.exists()) {
resultFile.delete();
}
}
}
示例5: call
import javax.xml.transform.stax.StAXResult; //导入依赖的package包/类
@Override
public StringValue call(XPathContext context, Sequence[] arguments) throws XPathException {
StringWriter sw = new StringWriter();
JsonXMLConfig config = new JsonXMLConfigBuilder().
prettyPrint(true).
build();
XMLOutputFactory jFactory = new JsonXMLOutputFactory(config);
TransformerFactory tFactory = new TransformerFactoryImpl();
try {
SequenceIterator iter = arguments[0].iterate();
Item item;
while ((item = iter.next()) != null) {
if (item instanceof NodeInfo) {
Transformer transformer = tFactory.newTransformer();
XMLStreamWriter xsw = jFactory.createXMLStreamWriter(sw);
transformer.transform((NodeInfo) item, new StAXResult(xsw));
} else {
sw.append(item.getStringValue());
}
}
} catch (Exception e) {
throw new XPathException("Error serializing sequence to JSON", e);
}
return StringValue.makeStringValue(sw.toString());
}
示例6: setupStAXResultHandler
import javax.xml.transform.stax.StAXResult; //导入依赖的package包/类
/**
* Sets up handler for <code>StAXResult</code>.
*/
private void setupStAXResultHandler(StAXResult result) {
// If there's no StAXResult, unset the validator handler
if (result == null) {
fStAXValidatorHandler = null;
fSchemaValidator.setDocumentHandler(null);
return;
}
XMLStreamWriter writer = result.getXMLStreamWriter();
if (writer != null) {
if (fStAXStreamResultBuilder == null) {
fStAXStreamResultBuilder = new StAXStreamResultBuilder(fNamespaceContext);
}
fStAXValidatorHandler = fStAXStreamResultBuilder;
fStAXStreamResultBuilder.setStAXResult(result);
}
else {
if (fStAXEventResultBuilder == null) {
fStAXEventResultBuilder = new StAXEventResultBuilder(this, fNamespaceContext);
}
fStAXValidatorHandler = fStAXEventResultBuilder;
fStAXEventResultBuilder.setStAXResult(result);
}
fSchemaValidator.setDocumentHandler(fStAXValidatorHandler);
}
示例7: anyxmlNode
import javax.xml.transform.stax.StAXResult; //导入依赖的package包/类
void anyxmlNode(final QName qname, final Object value) throws IOException {
if (value != null) {
checkArgument(value instanceof DOMSource, "AnyXML value must be DOMSource, not %s", value);
final DOMSource domSource = (DOMSource) value;
requireNonNull(domSource.getNode());
checkArgument(domSource.getNode().getNodeName().equals(qname.getLocalName()));
checkArgument(domSource.getNode().getNamespaceURI().equals(qname.getNamespace().toString()));
try {
// TODO can the transformer be a constant ? is it thread safe ?
final Transformer transformer = TRANSFORMER_FACTORY.newTransformer();
// Writer has to be wrapped in a wrapper that ignores endDocument event
// EndDocument event forbids any other modification to the writer so a nested anyXml breaks
// serialization
transformer.transform(domSource, new StAXResult(new DelegateWriterNoEndDoc(writer)));
} catch (final TransformerException e) {
throw new IOException("Unable to transform anyXml(" + qname + ") value: " + value, e);
}
}
}
示例8: createResult
import javax.xml.transform.stax.StAXResult; //导入依赖的package包/类
/**
* Retrieves a new Result for setting the XML value designated by this
* SQLXML instance.
*
* @param resultClass The class of the result, or null.
* @throws java.sql.SQLException if there is an error processing the XML
* value or the state is not writable
* @return for setting the XML value designated by this SQLXML instance.
*/
protected <T extends Result>T createResult(
Class<T> resultClass) throws SQLException {
checkWritable();
setWritable(false);
setReadable(true);
if (JAXBResult.class.isAssignableFrom(resultClass)) {
// Must go first presently, since JAXBResult extends SAXResult
// (purely as an implementation detail) and it's not possible
// to instantiate a valid JAXBResult with a Zero-Args
// constructor(or any subclass thereof, due to the finality of
// its private UnmarshallerHandler)
// FALL THROUGH... will throw an exception
} else if ((resultClass == null)
|| StreamResult.class.isAssignableFrom(resultClass)) {
return createStreamResult(resultClass);
} else if (DOMResult.class.isAssignableFrom(resultClass)) {
return createDOMResult(resultClass);
} else if (SAXResult.class.isAssignableFrom(resultClass)) {
return createSAXResult(resultClass);
} else if (StAXResult.class.isAssignableFrom(resultClass)) {
return createStAXResult(resultClass);
}
throw JDBCUtil.invalidArgument("resultClass: " + resultClass);
}
示例9: createResult
import javax.xml.transform.stax.StAXResult; //导入依赖的package包/类
/**
* Retrieves a new Result for setting the XML value designated by this
* SQLXML instance.
*
* @param resultClass The class of the result, or null.
* @throws java.sql.SQLException if there is an error processing the XML
* value or the state is not writable
* @return for setting the XML value designated by this SQLXML instance.
*/
protected <T extends Result>T createResult(
Class<T> resultClass) throws SQLException {
checkWritable();
setWritable(false);
setReadable(true);
if (JAXBResult.class.isAssignableFrom(resultClass)) {
// Must go first presently, since JAXBResult extends SAXResult
// (purely as an implmentation detail) and it's not possible
// to instantiate a valid JAXBResult with a Zero-Args
// constructor(or any subclass thereof, due to the finality of
// its private UnmarshallerHandler)
// FALL THROUGH... will throw an exception
} else if ((resultClass == null)
|| StreamResult.class.isAssignableFrom(resultClass)) {
return createStreamResult(resultClass);
} else if (DOMResult.class.isAssignableFrom(resultClass)) {
return createDOMResult(resultClass);
} else if (SAXResult.class.isAssignableFrom(resultClass)) {
return createSAXResult(resultClass);
} else if (StAXResult.class.isAssignableFrom(resultClass)) {
return createStAXResult(resultClass);
}
throw Util.invalidArgument("resultClass: " + resultClass);
}
示例10: adaptsToStAXResult
import javax.xml.transform.stax.StAXResult; //导入依赖的package包/类
@Test
public void adaptsToStAXResult() throws SAXException, XMLStreamException
{
filter.setResult(new StAXResult(this.xmlStreamWriter));
filter.endDocument();
verify(this.xmlStreamWriter).writeEndDocument();
}
示例11: testEventWriterWithStAXResultNStreamWriter
import javax.xml.transform.stax.StAXResult; //导入依赖的package包/类
@Test
public void testEventWriterWithStAXResultNStreamWriter() {
String encoding = "";
if (getSystemProperty("file.encoding").equals("UTF-8")) {
encoding = " encoding=\"UTF-8\"";
}
final String EXPECTED_OUTPUT = "<?xml version=\"1.0\"" + encoding + "?><root></root>";
try {
XMLOutputFactory ofac = XMLOutputFactory.newInstance();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
XMLStreamWriter swriter = ofac.createXMLStreamWriter(buffer);
StAXResult res = new StAXResult(swriter);
XMLEventWriter writer = ofac.createXMLEventWriter(res);
XMLEventFactory efac = XMLEventFactory.newInstance();
writer.add(efac.createStartDocument(null, "1.0"));
writer.add(efac.createStartElement("", "", "root"));
writer.add(efac.createEndElement("", "", "root"));
writer.add(efac.createEndDocument());
writer.close();
Assert.assertEquals(buffer.toString(), EXPECTED_OUTPUT);
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.toString());
}
}
示例12: testEventWriterWithStAXResultNEventWriter
import javax.xml.transform.stax.StAXResult; //导入依赖的package包/类
@Test
public void testEventWriterWithStAXResultNEventWriter() {
String encoding = "";
if (getSystemProperty("file.encoding").equals("UTF-8")) {
encoding = " encoding=\"UTF-8\"";
}
final String EXPECTED_OUTPUT = "<?xml version=\"1.0\"" + encoding + "?><root></root>";
try {
XMLOutputFactory ofac = XMLOutputFactory.newInstance();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
XMLEventWriter writer = ofac.createXMLEventWriter(buffer);
StAXResult res = new StAXResult(writer);
writer = ofac.createXMLEventWriter(res);
XMLEventFactory efac = XMLEventFactory.newInstance();
writer.add(efac.createStartDocument(null, "1.0"));
writer.add(efac.createStartElement("", "", "root"));
writer.add(efac.createEndElement("", "", "root"));
writer.add(efac.createEndDocument());
writer.close();
Assert.assertEquals(buffer.toString(), EXPECTED_OUTPUT);
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.toString());
}
}
示例13: testStreamWriterWithStAXResultNEventWriter
import javax.xml.transform.stax.StAXResult; //导入依赖的package包/类
@Test
public void testStreamWriterWithStAXResultNEventWriter() throws Exception {
try {
XMLOutputFactory ofac = XMLOutputFactory.newInstance();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
XMLEventWriter writer = ofac.createXMLEventWriter(buffer);
StAXResult res = new StAXResult(writer);
XMLStreamWriter swriter = ofac.createXMLStreamWriter(res);
Assert.fail("Expected an Exception as XMLStreamWriter can't be created " + "with a StAXResult which has EventWriter.");
} catch (Exception e) {
System.out.println(e.toString());
}
}
示例14: testFeatures
import javax.xml.transform.stax.StAXResult; //导入依赖的package包/类
/**
* Refer to 6631168 : StAXSource & StAXResult support in JavaSE6
*/
@Test
public final void testFeatures() {
TransformerFactory tff = TransformerFactory.newInstance();
Assert.assertTrue(tff.getFeature(StAXSource.FEATURE));
Assert.assertTrue(tff.getFeature(StAXResult.FEATURE));
}
示例15: testDOM2StAX
import javax.xml.transform.stax.StAXResult; //导入依赖的package包/类
@Test
public void testDOM2StAX() {
try {
Source input = domUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE));
StAXResult staxResult = (StAXResult) staxUtil.prepareResult();
idTransform.transform(input, staxResult);
staxUtil.checkResult(staxResult, EXPECTED_VERSION, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Exception occured: " + e.getMessage());
}
}