KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > xmloutput > impl > Abbreviated


1 /*
2  * (c) Copyright 2000, 2001, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3  * All rights reserved.
4  * [See end of file]
5  * $Id: Abbreviated.java,v 1.14 2005/02/21 12:22:28 andy_seaborne Exp $
6  */

7
8 package com.hp.hpl.jena.xmloutput.impl;
9
10 import com.hp.hpl.jena.rdf.model.*;
11 import com.hp.hpl.jena.vocabulary.*;
12
13 import java.io.*;
14 //Writer;
15
//import java.io.PrintWriter;
16

17 /** Writes out RDF in the abbreviated syntax, for human consumption
18    not only machine readable.
19  * It is not normal to call the constructor directly, but to use
20  * the method RDFWriterF.getWriter("RDF/XML-ABBREV").
21  * Does not support the <code>NSPREFIXPROPBASE</code> system properties.
22  * Use <code>setNsPrefix</code>.
23  * For best results it is necessary to set the property
24    <code>"prettyTypes"</code>. See setProperty for information.
25    @see com.hp.hpl.jena.rdf.model.RDFWriterF#getWriter
26  * @author jjc
27  * @version Release='$Name: $' Revision='$Revision: 1.14 $' Date='$Date: 2005/02/21 12:22:28 $'
28  */

29 public class Abbreviated extends BaseXMLWriter implements RDFErrorHandler {
30
31     private Resource types[] =
32         new Resource[] {
33             DAML_OIL.Ontology,
34             OWL.Ontology,
35             DAML_OIL.Datatype,
36             //OWL.DataRange, named or orphaned dataranges unusual.
37
RDFS.Datatype,
38             DAML_OIL.Class,
39             RDFS.Class,
40             OWL.Class,
41             DAML_OIL.Property,
42             OWL.ObjectProperty,
43             RDF.Property,
44             DAML_OIL.ObjectProperty,
45             OWL.DatatypeProperty,
46             DAML_OIL.DatatypeProperty,
47             OWL.TransitiveProperty,
48             OWL.SymmetricProperty,
49             OWL.FunctionalProperty,
50             OWL.InverseFunctionalProperty,
51             DAML_OIL.TransitiveProperty,
52             DAML_OIL.UnambiguousProperty,
53             DAML_OIL.UniqueProperty,
54             };
55             
56     boolean sReification;
57     
58     
59     boolean sIdAttr;
60     boolean sDamlCollection;
61     boolean sParseTypeCollectionPropertyElt;
62     boolean sListExpand;
63     boolean sParseTypeLiteralPropertyElt;
64     boolean sParseTypeResourcePropertyElt;
65     boolean sPropertyAttr;
66     
67
68     boolean sResourcePropertyElt;
69
70     void unblockAll() {
71         sDamlCollection = false;
72         sReification = false;
73         sResourcePropertyElt = false;
74         sParseTypeLiteralPropertyElt = false;
75         sParseTypeResourcePropertyElt = false;
76         sParseTypeCollectionPropertyElt = false;
77         sIdAttr = false;
78         sPropertyAttr = false;
79         sListExpand = false;
80     }
81     {
82         unblockAll();
83         blockRule(RDFSyntax.propertyAttr);
84     }
85     void blockRule(Resource r) {
86         if (r.equals(RDFSyntax.sectionReification)) sReification=true;
87        // else if (r.equals(RDFSyntax.resourcePropertyElt)) sResourcePropertyElt=true;
88
else if (r.equals(RDFSyntax.sectionListExpand)) sListExpand=true;
89         else if (r.equals(RDFSyntax.parseTypeLiteralPropertyElt)) sParseTypeLiteralPropertyElt=true;
90         else if (r.equals(RDFSyntax.parseTypeResourcePropertyElt)) sParseTypeResourcePropertyElt=true;
91         else if (r.equals(RDFSyntax.parseTypeCollectionPropertyElt)) sParseTypeCollectionPropertyElt=true;
92         else if (r.equals(RDFSyntax.idAttr)) {
93             sIdAttr=true;
94             sReification = true;
95         }
96         else if (r.equals(RDFSyntax.propertyAttr)) sPropertyAttr=true;
97         else if (r.equals(DAML_OIL.collection)) sDamlCollection=true;
98         else {
99             logger.warn("Cannot block rule <"+r.getURI()+">");
100         }
101     }
102     Resource[] setTypes(Resource[] propValue) {
103         Resource[] rslt = types;
104         types = (Resource[]) propValue;
105         return rslt;
106     }
107
108     synchronized public void write(Model baseModel, Writer out, String JavaDoc base)
109         {
110         if (baseModel.getGraph().getCapabilities().findContractSafe() == false)
111             {
112             logger.warn( "Workaround for bugs 803804 and 858163: using RDF/XML (not RDF/XML-ABBREV) writer for unsafe graph " + baseModel.getGraph().getClass() );
113             baseModel.write( out, "RDF/XML", base );
114             }
115         else
116             super.write( baseModel, out, base );
117         }
118         
119     void writeBody(
120         Model model,
121         PrintWriter pw,
122         String JavaDoc base,
123         boolean useXMLBase) {
124         Unparser unp = new Unparser(this, base, model, pw);
125
126         unp.setTopLevelTypes(types);
127         //unp.useNameSpaceDecl(nameSpacePrefices);
128
if (useXMLBase)
129             unp.setXMLBase(base);
130         unp.write();
131     }
132
133     // Implemenatation of RDFErrorHandler
134
public void error(Exception JavaDoc e) {
135         errorHandler.error(e);
136     }
137
138     public void warning(Exception JavaDoc e) {
139         errorHandler.warning(e);
140     }
141
142     public void fatalError(Exception JavaDoc e) {
143         errorHandler.fatalError(e);
144     }
145
146     static public void main(String JavaDoc args[]) throws Exception JavaDoc {
147         System.out.println("Test code for bug 77");
148         Model m = new com.hp.hpl.jena.mem.ModelMem();
149         m.read(
150             new FileInputStream("modules/rdf/regression/arp/bug51_0.rdf"),
151             "http://example.org/file");
152         RDFWriter pw = m.getWriter("RDF/XML-ABBREV");
153         m.setNsPrefix("eg", "http://example.org/");
154         m.setNsPrefix("eg2", "http://example.org/foo#");
155         pw.write(m, System.out, "http://example.org/file");
156     }
157
158 }
159 /*
160     (c) Copyright 200, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
161     All rights reserved.
162
163     Redistribution and use in source and binary forms, with or without
164     modification, are permitted provided that the following conditions
165     are met:
166
167     1. Redistributions of source code must retain the above copyright
168        notice, this list of conditions and the following disclaimer.
169
170     2. Redistributions in binary form must reproduce the above copyright
171        notice, this list of conditions and the following disclaimer in the
172        documentation and/or other materials provided with the distribution.
173
174     3. The name of the author may not be used to endorse or promote products
175        derived from this software without specific prior written permission.
176
177     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
178     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
179     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
180     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
181     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
182     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
183     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
184     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
185     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
186     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
187 */

188
Popular Tags