Class OoxmlFacade


  • public final class OoxmlFacade
    extends java.lang.Object
    Main entry point of the API. Example usage:
    
        public AppConfig parseConfigFile(Path path) throws IOException, XmlException {
            DocumentBuilder builder;
            try {
                builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            } catch (ParserConfigurationException e) {
                throw new IllegalStateException("Platform does not support XML", e);
            }
    
            PositionedXmlDoc doc;
            try (Reader reader = Files.newBufferedReader(path)) {
                // configure the input source's system ID to have
                // a file path in the messages
                InputSource iSource = new InputSource();
                iSource.setSystemId(path.toString());
                iSource.setCharacterStream(reader);
    
                // Here we go
                doc = new OoxmlFacade().parse(builder, iSource);
            }
    
            // This is the object that maps DOM nodes to file
            // positions
            XmlPositioner positioner = doc.getPositioner();
    
            // Create the reporter, which you can use during parsing
            // to report messages on specific nodes
            XmlErrorReporter reporter = new DefaultXmlErrorReporter(XmlMessageHandler.SYSTEM_ERR, positioner));
    
            return appSpecificParsing(doc.getDocument(), reporter);
        }
    
     
    • Constructor Detail

      • OoxmlFacade

        public OoxmlFacade()
    • Method Detail

      • withAnsiColors

        public OoxmlFacade withAnsiColors​(boolean useAnsiColors)
      • withContextLines

        public OoxmlFacade withContextLines​(int numContextLines)
      • isUseAnsiColors

        public boolean isUseAnsiColors()
      • getNumContextLines

        public int getNumContextLines()
      • parse

        public PositionedXmlDoc parse​(javax.xml.parsers.DocumentBuilder domBuilder,
                                      org.xml.sax.InputSource inputSource)
                               throws XmlException,
                                      java.io.IOException
        Parses an XML document and creates an associated XmlPositioner. Exceptions thrown by the parser (eg because of invalid XML syntax) are reported using the given message handler. Their position is recovered on a best-effort basis. Only fatal parsing exceptions are thrown, but even them are passed to the given XmlMessageHandler.

        To validate the document against a schema, you must use either setValidating or setSchema on the document builder factory. Text positions cannot be recovered by a Validator after the fact (this is a limitation of java.xml). Note that setting both may result in duplicate messages.

        For best messages back your InputSource with an InputStream, or better, a Reader.

        Parameters:
        domBuilder - Preconfigured DOM builder, the error handler is set by this method.
        inputSource - Source for the XML document. The system ID should be set for better error messages.
        Throws:
        java.io.IOException - If reading from the input source throws an IOException
        XmlException - If the parser throws a fatal exception
      • spyOn

        private static SpyInputSource spyOn​(org.xml.sax.InputSource inputSource)
                                     throws java.io.IOException
        Throws:
        java.io.IOException