001: /*
002: * (c) Copyright 2000, 2001, 2002, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * All rights reserved.
004: [See end of file]
005: $Id: Basic.java,v 1.19 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:
012: import com.hp.hpl.jena.rdf.model.Literal;
013: import com.hp.hpl.jena.rdf.model.Model;
014: import com.hp.hpl.jena.rdf.model.Property;
015: import com.hp.hpl.jena.rdf.model.RDFNode;
016: import com.hp.hpl.jena.rdf.model.ResIterator;
017: import com.hp.hpl.jena.rdf.model.Resource;
018: import com.hp.hpl.jena.rdf.model.Statement;
019: import com.hp.hpl.jena.rdf.model.StmtIterator;
020: import com.hp.hpl.jena.rdf.model.impl.Util;
021: import com.hp.hpl.jena.vocabulary.RDFSyntax;
022:
023: /** Writes out an XML serialization of a model.
024: *
025: * @author bwm
026: * @version Release='$Name: $' Revision='$Revision: 1.19 $' Date='$Date: 2008/01/02 12:09:33 $'
027: */
028: public class Basic extends BaseXMLWriter {
029: public Basic() {
030: }
031:
032: private String space;
033:
034: protected void writeBody(Model model, PrintWriter pw, String base,
035: boolean inclXMLBase) {
036: setSpaceFromTabCount();
037: writeRDFHeader(model, pw);
038: writeRDFStatements(model, pw);
039: writeRDFTrailer(pw, base);
040: pw.flush();
041: }
042:
043: private void setSpaceFromTabCount() {
044: space = "";
045: for (int i = 0; i < tabSize; i += 1)
046: space += " ";
047: }
048:
049: protected void writeSpace(PrintWriter writer) {
050: writer.print(space);
051: }
052:
053: private void writeRDFHeader(Model model, PrintWriter writer) {
054: String xmlns = xmlnsDecl();
055: writer.print("<" + rdfEl("RDF") + xmlns);
056: if (null != xmlBase && xmlBase.length() > 0)
057: writer.print("\n xml:base="
058: + substitutedAttribute(xmlBase));
059: writer.println(" > ");
060: }
061:
062: protected void writeRDFStatements(Model model, PrintWriter writer) {
063: ResIterator rIter = model.listSubjects();
064: while (rIter.hasNext())
065: writeRDFStatements(model, rIter.nextResource(), writer);
066: }
067:
068: protected void writeRDFTrailer(PrintWriter writer, String base) {
069: writer.println("</" + rdfEl("RDF") + ">");
070: }
071:
072: protected void writeRDFStatements(Model model, Resource subject,
073: PrintWriter writer) {
074: StmtIterator sIter = model.listStatements(subject, null,
075: (RDFNode) null);
076: writeDescriptionHeader(subject, writer);
077: while (sIter.hasNext())
078: writePredicate(sIter.nextStatement(), writer);
079: writeDescriptionTrailer(subject, writer);
080: }
081:
082: protected void writeDescriptionHeader(Resource subject,
083: PrintWriter writer) {
084: writer.print(space + "<" + rdfEl("Description") + " ");
085: writeResourceId(subject, writer);
086: writer.println(">");
087: }
088:
089: protected void writePredicate(Statement stmt,
090: final PrintWriter writer) {
091: final Property predicate = stmt.getPredicate();
092: final RDFNode object = stmt.getObject();
093:
094: writer.print(space
095: + space
096: + "<"
097: + startElementTag(predicate.getNameSpace(), predicate
098: .getLocalName()));
099:
100: if (object instanceof Resource) {
101: writer.print(" ");
102: writeResourceReference(((Resource) object), writer);
103: writer.println("/>");
104: } else {
105: writeLiteral((Literal) object, writer);
106: writer.println("</"
107: + endElementTag(predicate.getNameSpace(), predicate
108: .getLocalName()) + ">");
109: }
110: }
111:
112: protected void unblockAll() {
113: blockLiterals = false;
114: }
115:
116: private boolean blockLiterals = false;
117:
118: protected void blockRule(Resource r) {
119: if (r.equals(RDFSyntax.parseTypeLiteralPropertyElt)) {
120: // System.err.println("Blocking");
121: blockLiterals = true;
122: } else
123: logger.warn("Cannot block rule <" + r.getURI() + ">");
124: }
125:
126: protected void writeDescriptionTrailer(Resource subject,
127: PrintWriter writer) {
128: writer.println(space + "</" + rdfEl("Description") + ">");
129: }
130:
131: /**
132: @deprecated - use writeDescriptionTrailer( Resource subject, PrintWriter writer )
133: @param writer
134: */
135: protected void writeDescriptionTrailer(PrintWriter writer) {
136: writeDescriptionTrailer(null, writer);
137: }
138:
139: protected void writeResourceId(Resource r, PrintWriter writer) {
140: if (r.isAnon()) {
141: writer.print(rdfAt("nodeID") + "="
142: + attributeQuoted(anonId(r)));
143: } else {
144: writer.print(rdfAt("about") + "="
145: + substitutedAttribute(relativize(r.getURI())));
146: }
147: }
148:
149: protected void writeResourceReference(Resource r, PrintWriter writer) {
150: if (r.isAnon()) {
151: writer.print(rdfAt("nodeID") + "="
152: + attributeQuoted(anonId(r)));
153: } else {
154: writer.print(rdfAt("resource") + "="
155: + substitutedAttribute(relativize(r.getURI())));
156: }
157: }
158:
159: protected void writeLiteral(Literal l, PrintWriter writer) {
160: String lang = l.getLanguage();
161: String form = l.getLexicalForm();
162: if (!lang.equals("")) {
163: writer.print(" xml:lang=" + attributeQuoted(lang));
164: }
165: if (l.isWellFormedXML() && !blockLiterals) {
166: writer.print(" " + rdfAt("parseType") + "="
167: + attributeQuoted("Literal") + ">");
168: writer.print(form);
169: } else {
170: String dt = l.getDatatypeURI();
171: if (dt != null)
172: writer.print(" " + rdfAt("datatype") + "="
173: + substitutedAttribute(dt));
174: writer.print(">");
175: writer.print(Util.substituteEntitiesInElementContent(form));
176: }
177: }
178:
179: }
180:
181: /*
182: (c) Copyright 2000, 2001, 2002, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
183: All rights reserved.
184:
185: Redistribution and use in source and binary forms, with or without
186: modification, are permitted provided that the following conditions
187: are met:
188:
189: 1. Redistributions of source code must retain the above copyright
190: notice, this list of conditions and the following disclaimer.
191:
192: 2. Redistributions in binary form must reproduce the above copyright
193: notice, this list of conditions and the following disclaimer in the
194: documentation and/or other materials provided with the distribution.
195:
196: 3. The name of the author may not be used to endorse or promote products
197: derived from this software without specific prior written permission.
198:
199: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
200: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
201: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
202: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
203: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
204: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
205: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
206: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
207: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
208: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
209: */
|