1 package org.snipsnap.serialization.rdf; 2 3 import java.util.List ; 4 import java.util.ArrayList ; 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 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 ("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 getSupportedLabelTypes() { 48 return s_supportedTypes; 49 } 50 51 public int getOutputFormat() { 52 return SerializerFactory.RDF_10; 53 } 54 55 private static List s_supportedTypes; 56 57 static { 58 s_supportedTypes = new ArrayList (); 59 s_supportedTypes.add("SnipLabel"); 60 } 61 } 62 | Popular Tags |