001: /*
002: * (c) Copyright 2000, 2001, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * All rights reserved.
004: * [See end of file]
005: * $Id: Abbreviated.java,v 1.22 2008/01/02 12:09:33 andy_seaborne Exp $
006: */
007:
008: package com.hp.hpl.jena.xmloutput.impl;
009:
010: import java.io.PrintWriter;
011: import java.io.Writer;
012:
013: import com.hp.hpl.jena.rdf.model.Model;
014: import com.hp.hpl.jena.rdf.model.RDFErrorHandler;
015: import com.hp.hpl.jena.rdf.model.Resource;
016: import com.hp.hpl.jena.vocabulary.DAML_OIL;
017: import com.hp.hpl.jena.vocabulary.OWL;
018: import com.hp.hpl.jena.vocabulary.RDF;
019: import com.hp.hpl.jena.vocabulary.RDFS;
020: import com.hp.hpl.jena.vocabulary.RDFSyntax;
021:
022: //Writer;
023: //import java.io.PrintWriter;
024:
025: /** Writes out RDF in the abbreviated syntax, for human consumption
026: not only machine readable.
027: * It is not normal to call the constructor directly, but to use
028: * the method RDFWriterF.getWriter("RDF/XML-ABBREV").
029: * Does not support the <code>NSPREFIXPROPBASE</code> system properties.
030: * Use <code>setNsPrefix</code>.
031: * For best results it is necessary to set the property
032: <code>"prettyTypes"</code>. See setProperty for information.
033: @see com.hp.hpl.jena.rdf.model.RDFWriterF#getWriter(String)
034: * @author jjc
035: * @version Release='$Name: $' Revision='$Revision: 1.22 $' Date='$Date: 2008/01/02 12:09:33 $'
036: */
037: public class Abbreviated extends BaseXMLWriter implements
038: RDFErrorHandler {
039:
040: private Resource types[] = new Resource[] {
041: DAML_OIL.Ontology,
042: OWL.Ontology,
043: DAML_OIL.Datatype,
044: //OWL.DataRange, named or orphaned dataranges unusual.
045: RDFS.Datatype, DAML_OIL.Class, RDFS.Class, OWL.Class,
046: DAML_OIL.Property, OWL.ObjectProperty, RDF.Property,
047: DAML_OIL.ObjectProperty, OWL.DatatypeProperty,
048: DAML_OIL.DatatypeProperty, OWL.TransitiveProperty,
049: OWL.SymmetricProperty, OWL.FunctionalProperty,
050: OWL.InverseFunctionalProperty, DAML_OIL.TransitiveProperty,
051: DAML_OIL.UnambiguousProperty, DAML_OIL.UniqueProperty, };
052:
053: boolean sReification;
054:
055: boolean sIdAttr;
056: boolean sDamlCollection;
057: boolean sParseTypeCollectionPropertyElt;
058: boolean sListExpand;
059: boolean sParseTypeLiteralPropertyElt;
060: boolean sParseTypeResourcePropertyElt;
061: boolean sPropertyAttr;
062:
063: boolean sResourcePropertyElt;
064:
065: protected void unblockAll() {
066: sDamlCollection = false;
067: sReification = false;
068: sResourcePropertyElt = false;
069: sParseTypeLiteralPropertyElt = false;
070: sParseTypeResourcePropertyElt = false;
071: sParseTypeCollectionPropertyElt = false;
072: sIdAttr = false;
073: sPropertyAttr = false;
074: sListExpand = false;
075: }
076:
077: {
078: unblockAll();
079: blockRule(RDFSyntax.propertyAttr);
080: }
081:
082: protected void blockRule(Resource r) {
083: if (r.equals(RDFSyntax.sectionReification))
084: sReification = true;
085: // else if (r.equals(RDFSyntax.resourcePropertyElt)) sResourcePropertyElt=true;
086: else if (r.equals(RDFSyntax.sectionListExpand))
087: sListExpand = true;
088: else if (r.equals(RDFSyntax.parseTypeLiteralPropertyElt))
089: sParseTypeLiteralPropertyElt = true;
090: else if (r.equals(RDFSyntax.parseTypeResourcePropertyElt))
091: sParseTypeResourcePropertyElt = true;
092: else if (r.equals(RDFSyntax.parseTypeCollectionPropertyElt))
093: sParseTypeCollectionPropertyElt = true;
094: else if (r.equals(RDFSyntax.idAttr)) {
095: sIdAttr = true;
096: sReification = true;
097: } else if (r.equals(RDFSyntax.propertyAttr))
098: sPropertyAttr = true;
099: else if (r.equals(DAML_OIL.collection))
100: sDamlCollection = true;
101: else {
102: logger.warn("Cannot block rule <" + r.getURI() + ">");
103: }
104: }
105:
106: Resource[] setTypes(Resource[] propValue) {
107: Resource[] rslt = types;
108: types = propValue;
109: return rslt;
110: }
111:
112: synchronized public void write(Model baseModel, Writer out,
113: String base) {
114: if (baseModel.getGraph().getCapabilities().findContractSafe() == false) {
115: logger
116: .warn("Workaround for bugs 803804 and 858163: using RDF/XML (not RDF/XML-ABBREV) writer for unsafe graph "
117: + baseModel.getGraph().getClass());
118: baseModel.write(out, "RDF/XML", base);
119: } else
120: super .write(baseModel, out, base);
121: }
122:
123: protected void writeBody(Model model, PrintWriter pw, String base,
124: boolean useXMLBase) {
125: Unparser unp = new Unparser(this , base, model, pw);
126:
127: unp.setTopLevelTypes(types);
128: //unp.useNameSpaceDecl(nameSpacePrefices);
129: if (useXMLBase)
130: unp.setXMLBase(base);
131: unp.write();
132: }
133:
134: // Implemenatation of RDFErrorHandler
135: public void error(Exception e) {
136: errorHandler.error(e);
137: }
138:
139: public void warning(Exception e) {
140: errorHandler.warning(e);
141: }
142:
143: public void fatalError(Exception e) {
144: errorHandler.fatalError(e);
145: }
146:
147: }
148: /*
149: (c) Copyright 200, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
150: All rights reserved.
151:
152: Redistribution and use in source and binary forms, with or without
153: modification, are permitted provided that the following conditions
154: are met:
155:
156: 1. Redistributions of source code must retain the above copyright
157: notice, this list of conditions and the following disclaimer.
158:
159: 2. Redistributions in binary form must reproduce the above copyright
160: notice, this list of conditions and the following disclaimer in the
161: documentation and/or other materials provided with the distribution.
162:
163: 3. The name of the author may not be used to endorse or promote products
164: derived from this software without specific prior written permission.
165:
166: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
167: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
168: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
169: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
170: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
171: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
172: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
173: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
174: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
175: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
176: */
|