Search in sources :

Example 1 with XMLLocator

use of org.apache.xerces.xni.XMLLocator 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)

Example 2 with XMLLocator

use of org.apache.xerces.xni.XMLLocator in project webtools.sourceediting by eclipse.

the class XMLValidator method createXMLReader.

/**
 * Create an XML Reader.
 *
 * @return The newly created XML reader or null if unsuccessful.
 * @throws Exception
 */
protected XMLReader createXMLReader(final XMLValidationInfo valinfo, XMLEntityResolver entityResolver) throws Exception {
    XMLReader reader = null;
    // move to Xerces-2... add the contextClassLoader stuff
    ClassLoader prevClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        MyStandardParserConfiguration configuration = new MyStandardParserConfiguration(valinfo);
        reader = new org.apache.xerces.parsers.SAXParser(configuration) {

            private XMLLocator locator = null;

            /* (non-Javadoc)
         * @see org.apache.xerces.parsers.AbstractSAXParser#startDocument(org.apache.xerces.xni.XMLLocator, java.lang.String, org.apache.xerces.xni.NamespaceContext, org.apache.xerces.xni.Augmentations)
         */
            public void startDocument(org.apache.xerces.xni.XMLLocator theLocator, java.lang.String encoding, NamespaceContext nscontext, org.apache.xerces.xni.Augmentations augs) {
                locator = theLocator;
                valinfo.setXMLLocator(theLocator);
                super.startDocument(theLocator, encoding, nscontext, augs);
            }

            /* (non-Javadoc)
         * @see org.apache.xerces.parsers.AbstractSAXParser#startElement(org.apache.xerces.xni.QName, org.apache.xerces.xni.XMLAttributes, org.apache.xerces.xni.Augmentations)
         */
            public void startElement(QName element, XMLAttributes attributes, Augmentations augs) throws XNIException {
                valinfo.getStartElementLocations().push(new LocationCoordinate(locator.getLineNumber(), locator.getColumnNumber()));
                super.startElement(element, attributes, augs);
            }

            /* (non-Javadoc)
		 * @see org.apache.xerces.parsers.AbstractSAXParser#endElement(org.apache.xerces.xni.QName, org.apache.xerces.xni.Augmentations)
		 */
            public void endElement(QName element, Augmentations augs) throws XNIException {
                super.endElement(element, augs);
                valinfo.getStartElementLocations().pop();
            }
        };
        // $NON-NLS-1$
        reader.setFeature("http://apache.org/xml/features/continue-after-fatal-error", false);
        // $NON-NLS-1$
        reader.setFeature("http://xml.org/sax/features/namespace-prefixes", valinfo.isNamespaceEncountered());
        // $NON-NLS-1$
        reader.setFeature("http://xml.org/sax/features/namespaces", valinfo.isNamespaceEncountered());
        reader.setContentHandler(new DefaultHandler() {

            public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
                valinfo.getErrorCustomizationManager().startElement(uri, localName);
            }

            public void endElement(String uri, String localName, String qName) throws SAXException {
                valinfo.getErrorCustomizationManager().endElement(uri, localName);
            }
        });
        // MH make sure validation works even when a customer entityResolver is note set (i.e. via setURIResolver())
        if (entityResolver != null) {
            // $NON-NLS-1$
            reader.setProperty("http://apache.org/xml/properties/internal/entity-resolver", entityResolver);
        }
        // $NON-NLS-1$
        reader.setProperty("http://xml.org/sax/properties/declaration-handler", new MyDeclHandler());
    } catch (Exception e) {
        Logger.logException(e);
    // e.printStackTrace();
    } finally {
        Thread.currentThread().setContextClassLoader(prevClassLoader);
    }
    return reader;
}
Also used : Augmentations(org.apache.xerces.xni.Augmentations) QName(org.apache.xerces.xni.QName) XMLLocator(org.apache.xerces.xni.XMLLocator) XMLAttributes(org.apache.xerces.xni.XMLAttributes) Attributes(org.xml.sax.Attributes) XMLLocator(org.apache.xerces.xni.XMLLocator) XNIException(org.apache.xerces.xni.XNIException) URISyntaxException(java.net.URISyntaxException) XNIException(org.apache.xerces.xni.XNIException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) SAXParseException(org.xml.sax.SAXParseException) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException) Augmentations(org.apache.xerces.xni.Augmentations) XMLAttributes(org.apache.xerces.xni.XMLAttributes) NamespaceContext(org.apache.xerces.xni.NamespaceContext) XMLReader(org.xml.sax.XMLReader)

Example 3 with XMLLocator

use of org.apache.xerces.xni.XMLLocator in project webtools.sourceediting by eclipse.

the class XMLValidator method addValidationMessage.

/**
 * Add a validation message to the specified list.
 *
 * @param valinfo The validation info object to add the error to.
 * @param exception The exception that contains the validation information.
 */
protected void addValidationMessage(XMLValidationInfo valinfo, IOException exception) {
    String validationMessageStr = exception.getMessage();
    Throwable cause = exception.getCause() != null ? exception.getCause() : exception;
    while (validationMessageStr == null && cause != null) {
        String localizedMessage = cause.getLocalizedMessage();
        cause = cause.getCause();
        if (cause == null && localizedMessage != null) {
            validationMessageStr = localizedMessage;
        }
    }
    if (validationMessageStr != null) {
        if (cause instanceof FileNotFoundException) {
            validationMessageStr = NLS.bind(XMLValidationMessages._UI_PROBLEMS_VALIDATING_FILE_NOT_FOUND, new Object[] { validationMessageStr });
        } else if (cause instanceof UnknownHostException) {
            validationMessageStr = NLS.bind(XMLValidationMessages._UI_PROBLEMS_VALIDATING_UNKNOWN_HOST, new Object[] { validationMessageStr });
        } else if (cause instanceof ConnectException) {
            validationMessageStr = XMLValidationMessages._UI_PROBLEMS_CONNECTION_REFUSED;
        }
    }
    if (validationMessageStr != null) {
        XMLLocator locator = valinfo.getXMLLocator();
        valinfo.addWarning(validationMessageStr, locator != null ? locator.getLineNumber() : 1, locator != null ? locator.getColumnNumber() : 0, valinfo.getFileURI(), FILE_NOT_FOUND_KEY, null);
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) FileNotFoundException(java.io.FileNotFoundException) XMLLocator(org.apache.xerces.xni.XMLLocator) ConnectException(java.net.ConnectException)

Aggregations

XMLLocator (org.apache.xerces.xni.XMLLocator)3 FileNotFoundException (java.io.FileNotFoundException)2 ConnectException (java.net.ConnectException)2 UnknownHostException (java.net.UnknownHostException)2 Augmentations (org.apache.xerces.xni.Augmentations)2 NamespaceContext (org.apache.xerces.xni.NamespaceContext)2 QName (org.apache.xerces.xni.QName)2 XMLAttributes (org.apache.xerces.xni.XMLAttributes)2 SAXException (org.xml.sax.SAXException)2 DefaultHandler (org.xml.sax.helpers.DefaultHandler)2 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 DOMParser (org.apache.xerces.parsers.DOMParser)1 XMLResourceIdentifier (org.apache.xerces.xni.XMLResourceIdentifier)1 XMLString (org.apache.xerces.xni.XMLString)1 XNIException (org.apache.xerces.xni.XNIException)1 Document (org.w3c.dom.Document)1 Node (org.w3c.dom.Node)1 Attributes (org.xml.sax.Attributes)1 ErrorHandler (org.xml.sax.ErrorHandler)1