View Javadoc

1   /*
2    wsmo4j - a WSMO API and Reference Implementation
3   
4    Copyright (c) 2006, University of Innsbruck, Austria
5   
6    This library is free software; you can redistribute it and/or modify it under
7    the terms of the GNU Lesser General Public License as published by the Free
8    Software Foundation; either version 2.1 of the License, or (at your option)
9    any later version.
10   This library is distributed in the hope that it will be useful, but WITHOUT
11   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13   details.
14   You should have received a copy of the GNU Lesser General Public License along
15   with this library; if not, write to the Free Software Foundation, Inc.,
16   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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   * XML serializer of WSMO object (check wsml-xml-syntax.xsd)
36   * 
37   * @author not attributable
38   */
39  public class WsmlRdfSerializer implements Serializer {
40  
41  	// property that defines to what RDF syntax should be serialized
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      // property that defines how WSML axioms should be serialized in RDF
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       * Constructor should not be invoked directly.
62       *  Map properties = new TreeMap();
63       *  properties.put(Factory.PROVIDER_CLASS, "com.ontotext.wsmo4j.xmlparser.WsmlXmlSerializer");
64       *  serializer = Factory.createSerializer(properties);
65       * @param map All parameters are ignored
66       */
67      public WsmlRdfSerializer(Map map) {
68          if(map.get(RDF_SERIALIZER_TYPE)!=null) {
69              writerType = ((Integer)map.get(RDF_SERIALIZER_TYPE)).intValue();
70          } else {
71              //default serialization = RDF/XML
72              writerType = RDF_SERIALIZER_XML.intValue();
73          }
74          if (map.get(RDF_AXIOMS) != null) {
75          	String axiom_style = (String) map.get(RDF_AXIOMS);
76          	if (axiom_style.equals(RDF_AXIOMS_TRIPLE))
77          		axiomAsTriple = true;
78          	else if (axiom_style.equals(RDF_AXIOMS_PART))
79          		axiomAsTriple = false;
80          }
81          else {
82          	axiomAsTriple = false;
83          }
84      }
85      
86      public ValueFactory getFactory() { return vf; }
87      public RdfDocumentWriter getWriter() { return rdw;}
88  
89      private RdfDocumentWriter setSerializer(Writer target) {
90          switch(writerType) {
91              case 1:
92                  return new N3Writer(target);
93              case 2:
94                  return new NTriplesWriter(target);
95              case 3:
96                  return new RdfXmlWriter(target);
97              case 4:
98                  return new TurtleWriter(target);
99              default:
100                 return null;
101         }
102     }
103     
104     /**
105      * Serialize array of top entities to XML
106      * @param item Entities to serialize
107      * @param target Output
108      */
109     public void serialize(TopEntity[] item, Writer target) throws IOException {
110         rdw = setSerializer(target);
111         try {
112             //rdf
113             this.getWriter().setNamespace(
114                     "rdf",
115                     RDF.NAMESPACE);
116             //rdfs
117             this.getWriter().setNamespace(
118                     "rdfs",
119                     RDFS.NAMESPACE);
120             //xsd
121             this.getWriter().setNamespace(
122                     "xsd",
123                     XmlSchema.NAMESPACE);
124             //defined namespace bindings
125             for (int i = 0; i < item.length; i++) { 
126                 //default namespace
127                 this.getWriter().setNamespace(
128                         "",
129                         item[i].getDefaultNamespace().getIRI().toString());
130                 if(!item[i].listNamespaces().isEmpty()) {
131                     Iterator iter = item[i].listNamespaces().iterator();
132                     while (iter.hasNext()) {
133                         Namespace ns = (Namespace)iter.next();
134                         this.getWriter().setNamespace(
135                                 ns.getPrefix(),ns.getIRI().toString());
136                         
137                     }
138                 }
139             }
140             rdw.startDocument();
141             
142             NodeWsml.serializeTopEntity(item, this);
143             
144             rdw.endDocument();
145         }
146         catch (IOException e) {
147             throw new RuntimeException(e);
148         }
149     }
150 
151     /**
152      * Serialize array of top entities to XML
153      * @param item Entities to serialize
154      * @param target Output
155      * @param options Ignored
156      */
157     public void serialize(TopEntity[] item, Writer target, Map options) throws IOException {
158         serialize(item, target);
159     }
160 
161     /**
162      * Serialize array of top entities to XML
163      * @param item Entities to serialize
164      * @param target Output
165      */
166     public void serialize(TopEntity[] item, final StringBuffer target) {
167         Writer myWriter = new Writer() {
168 
169             public void write(String arg0) throws IOException {
170                 target.append(arg0);
171             }
172 
173             public void write(String arg0, int arg1, int arg2) throws IOException {
174                 target.append(arg0.toCharArray(), arg1, arg2);
175             }
176 
177             public void write(int arg0) throws IOException {
178                 if (arg0 <= 255) {
179                     target.append((char) arg0);
180                 }
181                 else {
182                     byte[] bytes = new byte[] {(byte) (arg0 & 0x00FF), (byte) (arg0 & 0xFF00)};
183                     target.append(new String(bytes));
184                 }
185             }
186 
187             public void write(char[] arg0) throws IOException {
188                 target.append(arg0);
189             }
190 
191             public void write(char[] arg0, int arg1, int arg2) throws IOException {
192                 target.append(arg0, arg1, arg2);
193             }
194 
195             public void flush() throws IOException {
196                 return;
197             }
198 
199             public void close() throws IOException {
200                 return;
201             }
202         };
203 
204         try {
205             serialize(item, myWriter);
206         }
207         catch (IOException e) {
208             return;
209         }
210     }
211 
212     /**
213      * Serialize array of top entities to XML
214      * @param item Entities to serialize
215      * @param target Output
216      * @param options Ignored
217      */
218     public void serialize(TopEntity[] item, StringBuffer target, Map options) {
219         serialize(item, target);
220     }
221 }
222 
223 /*
224  * $Log$
225  * Revision 1.2  2006/11/10 15:38:49  ohamano
226  * *** empty log message ***
227  *
228  * Revision 1.1  2006/11/10 15:02:59  ohamano
229  * *** empty log message ***
230  *
231  * Revision 1.1  2006/11/10 10:08:42  ohamano
232  * *** empty log message ***
233  *
234  *
235 */