Search in sources :

Example 1 with SAXValidator

use of org.dom4j.io.SAXValidator in project wechat by dllwh.

the class XmlUtils method validateBySchema.

/**
 * 检验指定的XML是否符合
 *
 * @param doc
 *            被检验的XML文档
 * @param schemePath
 *            schema的路径
 * @return 如果出错,返回错误信息,否则返回null
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws DocumentException
 */
public static String validateBySchema(Document doc, String schemePath) throws ParserConfigurationException, SAXException, DocumentException {
    XMLErrorHandler errorHandler = new XMLErrorHandler();
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(true);
    factory.setNamespaceAware(true);
    SAXParser parser = factory.newSAXParser();
    parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
    parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", "file:" + schemePath);
    SAXValidator validator = new SAXValidator(parser.getXMLReader());
    validator.setErrorHandler(errorHandler);
    if (errorHandler.getErrors().hasContent()) {
        return errorHandler.getErrors().asXML();
    }
    return null;
}
Also used : XMLErrorHandler(org.dom4j.util.XMLErrorHandler) SAXParser(javax.xml.parsers.SAXParser) SAXValidator(org.dom4j.io.SAXValidator) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 2 with SAXValidator

use of org.dom4j.io.SAXValidator in project openolat by klemens.

the class ItemFileResourceValidator method validateDocument.

private boolean validateDocument(Document in) {
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setValidating(true);
        factory.setNamespaceAware(true);
        SimpleErrorHandler errorHandler = new SimpleErrorHandler();
        ItemContentHandler contentHandler = new ItemContentHandler();
        SAXParser parser = factory.newSAXParser();
        XMLReader reader = parser.getXMLReader();
        reader.setEntityResolver(new IMSEntityResolver());
        reader.setErrorHandler(errorHandler);
        reader.setContentHandler(contentHandler);
        SAXValidator validator = new SAXValidator(reader);
        validator.validate(in);
        return errorHandler.isValid() && contentHandler.isItem();
    } catch (ParserConfigurationException e) {
        return false;
    } catch (SAXException e) {
        return false;
    } catch (Exception e) {
        return false;
    }
}
Also used : SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IMSEntityResolver(org.olat.ims.resources.IMSEntityResolver) SAXValidator(org.dom4j.io.SAXValidator) XMLReader(org.xml.sax.XMLReader) FileNotFoundException(java.io.FileNotFoundException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Example 3 with SAXValidator

use of org.dom4j.io.SAXValidator in project OpenOLAT by OpenOLAT.

the class ItemFileResourceValidator method validateDocument.

private boolean validateDocument(Document in) {
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setValidating(true);
        factory.setNamespaceAware(true);
        SimpleErrorHandler errorHandler = new SimpleErrorHandler();
        ItemContentHandler contentHandler = new ItemContentHandler();
        SAXParser parser = factory.newSAXParser();
        XMLReader reader = parser.getXMLReader();
        reader.setEntityResolver(new IMSEntityResolver());
        reader.setErrorHandler(errorHandler);
        reader.setContentHandler(contentHandler);
        SAXValidator validator = new SAXValidator(reader);
        validator.validate(in);
        return errorHandler.isValid() && contentHandler.isItem();
    } catch (ParserConfigurationException e) {
        return false;
    } catch (SAXException e) {
        return false;
    } catch (Exception e) {
        return false;
    }
}
Also used : SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IMSEntityResolver(org.olat.ims.resources.IMSEntityResolver) SAXValidator(org.dom4j.io.SAXValidator) XMLReader(org.xml.sax.XMLReader) FileNotFoundException(java.io.FileNotFoundException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Aggregations

SAXParser (javax.xml.parsers.SAXParser)3 SAXParserFactory (javax.xml.parsers.SAXParserFactory)3 SAXValidator (org.dom4j.io.SAXValidator)3 FileNotFoundException (java.io.FileNotFoundException)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 IMSEntityResolver (org.olat.ims.resources.IMSEntityResolver)2 SAXException (org.xml.sax.SAXException)2 SAXParseException (org.xml.sax.SAXParseException)2 XMLReader (org.xml.sax.XMLReader)2 XMLErrorHandler (org.dom4j.util.XMLErrorHandler)1