use of org.xml.sax.ContentHandler in project spring-framework by spring-projects.
the class XStreamMarshaller method marshalXmlEventWriter.
@Override
protected void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) throws XmlMappingException {
ContentHandler contentHandler = StaxUtils.createContentHandler(eventWriter);
LexicalHandler lexicalHandler = null;
if (contentHandler instanceof LexicalHandler) {
lexicalHandler = (LexicalHandler) contentHandler;
}
marshalSaxHandlers(graph, contentHandler, lexicalHandler);
}
use of org.xml.sax.ContentHandler in project spring-framework by spring-projects.
the class CastorMarshallerTests method marshalSaxResult.
@Test
public void marshalSaxResult() throws Exception {
ContentHandler contentHandler = mock(ContentHandler.class);
SAXResult result = new SAXResult(contentHandler);
marshaller.marshal(flights, result);
InOrder ordered = inOrder(contentHandler);
ordered.verify(contentHandler).startDocument();
ordered.verify(contentHandler).startPrefixMapping("tns", "http://samples.springframework.org/flight");
ordered.verify(contentHandler).startElement(eq("http://samples.springframework.org/flight"), eq("flights"), eq("tns:flights"), isA(Attributes.class));
ordered.verify(contentHandler).startElement(eq("http://samples.springframework.org/flight"), eq("flight"), eq("tns:flight"), isA(Attributes.class));
ordered.verify(contentHandler).startElement(eq("http://samples.springframework.org/flight"), eq("number"), eq("tns:number"), isA(Attributes.class));
ordered.verify(contentHandler).characters(eq(new char[] { '4', '2' }), eq(0), eq(2));
ordered.verify(contentHandler).endElement("http://samples.springframework.org/flight", "number", "tns:number");
ordered.verify(contentHandler).endElement("http://samples.springframework.org/flight", "flight", "tns:flight");
ordered.verify(contentHandler).endElement("http://samples.springframework.org/flight", "flights", "tns:flights");
ordered.verify(contentHandler).endPrefixMapping("tns");
ordered.verify(contentHandler).endDocument();
}
use of org.xml.sax.ContentHandler in project robovm by robovm.
the class SerializerFactory method getSerializer.
/**
* Returns a serializer for the specified output method. The output method
* is specified by the value of the property associated with the "method" key.
* If no implementation exists that supports the specified output method
* an exception of some type will be thrown.
* For a list of the output "method" key values see {@link Method}.
*
* @param format The output format, minimally the "method" property must be set.
* @return A suitable serializer.
* @throws IllegalArgumentException if method is
* null or an appropriate serializer can't be found
* @throws Exception if the class for the serializer is found but does not
* implement ContentHandler.
* @throws WrappedRuntimeException if an exception is thrown while trying to find serializer
*/
public static Serializer getSerializer(Properties format) {
Serializer ser;
try {
String method = format.getProperty(OutputKeys.METHOD);
if (method == null) {
String msg = Utils.messages.createMessage(MsgKey.ER_FACTORY_PROPERTY_MISSING, new Object[] { OutputKeys.METHOD });
throw new IllegalArgumentException(msg);
}
String className = format.getProperty(OutputPropertiesFactory.S_KEY_CONTENT_HANDLER);
if (null == className) {
// Missing Content Handler property, load default using OutputPropertiesFactory
Properties methodDefaults = OutputPropertiesFactory.getDefaultMethodProperties(method);
className = methodDefaults.getProperty(OutputPropertiesFactory.S_KEY_CONTENT_HANDLER);
if (null == className) {
String msg = Utils.messages.createMessage(MsgKey.ER_FACTORY_PROPERTY_MISSING, new Object[] { OutputPropertiesFactory.S_KEY_CONTENT_HANDLER });
throw new IllegalArgumentException(msg);
}
}
ClassLoader loader = ObjectFactory.findClassLoader();
Class cls = ObjectFactory.findProviderClass(className, loader, true);
// _serializers.put(method, cls);
Object obj = cls.newInstance();
if (obj instanceof SerializationHandler) {
// this is one of the supplied serializers
ser = (Serializer) cls.newInstance();
ser.setOutputFormat(format);
} else {
/*
* This must be a user defined Serializer.
* It had better implement ContentHandler.
*/
if (obj instanceof ContentHandler) {
/*
* The user defined serializer defines ContentHandler,
* but we need to wrap it with ToXMLSAXHandler which
* will collect SAX-like events and emit true
* SAX ContentHandler events to the users handler.
*/
className = SerializerConstants.DEFAULT_SAX_SERIALIZER;
cls = ObjectFactory.findProviderClass(className, loader, true);
SerializationHandler sh = (SerializationHandler) cls.newInstance();
sh.setContentHandler((ContentHandler) obj);
sh.setOutputFormat(format);
ser = sh;
} else {
// ContentHandler, ... very bad
throw new Exception(Utils.messages.createMessage(MsgKey.ER_SERIALIZER_NOT_CONTENTHANDLER, new Object[] { className }));
}
}
} catch (Exception e) {
throw new org.apache.xml.serializer.utils.WrappedRuntimeException(e);
}
// If we make it to here ser is not null.
return ser;
}
use of org.xml.sax.ContentHandler in project spring-framework by spring-projects.
the class StaxEventXMLReaderTests method partial.
@Test
public void partial() throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(CONTENT));
// skip to root
eventReader.nextTag();
StaxEventXMLReader xmlReader = new StaxEventXMLReader(eventReader);
ContentHandler contentHandler = mock(ContentHandler.class);
xmlReader.setContentHandler(contentHandler);
xmlReader.parse(new InputSource());
verify(contentHandler).startDocument();
verify(contentHandler).startElement(eq("http://springframework.org/spring-ws"), eq("child"), eq("child"), any(Attributes.class));
verify(contentHandler).endElement("http://springframework.org/spring-ws", "child", "child");
verify(contentHandler).endDocument();
}
use of org.xml.sax.ContentHandler in project spring-framework by spring-projects.
the class StaxStreamXMLReaderTests method partial.
@Test
public void partial() throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(CONTENT));
// skip to root
streamReader.nextTag();
assertEquals("Invalid element", new QName("http://springframework.org/spring-ws", "root"), streamReader.getName());
// skip to child
streamReader.nextTag();
assertEquals("Invalid element", new QName("http://springframework.org/spring-ws", "child"), streamReader.getName());
StaxStreamXMLReader xmlReader = new StaxStreamXMLReader(streamReader);
ContentHandler contentHandler = mock(ContentHandler.class);
xmlReader.setContentHandler(contentHandler);
xmlReader.parse(new InputSource());
verify(contentHandler).setDocumentLocator(any(Locator.class));
verify(contentHandler).startDocument();
verify(contentHandler).startElement(eq("http://springframework.org/spring-ws"), eq("child"), eq("child"), any(Attributes.class));
verify(contentHandler).endElement("http://springframework.org/spring-ws", "child", "child");
verify(contentHandler).endDocument();
}
Aggregations