Search in sources :

Example 66 with DefaultHandler

use of org.xml.sax.helpers.DefaultHandler in project BookReader by JustWayward.

the class Utils method domparse.

public static ArrayList<String> domparse(String filePath, String extractPath, String md5) throws IOException {
    final ArrayList<String> listSite = new ArrayList<>();
    listSite.add(md5);
    ///////////////////////////////////////////////////
    try {
        final FileOutputStream fosHTMLMap = new FileOutputStream(extractPath + "/" + md5);
        final FileOutputStream fosListSite = new FileOutputStream(extractPath + "/site_map_" + md5);
        try {
            fosListSite.write((md5 + ";").getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (chm.getResourceAsStream("") != null) {
            SAXParserImpl.newInstance(null).parse(chm.getResourceAsStream(""), new DefaultHandler() {

                class MyUrl {

                    public int status = 0;

                    public String name;

                    public String local;

                    public String toString() {
                        if (status == 1)
                            return "<a href=\"#\">" + name + "</a>";
                        else
                            return "<a href=\"" + local + "\">" + name + "</a>";
                    }
                }

                MyUrl url = new MyUrl();

                HashMap<String, String> myMap = new HashMap<String, String>();

                int count = 0;

                public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
                    if (qName.equals("param")) {
                        count++;
                        for (int i = 0; i < attributes.getLength(); i++) {
                            myMap.put(attributes.getQName(i).toLowerCase(), attributes.getValue(i).toLowerCase());
                        }
                        if (myMap.get("name").equals("name") && myMap.get("value") != null) {
                            url.name = myMap.get("value");
                            url.status = 1;
                        } else if (myMap.get("name").equals("local") && myMap.get("value") != null) {
                            url.local = myMap.get("value");
                            url.status = 2;
                            listSite.add(url.local.replaceAll("%20", " "));
                            try {
                                fosListSite.write((url.local.replaceAll("%20", " ") + ";").getBytes());
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        if (url.status == 2) {
                            url.status = 0;
                            try {
                                fosHTMLMap.write(url.toString().getBytes());
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    } else {
                        if (url.status == 1) {
                            try {
                                fosHTMLMap.write(url.toString().getBytes());
                                url.status = 0;
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    if (!qName.equals("object") && !qName.equals("param"))
                        try {
                            fosHTMLMap.write(("<" + qName + ">").getBytes());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                }

                public void endElement(String uri, String localName, String qName) throws SAXException {
                    if (!qName.equals("object") && !qName.equals("param"))
                        try {
                            fosHTMLMap.write(("</" + qName + ">").getBytes());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                }
            });
        } else {
            fosHTMLMap.write("<HTML> <BODY> <UL>".getBytes());
            for (String fileName : chm.list()) {
                fileName = fileName.substring(1);
                if (fileName.endsWith(".htm") || fileName.endsWith(".html")) {
                    fosListSite.write((fileName + ";").getBytes());
                    fosHTMLMap.write(("<li><a href=\"" + fileName + "\">" + fileName + "</a></li>").getBytes());
                    listSite.add(fileName);
                }
            }
            fosHTMLMap.write("</UL> </BODY> </HTML>".getBytes());
        }
        fosHTMLMap.close();
        fosListSite.close();
    } catch (SAXException | IOException e) {
        e.printStackTrace();
    }
    return listSite;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Attributes(org.xml.sax.Attributes) IOException(java.io.IOException) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException) FileOutputStream(java.io.FileOutputStream)

Example 67 with DefaultHandler

use of org.xml.sax.helpers.DefaultHandler in project syncany by syncany.

the class MultiCipherStreamsTest method testSaxParserWithMultiCipherTransformer.

public void testSaxParserWithMultiCipherTransformer(List<CipherSpec> cipherSuites) throws Exception {
    String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<database version=\"1\">\n" + "	<databaseVersions>\n" + "		<databaseVersion>\n" + "		</databaseVersion>\n" + "	</databaseVersions>\n" + "	<databaseVersions>\n" + "		<databaseVersion>\n" + "		</databaseVersion>\n" + "	</databaseVersions>\n" + "	<databaseVersions>\n" + "		<databaseVersion>\n" + "		</databaseVersion>\n" + "	</databaseVersions>\n" + "	<databaseVersions>\n" + "		<databaseVersion>\n" + "		</databaseVersion>\n" + "	</databaseVersions>\n" + "</database>";
    Transformer cipherTransformer = new CipherTransformer(cipherSuites, masterKey);
    // Test encrypt
    byte[] encryptedData = doEncrypt(StringUtil.toBytesUTF8(xmlStr), cipherTransformer);
    // Test decrypt with SAX parser
    InputStream is = cipherTransformer.createInputStream(new ByteArrayInputStream(encryptedData));
    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser saxParser = factory.newSAXParser();
    saxParser.parse(is, new DefaultHandler());
// Success if it does not throw an exception
// Regular CipherInputStream does NOT work with GCM mode
// GcmCompatibleCipherInputStream fixes this!
// See http://bouncy-castle.1462172.n4.nabble.com/Using-AES-GCM-NoPadding-with-javax-crypto-CipherInputStream-td4655271.html
// and http://bouncy-castle.1462172.n4.nabble.com/using-GCMBlockCipher-with-CipherInputStream-td4655147.html
}
Also used : CipherTransformer(org.syncany.chunk.CipherTransformer) Transformer(org.syncany.chunk.Transformer) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CipherTransformer(org.syncany.chunk.CipherTransformer) SAXParser(javax.xml.parsers.SAXParser) SAXParserFactory(javax.xml.parsers.SAXParserFactory) DefaultHandler(org.xml.sax.helpers.DefaultHandler)

Example 68 with DefaultHandler

use of org.xml.sax.helpers.DefaultHandler in project wcomponents by BorderTech.

the class DebugValidateXML method validateXMLAgainstSchema.

/**
 * Validate the component to make sure the generated XML is schema compliant.
 *
 * @param xml the xml to validate
 * @return Any errors found, or null if the XML is valid.
 */
public static String validateXMLAgainstSchema(final String xml) {
    // Validate XML against schema
    if (xml != null && !xml.equals("")) {
        // Wrap XML with a root element (if required)
        String testXML = wrapXMLInRootElement(xml);
        try {
            // Create SAX Parser Factory
            SAXParserFactory spf = SAXParserFactory.newInstance();
            spf.setNamespaceAware(true);
            spf.setValidating(true);
            // Create SAX Parser
            SAXParser parser = spf.newSAXParser();
            parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", XMLConstants.W3C_XML_SCHEMA_NS_URI);
            // Set schema location
            Object schema = DebugValidateXML.class.getResource(getSchemaPath()).toString();
            parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", schema);
            // Setup the handler to throw an exception when an error occurs
            DefaultHandler handler = new DefaultHandler() {

                @Override
                public void warning(final SAXParseException e) throws SAXException {
                    LOG.warn("XML Schema warning: " + e.getMessage(), e);
                    super.warning(e);
                }

                @Override
                public void fatalError(final SAXParseException e) throws SAXException {
                    throw e;
                }

                @Override
                public void error(final SAXParseException e) throws SAXException {
                    throw e;
                }
            };
            // Validate the XML
            InputSource xmlSource = new InputSource(new StringReader(testXML));
            parser.parse(xmlSource, handler);
        } catch (SAXParseException e) {
            return "At line " + e.getLineNumber() + ", column: " + e.getColumnNumber() + " ==> " + e.getMessage();
        } catch (Exception e) {
            return e.getMessage();
        }
    }
    return null;
}
Also used : InputSource(org.xml.sax.InputSource) SAXParseException(org.xml.sax.SAXParseException) StringReader(java.io.StringReader) SAXParser(javax.xml.parsers.SAXParser) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) SAXParserFactory(javax.xml.parsers.SAXParserFactory) DefaultHandler(org.xml.sax.helpers.DefaultHandler)

Example 69 with DefaultHandler

use of org.xml.sax.helpers.DefaultHandler in project cxf by apache.

the class AttachmentDeserializerTest method testCXF2542.

@Test
public void testCXF2542() throws Exception {
    StringBuilder buf = new StringBuilder();
    buf.append("------=_Part_0_2180223.1203118300920\n");
    buf.append("Content-Type: application/xop+xml; charset=UTF-8; type=\"text/xml\"\n");
    buf.append("Content-Transfer-Encoding: 8bit\n");
    buf.append("Content-ID: <soap.xml@xfire.codehaus.org>\n");
    buf.append("\n");
    buf.append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" " + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" + "<soap:Body><getNextMessage xmlns=\"http://foo.bar\" /></soap:Body>" + "</soap:Envelope>\n");
    buf.append("------=_Part_0_2180223.1203118300920--\n");
    InputStream rawInputStream = new ByteArrayInputStream(buf.toString().getBytes());
    MessageImpl message = new MessageImpl();
    message.setContent(InputStream.class, rawInputStream);
    message.put(Message.CONTENT_TYPE, "multipart/related; type=\"application/xop+xml\"; " + "start=\"<soap.xml@xfire.codehaus.org>\"; " + "start-info=\"text/xml\"; boundary=\"----=_Part_0_2180223.1203118300920\"");
    new AttachmentDeserializer(message).initializeAttachments();
    InputStream inputStreamWithoutAttachments = message.getContent(InputStream.class);
    SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
    parser.parse(inputStreamWithoutAttachments, new DefaultHandler());
    inputStreamWithoutAttachments.close();
    rawInputStream.close();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PushbackInputStream(java.io.PushbackInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SAXParser(javax.xml.parsers.SAXParser) MessageImpl(org.apache.cxf.message.MessageImpl) DefaultHandler(org.xml.sax.helpers.DefaultHandler) Test(org.junit.Test)

Example 70 with DefaultHandler

use of org.xml.sax.helpers.DefaultHandler in project cxf by apache.

the class AbstractJAXBProvider method validateObjectIfNeeded.

protected void validateObjectIfNeeded(Marshaller marshaller, Class<?> cls, Object obj) throws JAXBException {
    if (validateOutputIfPossible) {
        Schema theSchema = getSchema(cls);
        if (theSchema != null) {
            marshaller.setEventHandler(eventHandler);
            marshaller.setSchema(theSchema);
            if (validateBeforeWrite) {
                marshaller.marshal(obj, new DefaultHandler());
                marshaller.setSchema(null);
            }
        }
    }
}
Also used : Schema(javax.xml.validation.Schema) DefaultHandler(org.xml.sax.helpers.DefaultHandler)

Aggregations

DefaultHandler (org.xml.sax.helpers.DefaultHandler)148 InputStream (java.io.InputStream)65 Metadata (org.apache.tika.metadata.Metadata)59 ParseContext (org.apache.tika.parser.ParseContext)52 Test (org.junit.Test)44 Attributes (org.xml.sax.Attributes)41 SAXParser (javax.xml.parsers.SAXParser)40 SAXException (org.xml.sax.SAXException)39 ByteArrayInputStream (java.io.ByteArrayInputStream)32 SAXParserFactory (javax.xml.parsers.SAXParserFactory)29 IOException (java.io.IOException)26 InputSource (org.xml.sax.InputSource)23 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)22 Parser (org.apache.tika.parser.Parser)22 TikaInputStream (org.apache.tika.io.TikaInputStream)20 ContentHandler (org.xml.sax.ContentHandler)20 File (java.io.File)19 AutoDetectParser (org.apache.tika.parser.AutoDetectParser)17 BodyContentHandler (org.apache.tika.sax.BodyContentHandler)16 FileInputStream (java.io.FileInputStream)15