KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.snipsnap.serialization.rdf;
2
3 import java.util.List JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import com.hp.hpl.mesa.rdf.jena.model.*;
6 import com.hp.hpl.mesa.rdf.jena.vocabulary.RDF;
7 import com.hp.hpl.mesa.rdf.jena.common.ResourceImpl;
8
9 import org.snipsnap.snip.Snip;
10 import org.snipsnap.snip.label.Label;
11 import org.snipsnap.serialization.LabelContext;
12 import org.snipsnap.serialization.LabelSerializer;
13 import org.snipsnap.serialization.SerializerFactory;
14 import org.snipsnap.serialization.rdf.vocabulary.LABEL;
15 import org.snipsnap.serialization.rdf.vocabulary.SNIP;
16
17 /**
18  * Implements the serialization of SnipLabels to RDF.
19  * @author gis
20  */

21 public class RDFSnipLabelSerializer implements LabelSerializer {
22
23     private Bag labelsBag = null;
24
25     public void serialize(LabelContext labelContext) {
26         if (!(labelContext instanceof RDFLabelContext))
27             throw new RuntimeException JavaDoc("RDFSnipLabelSerializer expects an RDFLabelContext!");
28         RDFLabelContext rdfLabelContext = (RDFLabelContext) labelContext;
29         Model model = rdfLabelContext.model;
30         Label label = rdfLabelContext.label;
31         Snip snip = rdfLabelContext.snip;
32         Resource snipResource = rdfLabelContext.snipResource;
33         try {
34             if (labelsBag == null) {
35                 labelsBag = model.createBag();
36                 snipResource.addProperty(LABEL.snipLabels, labelsBag);
37             }
38             Resource labelResource = model.createResource(label.getName());
39             labelResource.addProperty(RDF.type, LABEL.SnipLabel);
40             labelResource.addProperty(SNIP.value, new ResourceImpl(rdfLabelContext.uriPrefix + "#" + label.getValue()));
41             labelsBag.add(labelResource);
42         } catch (RDFException re) {
43             re.printStackTrace();
44         }
45     }
46
47     public List JavaDoc getSupportedLabelTypes() {
48         return s_supportedTypes;
49     }
50
51     public int getOutputFormat() {
52         return SerializerFactory.RDF_10;
53     }
54
55     private static List JavaDoc s_supportedTypes;
56
57     static {
58         s_supportedTypes = new ArrayList JavaDoc();
59         s_supportedTypes.add("SnipLabel");
60     }
61 }
62
Popular Tags