static Document loadDocBook5XML()

in freemarker-docgen-core/src/main/java/org/freemarker/docgen/core/XMLUtil.java [97:167]


    static Document loadDocBook5XML(File bookFile, boolean validate,
            DocgenValidationOptions validationOps, DocgenLogger logger)
            throws SAXException, IOException, DocgenException {
        logger.info("Loading " + bookFile.getAbsolutePath() + "...");
        if (validate) {
            if (!isJingAvilable()) {
                throw new DocgenException("Jing classes are reqired for the "
                        + "validation but couldn't be found.");
            }
            
            // Reflection is used to prevent static linking to Jing.
            Method vm;
            try {
                vm = Transform.class.getClassLoader().loadClass(
                        "org.freemarker.docgen.core.RelaxNGValidator")
                            .getMethod("load", new Class[] {
                                    File.class,
                                    DocgenValidationOptions.class});
            } catch (Throwable e) {
                throw new BugException(
                        "Failed to get the "
                        + "org.freemarker.docgen.RelaxNGValidator.validate "
                        + "method (see cause exception).",
                        e);
            }
            try {
                return (Document) vm.invoke(null, bookFile, validationOps);
            } catch (InvocationTargetException e) {
                Throwable te = e.getTargetException();
                if (te instanceof SAXException) {
                    throw (SAXException) te;
                }
                if (te instanceof IOException) {
                    throw (IOException) te;
                }
                throw new BugException(
                        "Failed to setup Relax NG validation "
                        + "(see cause exception).", e);
            } catch (Throwable e) {
                throw new BugException(
                        "Failed to invoke docgen.RelaxNGValidator method "
                        + "(see cause exception).", e);
            }
        } else {
            logger.info("Validation disabled. Be sure the source is "
                    + "valid Docgen-restricted DocBook 5.");
        }
            
        // Here we will use JAXP DocumentBuilderFactory and W3C XML Schema
        ErrorHandler eh = new DraconianErrorHandler(logger);
        
        DocumentBuilderFactory dbf = newDocumentBuilderFactory();

        SchemaFactory schemaFact = SchemaFactory.newInstance(
                XMLConstants.W3C_XML_SCHEMA_NS_URI);
        schemaFact.setErrorHandler(eh);
        Schema schema = schemaFact.newSchema(
                Transform.class.getResource("schema/docbook.xsd"));
        if (validate) {
            dbf.setSchema(schema);
        }
        DocumentBuilder db;
        try {
            db = dbf.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            throw new BugException(e);
        }
        db.setErrorHandler(eh);
        
        return db.parse(bookFile);
    }