1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.ontotext.wsmo4j.serializer.xml;
19
20
21
22
23
24
25
26
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
46
47
48
49 public class WsmlXmlSerializer implements Serializer {
50
51 private Document document;
52
53
54
55
56
57
58
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
73
74
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
97
98
99
100
101 public void serialize(TopEntity[] item, Writer target, Map options) throws IOException {
102 serialize(item, target);
103 }
104
105
106
107
108
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
158
159
160
161
162 public void serialize(TopEntity[] item, StringBuffer target, Map options) {
163 serialize(item, target);
164 }
165 }
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181