/[cashew-s-editor]/cashews/src/nongnu/cashews/xml/Serializer.java
ViewVC logotype

Diff of /cashews/src/nongnu/cashews/xml/Serializer.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.4 by gnu_andrew, Sun May 8 12:03:41 2005 UTC revision 1.5 by gnu_andrew, Mon May 9 02:36:56 2005 UTC
# Line 22  Line 22 
22  package nongnu.cashews.xml;  package nongnu.cashews.xml;
23    
24  import java.io.ObjectStreamField;  import java.io.ObjectStreamField;
25    import java.io.OutputStream;
26  import java.io.Serializable;  import java.io.Serializable;
27    
28  import java.lang.reflect.Field;  import java.lang.reflect.Field;
# Line 29  import java.lang.reflect.Modifier; Line 30  import java.lang.reflect.Modifier;
30    
31  import java.net.URISyntaxException;  import java.net.URISyntaxException;
32    
 import java.util.Arrays;  
33  import java.util.Collection;  import java.util.Collection;
 import java.util.LinkedList;  
 import java.util.List;  
34    
35  import static javax.xml.XMLConstants.XMLNS_ATTRIBUTE;  import static javax.xml.XMLConstants.XMLNS_ATTRIBUTE;
36  import static javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI;  import static javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
# Line 43  import javax.xml.namespace.QName; Line 41  import javax.xml.namespace.QName;
41  import nongnu.cashews.commons.Pair;  import nongnu.cashews.commons.Pair;
42  import nongnu.cashews.commons.PairList;  import nongnu.cashews.commons.PairList;
43    
44  import nongnu.cashews.language.grounding.MessagePart;  import static nongnu.cashews.services.Processes.TEST_COMPOSITE_SEQUENCE;
 import nongnu.cashews.language.grounding.SoapMessage;  
 import nongnu.cashews.language.grounding.SoapOperation;  
   
 import nongnu.cashews.language.process.AtomicProcess;  
 import nongnu.cashews.language.process.CompositeProcess;  
 import nongnu.cashews.language.process.Consume;  
 import nongnu.cashews.language.process.Performance;  
 import nongnu.cashews.language.process.Produce;  
 import nongnu.cashews.language.process.Sequence;  
45    
46  import nongnu.cashews.xml.schema.TypeMapper;  import nongnu.cashews.xml.schema.TypeMapper;
47  import nongnu.cashews.xml.schema.XsdType;  import nongnu.cashews.xml.schema.XsdType;
# Line 60  import nongnu.cashews.xml.schema.XsdType Line 49  import nongnu.cashews.xml.schema.XsdType
49  import org.w3c.dom.Document;  import org.w3c.dom.Document;
50  import org.w3c.dom.DOMImplementation;  import org.w3c.dom.DOMImplementation;
51  import org.w3c.dom.Element;  import org.w3c.dom.Element;
 import org.w3c.dom.Node;  
