use of org.apache.jena.rdfxml.xmlinput.ARP 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];
}
use of org.apache.jena.rdfxml.xmlinput.ARP in project jena by apache.
the class TestErrorMsg method check.
/**
* @param filename
* Read this file
* @param regex
* Error msg must match this.
*
private void check(String filename, String regex)
throws IOException, MalformedPatternException, SAXException {
check(filename, regex, null);
}
*/
private void check(String filename, String regexPresent, String regexAbsent) throws IOException {
final StringBuffer buf = new StringBuffer();
ARP arp = new ARP();
arp.getHandlers().setErrorHandler(new ErrorHandler() {
@Override
public void warning(SAXParseException exception) {
buf.append(exception.getMessage());
buf.append("\n");
}
@Override
public void error(SAXParseException e) {
warning(e);
}
@Override
public void fatalError(SAXParseException e) {
warning(e);
}
});
try (InputStream in = new FileInputStream("testing/arp/error-msgs/" + filename + ".rdf")) {
arp.load(in, "file:///" + filename);
} catch (SAXException e) {
}
String contents = buf.toString();
if (regexPresent != null)
assertTrue("Should find /" + regexPresent + "/", Pattern.compile(regexPresent, Pattern.DOTALL).matcher(contents).find());
if (regexAbsent != null)
assertTrue("Should not find /" + regexAbsent + "/", !Pattern.compile(regexAbsent, Pattern.DOTALL).matcher(contents).find());
contents = null;
}
Aggregations