View Javadoc

1   /*
2    wsmo4j - a WSMO API and Reference Implementation
3   
4    Copyright (c) 2004-2005, OntoText Lab. / SIRMA
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 com.ontotext.wsmo4j.serializer.xml;
19  
20  /**
21   * <p>Title: WSMO4J</p>
22   * <p>Description: WSMO API and a Reference Implementation</p>
23   * <p>Copyright:  Copyright (c) 2004-2005</p>
24   * <p>Company: OntoText Lab. / SIRMA </p>
25   * @author not attributable
26   * @version 1.0
27   *
28   */
29  
30  import java.io.*;
31  import java.util.*;
32  
33  import javax.xml.parsers.*;
34  import javax.xml.transform.*;
35  import javax.xml.transform.dom.*;
36  import javax.xml.transform.stream.*;
37  
38  import org.w3c.dom.*;
39  import org.wsmo.common.*;
40  import org.wsmo.wsml.*;
41  
42  import com.ontotext.wsmo4j.parser.xml.*;
43  
44  /**
45   * XML serializer of WSMO object (check wsml-xml-syntax.xsd)
46   * 
47   * @author not attributable
48   */
49  public class WsmlXmlSerializer implements Serializer {
50  
51      private Document document;
52  
53      /**
54       * Constructor should not be invoked directly.
55       *  Map properties = new TreeMap();
56       *  properties.put(Factory.PROVIDER_CLASS, "com.ontotext.wsmo4j.xmlparser.WsmlXmlSerializer");
57       *  serializer = Factory.createSerializer(properties);
58       * @param map All parameters are ignored
59       */
60      public WsmlXmlSerializer(Map <String, Object> map) {
61      }
62  
63      public Element createElement(String name) {
64          return document.createElement(name);
65      }
66  
67      public Text createTextNode(String value) {
68          return document.createTextNode(value);
69      }
70  
71      /**
72       * Serialize array of top entities to XML
73       * @param item Entities to serialize
74       * @param target Output
75       */
76      public void serialize(TopEntity[] item, Writer target) throws IOException {
77          try {
78              document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
79              document.appendChild(NodeWsml.serializeTopEntity(item, this));
80              Transformer xformer = TransformerFactory.newInstance().newTransformer();
81              xformer.setOutputProperty(OutputKeys.ENCODING, "UTF8");
82              xformer.setOutputProperty(OutputKeys.INDENT, "yes");
83              xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
84              xformer.transform(new DOMSource(document), new StreamResult(target));
85              target.flush();
86          }
87          catch (ParserConfigurationException e) {
88              throw new RuntimeException("Cannot create instance of xml parser!", e);
89          }
90          catch (TransformerException e) {
91              throw new RuntimeException(e);
92          }
93      }
94  
95      /**
96       * Serialize array of top entities to XML
97       * @param item Entities to serialize
98       * @param target Output
99       * @param options Ignored
100      */
101     public void serialize(TopEntity[] item, Writer target, Map options) throws IOException {
102         serialize(item, target);
103     }
104 
105     /**
106      * Serialize array of top entities to XML
107      * @param item Entities to serialize
108      * @param target Output
109      */
110     public void serialize(TopEntity[] item, final StringBuffer target) {
111         Writer myWriter = new Writer() {
112 
113             public void write(String arg0) throws IOException {
114                 target.append(arg0);
115             }
116 
117             public void write(String arg0, int arg1, int arg2) throws IOException {
118                 target.append(arg0.toCharArray(), arg1, arg2);
119             }
120 
121             public void write(int arg0) throws IOException {
122                 if (arg0 <= 255) {
123                     target.append((char) arg0);
124                 }
125                 else {
126                     byte[] bytes = new byte[] {(byte) (arg0 & 0x00FF), (byte) (arg0 & 0xFF00)};
127                     target.append(new String(bytes));
128                 }
129             }
130 
131             public void write(char[] arg0) throws IOException {
132                 target.append(arg0);
133             }
134 
135             public void write(char[] arg0, int arg1, int arg2) throws IOException {
136                 target.append(arg0, arg1, arg2);
137             }
138 
139             public void flush() throws IOException {
140                 return;
141             }
142 
143             public void close() throws IOException {
144                 return;
145             }
146         };
147 
148         try {
149             serialize(item, myWriter);
150         }
151         catch (IOException e) {
152             return;
153         }
154     }
155 
156     /**
157      * Serialize array of top entities to XML
158      * @param item Entities to serialize
159      * @param target Output
160      * @param options Ignored
161      */
162     public void serialize(TopEntity[] item, StringBuffer target, Map options) {
163         serialize(item, target);
164     }
165 }
166 
167 /*
168  * $Log$
169  * Revision 1.4  2007/04/02 12:13:28  morcen
170  * Generics support added to wsmo-api, wsmo4j and wsmo-test
171  *
172  * Revision 1.3  2005/11/28 14:58:20  vassil_momtchev
173  * imports organized; createElement(String) and createTextNode(String) visibility changed to public
174  *
175  * Revision 1.2  2005/11/28 14:39:44  vassil_momtchev
176  * moved from com.ontotext.wsmo4j.xmlparser
177  *
178  * Revision 1.3  2005/08/08 08:13:10  vassil_momtchev
179  * javadoc added, implemented all serialize methods, optimized the xml transformation, well formatted output xml
180  *
181 */