Search in sources :

Example 1 with AResource

use of org.apache.jena.rdfxml.xmlinput.AResource in project jena by apache.

the class XMLLiteralType method isValid.

/**
     * Test whether the given string is a legal lexical form
     * of this datatype.
     */
@Override
public boolean isValid(final String lexicalForm) {
    /*
         * To check the lexical form we construct
         * a dummy RDF/XML document and parse it with
         * ARP. ARP performs an exclusive canonicalization,
         * the dummy document has exactly one triple.
         * If the lexicalForm is valid then the resulting
         * literal found by ARP is unchanged.
         * All other scenarios are either impossible
         * or occur because the lexical form is invalid.
         */
    final boolean[] status = new boolean[] { false, false, false };
    // status[0] true on error or other reason to know that this is not well-formed
    // status[1] true once first triple found
    // status[2] the result (good if status[1] and not status[0]).
    ARP arp = new ARP();
    arp.getHandlers().setErrorHandler(new ErrorHandler() {

        @Override
        public void fatalError(SAXParseException e) {
            status[0] = true;
        }

        @Override
        public void error(SAXParseException e) {
            status[0] = true;
        }

        @Override
        public void warning(SAXParseException e) {
            status[0] = true;
        }
    });
    arp.getHandlers().setStatementHandler(new StatementHandler() {

        @Override
        public void statement(AResource a, AResource b, ALiteral l) {
            /* this method is invoked exactly once
        	 * while parsing the dummy document.
        	 * The l argument is in exclusive canonical XML and
        	 * corresponds to where the lexical form has been 
        	 * in the dummy document. The lexical form is valid
        	 * iff it is unchanged.
        	 */
            if (status[1] || !l.isWellFormedXML()) {
                status[0] = true;
            }
            //throw new BrokenException("plain literal in XMLLiteral code.");
            status[1] = true;
            status[2] = l.toString().equals(lexicalForm);
        }

        @Override
        public void statement(AResource a, AResource b, AResource l) {
            status[0] = true;
        //throw new BrokenException("resource valued RDF/XML in XMLLiteral code.");
        }
    });
    try {
        arp.load(new StringReader("<rdf:RDF  xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>\n" + "<rdf:Description><rdf:value rdf:parseType='Literal'>" + lexicalForm + "</rdf:value>\n" + "</rdf:Description></rdf:RDF>"));
    } catch (IOException ioe) {
        throw new BrokenException(ioe);
    } catch (SAXException s) {
        return false;
    }
    return (!status[0]) && status[1] && status[2];
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) SAXParseException(org.xml.sax.SAXParseException) StatementHandler(org.apache.jena.rdfxml.xmlinput.StatementHandler) StringReader(java.io.StringReader) AResource(org.apache.jena.rdfxml.xmlinput.AResource) BrokenException(org.apache.jena.shared.BrokenException) ALiteral(org.apache.jena.rdfxml.xmlinput.ALiteral) IOException(java.io.IOException) ARP(org.apache.jena.rdfxml.xmlinput.ARP) SAXException(org.xml.sax.SAXException)

Aggregations

IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 ALiteral (org.apache.jena.rdfxml.xmlinput.ALiteral)1 ARP (org.apache.jena.rdfxml.xmlinput.ARP)1 AResource (org.apache.jena.rdfxml.xmlinput.AResource)1 StatementHandler (org.apache.jena.rdfxml.xmlinput.StatementHandler)1 BrokenException (org.apache.jena.shared.BrokenException)1 ErrorHandler (org.xml.sax.ErrorHandler)1 SAXException (org.xml.sax.SAXException)1 SAXParseException (org.xml.sax.SAXParseException)1