Search in sources :

Example 1 with XMLResourceIdentifier

use of org.apache.xerces.xni.XMLResourceIdentifier in project intellij-community by JetBrains.

the class ModelGen method loadModel.

public void loadModel(final File... modelRoots) throws Exception {
    XMLEntityResolver resolver = new XMLEntityResolver() {

        public XMLInputSource resolveEntity(XMLResourceIdentifier xmlResourceIdentifier) throws XNIException, IOException {
            String esid = xmlResourceIdentifier.getExpandedSystemId();
            if (esid == null) {
                final String location = schemaLocationMap.get(xmlResourceIdentifier.getNamespace());
                if (location != null) {
                    esid = location;
                } else {
                    return null;
                }
            }
            // Util.log("resolving "+esid);
            File f = null;
            for (File root : modelRoots) {
                if (root == null)
                    continue;
                if (root.isDirectory()) {
                    final String fileName = esid.substring(esid.lastIndexOf('/') + 1);
                    f = new File(root, fileName);
                } else {
                    f = root;
                }
            }
            if (f == null || !f.exists()) {
                Util.logerr("unable to resolve: " + esid);
                return null;
            }
            esid = f.getPath();
            return new XMLInputSource(null, esid, null);
        }
    };
    ArrayList<File> files = new ArrayList<>();
    for (File root : modelRoots) {
        ContainerUtil.addAll(files, root.listFiles());
    }
    loader.loadModel(model, files, resolver);
    Util.log(model.jtMap.size() + " java types loaded");
}
Also used : XMLResourceIdentifier(org.apache.xerces.xni.XMLResourceIdentifier) XMLInputSource(org.apache.xerces.xni.parser.XMLInputSource) ArrayList(java.util.ArrayList) XMLEntityResolver(org.apache.xerces.xni.parser.XMLEntityResolver) File(java.io.File)

Example 2 with XMLResourceIdentifier

use of org.apache.xerces.xni.XMLResourceIdentifier in project ofbiz-framework by apache.

the class UtilXml method readXmlDocument.

