Search in sources :

Example 21 with ContentHandler

use of org.xml.sax.ContentHandler in project j2objc by google.

the class SerializerSwitcher method switchSerializerIfHTML.

/**
   * Switch to HTML serializer if element is HTML
   *
   *
   * @param transformer Non-null transformer instance
   * @param ns Namespace URI of the element
   * @param localName Local part of name of element
   *
   * @throws TransformerException
   */
public static void switchSerializerIfHTML(TransformerImpl transformer, String ns, String localName) throws TransformerException {
    if (null == transformer)
        return;
    if (((null == ns) || (ns.length() == 0)) && localName.equalsIgnoreCase("html")) {
        // Access at level of hashtable to see if the method has been set.
        if (null != transformer.getOutputPropertyNoDefault(OutputKeys.METHOD))
            return;
        // Getting the output properties this way won't cause a clone of 
        // the properties.
        Properties prevProperties = transformer.getOutputFormat().getProperties();
        // We have to make sure we get an output properties with the proper 
        // defaults for the HTML method.  The easiest way to do this is to 
        // have the OutputProperties class do it.
        OutputProperties htmlOutputProperties = new OutputProperties(Method.HTML);
        htmlOutputProperties.copyFrom(prevProperties, true);
        Properties htmlProperties = htmlOutputProperties.getProperties();
        try {
            //        Serializer oldSerializer = transformer.getSerializer();
            Serializer oldSerializer = null;
            if (null != oldSerializer) {
                Serializer serializer = SerializerFactory.getSerializer(htmlProperties);
                Writer writer = oldSerializer.getWriter();
                if (null != writer)
                    serializer.setWriter(writer);
                else {
                    OutputStream os = oldSerializer.getOutputStream();
                    if (null != os)
                        serializer.setOutputStream(os);
                }
                //          transformer.setSerializer(serializer);
                ContentHandler ch = serializer.asContentHandler();
                transformer.setContentHandler(ch);
            }
        } catch (java.io.IOException e) {
            throw new TransformerException(e);
        }
    }
}
Also used : OutputStream(java.io.OutputStream) OutputProperties(org.apache.xalan.templates.OutputProperties) Properties(java.util.Properties) OutputProperties(org.apache.xalan.templates.OutputProperties) Writer(java.io.Writer) ContentHandler(org.xml.sax.ContentHandler) TransformerException(javax.xml.transform.TransformerException) Serializer(org.apache.xml.serializer.Serializer)

Example 22 with ContentHandler

use of org.xml.sax.ContentHandler in project translationstudio8 by heartsome.

the class Xlsx2TmxHelper method parse.

public void parse(InputStream sheetInputStream, ReadOnlySharedStringsTable sharedStringsTable, AbstractWriter tmxWriter) throws ParserConfigurationException, SAXException, IOException {
    InputSource sheetSource = new InputSource(sheetInputStream);
    SAXParserFactory saxFactory = SAXParserFactory.newInstance();
    SAXParser saxParser = saxFactory.newSAXParser();
    XMLReader sheetParser = saxParser.getXMLReader();
    ContentHandler handler = new XSSFHander(sharedStringsTable);
    sheetParser.setContentHandler(handler);
    sheetParser.parse(sheetSource);
    if (langCodes.isEmpty()) {
        throw new SAXException("EMPTY-LANG-CODE");
    }
    writeEnd();
}
Also used : InputSource(org.xml.sax.InputSource) SAXParser(javax.xml.parsers.SAXParser) XMLReader(org.xml.sax.XMLReader) ContentHandler(org.xml.sax.ContentHandler) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Example 23 with ContentHandler

use of org.xml.sax.ContentHandler in project jdk8u_jdk by JetBrains.

the class XMLKit method readFrom.

public static Element readFrom(Reader in, boolean tokenizing, boolean makeFrozen) throws IOException {
    Element sink = new Element();
    ContentHandler b = makeBuilder(sink.asList(), tokenizing, makeFrozen);
    XMLReader parser;
    try {
        parser = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
    } catch (SAXException ee) {
        throw new Error(ee);
    }
    //parser.setFastStandalone(true);
    parser.setContentHandler(b);
    try {
        parser.setProperty("http://xml.org/sax/properties/lexical-handler", (LexicalHandler) b);
    } catch (SAXException ee) {
    // Ignore.  We will miss the comments and whitespace.
    }
    try {
        parser.parse(new InputSource(in));
    } catch (SAXParseException ee) {
        throw new RuntimeException("line " + ee.getLineNumber() + " col " + ee.getColumnNumber() + ": ", ee);
    } catch (SAXException ee) {
        throw new RuntimeException(ee);
    }
    switch(sink.size()) {
        case 0:
            return null;
        case 1:
            if (sink.get(0) instanceof Element) {
                return (Element) sink.get(0);
            }
        // fall through
        default:
            if (makeFrozen) {
                sink.shallowFreeze();
            }
            return sink;
    }
}
Also used : InputSource(org.xml.sax.InputSource) SAXParseException(org.xml.sax.SAXParseException) ContentHandler(org.xml.sax.ContentHandler) XMLReader(org.xml.sax.XMLReader) SAXException(org.xml.sax.SAXException)

Example 24 with ContentHandler

use of org.xml.sax.ContentHandler in project android_frameworks_base by AOSPA.

the class SafeSaxTest method testPerformance.

@LargeTest
public void testPerformance() throws Exception {
    InputStream in = mContext.getResources().openRawResource(R.raw.youtube);
    byte[] xmlBytes;
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int length;
        while ((length = in.read(buffer)) != -1) {
            out.write(buffer, 0, length);
        }
        xmlBytes = out.toByteArray();
    } finally {
        in.close();
    }
    Log.i("***", "File size: " + (xmlBytes.length / 1024) + "k");
    VideoAdapter videoAdapter = new VideoAdapter();
    ContentHandler handler = newContentHandler(videoAdapter);
    for (int i = 0; i < 2; i++) {
        pureSaxTest(new ByteArrayInputStream(xmlBytes));
        saxyModelTest(new ByteArrayInputStream(xmlBytes));
        saxyModelTest(new ByteArrayInputStream(xmlBytes), handler);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ContentHandler(org.xml.sax.ContentHandler) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 25 with ContentHandler

use of org.xml.sax.ContentHandler in project android_frameworks_base by ResurrectionRemix.

the class SafeSaxTest method testPerformance.

@LargeTest
public void testPerformance() throws Exception {
    InputStream in = mContext.getResources().openRawResource(R.raw.youtube);
    byte[] xmlBytes;
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int length;
        while ((length = in.read(buffer)) != -1) {
            out.write(buffer, 0, length);
        }
        xmlBytes = out.toByteArray();
    } finally {
        in.close();
    }
    Log.i("***", "File size: " + (xmlBytes.length / 1024) + "k");
    VideoAdapter videoAdapter = new VideoAdapter();
    ContentHandler handler = newContentHandler(videoAdapter);
    for (int i = 0; i < 2; i++) {
        pureSaxTest(new ByteArrayInputStream(xmlBytes));
        saxyModelTest(new ByteArrayInputStream(xmlBytes));
        saxyModelTest(new ByteArrayInputStream(xmlBytes), handler);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ContentHandler(org.xml.sax.ContentHandler) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Aggregations

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