Example usage for org.xml.sax.helpers DefaultHandler toString

List of usage examples for org.xml.sax.helpers DefaultHandler toString

Introduction

In this page you can find the example usage for org.xml.sax.helpers DefaultHandler toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:cognition.common.service.DocumentConversionService.java

private String convertWithHandler(byte[] data, DefaultHandler handler) {
    AutoDetectParser parser = new AutoDetectParser();

    Metadata metadata = new Metadata();
    try (InputStream stream = new ByteArrayInputStream(data)) {
        parser.parse(stream, handler, metadata);
        return handler.toString();
    } catch (Exception e) {
        logger.error(e.getMessage());//from  w w w. j a  v  a  2s.  co m
        e.printStackTrace();
    }

    return "";
}

From source file:de.mpg.mpdl.inge.xmltransforming.TestBase.java

/**
 * @param xmlData/*from   ww  w .  ja  v a  2  s .c o m*/
 * @return
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 * @throws UnsupportedEncodingException
 */
private static String getNameSpaceFromXml(final String xmlData)
        throws ParserConfigurationException, SAXException, IOException, UnsupportedEncodingException {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser parser = factory.newSAXParser();
    DefaultHandler handler = new DefaultHandler() {
        private String nameSpace = null;
        private boolean first = true;

        public void startElement(String uri, String localName, String qName, Attributes attributes) {
            if (first) {
                if (qName.contains(":")) {
                    String prefix = qName.substring(0, qName.indexOf(":"));
                    String attributeName = "xmlns:" + prefix;
                    nameSpace = attributes.getValue(attributeName);
                } else {
                    nameSpace = attributes.getValue("xmlns");
                }
                first = false;
            }

        }

        public String toString() {
            return nameSpace;
        }
    };
    parser.parse(new ByteArrayInputStream(xmlData.getBytes("UTF-8")), handler);
    String nameSpace = handler.toString();
    return nameSpace;
}

From source file:de.mpg.mpdl.inge.xmltransforming.TestBase.java

/**
 * @throws IOException//  w w w . j a va  2  s .co m
 * @throws SAXException
 * @throws ParserConfigurationException
 */
private static void initializeSchemas() throws IOException, SAXException, ParserConfigurationException {
    File[] schemaFiles = ResourceUtil.getFilenamesInDirectory("xsd/", TestBase.class.getClassLoader());
    schemas = new HashMap<String, Schema>();
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    for (File file : schemaFiles) {
        try {
            Schema schema = sf.newSchema(file);
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            DefaultHandler handler = new DefaultHandler() {
                private String nameSpace = null;
                private boolean found = false;

                public void startElement(String uri, String localName, String qName, Attributes attributes) {
                    if (!found) {
                        String tagName = null;
                        int ix = qName.indexOf(":");
                        if (ix >= 0) {
                            tagName = qName.substring(ix + 1);
                        } else {
                            tagName = qName;
                        }
                        if ("schema".equals(tagName)) {
                            nameSpace = attributes.getValue("targetNamespace");
                            found = true;
                        }
                    }
                }

                public String toString() {
                    return nameSpace;
                }
            };
            parser.parse(file, handler);
            if (handler.toString() != null) {
                schemas.put(handler.toString(), schema);
            } else {
                logger.warn("Error reading xml schema: " + file);
            }

        } catch (Exception e) {
            logger.warn("Invalid xml schema " + file);
            logger.debug("Stacktrace: ", e);
        }

    }
}

From source file:test.common.TestBase.java

/**
 * @param xmlData/*from w w  w.  j ava 2  s .co  m*/
 * @return
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 * @throws UnsupportedEncodingException
 */
private static String getNameSpaceFromXml(final String xmlData)
        throws ParserConfigurationException, SAXException, IOException, UnsupportedEncodingException {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser parser = factory.newSAXParser();
    DefaultHandler handler = new DefaultHandler() {
        private String nameSpace = null;
        private boolean first = true;

        public void startElement(String uri, String localName, String qName, Attributes attributes) {
            if (first) {
                if (qName.contains(":")) {
                    String prefix = qName.substring(0, qName.indexOf(":"));
                    String attributeName = "xmlns:" + prefix;
                    nameSpace = attributes.getValue(attributeName);
                } else {
                    nameSpace = attributes.getValue("xmlns");
                }
                first = false;
            }
        }

        public String toString() {
            return nameSpace;
        }
    };
    parser.parse(new ByteArrayInputStream(xmlData.getBytes("UTF-8")), handler);
    String nameSpace = handler.toString();
    return nameSpace;
}

From source file:test.common.TestBase.java

/**
 * @throws IOException/*  w  w  w  .j  av  a2  s  . c o m*/
 * @throws SAXException
 * @throws ParserConfigurationException
 */
private static void initializeSchemas() throws IOException, SAXException, ParserConfigurationException {
    File[] schemaFiles = ResourceUtil.getFilenamesInDirectory("xsd/", TestBase.class.getClassLoader());
    PrintWriter pwriter = new PrintWriter("target/schemas.txt");
    logger.debug("Number of schema files: " + schemaFiles.length);
    pwriter.println("Number of schema files: " + schemaFiles.length);

    schemas = new HashMap<String, Schema>();
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    //        sf.setResourceResolver(new ImportResolver());
    for (File file : schemaFiles) {
        logger.debug("Schema file: " + file.getCanonicalPath());
        pwriter.println("Schema file: " + file.getCanonicalPath());

        try {

            //TODO remove this hack when xsd files are cleared
            if (file.getCanonicalPath().contains("rest")) {
                logger.debug("Skipping schema file: " + file.getCanonicalPath());
                continue;
            }
            if (file.getCanonicalPath().endsWith("srw-types.xsd") && !file.getCanonicalPath().contains("0.8")) {
                logger.debug("Skipping schema file: " + file.getCanonicalPath());
                continue;
            }
            // end TODO                

            Schema schema = sf.newSchema(file);

            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            DefaultHandler handler = new DefaultHandler() {
                private String nameSpace = null;
                private boolean found = false;

                public void startElement(String uri, String localName, String qName, Attributes attributes) {
                    if (!found) {
                        String tagName = null;
                        int ix = qName.indexOf(":");
                        if (ix >= 0) {
                            tagName = qName.substring(ix + 1);
                        } else {
                            tagName = qName;
                        }
                        if ("schema".equals(tagName)) {
                            nameSpace = attributes.getValue("targetNamespace");
                            found = true;
                        }
                    }
                }

                public String toString() {
                    return nameSpace;
                }
            };
            parser.parse(file, handler);
            if (handler.toString() != null) {
                Schema s = schemas.get(handler.toString());
                if (s != null) {
                    logger.debug("overwriting key '" + handler.toString() + "'");
                }
                schemas.put(handler.toString(), schema);
                logger.debug("Successfully added: " + file.getCanonicalPath() + " key: '" + handler.toString()
                        + "' value: " + schema.toString() + " " + schema.newValidator());
            } else {
                logger.warn("Error reading xml schema: " + file);
            }
        } catch (Exception e) {
            logger.warn("Invalid xml schema " + file + " , cause " + e.getLocalizedMessage());
            logger.debug("Stacktrace: ", e);
        }
    }
    logger.info("XSD Schemas found: " + schemas);
    pwriter.close();
}