public static Document readXmlDocument(InputStream is, boolean validate, String docDescription, boolean withPosition) throws SAXException, ParserConfigurationException, java.io.IOException {
    if (!withPosition) {
        return readXmlDocument(is, validate, docDescription);
    }
    if (is == null) {
        Debug.logWarning("[UtilXml.readXmlDocument] InputStream was null, doing nothing", module);
        return null;
    }
    long startTime = System.currentTimeMillis();
    Document document = null;
    DOMParser parser = new DOMParser() {

        private XMLLocator locator = null;

        private void setLineColumn(Node node) {
            if (locator == null) {
                throw new java.lang.IllegalStateException("XMLLocator is null");
            }
            if (node.getUserData("startLine") != null) {
                return;
            }
            node.setUserData("systemId", locator.getLiteralSystemId(), null);
            node.setUserData("startLine", locator.getLineNumber(), null);
            node.setUserData("startColumn", locator.getColumnNumber(), null);
        }

        private void setLineColumn() {
            try {
                Node node = (Node) getProperty("http://apache.org/xml/properties/dom/current-element-node");
                if (node != null) {
                    setLineColumn(node);
                }
            } catch (SAXException ex) {
                Debug.logWarning(ex, module);
            }
        }

        private void setLastChildLineColumn() {
            try {
                Node node = (Node) getProperty("http://apache.org/xml/properties/dom/current-element-node");
                if (node != null) {
                    setLineColumn(node.getLastChild());
                }
            } catch (SAXException ex) {
                Debug.logWarning(ex, module);
            }
        }

        @Override
        public void startGeneralEntity(String name, XMLResourceIdentifier identifier, String encoding, Augmentations augs) throws XNIException {
            super.startGeneralEntity(name, identifier, encoding, augs);
            setLineColumn();
        }

        @Override
        public void comment(XMLString text, Augmentations augs) throws XNIException {
            super.comment(text, augs);
            setLastChildLineColumn();
        }

        @Override
        public void processingInstruction(String target, XMLString data, Augmentations augs) throws XNIException {
            super.processingInstruction(target, data, augs);
            setLastChildLineColumn();
        }

        @Override
        public void startDocument(XMLLocator locator, String encoding, NamespaceContext namespaceContext, Augmentations augs) throws XNIException {
            super.startDocument(locator, encoding, namespaceContext, augs);
            this.locator = locator;
            setLineColumn();
        }

        @Override
        public void doctypeDecl(String rootElement, String publicId, String systemId, Augmentations augs) throws XNIException {
            super.doctypeDecl(rootElement, publicId, systemId, augs);
        }

        @Override
        public void startElement(QName elementQName, XMLAttributes attrList, Augmentations augs) throws XNIException {
            super.startElement(elementQName, attrList, augs);
            setLineColumn();
        }

        @Override
        public void characters(XMLString text, Augmentations augs) throws XNIException {
            super.characters(text, augs);
            setLastChildLineColumn();
        }

        @Override
        public void ignorableWhitespace(XMLString text, Augmentations augs) throws XNIException {
            super.ignorableWhitespace(text, augs);
            setLastChildLineColumn();
        }
    };
    parser.setFeature("http://xml.org/sax/features/namespaces", true);
    parser.setFeature("http://xml.org/sax/features/validation", validate);
    parser.setFeature("http://apache.org/xml/features/validation/schema", validate);
    parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false);
    // with a SchemaUrl, a URL object
    if (validate) {
        LocalResolver lr = new LocalResolver(new DefaultHandler());
        ErrorHandler eh = new LocalErrorHandler(docDescription, lr);
        parser.setEntityResolver(lr);
        parser.setErrorHandler(eh);
    }
    InputSource inputSource = new InputSource(is);
    inputSource.setSystemId(docDescription);
    parser.parse(inputSource);
    document = parser.getDocument();
    double totalSeconds = (System.currentTimeMillis() - startTime) / 1000.0;
    if (Debug.verboseOn()) {
        Debug.logVerbose("XML Read " + totalSeconds + "s: " + docDescription, module);
    }
    return document;
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) InputSource(org.xml.sax.InputSource) Augmentations(org.apache.xerces.xni.Augmentations) QName(org.apache.xerces.xni.QName) Node(org.w3c.dom.Node) XMLString(org.apache.xerces.xni.XMLString) Document(org.w3c.dom.Document) XMLLocator(org.apache.xerces.xni.XMLLocator) SAXException(org.xml.sax.SAXException) DefaultHandler(org.xml.sax.helpers.DefaultHandler) XMLResourceIdentifier(org.apache.xerces.xni.XMLResourceIdentifier) XMLAttributes(org.apache.xerces.xni.XMLAttributes) NamespaceContext(org.apache.xerces.xni.NamespaceContext) DOMParser(org.apache.xerces.parsers.DOMParser) XMLString(org.apache.xerces.xni.XMLString)

Aggregations

XMLResourceIdentifier (org.apache.xerces.xni.XMLResourceIdentifier)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 DOMParser (org.apache.xerces.parsers.DOMParser)1 Augmentations (org.apache.xerces.xni.Augmentations)1 NamespaceContext (org.apache.xerces.xni.NamespaceContext)1 QName (org.apache.xerces.xni.QName)1 XMLAttributes (org.apache.xerces.xni.XMLAttributes)1 XMLLocator (org.apache.xerces.xni.XMLLocator)1 XMLString (org.apache.xerces.xni.XMLString)1 XMLEntityResolver (org.apache.xerces.xni.parser.XMLEntityResolver)1 XMLInputSource (org.apache.xerces.xni.parser.XMLInputSource)1 Document (org.w3c.dom.Document)1 Node (org.w3c.dom.Node)1 ErrorHandler (org.xml.sax.ErrorHandler)1 InputSource (org.xml.sax.InputSource)1 SAXException (org.xml.sax.SAXException)1 DefaultHandler (org.xml.sax.helpers.DefaultHandler)1