Search in sources :

Example 1 with DOMReaderSettings

use of com.helger.xml.serialize.read.DOMReaderSettings in project ph-schematron by phax.

the class SchematronResourcePureTest method testFunctXAreDistinctValuesWithXSD.

@Test
public void testFunctXAreDistinctValuesWithXSD() throws Exception {
    final String sTest = "<?xml version='1.0' encoding='iso-8859-1'?>\n" + "<schema xmlns='http://purl.oclc.org/dsdl/schematron'>\n" + "  <ns prefix=\"xs\" uri=\"http://www.w3.org/2001/XMLSchema\"/>\n" + "  <ns prefix='fn' uri='http://www.w3.org/2005/xpath-functions' />\n" + "  <ns prefix='functx' uri='http://www.functx.com' />\n" + "  <pattern name='toto'>\n" + "    <title>A very simple pattern with a title</title>\n" + "    <rule context='chapter'>\n" + "      <assert test='fn:count(fn:distinct-values(para)) = fn:count(para)'>Should have distinct values</assert>\n" + "      </rule>\n" + "  </pattern>\n" + "</schema>";
    final MapBasedXPathFunctionResolver aFunctionResolver = new XQueryAsXPathFunctionConverter().loadXQuery(ClassPathResource.getInputStream("xquery/functx-1.0-nodoc-2007-01.xq"));
    final Schema aSchema = XMLSchemaCache.getInstance().getSchema(new ClassPathResource("issues/20141124/chapter.xsd"));
    final Document aTestDoc = DOMReader.readXMLDOM("<?xml version='1.0'?>" + "<chapter>" + " <title />" + " <para>09</para>" + " <para>9</para>" + "</chapter>", new DOMReaderSettings().setSchema(aSchema));
    final SchematronOutputType aOT = SchematronResourcePure.fromString(sTest, StandardCharsets.UTF_8).setFunctionResolver(aFunctionResolver).applySchematronValidationToSVRL(aTestDoc, null);
    assertNotNull(aOT);
    if (SVRLHelper.getAllFailedAssertions(aOT).size() != 0) {
        System.out.println(SVRLHelper.getAllFailedAssertions(aOT).get(0).getText());
    }
    assertTrue(SVRLHelper.getAllFailedAssertions(aOT).isEmpty());
}
Also used : SchematronOutputType(org.oclc.purl.dsdl.svrl.SchematronOutputType) Schema(javax.xml.validation.Schema) MapBasedXPathFunctionResolver(com.helger.xml.xpath.MapBasedXPathFunctionResolver) XQueryAsXPathFunctionConverter(com.helger.schematron.xpath.XQueryAsXPathFunctionConverter) DOMReaderSettings(com.helger.xml.serialize.read.DOMReaderSettings) Document(org.w3c.dom.Document) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) Test(org.junit.Test)

Example 2 with DOMReaderSettings

use of com.helger.xml.serialize.read.DOMReaderSettings in project ph-schematron by phax.

the class AbstractSchematronResource method internalCreateDOMReaderSettings.

/**
 * @return The {@link DOMReaderSettings} to be used for reading the XML files
 *         to be validated. This includes the {@link EntityResolver} to be
 *         used.
 * @see #getEntityResolver()
 */
@Nonnull
@ReturnsMutableCopy
protected DOMReaderSettings internalCreateDOMReaderSettings() {
    final DOMReaderSettings aDRS = new DOMReaderSettings();
    if (m_aEntityResolver != null)
        aDRS.setEntityResolver(m_aEntityResolver);
    if (false) {
        final boolean m_bLoadExternalSchemas = false;
        aDRS.setFeatureValue(EXMLParserFeature.EXTERNAL_GENERAL_ENTITIES, m_bLoadExternalSchemas);
        aDRS.setFeatureValue(EXMLParserFeature.EXTERNAL_PARAMETER_ENTITIES, m_bLoadExternalSchemas);
        aDRS.setFeatureValue(EXMLParserFeature.LOAD_EXTERNAL_DTD, m_bLoadExternalSchemas);
        aDRS.setFeatureValue(EXMLParserFeature.VALIDATION, true);
        aDRS.setFeatureValue(EXMLParserFeature.NAMESPACES, true);
    }
    return aDRS;
}
Also used : DOMReaderSettings(com.helger.xml.serialize.read.DOMReaderSettings) ReturnsMutableCopy(com.helger.commons.annotation.ReturnsMutableCopy) Nonnull(javax.annotation.Nonnull)

Example 3 with DOMReaderSettings

use of com.helger.xml.serialize.read.DOMReaderSettings in project ph-schematron by phax.

