KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > serialization > rdf > RDFSerializer


1 package org.snipsnap.serialization.rdf;
2
3 import java.io.Writer JavaDoc;
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   /** Hook for subclasses */
27   protected Model createModel() {
28     return new ModelMem();
29   }
30
31   /**
32    * Subclasses should only need to reimplement this method to add one single snip to the model,
33    * and eventually createModel().
34    */

35   protected void addSingleSnipToModel(Snip snip, Model model) throws RDFException {
36     Resource rootSnipResource = addSnipResource(model, snip);
37     // add the Snip's comments to the model:
38
rootSnipResource.addProperty(SNIP.comments, addCommentsBag(model, snip));
39     // add the Snip's SnipLinks to the model:
40
rootSnipResource.addProperty(SNIP.snipLinks, addSnipLinksBag(model, snip));
41     // add the Snip's Attachments to the model:
42
rootSnipResource.addProperty(SNIP.attachments, addAttachmentsBag(model, snip));
43     // add the Snip's Labels to the model:
44
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 JavaDoc 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   /** May be reimplemented, but typically not necessary */
65   protected void writeModel(Model model, Writer JavaDoc writer) throws RDFException {
66     // get an RDFWriter for the used RDF format (e.g. "RDF/XML-ABBREV"):
67
RDFWriter rdfWriter = model.getWriter(_rdfFormat);
68     // set xml:base attribute to the local URI used:
69
rdfWriter.setProperty("xmlBase", _uriPrefix);
70     // set xmlns:s namespace attribute for the Snip Schema:
71
rdfWriter.setNsPrefix("s", SNIP.getURI());
72     // finally, write out the model:
73
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     // depth = -1 -> unlimited recursion. 0 = don't process children
79
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       // TODO: test if bag is empty ...
94
Iterator iterator = getCommentsIterator(snip);
95       if (iterator != null) {
96         while (iterator.hasNext()) {
97           Snip comment = (Snip) iterator.next();
98           // add comment to model as new resource:
99
Resource commentResource = addCommentResource(model, comment, snip);
100           // add comment to the comments Bag:
101
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       // TODO: test if list of links is empty?
115
Iterator iterator = getLinksIterator(snip);
116       if (iterator != null) {
117         while (iterator.hasNext()) {
118           String JavaDoc snipLink = (String JavaDoc) iterator.next();
119           // add snipLink to the comments Bag:
120
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       // TODO: test if list of attachments is empty?
134
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           // add attachment to the attachments Bag:
140
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