Search in sources :

Example 1 with ValidationInfo

use of org.eclipse.wst.xml.core.internal.validation.core.ValidationInfo in project webtools.sourceediting by eclipse.

the class XSDValidator method validate.

/**
 * Validate the XSD file specified by the URI.
 *
 * @param uri
 * 		The URI of the XSD file to validate.
 * @param inputStream
 * 		An input stream representing the XSD file to validate.
 * @param configuration
 * 		A configuration for this validation run.
 */
public ValidationReport validate(String uri, InputStream inputStream, XSDValidationConfiguration configuration) {
    if (configuration == null) {
        configuration = new XSDValidationConfiguration();
    }
    ValidationInfo valinfo = new ValidationInfo(uri);
    XSDErrorHandler errorHandler = new XSDErrorHandler(valinfo);
    try {
        XMLGrammarPreparser grammarPreparser = new XMLGrammarPreparser();
        grammarPreparser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null);
        grammarPreparser.setProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY, new XMLGrammarPoolImpl());
        grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE, false);
        grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE, true);
        grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.NAMESPACE_PREFIXES_FEATURE, true);
        grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.VALIDATION_FEATURE, true);
        grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_VALIDATION_FEATURE, true);
        grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.EXTERNAL_GENERAL_ENTITIES_FEATURE, true);
        grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.EXTERNAL_PARAMETER_ENTITIES_FEATURE, true);
        grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_DUPLICATE_ATTDEF_FEATURE, true);
        if (configuration.getFeature(XSDValidationConfiguration.HONOUR_ALL_SCHEMA_LOCATIONS)) {
            try {
                // $NON-NLS-1$
                grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + "honour-all-schemaLocations", true);
            } catch (Exception e) {
            // catch the exception and ignore
            }
        }
        if (configuration.getFeature(XSDValidationConfiguration.FULL_SCHEMA_CONFORMANCE)) {
            try {
                grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_FULL_CHECKING, true);
            } catch (Exception e) {
            // ignore since we don't want to set it or can't.
            }
        }
        grammarPreparser.setErrorHandler(errorHandler);
        if (uriresolver != null) {
            XSDEntityResolver resolver = new XSDEntityResolver(uriresolver, uri);
            if (resolver != null) {
                grammarPreparser.setEntityResolver(resolver);
            }
        }
        try {
            XMLInputSource is = new XMLInputSource(null, uri, uri, inputStream, null);
            grammarPreparser.getLoader(XMLGrammarDescription.XML_SCHEMA);
            grammarPreparser.preparseGrammar(XMLGrammarDescription.XML_SCHEMA, is);
        } catch (Exception e) {
        // parser will return null pointer exception if the document is structurally invalid
        // TODO: log error message
        // System.out.println(e);
        }
    } catch (Exception e) {
    // TODO: log error.
    // System.out.println(e);
    }
    return valinfo;
}
Also used : ValidationInfo(org.eclipse.wst.xml.core.internal.validation.core.ValidationInfo) XMLGrammarPreparser(org.apache.xerces.parsers.XMLGrammarPreparser) XMLInputSource(org.apache.xerces.xni.parser.XMLInputSource) XMLGrammarPoolImpl(org.apache.xerces.util.XMLGrammarPoolImpl) IOException(java.io.IOException) XMLParseException(org.apache.xerces.xni.parser.XMLParseException) XNIException(org.apache.xerces.xni.XNIException)

Example 2 with ValidationInfo

use of org.eclipse.wst.xml.core.internal.validation.core.ValidationInfo in project webtools.sourceediting by eclipse.

the class DTDValidator method validate.

/**
 * Validate the DTD file located at the URI.
 *
 * @param uri
 *            The URI of the file to validate.
 * @return A validation report for the validation.
 */
