KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.snipsnap.serialization.rdf;
2
3 import java.util.Properties JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
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 JavaDoc;
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     // properties for Namespace and RDF output format:
27
protected String JavaDoc _uriPrefix;
28     protected String JavaDoc _rdfFormat;
29
30     /**
31      * set the namespace for the generated RDF. If not set, a default URI is used (http://snipsnap.org/rdf/default)
32      * @param uri the namespace URI to use (must not end with '#' or '/')
33      */

34     public void setURIPrefix(String JavaDoc uri) {
35         // TODO: use URI checker to test if given URI is valid ...
36
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     /**
44      * get the associated namespace URI
45      * @return the URI used in generated RDF
46      */

47     public String JavaDoc getURIPrefix() {
48         return _uriPrefix;
49     }
50
51     /**
52      * set the RDF output format for the generated RDF ("RDF/XML", "RDF/XML-ABBREV", "N-TRIPLE", "N3").
53      * If not set, the default "RDF/XML-ABBREV" is used
54      * @param format the RDF output format to use
55      */

56     public void setRDFFormat(String JavaDoc format) {
57         // TODO: use of constant values (?) ...
58
_rdfFormat = format;
59     }
60
61     /** Hook for subclasses to use a custom LabelContext */
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 JavaDoc 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     /**
78      * serialize the single given Snip to the given Writer
79      * @param snip the Snip to serialize (perhaps only the "entry point" or "root snip")
80      * @param writer a Writer to write generated RDF to
81      */

82     public final void serialize(Snip snip, Writer JavaDoc writer) {
83         serialize(snip, writer, 0); // just one level
84
}
85
86     /**
87      * recursively serialize the given Snip to the given Writer
88      * @param snip the Snip to serialize (perhaps only the "entry point" or "root snip")
89      * @param writer a Writer to write generated RDF to
90      * @param depth How many levels of snips shall be serialized. Set to -1 if you want to serialize ALL of them.
91      */

92     public final void serialize(Snip startSnip, Writer JavaDoc writer, int depth) {
93         if (startSnip == null || writer == null) {
94             throw new RuntimeException JavaDoc("snip and writer must not be null!");
95         }
96         try {
97             Model model = createModel();
98             recursiveFillModel(startSnip, model, depth);
99             writeModel(model, writer); // flush the model down the drain
100
}
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 JavaDoc writer) throws RDFException;
109
110     protected final static Iterator JavaDoc getCommentsIterator(Snip snip) {
111         List JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc e) {
142             e.printStackTrace();
143         }
144         return snipResource;
145     }
146
147     protected String JavaDoc getSnipResURI(Snip snip) {
148         return _uriPrefix + '#' + snip.getNameEncoded();
149     }
150
151     protected String JavaDoc getAttachmentURL(Attachment att, Snip snip) {
152         String JavaDoc 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