Example usage for javax.xml.validation Schema toString

List of usage examples for javax.xml.validation Schema toString

Introduction

In this page you can find the example usage for javax.xml.validation Schema toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:test.common.TestBase.java

/**
 * @throws IOException//from  w  w w.  java 2  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();
}