1 package org.snipsnap.serialization.rdf; 2 3 import java.util.Properties ; 4 import java.util.Iterator ; 5 import java.util.List ; 6 7 import com.hp.hpl.mesa.rdf.jena.model.Resource; 8 import com.hp.hpl.mesa.rdf.jena.model.Model; 9 10 import org.snipsnap.snip.Snip; 11 import org.snipsnap.snip.Links; 12 import org.snipsnap.snip.SnipLink; 13 import org.snipsnap.snip.label.Label; 14 import org.snipsnap.snip.attachment.*; 15 import org.snipsnap.serialization.Serializer; 16 import org.snipsnap.serialization.LabelContext; 17 import java.io.Writer ; 18 import com.hp.hpl.mesa.rdf.jena.model.RDFException; 19 20 public abstract class RDFSerializerBase extends Serializer { 21 22 RDFSerializerBase(int outputFormat) { 23 super(outputFormat); 24 } 25 26 protected String _uriPrefix; 28 protected String _rdfFormat; 29 30 34 public void setURIPrefix(String uri) { 35 if (uri.length() > 0 && (uri.endsWith("#") || uri.endsWith("/"))) { 37 _uriPrefix = uri.substring(0, uri.length() - 1); 38 } 39 else 40 _uriPrefix = uri; 41 } 42 43 47 public String getURIPrefix() { 48 return _uriPrefix; 49 } 50 51 56 public void setRDFFormat(String format) { 57 _rdfFormat = format; 59 } 60 61 62 protected LabelContext getLabelContext(Label label, Snip snip, Model model) { 63 m_labelContext.snip = snip; 64 m_labelContext.snipResource = getSnipResource(model, snip); 65 m_labelContext.uriPrefix = _uriPrefix; 66 m_labelContext.model = model; 67 m_labelContext.label = label; 68 return m_labelContext; 69 } 70 71 public void configure(Properties props) { 72 super.configure(props); 73 _uriPrefix = m_props.getProperty("uri.prefix", "http://snipsnap.org/rdf/default"); 74 _rdfFormat = m_props.getProperty("rdf.format", "RDF/XML-ABBREV"); 75 } 76 77 82 public final void serialize(Snip snip, Writer writer) { 83 serialize(snip, writer, 0); } 85 86 92 public final void serialize(Snip startSnip, Writer writer, int depth) { 93 if (startSnip == null || writer == null) { 94 throw new RuntimeException ("snip and writer must not be null!"); 95 } 96 try { 97 Model model = createModel(); 98 recursiveFillModel(startSnip, model, depth); 99 writeModel(model, writer); } 101 catch (RDFException e) { 102 e.printStackTrace(); 103 } 104 } 105 106 protected abstract Model createModel(); 107 protected abstract void recursiveFillModel(Snip snip, Model model, int depth) throws RDFException; 108 protected abstract void writeModel(Model model, Writer writer) throws RDFException; 109 110 protected final static Iterator getCommentsIterator(Snip snip) { 111 List comments = snip.getComments().getComments(); 112 if (comments == null) { 113 return null; 114 } else { 115 return comments.iterator(); 116 } 117 } 118 119 protected final static Iterator getLinksIterator(Snip snip) { 120 Links snipLinks = snip.getSnipLinks(); 121 if (snipLinks == null) { 122 return null; 123 } else { 124 return snipLinks.iterator(); 125 } 126 } 127 128 protected final static Iterator getAttachmentsIterator(Snip snip) { 129 Attachments attachments = snip.getAttachments(); 130 if (attachments == null) { 131 return null; 132 } else { 133 return attachments.iterator(); 134 } 135 } 136 137 protected Resource getSnipResource(Model model, Snip snip) { 138 Resource snipResource = null; 139 try { 140 snipResource = model.getResource(getSnipResURI(snip)); 141 } catch (Exception e) { 142 e.printStackTrace(); 143 } 144 return snipResource; 145 } 146 147 protected String getSnipResURI(Snip snip) { 148 return _uriPrefix + '#' + snip.getNameEncoded(); 149 } 150 151 protected String getAttachmentURL(Attachment att, Snip snip) { 152 String url = _uriPrefix.substring(0, _uriPrefix.lastIndexOf("/rdf")); 153 url = url.concat("/space/" + SnipLink.encode(snip.getName()) + "/" + SnipLink.encode(att.getName())); 154 return url; 155 } 156 157 protected RDFLabelContext m_labelContext = new RDFLabelContext(); 158 } 159 | Popular Tags |