Search in sources :

Example 41 with ContentHandler

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);
}
Also used : LexicalHandler(org.xml.sax.ext.LexicalHandler) ContentHandler(org.xml.sax.ContentHandler)

Example 42 with ContentHandler

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();
}
Also used : InOrder(org.mockito.InOrder) SAXResult(javax.xml.transform.sax.SAXResult) Attributes(org.xml.sax.Attributes) ContentHandler(org.xml.sax.ContentHandler) Test(org.junit.Test)

Example 43 with ContentHandler

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;
}
Also used : Properties(java.util.Properties) ContentHandler(org.xml.sax.ContentHandler)

Example 44 with ContentHandler

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();
}
Also used : InputSource(org.xml.sax.InputSource) StringReader(java.io.StringReader) Attributes(org.xml.sax.Attributes) XMLEventReader(javax.xml.stream.XMLEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory) ContentHandler(org.xml.sax.ContentHandler) Test(org.junit.Test)

Example 45 with ContentHandler

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();
}
Also used : InputSource(org.xml.sax.InputSource) Locator(org.xml.sax.Locator) XMLStreamReader(javax.xml.stream.XMLStreamReader) QName(javax.xml.namespace.QName) StringReader(java.io.StringReader) Attributes(org.xml.sax.Attributes) XMLInputFactory(javax.xml.stream.XMLInputFactory) ContentHandler(org.xml.sax.ContentHandler) Test(org.junit.Test)

Aggregations

ContentHandler (org.xml.sax.ContentHandler)354 Metadata (org.apache.tika.metadata.Metadata)229 BodyContentHandler (org.apache.tika.sax.BodyContentHandler)229 InputStream (java.io.InputStream)210 Test (org.junit.Test)208 ParseContext (org.apache.tika.parser.ParseContext)164 Parser (org.apache.tika.parser.Parser)106 TikaTest (org.apache.tika.TikaTest)103 AutoDetectParser (org.apache.tika.parser.AutoDetectParser)102 TikaInputStream (org.apache.tika.io.TikaInputStream)75 ByteArrayInputStream (java.io.ByteArrayInputStream)64 SAXException (org.xml.sax.SAXException)40 IOException (java.io.IOException)34 TeeContentHandler (org.apache.tika.sax.TeeContentHandler)28 TikaException (org.apache.tika.exception.TikaException)24 ExcelParserTest (org.apache.tika.parser.microsoft.ExcelParserTest)24 WordParserTest (org.apache.tika.parser.microsoft.WordParserTest)24 XHTMLContentHandler (org.apache.tika.sax.XHTMLContentHandler)21 AttributesImpl (org.xml.sax.helpers.AttributesImpl)21 InputSource (org.xml.sax.InputSource)20