1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| package org.deri.wsmo4j.io.serializer.rdf; |
19 |
| |
20 |
| import java.io.*; |
21 |
| import java.util.*; |
22 |
| |
23 |
| import org.openrdf.model.*; |
24 |
| import org.openrdf.model.impl.*; |
25 |
| import org.openrdf.rio.*; |
26 |
| import org.openrdf.rio.n3.*; |
27 |
| import org.openrdf.rio.ntriples.*; |
28 |
| import org.openrdf.rio.rdfxml.*; |
29 |
| import org.openrdf.rio.turtle.*; |
30 |
| import org.openrdf.vocabulary.*; |
31 |
| import org.wsmo.common.*; |
32 |
| import org.wsmo.wsml.*; |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| public class WsmlRdfSerializer implements Serializer { |
40 |
| |
41 |
| |
42 |
| public final static String RDF_SERIALIZER_TYPE = "rdf_serializer"; |
43 |
| public final static Integer RDF_SERIALIZER_N3 = new Integer(1); |
44 |
| public final static Integer RDF_SERIALIZER_NTRIPLES = new Integer(2); |
45 |
| public final static Integer RDF_SERIALIZER_XML = new Integer(3); |
46 |
| public final static Integer RDF_SERIALIZER_TURTLE = new Integer(4); |
47 |
| |
48 |
| |
49 |
| public final static String RDF_AXIOMS = "rdf_axioms"; |
50 |
| public final static String RDF_AXIOMS_TRIPLE = "rdf_axioms_triple"; |
51 |
| public final static String RDF_AXIOMS_PART = "rdf_axioms_part"; |
52 |
| |
53 |
| public final static String DC_RELATION = "http://purl.org/dc/elements/1.1#relation"; |
54 |
| |
55 |
| private ValueFactory vf = new ValueFactoryImpl(); |
56 |
| private RdfDocumentWriter rdw = null; |
57 |
| private int writerType = 0; |
58 |
| protected boolean axiomAsTriple = false; |
59 |
| |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
0
| public WsmlRdfSerializer(Map map) {
|
68 |
0
| if(map.get(RDF_SERIALIZER_TYPE)!=null) {
|
69 |
0
| writerType = ((Integer)map.get(RDF_SERIALIZER_TYPE)).intValue();
|
70 |
| } else { |
71 |
| |
72 |
0
| writerType = RDF_SERIALIZER_XML.intValue();
|
73 |
| } |
74 |
0
| if (map.get(RDF_AXIOMS) != null) {
|
75 |
0
| String axiom_style = (String) map.get(RDF_AXIOMS);
|
76 |
0
| if (axiom_style.equals(RDF_AXIOMS_TRIPLE))
|
77 |
0
| axiomAsTriple = true;
|
78 |
0
| else if (axiom_style.equals(RDF_AXIOMS_PART))
|
79 |
0
| axiomAsTriple = false;
|
80 |
| } |
81 |
| else { |
82 |
0
| axiomAsTriple = false;
|
83 |
| } |
84 |
| } |
85 |
| |
86 |
0
| public ValueFactory getFactory() { return vf; }
|
87 |
0
| public RdfDocumentWriter getWriter() { return rdw;}
|
88 |
| |
89 |
0
| private RdfDocumentWriter setSerializer(Writer target) {
|
90 |
0
| switch(writerType) {
|
91 |
0
| case 1:
|
92 |
0
| return new N3Writer(target);
|
93 |
0
| case 2:
|
94 |
0
| return new NTriplesWriter(target);
|
95 |
0
| case 3:
|
96 |
0
| return new RdfXmlWriter(target);
|
97 |
0
| case 4:
|
98 |
0
| return new TurtleWriter(target);
|
99 |
0
| default:
|
100 |
0
| return null;
|
101 |
| } |
102 |
| } |
103 |
| |
104 |
| |
105 |
| |
106 |
| |
107 |
| |
108 |
| |
109 |
0
| public void serialize(TopEntity[] item, Writer target) throws IOException {
|
110 |
0
| rdw = setSerializer(target);
|
111 |
0
| try {
|
112 |
| |
113 |
0
| this.getWriter().setNamespace(
|
114 |
| "rdf", |
115 |
| RDF.NAMESPACE); |
116 |
| |
117 |
0
| this.getWriter().setNamespace(
|
118 |
| "rdfs", |
119 |
| RDFS.NAMESPACE); |
120 |
| |
121 |
0
| this.getWriter().setNamespace(
|
122 |
| "xsd", |
123 |
| XmlSchema.NAMESPACE); |
124 |
| |
125 |
0
| for (int i = 0; i < item.length; i++) {
|
126 |
| |
127 |
0
| this.getWriter().setNamespace(
|
128 |
| "", |
129 |
| item[i].getDefaultNamespace().getIRI().toString()); |
130 |
0
| if(!item[i].listNamespaces().isEmpty()) {
|
131 |
0
| Iterator iter = item[i].listNamespaces().iterator();
|
132 |
0
| while (iter.hasNext()) {
|
133 |
0
| Namespace ns = (Namespace)iter.next();
|
134 |
0
| this.getWriter().setNamespace(
|
135 |
| ns.getPrefix(),ns.getIRI().toString()); |
136 |
| |
137 |
| } |
138 |
| } |
139 |
| } |
140 |
0
| rdw.startDocument();
|
141 |
| |
142 |
0
| NodeWsml.serializeTopEntity(item, this);
|
143 |
| |
144 |
0
| rdw.endDocument();
|
145 |
| } |
146 |
| catch (IOException e) { |
147 |
0
| throw new RuntimeException(e);
|
148 |
| } |
149 |
| } |
150 |
| |
151 |
| |
152 |
| |
153 |
| |
154 |
| |
155 |
| |
156 |
| |
157 |
0
| public void serialize(TopEntity[] item, Writer target, Map options) throws IOException {
|
158 |
0
| serialize(item, target);
|
159 |
| } |
160 |
| |
161 |
| |
162 |
| |
163 |
| |
164 |
| |
165 |
| |
166 |
0
| public void serialize(TopEntity[] item, final StringBuffer target) {
|
167 |
0
| Writer myWriter = new Writer() {
|
168 |
| |
169 |
0
| public void write(String arg0) throws IOException {
|
170 |
0
| target.append(arg0);
|
171 |
| } |
172 |
| |
173 |
0
| public void write(String arg0, int arg1, int arg2) throws IOException {
|
174 |
0
| target.append(arg0.toCharArray(), arg1, arg2);
|
175 |
| } |
176 |
| |
177 |
0
| public void write(int arg0) throws IOException {
|
178 |
0
| if (arg0 <= 255) {
|
179 |
0
| target.append((char) arg0);
|
180 |
| } |
181 |
| else { |
182 |
0
| byte[] bytes = new byte[] {(byte) (arg0 & 0x00FF), (byte) (arg0 & 0xFF00)};
|
183 |
0
| target.append(new String(bytes));
|
184 |
| } |
185 |
| } |
186 |
| |
187 |
0
| public void write(char[] arg0) throws IOException {
|
188 |
0
| target.append(arg0);
|
189 |
| } |
190 |
| |
191 |
0
| public void write(char[] arg0, int arg1, int arg2) throws IOException {
|
192 |
0
| target.append(arg0, arg1, arg2);
|
193 |
| } |
194 |
| |
195 |
0
| public void flush() throws IOException {
|
196 |
0
| return;
|
197 |
| } |
198 |
| |
199 |
0
| public void close() throws IOException {
|
200 |
0
| return;
|
201 |
| } |
202 |
| }; |
203 |
| |
204 |
0
| try {
|
205 |
0
| serialize(item, myWriter);
|
206 |
| } |
207 |
| catch (IOException e) { |
208 |
0
| return;
|
209 |
| } |
210 |
| } |
211 |
| |
212 |
| |
213 |
| |
214 |
| |
215 |
| |
216 |
| |
217 |
| |
218 |
0
| public void serialize(TopEntity[] item, StringBuffer target, Map options) {
|
219 |
0
| serialize(item, target);
|
220 |
| } |
221 |
| } |
222 |
| |
223 |
| |
224 |
| |
225 |
| |
226 |
| |
227 |
| |
228 |
| |
229 |
| |
230 |
| |
231 |
| |
232 |
| |
233 |
| |
234 |
| |
235 |
| |