1 10 package com.hp.hpl.jena.datatypes.xsd.impl; 11 12 import com.hp.hpl.jena.datatypes.*; 13 import com.hp.hpl.jena.vocabulary.RDF; 14 import com.hp.hpl.jena.rdf.arp.*; 15 import org.xml.sax.ErrorHandler ; 16 import org.xml.sax.SAXParseException ; 17 import org.xml.sax.SAXException ; 18 import com.hp.hpl.jena.shared.BrokenException; 19 import java.io.*; 20 21 28 public class XMLLiteralType extends BaseDatatype implements RDFDatatype { 29 30 public static final RDFDatatype theXMLLiteralType = new XMLLiteralType(RDF.getURI() + "XMLLiteral"); 31 32 35 private XMLLiteralType(String uri) { 36 super(uri); 37 } 38 39 43 public String unparse(Object value) { 44 return value.toString(); 45 } 46 47 51 public Object parse(String lexicalForm) throws DatatypeFormatException { 52 if ( !isValid(lexicalForm)) 53 throw new DatatypeFormatException("Bad rdf:XMLLiteral"); 54 return lexicalForm; 55 } 56 57 61 public boolean isValid(final String lexicalForm) { 62 72 final boolean status[] = new boolean[]{false,false,false}; 73 77 ARP arp = new ARP(); 78 79 arp.getHandlers().setErrorHandler(new ErrorHandler (){ 80 public void fatalError(SAXParseException e){ 81 status[0] = true; 82 } 83 public void error(SAXParseException e){ 84 status[0] = true; 85 } 86 public void warning(SAXParseException e){ 87 status[0] = true; 88 } 89 }); 90 arp.getHandlers().setStatementHandler(new StatementHandler(){ 91 public void statement(AResource a, AResource b, ALiteral l){ 92 99 if (status[1] || !l.isWellFormedXML()) { 100 status[0] = true; 101 } 102 status[1] = true; 104 status[2] = l.toString().equals(lexicalForm); 105 } 106 public void statement(AResource a, AResource b, AResource l){ 107 status[0] = true; 108 } 110 }); 111 try { 112 113 arp.load(new StringReader( 114 "<rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>\n" 115 +"<rdf:Description><rdf:value rdf:parseType='Literal'>" 116 +lexicalForm+"</rdf:value>\n" 117 +"</rdf:Description></rdf:RDF>" 118 )); 119 120 } 121 catch (IOException ioe){ 122 throw new BrokenException(ioe); 123 } 124 catch (SAXException s){ 125 return false; 126 } 127 128 129 return (!status[0])&&status[1]&&status[2]; 130 } 131 132 } 133 134 163 | Popular Tags |