the class SchematronResourceHelper method getNodeOfSource.

/**
 * Convert the passed transform source into a DOM node. Currently on
 * {@link DOMSource} and {@link StreamSource} can be handled.
 *
 * @param aSource
 *        The transform source to use. May not be <code>null</code>.
 * @param aDRS
 *        DOMReader settings to use. May not be <code>null</code>.
 * @return The DOM node and never <code>null</code>.
 * @throws SAXException
 *         In case XML parsing fails
 * @throws IllegalArgumentException
 *         in case an unsupported {@link Source} implementation is provided.
 */
@Nullable
public static Node getNodeOfSource(@Nonnull final Source aSource, @Nonnull final DOMReaderSettings aDRS) throws SAXException {
    ValueEnforcer.notNull(aSource, "Source");
    ValueEnforcer.notNull(aDRS, "DOMReaderSettings");
    if (aSource instanceof DOMSource) {
        // Node is already in DOMSource
        return ((DOMSource) aSource).getNode();
    }
    if (aSource instanceof StreamSource) {
        // In StreamSource it can either be a byte stream or a character stream or
        // a system ID
        final StreamSource aStreamSource = (StreamSource) aSource;
        final InputStream aIS = aStreamSource.getInputStream();
        if (aIS != null) {
            // Byte stream
            final Document aDoc = DOMReader.readXMLDOM(aIS, aDRS != null ? aDRS : new DOMReaderSettings());
            if (aDoc == null)
                throw new IllegalArgumentException("Failed to read source " + aSource + " as XML from InputStream " + aIS);
            return aDoc;
        }
        final Reader aReader = aStreamSource.getReader();
        if (aReader != null) {
            // CHaracter stream
            final Document aDoc = DOMReader.readXMLDOM(aReader, aDRS);
            if (aDoc == null)
                throw new IllegalArgumentException("Failed to read source " + aSource + " as XML from Reader " + aReader);
            return aDoc;
        }
        final String sSystemID = aStreamSource.getSystemId();
        if (StringHelper.hasText(sSystemID)) {
            // System ID
            try {
                final URLResource aURL = new URLResource(sSystemID);
                final Document aDoc = DOMReader.readXMLDOM(aURL, aDRS);
                if (aDoc == null)
                    throw new IllegalArgumentException("Failed to read source " + aSource + " as XML from SystemID '" + sSystemID + "'");
                return aDoc;
            } catch (final MalformedURLException ex) {
                throw new IllegalArgumentException("Failed to read source " + aSource + " as XML from SystemID '" + sSystemID + "': " + ex.getMessage());
            }
        }
        // Neither InputStream nor Reader present
        s_aLogger.error("StreamSource contains neither InputStream nor Reader nor SystemID - cannot handle!");
        return null;
    }
    final String sMsg = "Can only handle DOMSource and StreamSource - having " + aSource + " with system ID '" + aSource.getSystemId() + "'";
    s_aLogger.error(sMsg);
    throw new IllegalArgumentException(sMsg);
}
Also used : URLResource(com.helger.commons.io.resource.URLResource) DOMSource(javax.xml.transform.dom.DOMSource) MalformedURLException(java.net.MalformedURLException) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) Reader(java.io.Reader) DOMReader(com.helger.xml.serialize.read.DOMReader) DOMReaderSettings(com.helger.xml.serialize.read.DOMReaderSettings) Document(org.w3c.dom.Document) Nullable(javax.annotation.Nullable)

Aggregations

DOMReaderSettings (com.helger.xml.serialize.read.DOMReaderSettings)3 Document (org.w3c.dom.Document)2 ReturnsMutableCopy (com.helger.commons.annotation.ReturnsMutableCopy)1 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)1 URLResource (com.helger.commons.io.resource.URLResource)1 XQueryAsXPathFunctionConverter (com.helger.schematron.xpath.XQueryAsXPathFunctionConverter)1 DOMReader (com.helger.xml.serialize.read.DOMReader)1 MapBasedXPathFunctionResolver (com.helger.xml.xpath.MapBasedXPathFunctionResolver)1 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 MalformedURLException (java.net.MalformedURLException)1 Nonnull (javax.annotation.Nonnull)1 Nullable (javax.annotation.Nullable)1 DOMSource (javax.xml.transform.dom.DOMSource)1 StreamSource (javax.xml.transform.stream.StreamSource)1 Schema (javax.xml.validation.Schema)1 Test (org.junit.Test)1 SchematronOutputType (org.oclc.purl.dsdl.svrl.SchematronOutputType)1