001: /******************************************************************
002: * File: XMLLiteralType.java
003: * Created by: Dave Reynolds
004: * Created on: 08-Dec-02
005: *
006: * (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
007: * [See end of file]
008: * $Id: XMLLiteralType.java,v 1.12 2008/01/02 12:04:01 andy_seaborne Exp $
009: *****************************************************************/package com.hp.hpl.jena.datatypes.xsd.impl;
010:
011: import com.hp.hpl.jena.datatypes.*;
012: import com.hp.hpl.jena.vocabulary.RDF;
013: import com.hp.hpl.jena.rdf.arp.*;
014: import org.xml.sax.ErrorHandler;
015: import org.xml.sax.SAXParseException;
016: import org.xml.sax.SAXException;
017: import com.hp.hpl.jena.shared.BrokenException;
018: import java.io.*;
019:
020: /**
021: * Builtin data type to represent XMLLiteral (i.e. items created
022: * by use of <code>rdf:parsetype='literal'</code>.
023: *
024: * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
025: * @version $Revision: 1.12 $ on $Date: 2008/01/02 12:04:01 $
026: */
027: public class XMLLiteralType extends BaseDatatype implements RDFDatatype {
028: /** Singleton instance */
029: public static final RDFDatatype theXMLLiteralType = new XMLLiteralType(
030: RDF.getURI() + "XMLLiteral");
031:
032: /**
033: * Private constructor.
034: */
035: private XMLLiteralType(String uri) {
036: super (uri);
037: }
038:
039: /**
040: * Convert a serialize a value of this datatype out
041: * to lexical form.
042: */
043: public String unparse(Object value) {
044: return value.toString();
045: }
046:
047: /**
048: * Parse a lexical form of this datatype to a value
049: * @throws DatatypeFormatException if the lexical form is not legal
050: */
051: public Object parse(String lexicalForm)
052: throws DatatypeFormatException {
053: if (!isValid(lexicalForm))
054: throw new DatatypeFormatException("Bad rdf:XMLLiteral");
055: return lexicalForm;
056: }
057:
058: /**
059: * Test whether the given string is a legal lexical form
060: * of this datatype.
061: */
062: public boolean isValid(final String lexicalForm) {
063: /*
064: * To check the lexical form we construct
065: * a dummy RDF/XML document and parse it with
066: * ARP. ARP performs an exclusive canonicalization,
067: * the dummy document has exactly one triple.
068: * If the lexicalForm is valid then the resulting
069: * literal found by ARP is unchanged.
070: * All other scenarios are either impossible
071: * or occur because the lexical form is invalid.
072: */
073: final boolean status[] = new boolean[] { false, false, false };
074: // status[0] true on error or other reason to know that this is not well-formed
075: // status[1] true once first triple found
076: // status[2] the result (good if status[1] and not status[0]).
077:
078: ARP arp = new ARP();
079:
080: arp.getHandlers().setErrorHandler(new ErrorHandler() {
081: public void fatalError(SAXParseException e) {
082: status[0] = true;
083: }
084:
085: public void error(SAXParseException e) {
086: status[0] = true;
087: }
088:
089: public void warning(SAXParseException e) {
090: status[0] = true;
091: }
092: });
093: arp.getHandlers().setStatementHandler(new StatementHandler() {
094: public void statement(AResource a, AResource b, ALiteral l) {
095: /* this method is invoked exactly once
096: * while parsing the dummy document.
097: * The l argument is in exclusive canonical XML and
098: * corresponds to where the lexical form has been
099: * in the dummy document. The lexical form is valid
100: * iff it is unchanged.
101: */
102: if (status[1] || !l.isWellFormedXML()) {
103: status[0] = true;
104: }
105: //throw new BrokenException("plain literal in XMLLiteral code.");
106: status[1] = true;
107: status[2] = l.toString().equals(lexicalForm);
108: }
109:
110: public void statement(AResource a, AResource b, AResource l) {
111: status[0] = true;
112: //throw new BrokenException("resource valued RDF/XML in XMLLiteral code.");
113: }
114: });
115: try {
116:
117: arp
118: .load(new StringReader(
119: "<rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>\n"
120: + "<rdf:Description><rdf:value rdf:parseType='Literal'>"
121: + lexicalForm + "</rdf:value>\n"
122: + "</rdf:Description></rdf:RDF>"));
123:
124: } catch (IOException ioe) {
125: throw new BrokenException(ioe);
126: } catch (SAXException s) {
127: return false;
128: }
129:
130: return (!status[0]) && status[1] && status[2];
131: }
132:
133: }
134:
135: /*
136: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
137: All rights reserved.
138:
139: Redistribution and use in source and binary forms, with or without
140: modification, are permitted provided that the following conditions
141: are met:
142:
143: 1. Redistributions of source code must retain the above copyright
144: notice, this list of conditions and the following disclaimer.
145:
146: 2. Redistributions in binary form must reproduce the above copyright
147: notice, this list of conditions and the following disclaimer in the
148: documentation and/or other materials provided with the distribution.
149:
150: 3. The name of the author may not be used to endorse or promote products
151: derived from this software without specific prior written permission.
152:
153: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
154: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
155: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
156: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
157: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
158: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
159: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
160: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
161: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
162: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
163: */
|