public ValidationReport validate(String uri) {
    ValidationInfo valinfo = new ValidationInfo(uri);
    try {
        SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
        XMLReader reader = parser.getXMLReader();
        MultiHandler dtdHandler = new MultiHandler();
        // $NON-NLS-1$
        reader.setProperty("http://xml.org/sax/properties/declaration-handler", dtdHandler);
        // $NON-NLS-1$
        reader.setProperty("http://xml.org/sax/properties/lexical-handler", dtdHandler);
        reader.setContentHandler(dtdHandler);
        reader.setDTDHandler(dtdHandler);
        reader.setErrorHandler(new DTDErrorHandler(valinfo));
        reader.setEntityResolver(new DTDEntityResolver(fResolver, uri));
        // $NON-NLS-1$ //$NON-NLS-2$
        String document = "<!DOCTYPE root SYSTEM \"" + uri + "\"><root/>";
        reader.parse(new InputSource(new StringReader(document)));
        Map elemDecls = dtdHandler.getElementDeclarations();
        Hashtable elemRefs = dtdHandler.getElementReferences();
        validateElementReferences(elemDecls, elemRefs, valinfo);
        validateDuplicateElementDecls(elemDecls, valinfo);
    } catch (ParserConfigurationException e) {
    } catch (IOException e) {
    } catch (SAXException e) {
    }
    return valinfo;
}
Also used : InputSource(org.xml.sax.InputSource) Hashtable(java.util.Hashtable) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) ValidationInfo(org.eclipse.wst.xml.core.internal.validation.core.ValidationInfo) StringReader(java.io.StringReader) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) HashMap(java.util.HashMap) Map(java.util.Map) XMLReader(org.xml.sax.XMLReader)

Example 3 with ValidationInfo

use of org.eclipse.wst.xml.core.internal.validation.core.ValidationInfo in project webtools.sourceediting by eclipse.

the class ErrorCustomizationManagerTest method testConsiderReportedError.

/**
 * Test the considerReportedError method with the following tests:
 * 1. Check that the messageForConsideration is not set if there are no current
 *    validation messages.
 * 2. Check that the messageForConsideration is set correctly if there is a
 *    validation message.
 */
public void testConsiderReportedError() {
    // 1. Check that the messageForConsideration is not set if there are no current validation messages.
    String namespace1 = "http://namespace1";
    ErrorCustomizationManagerWrapper manager = new ErrorCustomizationManagerWrapper();
    ValidationInfo valinfo = new ValidationInfo(namespace1);
    manager.considerReportedError(valinfo, "key", null);
    assertNull("1. The messageForConsideration is not null when no validation messages exist.", manager.getMessageForConsideration());
    // 2. Check that the messageForConsideration is set correctly if there is a validation message.
    ErrorCustomizationManagerWrapper manager2 = new ErrorCustomizationManagerWrapper();
    ValidationInfo valinfo2 = new ValidationInfo(namespace1);
    valinfo2.addError("message", 1, 1, namespace1);
    manager2.considerReportedError(valinfo2, "key", null);
    assertNotNull("2. The messageForConsideration is null when a validation message exists.", manager2.getMessageForConsideration());
}
Also used : ValidationInfo(org.eclipse.wst.xml.core.internal.validation.core.ValidationInfo)

Aggregations

ValidationInfo (org.eclipse.wst.xml.core.internal.validation.core.ValidationInfo)3 IOException (java.io.IOException)2 StringReader (java.io.StringReader)1 HashMap (java.util.HashMap)1 Hashtable (java.util.Hashtable)1 Map (java.util.Map)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 SAXParser (javax.xml.parsers.SAXParser)1 XMLGrammarPreparser (org.apache.xerces.parsers.XMLGrammarPreparser)1 XMLGrammarPoolImpl (org.apache.xerces.util.XMLGrammarPoolImpl)1 XNIException (org.apache.xerces.xni.XNIException)1 XMLInputSource (org.apache.xerces.xni.parser.XMLInputSource)1 XMLParseException (org.apache.xerces.xni.parser.XMLParseException)1 InputSource (org.xml.sax.InputSource)1 SAXException (org.xml.sax.SAXException)1 XMLReader (org.xml.sax.XMLReader)1