KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * (c) Copyright 2000, 2001, 2002, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3  * All rights reserved.
4   [See end of file]
5   $Id: Basic.java,v 1.12 2005/03/14 16:01:56 chris-dollin Exp $
6 */

7
8 package com.hp.hpl.jena.xmloutput.impl;
9
10 import com.hp.hpl.jena.rdf.model.impl.Util;
11 import com.hp.hpl.jena.rdf.model.*;
12 import com.hp.hpl.jena.vocabulary.RDF;
13 import com.hp.hpl.jena.vocabulary.RDFSyntax;
14
15 import java.io.PrintWriter JavaDoc;
16
17 /** Writes out an XML serialization of a model.
18  *
19  * @author bwm
20  * @version Release='$Name: $' Revision='$Revision: 1.12 $' Date='$Date: 2005/03/14 16:01:56 $'
21  */

22 public class Basic extends BaseXMLWriter {
23
24     static String JavaDoc RDFNS = RDF.getURI();
25
26     public Basic() {
27     }
28     
29     private String JavaDoc space;
30     
31     void writeBody(
32         Model model,
33         PrintWriter JavaDoc pw,
34         String JavaDoc base,
35         boolean inclXMLBase) {
36         
37         space = "";
38         for (int i=0; i<tab;i++)
39          space += " ";
40         
41         writeRDFHeader(model, pw);
42         writeRDFStatements(model, pw);
43         writeRDFTrailer(pw, base);
44         pw.flush();
45         
46     }
47     
48     protected void writeSpace( PrintWriter JavaDoc writer )
49         { writer.print( space ); }
50
51     private void writeRDFHeader(Model model, PrintWriter JavaDoc writer) {
52         String JavaDoc xmlns = xmlnsDecl();
53         NsIterator nsIter = model.listNameSpaces();
54         String JavaDoc ns;
55
56         writer.print("<" + rdfEl("RDF") + xmlns);
57
58         if (null != xmlBase && xmlBase.length() > 0) {
59
60             writer.print("\n xml:base=" + qq(xmlBase));
61         }
62         writer.println(" >");
63     }
64
65     protected void writeRDFStatements(Model model, PrintWriter JavaDoc writer)
66          {
67         ResIterator rIter = model.listSubjects();
68         while (rIter.hasNext()) {
69             writeRDFStatements(model, rIter.nextResource(), writer);
70         }
71     }
72
73     protected void writeRDFTrailer(PrintWriter JavaDoc writer, String JavaDoc base) {
74         writer.println("</" + rdfEl("RDF") + ">");
75     }
76
77     protected void writeRDFStatements(
78         Model model,
79         Resource subject,
80         PrintWriter JavaDoc writer)
81          {
82         StmtIterator sIter = model.listStatements( subject, null, (RDFNode) null );
83
84         writeDescriptionHeader(subject, writer);
85         
86         while (sIter.hasNext()) {
87             writePredicate(sIter.nextStatement(), writer);
88         }
89         writeDescriptionTrailer( subject, writer);
90
91     }
92
93     protected void writeDescriptionHeader(Resource subject, PrintWriter JavaDoc writer)
94          {
95         writer.print(space + "<" + rdfEl("Description") + " ");
96         writeResourceId(subject, writer);
97         writer.println(">");
98     }
99
100     protected void writePredicate(Statement stmt, final PrintWriter JavaDoc writer)
101          {
102
103         final Property predicate = stmt.getPredicate();
104         final RDFNode object = stmt.getObject();
105
106         writer.print(space+space+
107             "<"
108                 + startElementTag(
109                     predicate.getNameSpace(),
110                     predicate.getLocalName()));
111                            
112         if (object instanceof Resource) {
113             writer.print(" ");
114             writeResourceReference(((Resource) object), writer);
115             writer.println("/>");
116         } else {
117             writeLiteral((Literal) object, writer);
118             writer.println(
119                 "</"
120                     + endElementTag(
121                         predicate.getNameSpace(),
122                         predicate.getLocalName())
123                     + ">");
124         }
125     }
126     void unblockAll() {
127         blockLiterals = false;
128     }
129     private boolean blockLiterals = false;
130     void blockRule(Resource r) {
131         if (r.equals(RDFSyntax.parseTypeLiteralPropertyElt)) {
132      // System.err.println("Blocking");
133
blockLiterals = true;
134         } else
135            logger.warn("Cannot block rule <"+r.getURI()+">");
136     }
137
138     protected void writeDescriptionTrailer( Resource subject, PrintWriter JavaDoc writer) {
139         writer.println(space + "</" + rdfEl("Description") + ">");
140     }
141     
142     /**
143         @deprecated - use writeDescriptionTrailer( Resource subject, PrintWriter writer )
144         @param writer
145     */

146     protected void writeDescriptionTrailer( PrintWriter JavaDoc writer )
147         { writeDescriptionTrailer( null, writer ); }
148     
149     protected void writeResourceId(Resource r, PrintWriter JavaDoc writer)
150          {
151         if (r.isAnon()) {
152             writer.print(rdfAt("nodeID") + "=" + q(anonId(r)));
153         } else {
154             writer.print(
155                 rdfAt("about")
156                     + "="
157                     + qq(relativize(r.getURI())));
158         }
159     }
160
161     protected void writeResourceReference(Resource r, PrintWriter JavaDoc writer)
162          {
163         if (r.isAnon()) {
164             writer.print(rdfAt("nodeID") + "=" + q(anonId(r)));
165         } else {
166             writer.print(
167                 rdfAt("resource")
168                     + "="
169                     + qq(relativize(r.getURI())));
170         }
171     }
172
173     protected void writeLiteral(Literal l, PrintWriter JavaDoc writer) {
174         String JavaDoc lang = l.getLanguage();
175         String JavaDoc form = l.getLexicalForm();
176         if (!lang.equals("")) {
177             writer.print(" xml:lang=" + q(lang));
178         }
179         if (l.getWellFormed() && !blockLiterals) {
180             writer.print(" " + rdfAt("parseType") + "=" + q("Literal")+">");
181             writer.print( form );
182         } else {
183             String JavaDoc dt = l.getDatatypeURI();
184             if (dt != null) writer.print( " " + rdfAt( "datatype" ) + "=" + qq( dt ) );
185             writer.print(">");
186             writer.print(Util.substituteEntitiesInElementContent( form ));
187         }
188     }
189
190 }
191
192 /*
193     (c) Copyright 2000, 2001, 2002, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
194     All rights reserved.
195
196     Redistribution and use in source and binary forms, with or without
197     modification, are permitted provided that the following conditions
198     are met:
199
200     1. Redistributions of source code must retain the above copyright
201        notice, this list of conditions and the following disclaimer.
202
203     2. Redistributions in binary form must reproduce the above copyright
204        notice, this list of conditions and the following disclaimer in the
205        documentation and/or other materials provided with the distribution.
206
207     3. The name of the author may not be used to endorse or promote products
208        derived from this software without specific prior written permission.
209
210     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
211     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
212     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
213     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
214     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
215     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
216     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
217     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
218     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
219     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
220 */
Popular Tags