1 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 ; 16 17 22 public class Basic extends BaseXMLWriter { 23 24 static String RDFNS = RDF.getURI(); 25 26 public Basic() { 27 } 28 29 private String space; 30 31 void writeBody( 32 Model model, 33 PrintWriter pw, 34 String 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 writer ) 49 { writer.print( space ); } 50 51 private void writeRDFHeader(Model model, PrintWriter writer) { 52 String xmlns = xmlnsDecl(); 53 NsIterator nsIter = model.listNameSpaces(); 54 String 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 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 writer, String base) { 74 writer.println("</" + rdfEl("RDF") + ">"); 75 } 76 77 protected void writeRDFStatements( 78 Model model, 79 Resource subject, 80 PrintWriter 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 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 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 blockLiterals = true; 134 } else 135 logger.warn("Cannot block rule <"+r.getURI()+">"); 136 } 137 138 protected void writeDescriptionTrailer( Resource subject, PrintWriter writer) { 139 writer.println(space + "</" + rdfEl("Description") + ">"); 140 } 141 142 146 protected void writeDescriptionTrailer( PrintWriter writer ) 147 { writeDescriptionTrailer( null, writer ); } 148 149 protected void writeResourceId(Resource r, PrintWriter 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 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 writer) { 174 String lang = l.getLanguage(); 175 String 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 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 | Popular Tags |