Search code examples
javaxmlvalidationxsdxml-signature

Impossible to resolve the name "ds:Signature" in a component element declaration


I'm struggling with the XML validation via XSD. I tried many and many variants , I tried to change the XSD with a more simple one , I tried to specify a ResourceResolver but nothing changed and honestly I don't have any idea.

The error i get

org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 45; src-resolve: impossibile risolvere il nome "ds:Signature" in un componente element declaration. at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)

The XSD i use is this

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
       xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
       elementFormDefault="qualified"
       attributeFormDefault="unqualified">
<xs:import namespace="http://www.w3.org/2000/09/xmldsig#"
           schemaLocation="http://www.w3.org/TR/2002/REC-xmldsig-core-20020212/xmldsig-core-schema.xsd"/>
<xs:complexType name="headerType">
    <xs:sequence>
        <xs:element name="doorNumber" type="xs:int"/>
        <xs:element ref="ds:Signature"/>
    </xs:sequence>
</xs:complexType>

Java Code

    String xml = "SOME XML ";
    Optional<List<SAXParseException>> optionalSAXParseExceptions = Optional.empty();
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    InputStream inputStream = Main.class.getResourceAsStream(xsd);
    StreamSource streamSource = new StreamSource(inputStream);
    Schema schema = factory.newSchema(streamSource);
    Validator validator = schema.newValidator();
    final List<SAXParseException> exceptions = new LinkedList<SAXParseException>();
    validator.setErrorHandler(new ErrorHandler() {

        public void warning(SAXParseException exception) throws SAXParseException {

            exceptions.add(exception);
        }


        public void fatalError(SAXParseException exception) throws SAXParseException {
            exceptions.add(exception);
        }


        public void error(SAXParseException exception) throws SAXParseException {
            exceptions.add(exception);
        }
    });
    validator.validate(new StreamSource(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8))));

Any suggestion will be helpful ! Thanks in advance


Solution

  • Ok , finally after hours and hours of research a colleague found this solution.

    A class which implements LSInput like this

    public class LSInputImpl implements LSInput {
    
    private Reader characterStream;
    private InputStream byteStream;
    private String stringData;
    private String systemId;
    private String publicId;
    private String baseURI;
    private String encoding;
    private boolean certifiedText;
    
    
    public Reader getCharacterStream() {
        return characterStream;
    }
    
    public void setCharacterStream(Reader characterStream) {
        this.characterStream = characterStream;
    }
    
    public InputStream getByteStream() {
        return byteStream;
    }
    
    public void setByteStream(InputStream byteStream) {
        this.byteStream = byteStream;
    }
    
    public String getStringData() {
        return stringData;
    }
    
    public void setStringData(String stringData) {
        this.stringData = stringData;
    }
    
    public String getSystemId() {
        return systemId;
    }
    
    public void setSystemId(String systemId) {
        this.systemId = systemId;
    }
    
    public String getPublicId() {
        return publicId;
    }
    
    public void setPublicId(String publicId) {
        this.publicId = publicId;
    }
    
    public String getBaseURI() {
        return baseURI;
    }
    
    public void setBaseURI(String baseURI) {
        this.baseURI = baseURI;
    }
    
    public String getEncoding() {
        return encoding;
    }
    
    public void setEncoding(String encoding) {
        this.encoding = encoding;
    }
    
    @Override
    public boolean getCertifiedText() {
        return false;
    }
    
    public boolean isCertifiedText() {
        return certifiedText;
    }
    
    public void setCertifiedText(boolean certifiedText) {
        this.certifiedText = certifiedText;
    }
    }
    

    Another Class which implements LSResourceResolver like this

    package test;
    
    import org.w3c.dom.ls.LSInput;
    import org.w3c.dom.ls.LSResourceResolver;
    
    import java.io.InputStream;
    import java.io.InputStreamReader;
    
    public class CustomResolver implements LSResourceResolver {
        @Override
        public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
    
            LSInputImpl input = new LSInputImpl();
    
            InputStream stream = getClass().getClassLoader().getResourceAsStream(systemId);
    
            input.setPublicId(publicId);
            input.setSystemId(systemId);
            input.setBaseURI(baseURI);
            input.setCharacterStream(new InputStreamReader(stream));
    
            return input;
        }
    }
    

    In the class which you use the SchemaFactory add this

    factory.setResourceResolver(new CustomResolver());