KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > datatypes > xsd > impl > XMLLiteralType


1 /******************************************************************
2  * File: XMLLiteralType.java
3  * Created by: Dave Reynolds
4  * Created on: 08-Dec-02
5  *
6  * (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: XMLLiteralType.java,v 1.9 2005/02/21 12:02:19 andy_seaborne Exp $
9  *****************************************************************/

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 JavaDoc;
16 import org.xml.sax.SAXParseException JavaDoc;
17 import org.xml.sax.SAXException JavaDoc;
18 import com.hp.hpl.jena.shared.BrokenException;
19 import java.io.*;
20
21 /**
22  * Builtin data type to represent XMLLiteral (i.e. items created
23  * by use of <code>rdf:parsetype='literal'</code>.
24  *
25  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
26  * @version $Revision: 1.9 $ on $Date: 2005/02/21 12:02:19 $
27  */

28 public class XMLLiteralType extends BaseDatatype implements RDFDatatype {
29     /** Singleton instance */
30     public static final RDFDatatype theXMLLiteralType = new XMLLiteralType(RDF.getURI() + "XMLLiteral");
31     
32     /**
33      * Private constructor.
34      */

35     private XMLLiteralType(String JavaDoc uri) {
36         super(uri);
37     }
38     
39     /**
40      * Convert a serialize a value of this datatype out
41      * to lexical form.
42      */

43     public String JavaDoc unparse(Object JavaDoc value) {
44         return value.toString();
45     }
46     
47     /**
48      * Parse a lexical form of this datatype to a value
49      * @throws DatatypeFormatException if the lexical form is not legal
50      */

51     public Object JavaDoc parse(String JavaDoc lexicalForm) throws DatatypeFormatException {
52         if ( !isValid(lexicalForm))
53           throw new DatatypeFormatException("Bad rdf:XMLLiteral");
54         return lexicalForm;
55     }
56     
57     /**
58      * Test whether the given string is a legal lexical form
59      * of this datatype.
60      */

61     public boolean isValid(final String JavaDoc lexicalForm) {
62         /*
63          * To check the lexical form we construct
64          * a dummy RDF/XML document and parse it with
65          * ARP. ARP performs an exclusive canonicalization,
66          * the dummy document has exactly one triple.
67          * If the lexicalForm is valid then the resulting
68          * literal found by ARP is unchanged.
69          * All other scenarios are either impossible
70          * or occur because the lexical form is invalid.
71          */

72         final boolean status[] = new boolean[]{false,false,false};
73         // status[0] true on error or other reason to know that this is not well-formed
74
// status[1] true once first triple found
75
// status[2] the result (good if status[1] and not status[0]).
76

77         ARP arp = new ARP();
78         
79         arp.getHandlers().setErrorHandler(new ErrorHandler JavaDoc(){
80             public void fatalError(SAXParseException JavaDoc e){
81                 status[0] = true;
82             }
83             public void error(SAXParseException JavaDoc e){
84                 status[0] = true;
85             }
86             public void warning(SAXParseException JavaDoc e){
87                 status[0] = true;
88             }
89         });
90         arp.getHandlers().setStatementHandler(new StatementHandler(){
91         public void statement(AResource a, AResource b, ALiteral l){
92             /* this method is invoked exactly once
93              * while parsing the dummy document.
94              * The l argument is in exclusive canonical XML and
95              * corresponds to where the lexical form has been
96              * in the dummy document. The lexical form is valid
97              * iff it is unchanged.
98              */

99             if (status[1] || !l.isWellFormedXML()) {
100                 status[0] = true;
101             }
102             //throw new BrokenException("plain literal in XMLLiteral code.");
103
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           //throw new BrokenException("resource valued RDF/XML in XMLLiteral code.");
109
}
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 JavaDoc s){
125             return false;
126         }
127         
128         
129         return (!status[0])&&status[1]&&status[2];
130     }
131
132 }
133
134 /*
135     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
136     All rights reserved.
137
138     Redistribution and use in source and binary forms, with or without
139     modification, are permitted provided that the following conditions
140     are met:
141
142     1. Redistributions of source code must retain the above copyright
143        notice, this list of conditions and the following disclaimer.
144
145     2. Redistributions in binary form must reproduce the above copyright
146        notice, this list of conditions and the following disclaimer in the
147        documentation and/or other materials provided with the distribution.
148
149     3. The name of the author may not be used to endorse or promote products
150        derived from this software without specific prior written permission.
151
152     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
153     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
154     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
155     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
156     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
157     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
158     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
159     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
160     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
161     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
162 */

163
Popular Tags