1 package org.snipsnap.serialization.rdf; 2 3 import java.io.Writer ; 4 import java.util.*; 5 6 import com.hp.hpl.mesa.rdf.jena.mem.ModelMem; 7 import com.hp.hpl.mesa.rdf.jena.model.*; 8 import com.hp.hpl.mesa.rdf.jena.vocabulary.*; 9 import com.hp.hpl.mesa.rdf.jena.common.*; 10 11 import org.snipsnap.snip.Links; 12 import org.snipsnap.snip.Snip; 13 import org.snipsnap.snip.SnipLink; 14 import org.snipsnap.snip.attachment.*; 15 import org.snipsnap.snip.label.*; 16 import org.snipsnap.serialization.LabelSerializer; 17 import org.snipsnap.serialization.LabelSerializerFactory; 18 import org.snipsnap.serialization.rdf.vocabulary.*; 19 20 public class RDFSerializer extends RDFSerializerBase { 21 22 public RDFSerializer(int outputFormat) { 23 super(outputFormat); 24 } 25 26 27 protected Model createModel() { 28 return new ModelMem(); 29 } 30 31 35 protected void addSingleSnipToModel(Snip snip, Model model) throws RDFException { 36 Resource rootSnipResource = addSnipResource(model, snip); 37 rootSnipResource.addProperty(SNIP.comments, addCommentsBag(model, snip)); 39 rootSnipResource.addProperty(SNIP.snipLinks, addSnipLinksBag(model, snip)); 41 rootSnipResource.addProperty(SNIP.attachments, addAttachmentsBag(model, snip)); 43 addLabelsToModel(snip, model); 45 } 46 47 protected final void addLabelsToModel(Snip snip, Model model) { 48 LabelSerializerFactory factory = getLabelSerializerFactory(); 49 Labels labels = snip.getLabels(); 50 Iterator it = labels.getAll().iterator(); 51 Map serializers = new HashMap(); 52 while (it.hasNext()) { 53 Label label = (Label) it.next(); 54 String labelType = label.getType(); 55 LabelSerializer ls = (LabelSerializer) serializers.get(labelType); 56 if (ls == null) { 57 ls = factory.createSerializerFor(labelType); 58 serializers.put(labelType, ls); 59 } 60 ls.serialize(getLabelContext(label, snip, model)); 61 } 62 } 63 64 65 protected void writeModel(Model model, Writer writer) throws RDFException { 66 RDFWriter rdfWriter = model.getWriter(_rdfFormat); 68 rdfWriter.setProperty("xmlBase", _uriPrefix); 70 rdfWriter.setNsPrefix("s", SNIP.getURI()); 72 rdfWriter.write(model, writer, _uriPrefix); 74 } 75 76 protected final void recursiveFillModel(Snip snip, Model model, int depth) throws RDFException { 77 addSingleSnipToModel(snip, model); 78 if (depth-- == 0) 80 return; 81 Iterator iterator = getLinksIterator(snip); 82 if (iterator != null) { 83 while (iterator.hasNext()) { 84 recursiveFillModel((Snip) iterator.next(), model, depth); 85 } 86 } 87 } 88 89 private Bag addCommentsBag(Model model, Snip snip) { 90 Bag commentsBag = null; 91 try { 92 commentsBag = model.createBag(); 93 Iterator iterator = getCommentsIterator(snip); 95 if (iterator != null) { 96 while (iterator.hasNext()) { 97 Snip comment = (Snip) iterator.next(); 98 Resource commentResource = addCommentResource(model, comment, snip); 100 commentsBag.add(commentResource); 102 } 103 } 104 } catch (RDFException e) { 105 e.printStackTrace(); 106 } 107 return commentsBag; 108 } 109 110 private Bag addSnipLinksBag(Model model, Snip snip) { 111 Bag snipLinksBag = null; 112 try { 113 snipLinksBag = model.createBag(); 114 Iterator iterator = getLinksIterator(snip); 116 if (iterator != null) { 117 while (iterator.hasNext()) { 118 String snipLink = (String ) iterator.next(); 119 snipLinksBag.add(new ResourceImpl(_uriPrefix + '#' + snipLink)); 121 } 122 } 123 } catch (RDFException e) { 124 e.printStackTrace(); 125 } 126 return snipLinksBag; 127 } 128 129 private Bag addAttachmentsBag(Model model, Snip snip) { 130 Bag snipAttachmentsBag = null; 131 try { 132 snipAttachmentsBag = model.createBag(); 133 Iterator iterator = getAttachmentsIterator(snip); 135 if (iterator != null) { 136 while (iterator.hasNext()) { 137 Attachment attachment = (Attachment) iterator.next(); 138 Resource attachmentResource = addAttachmentResource(model, attachment, snip); 139 snipAttachmentsBag.add(attachmentResource); 141 } 142 } 143 } catch (RDFException e) { 144 e.printStackTrace(); 145 } 146 return snipAttachmentsBag; 147 } 148 149 private Resource addSnipResource(Model model, Snip snip) { 150 Resource snipResource = null; 151 try { 152 snipResource = model.createResource(getSnipResURI(snip)); 153 snipResource.addProperty(RDF.type, SNIP.Snip); 154 snipResource.addProperty(SNIP.name, snip.getName()); 155 snipResource.addProperty(SNIP.content, snip.getContent()); 156 snipResource.addProperty(SNIP.cUser, snip.getCUser()); 157 snipResource.addProperty(SNIP.oUser, snip.getOUser()); 158 snipResource.addProperty(SNIP.mUser, snip.getMUser()); 159 snipResource.addProperty(SNIP.mTime, snip.getMTime()); 160 snipResource.addProperty(SNIP.cTime, snip.getCTime()); 161 } catch (RDFException e) { 162 e.printStackTrace(); 163 } 164 return snipResource; 165 } 166 167 private Resource addCommentResource(Model model, Snip comment, Snip commentedSnip) { 168 Resource commentResource = null; 169 try { 170 commentResource = model.createResource(getSnipResURI(comment)); 171 commentResource.addProperty(RDF.type, SNIP.Comment); 172 commentResource.addProperty(SNIP.name, comment.getName()); 173 commentResource.addProperty(SNIP.content, comment.getContent()); 174 commentResource.addProperty(SNIP.cUser, comment.getCUser()); 175 commentResource.addProperty(SNIP.oUser, comment.getOUser()); 176 commentResource.addProperty(SNIP.mUser, comment.getMUser()); 177 commentResource.addProperty(SNIP.mTime, comment.getMTime()); 178 commentResource.addProperty(SNIP.cTime, comment.getCTime()); 179 commentResource.addProperty(SNIP.commentedSnip, getSnipResource(model, commentedSnip)); 180 } catch (RDFException e) { 181 e.printStackTrace(); 182 } 183 return commentResource; 184 } 185 186 private Resource addAttachmentResource(Model model, Attachment att, Snip snip) { 187 Resource attachmentResource = null; 188 try { 189 attachmentResource = model.createResource(getAttachmentURL(att, snip)); 190 attachmentResource.addProperty(RDF.type, SNIP.Attachment); 191 attachmentResource.addProperty(SNIP.fileName, att.getName()); 192 attachmentResource.addProperty(SNIP.contentType, att.getContentType()); 193 attachmentResource.addProperty(SNIP.size, att.getSize()); 194 attachmentResource.addProperty(SNIP.date, att.getDate()); 195 } catch (RDFException e) { 196 e.printStackTrace(); 197 } 198 return attachmentResource; 199 } 200 201 } 202 | Popular Tags |