52  import org.w3c.dom.bootstrap.DOMImplementationRegistry;  import org.w3c.dom.bootstrap.DOMImplementationRegistry;
53  import org.w3c.dom.ls.DOMImplementationLS;  import org.w3c.dom.ls.DOMImplementationLS;
54    import org.w3c.dom.ls.LSOutput;
55  import org.w3c.dom.ls.LSSerializer;  import org.w3c.dom.ls.LSSerializer;
56    
57  /**  /**
# Line 225  public class Serializer Line 214  public class Serializer
214          Field field = pair.getRight();          Field field = pair.getRight();
215          if (Modifier.isTransient(field.getModifiers()))          if (Modifier.isTransient(field.getModifiers()))
216            continue;            continue;
217          System.out.println("field: " + field);          serializeValue(field.getName(), field.get(object),
218          Object value = field.get(object);                         xField.isFieldNameSerialized(),
219          if (value == null)                         xField.isClassNameSerialized(),
220            continue;                         mapper, document, objRoot);
         Class valueClazz = value.getClass();  
         System.out.println("value: " + value + ", " + valueClazz);  
         XsdType schemaType = mapper.map(valueClazz);  
         if (schemaType != null)  
           {  
             Element element = createElement(document, field.getName());  
             element.appendChild(schemaType.translateValue(document, value));  
             objRoot.appendChild(element);  
           }  
         else if (value instanceof Collection)  
           {  
             Collection collection = (Collection) value;  
             for (Object obj : collection)  
               if (obj instanceof Serializable)  
                 serialize((Serializable) obj, objRoot, document);  
           }  
         else if (value instanceof Serializable)  
           {  
             Element element;  
             if (xField.isFieldNameSerialized())  
               {  
                 element = createElement(document, field.getName());  
                 objRoot.appendChild(element);  
               }  
             else  
               element = objRoot;  
             serialize((Serializable) value, element, document,  
                       xField.isClassNameSerialized());  
           }  
         else  
           {  
             Element element = createElement(document, field.getName());  
             element.appendChild(document.createTextNode(value.toString()));  
             objRoot.appendChild(element);  
           }  
221        }        }
222      if (root != null)      if (root != null)
223        {        {
# Line 277  public class Serializer Line 231  public class Serializer
231     * Converts an XML document to a <code>String</code>.     * Converts an XML document to a <code>String</code>.
232     *     *
233     * @param document the document to convert.     * @param document the document to convert.
234       * @return a <code>String</code> containing the serialized document.
235     */       */  
236    public static String convertDocumentToString(Document document)    public static String convertDocumentToString(Document document)
237    {    {
# Line 318  public class Serializer Line 273  public class Serializer
273    }    }
274    
275    /**    /**
276       * Finalizes an XML document by adding the specified namespace
277       * declarations and root element.
278       *
279       * @param document the document to finalize.
280       * @param root the root element of the document.
281       * @param namespaces the namespaces to declare as part of the document.
282       *                   This may be <code>null</code> if there are no
283       *                   namespaces to declare.
284       * @return the XML document.
285       */
286      public static Document finalizeXmlDocument(Document document, Element root,
287                                                 QName[] namespaces)
288      {
289        addNamespaceDeclarations(namespaces, root);
290        document.appendChild(root);
291        return document;
292      }
293    
294      /**
295       * Retrieves an empty XML document for content generation.
296       *
297       * @return a blank XML document.
298       * @throws InstantiationException if the implementation class couldn't
299       *         be instantiated.
300       * @throws IllegalAccessException if the implementation class can't be
301       *         accessed.
302       * @throws ClassNotFoundException if the implementation class can't be
303       *         found.
304       */
305      public static Document getXmlDocument()
306        throws InstantiationException, IllegalAccessException,
307               ClassNotFoundException
308      {
309        initializeImpl();
310        return domImpl.createDocument(null,null,null);
311      }
312    
313      /**
314       * Serializes an XML document to an <code>OutputStream</code>.
315       *
316       * @param document the document to convert.
317       * @param stream the stream to serialize to.
318       * @return true if serialization was successful.
319       */  
320      public static boolean serializeToStream(Document document,
321                                              OutputStream stream)
322      {
323        DOMImplementationLS loadAndSave = (DOMImplementationLS) domImpl;
324        LSSerializer serializer = loadAndSave.createLSSerializer();
325        LSOutput output = loadAndSave.createLSOutput();
326        output.setByteStream(stream);
327        output.setEncoding("utf-8");
328        return serializer.write(document, output);
329      }
330    
331      /**
332       * Serializes a value to XML.  The default of no field names and
333       * class names is used.
334       *
335       * @param name the name of the field for this value.
336       * @param value the value itself.
337       * @param mapper for converting between types.
338       * @param document the document to create nodes with or add nodes to.
339       * @param objRoot the root to which the serialized document should be
340       *                presented.
341       */
342      public static void serializeValue(String name, Object value,
343                                        TypeMapper mapper, Document document,
344                                        Element objRoot)
345        throws IllegalAccessException
346      {
347        serializeValue(name, value, false, true, mapper, document, objRoot);
348      }
349    
350      /**
351       * Serializes a value to XML.
352       *
353       * @param name the name of the field for this value.
354       * @param value the value itself.
355       * @param includeFieldName <code>true</code> means that an element
356       *                         with the name of the field is serialized.
357       * @param includeTypeName <code>true</code> means that an element
358       *                         with the name of its class is serialized.
359       * @param mapper for converting between types.
360       * @param document the document to create nodes with or add nodes to.
361       * @param objRoot the root to which the serialized document should be
362       *                presented.
363       */
364      public static void serializeValue(String name, Object value,
365                                        boolean includeFieldName,
366                                        boolean includeTypeName,
367                                        TypeMapper mapper, Document document,
368                                        Element objRoot)
369        throws IllegalAccessException
370      {
371        System.out.println("field: " + name);
372        if (value == null)
373          return;
374        Class valueClazz = value.getClass();
375        System.out.println("value: " + value + ", " + valueClazz);
376        XsdType schemaType = mapper.map(valueClazz);
377        if (schemaType != null)
378          {
379            Element element = createElement(document, name);
380            element.appendChild(schemaType.translateValue(document, value));
381            objRoot.appendChild(element);
382          }
383        else if (value instanceof Collection)
384          {
385            Collection collection = (Collection) value;
386            for (Object obj : collection)
387              if (obj instanceof Serializable)
388                serialize((Serializable) obj, objRoot, document);
389          }
390        else if (value instanceof Serializable)
391          {
392            Element element;
393            if (includeFieldName)
394              {
395                element = createElement(document, name);
396                objRoot.appendChild(element);
397              }
398            else
399              element = objRoot;
400            serialize((Serializable) value, element, document, includeTypeName);
401          }
402        else
403          {
404            Element element = createElement(document, name);
405            element.appendChild(document.createTextNode(value.toString()));
406            objRoot.appendChild(element);
407          }
408      }
409    
410      /**
411     * A simple test harness to ensure that objects can be successfully     * A simple test harness to ensure that objects can be successfully
412     * converted to XML.     * converted to XML.
413     *     *
# Line 332  public class Serializer Line 422  public class Serializer
422      throws InstantiationException, IllegalAccessException,      throws InstantiationException, IllegalAccessException,
423             ClassNotFoundException, URISyntaxException             ClassNotFoundException, URISyntaxException
424    {    {
425      CompositeProcess process = new CompositeProcess("MyProcess");      Document document = getXmlDocument();
426      Sequence sequence = new Sequence();      Element root = Serializer.serialize(TEST_COMPOSITE_SEQUENCE, null,
427      Performance performance = new Performance("MyPerform");                                          document);
428      AtomicProcess atomic1 = new AtomicProcess("MyAtomic");      finalizeXmlDocument(document, root, DOCUMENT_NAMESPACES);
     SoapOperation operation1 = new  
       SoapOperation("http://soapclient.com/xml/soapresponder.wsdl");  
     SoapMessage method1 = new  
       SoapMessage("http://soapclient.com/xml/soapresponder.wsdl", "Method1",  
                   "ns1");  
     MessagePart input1a = new MessagePart("input1");  
     input1a.setName(null, "bstrParam1");  
     input1a.setType(W3C_XML_SCHEMA_NS_URI, "string", "xsd");  
     method1.addPart(input1a);  
     MessagePart input1b = new MessagePart("input2");  
     input1b.setName(null, "bstrParam2");  
     input1b.setType(W3C_XML_SCHEMA_NS_URI, "string", "xsd");  
     method1.addPart(input1b);  
     SoapMessage method1Response = new  
       SoapMessage("http://soapclient.com/xml/soapresponder.wsdl",  
                   "Method1Response","ns1");  
     MessagePart output1 = new MessagePart("output");  
     output1.setName(null, "bstrReturn");  
     output1.setType(W3C_XML_SCHEMA_NS_URI, "string", "xsd");  
     method1Response.addPart(output1);  
     operation1.setInputMessage(method1);  
     operation1.setOutputMessage(method1Response);  
     atomic1.setGrounding(operation1);  
     performance.setProcess(atomic1);  
     sequence.add(performance);  
     process.setControlStructure(sequence);  
     Consume consume = new Consume("in1","MyPerform","input1",0);  
     process.addConsumer(consume);  
     Produce produce = new Produce("out1","MyPerform","output");  
     process.addProducer(produce);  
     initializeImpl();  
     Document document = domImpl.createDocument(null,null,null);  
     Element root = Serializer.serialize(process, null, document);  
     addNamespaceDeclarations(DOCUMENT_NAMESPACES, root);  
     document.appendChild(root);  
429      System.out.println(convertDocumentToString(document));      System.out.println(convertDocumentToString(document));
430    }    }
431    

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.5

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26