1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.ontotext.ordi.wsmo4rdf;
19
20 import java.io.IOException;
21 import java.io.StringWriter;
22 import java.io.Writer;
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.Map;
26 import java.util.Map.Entry;
27
28 import org.openrdf.model.URI;
29 import org.openrdf.model.Value;
30 import org.openrdf.model.impl.StatementImpl;
31 import org.openrdf.rio.RDFHandlerException;
32 import org.openrdf.rio.RDFWriter;
33 import org.openrdf.rio.RDFWriterFactory;
34 import org.openrdf.rio.rdfxml.util.RDFXMLPrettyWriterFactory;
35 import org.wsmo.common.IRI;
36 import org.wsmo.common.Namespace;
37 import org.wsmo.common.TopEntity;
38 import org.wsmo.factory.Factory;
39 import org.wsmo.factory.LogicalExpressionFactory;
40 import org.wsmo.factory.WsmoFactory;
41 import org.wsmo.wsml.Serializer;
42
43 import com.ontotext.ordi.exception.ORDIRuntimeException;
44 import com.ontotext.ordi.wsmo4rdf.impl.WSML2TriplesImpl;
45
46 public class WSMLTripleSerializer implements Serializer {
47
48 private final WsmoFactory wsmoFactory;
49
50 public WSMLTripleSerializer(Map<Object, Object> params) {
51
52 Object factory = params.get(Factory.WSMO_FACTORY);
53 if (factory == null || factory instanceof WsmoFactory == false) {
54 factory = Factory.createWsmoFactory(null);
55 }
56 wsmoFactory = (WsmoFactory) factory;
57 factory = params.get(Factory.LE_FACTORY);
58 if (factory == null
59 || factory instanceof LogicalExpressionFactory == false) {
60 factory = Factory.createLogicalExpressionFactory(null);
61 }
62 }
63
64 public void serialize(TopEntity[] item, Writer target) throws IOException {
65 if (target == null || item == null) {
66 throw new IllegalArgumentException();
67 }
68
69 RDFWriterFactory factory = new RDFXMLPrettyWriterFactory();
70 RDFWriter rdfWriter = factory.getWriter(target);
71 WSMLtoTriples wsmlTripliser = new WSML2TriplesImpl(wsmoFactory,
72 new WSMLTripleDispatcher(rdfWriter));
73
74 Map<String, IRI> namespaces = new HashMap<String, IRI>();
75 namespaces.put("wsmo4rdf_wsml", wsmoFactory.createIRI(Constants.WSML_NS));
76 namespaces.put("wsmo4rdf_owl", wsmoFactory.createIRI(Constants.OWL_NS));
77 namespaces.put("wsmo4rdf_part", wsmoFactory.createIRI(Constants.PART_WHOLE_NS));
78 for (int i = 0; i < item.length; i++) {
79 Iterator<Namespace> iterator = item[i].listNamespaces().iterator();
80 while (iterator.hasNext()) {
81 namespaces.put("", item[i].getDefaultNamespace().getIRI());
82 Namespace namespace = iterator.next();
83 if (namespaces.containsKey(namespace.getPrefix()) == false)
84 namespaces.put(namespace.getPrefix(), namespace.getIRI());
85 }
86 }
87
88 try {
89 rdfWriter.startRDF();
90 Iterator<Entry<String, IRI>> iterator = namespaces.entrySet()
91 .iterator();
92 while (iterator.hasNext()) {
93 Entry<String, IRI> entry = iterator.next();
94 rdfWriter.handleNamespace(entry.getKey(), entry.getValue()
95 .toString());
96 }
97 for (int i = 0; i < item.length; i++) {
98 wsmlTripliser.process(item[i]);
99 }
100 rdfWriter.endRDF();
101 } catch (Exception e) {
102 throw new ORDIRuntimeException("Error while writting RDF!", e);
103 }
104 }
105
106 public void serialize(TopEntity[] item, StringBuffer target) {
107 if (target == null || item == null) {
108 throw new IllegalArgumentException();
109 }
110
111 try {
112 StringWriter writer = new StringWriter();
113 serialize(item, writer);
114 writer.flush();
115 target.append(writer.getBuffer());
116 } catch (IOException e) {
117 throw new RuntimeException("Internal ORDI exception!", e);
118 }
119 }
120
121 @SuppressWarnings("unchecked")
122 public void serialize(TopEntity[] item, StringBuffer target, Map options) {
123 serialize(item, target);
124 }
125
126 @SuppressWarnings("unchecked")
127 public void serialize(TopEntity[] item, Writer target, Map options)
128 throws IOException {
129 serialize(item, target);
130 }
131
132 private class WSMLTripleDispatcher implements WSMLTripleHandler {
133
134 private RDFWriter writer;
135
136 public WSMLTripleDispatcher(RDFWriter writer) {
137 if (writer == null) {
138 throw new IllegalArgumentException();
139 }
140 this.writer = writer;
141 }
142
143 public void handleTriple(URI subject, URI predicate, Value object,
144 URI namedGraph, URI... tripleSets) {
145 try {
146 writer.handleStatement(new StatementImpl(subject, predicate,
147 object));
148 } catch (RDFHandlerException e) {
149 throw new ORDIRuntimeException("Error while writting RDF!", e);
150 }
151 }
152 }
153 }