21 |
|
|
22 |
package nongnu.cashews.xml; |
package nongnu.cashews.xml; |
23 |
|
|
24 |
|
import java.io.ObjectStreamClass; |
25 |
|
import java.io.Serializable; |
26 |
|
|
27 |
import java.lang.reflect.Field; |
import java.lang.reflect.Field; |
28 |
|
import java.lang.reflect.Modifier; |
29 |
|
|
30 |
import java.net.URISyntaxException; |
import java.net.URISyntaxException; |
31 |
|
|
34 |
import java.util.LinkedList; |
import java.util.LinkedList; |
35 |
import java.util.List; |
import java.util.List; |
36 |
|
|
37 |
|
import static javax.xml.XMLConstants.XMLNS_ATTRIBUTE; |
38 |
|
import static javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI; |
39 |
|
import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI; |
40 |
|
|
41 |
|
import javax.xml.namespace.QName; |
42 |
|
|
43 |
|
import nongnu.cashews.language.grounding.MessagePart; |
44 |
|
import nongnu.cashews.language.grounding.SoapMessage; |
45 |
import nongnu.cashews.language.grounding.SoapOperation; |
import nongnu.cashews.language.grounding.SoapOperation; |
46 |
|
|
47 |
import nongnu.cashews.language.process.AtomicProcess; |
import nongnu.cashews.language.process.AtomicProcess; |
48 |
import nongnu.cashews.language.process.CompositeProcess; |
import nongnu.cashews.language.process.CompositeProcess; |
49 |
|
import nongnu.cashews.language.process.Consume; |
50 |
import nongnu.cashews.language.process.Performance; |
import nongnu.cashews.language.process.Performance; |
51 |
|
import nongnu.cashews.language.process.Produce; |
52 |
import nongnu.cashews.language.process.Sequence; |
import nongnu.cashews.language.process.Sequence; |
53 |
|
|
54 |
import nongnu.cashews.xml.schema.TypeMapper; |
import nongnu.cashews.xml.schema.TypeMapper; |
55 |
import nongnu.cashews.xml.schema.XsdType; |
import nongnu.cashews.xml.schema.XsdType; |
56 |
|
|
76 |
private static DOMImplementation domImpl; |
private static DOMImplementation domImpl; |
77 |
|
|
78 |
/** |
/** |
79 |
|
* The document namespaces. |
80 |
|
*/ |
81 |
|
private static final QName[] DOCUMENT_NAMESPACES = new QName[] |
82 |
|
{ |
83 |
|
new QName(W3C_XML_SCHEMA_NS_URI, "", "xsd") |
84 |
|
}; |
85 |
|
|
86 |
|
/** |
87 |
* Initialize the DOM implementation (one time operation only). |
* Initialize the DOM implementation (one time operation only). |
88 |
* |
* |
89 |
* @throws InstantiationException if the implementation class couldn't |
* @throws InstantiationException if the implementation class couldn't |
117 |
* @return the serialized object in XML form. |
* @return the serialized object in XML form. |
118 |
* @throws IllegalAccessException if a field can't be accessed. |
* @throws IllegalAccessException if a field can't be accessed. |
119 |
*/ |
*/ |
120 |
public static Node serialize(Xmlizable object, Node root, |
public static Element serialize(Serializable object, Element root, |
121 |
Document document) |
Document document) |
122 |
throws IllegalAccessException |
throws IllegalAccessException |
123 |
{ |
{ |
124 |
List<Field> fields = new LinkedList<Field>(); |
List<Field> fields = new LinkedList<Field>(); |
125 |
Class clazz = object.getClass(); |
Class clazz = object.getClass(); |
126 |
String elementName = null; |
String elementName = null; |
127 |
CustomXmlizable customObject = null; |
Xmlizable customObject = null; |
128 |
if (object instanceof CustomXmlizable) |
if (object instanceof Xmlizable) |
129 |
{ |
{ |
130 |
customObject = (CustomXmlizable) object; |
customObject = (Xmlizable) object; |
131 |
elementName = customObject.getElementName(); |
elementName = customObject.getElementName(); |
132 |
} |
} |
133 |
if (elementName == null) |
if (elementName == null) |
134 |
elementName = clazz.getSimpleName(); |
elementName = clazz.getSimpleName(); |
135 |
Element objRoot = createElement(document, elementName); |
Element objRoot = createElement(document, elementName); |
136 |
|
if (customObject != null) |
137 |
|
addNamespaceDeclarations(customObject.getDeclaredNamespaces(), objRoot); |
138 |
while (clazz != null) |
while (clazz != null) |
139 |
{ |
{ |
140 |
fields.addAll(0, Arrays.asList(clazz.getDeclaredFields())); |
fields.addAll(0, Arrays.asList(clazz.getDeclaredFields())); |
141 |
clazz = clazz.getSuperclass(); |
clazz = clazz.getSuperclass(); |
142 |
} |
} |
143 |
|
TypeMapper mapper = new TypeMapper(); |
144 |
for (Field field: fields) |
for (Field field: fields) |
145 |
{ |
{ |
146 |
|
if (Modifier.isTransient(field.getModifiers())) |
147 |
|
continue; |
148 |
System.out.println("field: " + field); |
System.out.println("field: " + field); |
149 |
Object value = field.get(object); |
Object value = field.get(object); |
|
System.out.println("value: " + value); |
|
150 |
if (value == null) |
if (value == null) |
151 |
continue; |
continue; |
152 |
if (value instanceof Collection) |
Class valueClazz = value.getClass(); |
153 |
|
System.out.println("value: " + value + ", " + valueClazz); |
154 |
|
XsdType schemaType = mapper.map(valueClazz); |
155 |
|
if (schemaType != null) |
156 |
|
{ |
157 |
|
Element element = createElement(document, field.getName()); |
158 |
|
element.appendChild(schemaType.translateValue(document, value)); |
159 |
|
objRoot.appendChild(element); |
160 |
|
} |
161 |
|
else if (value instanceof Collection) |
162 |
{ |
{ |
163 |
Collection collection = (Collection) value; |
Collection collection = (Collection) value; |
164 |
for (Object obj : collection) |
for (Object obj : collection) |
165 |
{ |
if (obj instanceof Serializable) |
166 |
if (obj instanceof Xmlizable) |
serialize((Serializable) obj, objRoot, document); |
|
serialize((Xmlizable) obj, objRoot, document); |
|
|
} |
|
167 |
} |
} |
168 |
else if (value instanceof Xmlizable) |
else if (value instanceof Serializable) |
169 |
serialize((Xmlizable) value, objRoot, document); |
serialize((Serializable) value, objRoot, document); |
170 |
else |
else |
171 |
{ |
{ |
172 |
Element element = createElement(document, field.getName()); |
Element element = createElement(document, field.getName()); |
173 |
serializeValue(document, element, value); |
element.appendChild(document.createTextNode(value.toString())); |
174 |
objRoot.appendChild(element); |
objRoot.appendChild(element); |
175 |
} |
} |
176 |
} |
} |
195 |
} |
} |
196 |
|
|
197 |
/** |
/** |
|
* Serializes a reflected class field and its contents into an XML element. |
|
|
* An XML schema datatype is searched for first, in order to provide |
|
|
* the most appropriate translation. If this fails, the |
|
|
* <code>toString()</code> content is used. |
|
|
* |
|
|
* @param document the document instance for creating further XML nodes. |
|
|
* @param element the element to serialize to. |
|
|
* @param value the value to serialize. |
|
|
* @return the serialized element. |
|
|
*/ |
|
|
private static void serializeValue(Document document, Element element, |
|
|
Object value) |
|
|
{ |
|
|
TypeMapper mapper = new TypeMapper(); |
|
|
Class clazz = value.getClass(); |
|
|
XsdType schemaType = mapper.map(clazz); |
|
|
if (schemaType == null) |
|
|
element.appendChild(document.createTextNode(value.toString())); |
|
|
else |
|
|
element.appendChild(schemaType.translateValue(document, value)); |
|
|
} |
|
|
|
|
|
/** |
|
198 |
* Creates an element with the appropriate naming schema. |
* Creates an element with the appropriate naming schema. |
199 |
* |
* |
200 |
* @param document the document for creating elements. |
* @param document the document for creating elements. |
211 |
} |
} |
212 |
|
|
213 |
/** |
/** |
214 |
|
* Adds namespace declarations to an element. |
215 |
|
* |
216 |
|
* @param qnames the qualified names for which namespace declarations |
217 |
|
* should be made. |
218 |
|
* @param element the element to which to add declarations. |
219 |
|
*/ |
220 |
|
private static void addNamespaceDeclarations(QName[] qnames, Element element) |
221 |
|
{ |
222 |
|
if (qnames != null) |
223 |
|
for (QName qname : qnames) |
224 |
|
element.setAttributeNS(XMLNS_ATTRIBUTE_NS_URI, |
225 |
|
XMLNS_ATTRIBUTE + ":" + qname.getPrefix(), |
226 |
|
qname.getNamespaceURI()); |
227 |
|
} |
228 |
|
|
229 |
|
/** |
230 |
* A simple test harness to ensure that objects can be successfully |
* A simple test harness to ensure that objects can be successfully |
231 |
* converted to XML. |
* converted to XML. |
232 |
* |
* |
245 |
Sequence sequence = new Sequence(); |
Sequence sequence = new Sequence(); |
246 |
Performance performance = new Performance("MyPerform"); |
Performance performance = new Performance("MyPerform"); |
247 |
AtomicProcess atomic1 = new AtomicProcess("MyAtomic"); |
AtomicProcess atomic1 = new AtomicProcess("MyAtomic"); |
248 |
atomic1.setGrounding(new SoapOperation()); |
SoapOperation operation1 = new |
249 |
|
SoapOperation("http://soapclient.com/xml/soapresponder.wsdl"); |
250 |
|
SoapMessage method1 = new |
251 |
|
SoapMessage("http://soapclient.com/xml/soapresponder.wsdl", "Method1", |
252 |
|
"ns1"); |
253 |
|
MessagePart input1a = new MessagePart("input1"); |
254 |
|
input1a.setName(null, "bstrParam1"); |
255 |
|
input1a.setType(W3C_XML_SCHEMA_NS_URI, "string", "xsd"); |
256 |
|
method1.addPart(input1a); |
257 |
|
MessagePart input1b = new MessagePart("input2"); |
258 |
|
input1b.setName(null, "bstrParam2"); |
259 |
|
input1b.setType(W3C_XML_SCHEMA_NS_URI, "string", "xsd"); |
260 |
|
method1.addPart(input1b); |
261 |
|
SoapMessage method1Response = new |
262 |
|
SoapMessage("http://soapclient.com/xml/soapresponder.wsdl", |
263 |
|
"Method1Response","ns1"); |
264 |
|
MessagePart output1 = new MessagePart("output"); |
265 |
|
output1.setName(null, "bstrReturn"); |
266 |
|
output1.setType(W3C_XML_SCHEMA_NS_URI, "string", "xsd"); |
267 |
|
method1Response.addPart(output1); |
268 |
|
operation1.setInputMessage(method1); |
269 |
|
operation1.setOutputMessage(method1Response); |
270 |
|
atomic1.setGrounding(operation1); |
271 |
performance.setProcess(atomic1); |
performance.setProcess(atomic1); |
272 |
sequence.add(performance); |
sequence.add(performance); |
273 |
process.setControlStructure(sequence); |
process.setControlStructure(sequence); |
274 |
|
Consume consume = new Consume("in1","MyPerform","input1",0); |
275 |
|
process.addConsumer(consume); |
276 |
|
Produce produce = new Produce("out1","MyPerform","output"); |
277 |
|
process.addProducer(produce); |
278 |
initializeImpl(); |
initializeImpl(); |
279 |
Document document = domImpl.createDocument(null,null,null); |
Document document = domImpl.createDocument(null,null,null); |
280 |
document.appendChild(Serializer.serialize(process, null, document)); |
Element root = Serializer.serialize(process, null, document); |
281 |
|
addNamespaceDeclarations(DOCUMENT_NAMESPACES, root); |
282 |
|
document.appendChild(root); |
283 |
System.out.println(convertDocumentToString(document)); |
System.out.println(convertDocumentToString(document)); |
284 |
} |
} |
285 |
|
|