KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > util > xml > idefix > DefaultProcessor


1 package org.sapia.util.xml.idefix;
2
3
4 //import org.apache.log4j.Logger;
5
import org.sapia.util.xml.ProcessingException;
6
7 import java.io.BufferedOutputStream JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.io.OutputStream JavaDoc;
10 import java.io.OutputStreamWriter JavaDoc;
11
12
13 /**
14  *
15  *
16  * @author <a HREF="mailto:jc@sapia-oss.org">Jean-Cedric Desrochers</a>
17  * <dl>
18  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
19  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
20  * <a HREF="http://www.sapia-oss.org/license.html" target="sapia-license">license page</a> at the Sapia OSS web site</dd></dt>
21  * </dl>
22  */

23 public class DefaultProcessor implements IdefixProcessorIF {
24   /** Define the logger instance of this class. */
25
26   /* private static final Logger _theLogger =
27             Logger.getLogger(DefaultProcessor.class);*/

28
29   /** The serializer factory used by this processor. */
30   private SerializerFactoryIF _theSerializerFactory;
31
32   /** The namespace factory used by this processor. */
33   private NamespaceFactoryIF _theNamespaceFactory;
34
35   /**
36    * Creates a new DefaultProcessor instance with the argument passed in.
37    *
38    * @param aSerializerFactory The serializer factory of this new processor.
39    */

40   public DefaultProcessor(SerializerFactoryIF aSerializerFactory,
41     NamespaceFactoryIF aNamespaceFactory) {
42     _theSerializerFactory = aSerializerFactory;
43     _theNamespaceFactory = aNamespaceFactory;
44   }
45
46   /**
47    * This method takes an object and walk though its hierarchy to generate an XML
48    * representation of it. The result XML will be returned as a <CODE>String</CODE>
49    * object.
50    *
51    * @param anObject The object to process.
52    * @return The string of the XML representation.
53    * @exception ProcessingException If an error occurs while processing the object.
54    */

55   public String JavaDoc process(Object JavaDoc anObject) throws ProcessingException {
56     if (anObject == null) {
57       throw new IllegalArgumentException JavaDoc("The object passed in is null");
58     }
59
60     try {
61       XmlBuffer anXmlBuffer = new XmlBuffer("UTF-8");
62       SerializationContext aContext = new SerializationContext(_theSerializerFactory,
63           _theNamespaceFactory, anXmlBuffer);
64
65       SerializerIF aSerializer = _theSerializerFactory.getSerializer(anObject.getClass());
66       aSerializer.serialize(anObject, aContext);
67
68       return aContext.getXmlBuffer().toString();
69     } catch (SerializerNotFoundException sne) {
70       String JavaDoc aMessage = "Could not process the object: " + anObject;
71
72       // _theLogger.error(aMessage, sne);
73
throw new ProcessingException(aMessage, sne);
74     } catch (SerializationException se) {
75       String JavaDoc aMessage = "Could not serialize the object: " + anObject;
76
77       // _theLogger.error(aMessage, se);
78
throw new ProcessingException(aMessage, se);
79     } catch (RuntimeException JavaDoc re) {
80       String JavaDoc aMessage = "System error processing the object: " + anObject;
81
82       // _theLogger.error(aMessage, re);
83
throw new ProcessingException(aMessage, re);
84     }
85   }
86
87   /**
88    * This method takes an object and walk though its hierarchy to generate an XML
89    * representation of it. The result XML will be streamed into the output stream
90    * passed in.
91    *
92    * @param anObject The object to process.
93    * @param anOutput The output stream in which to add the XML.
94    * @exception IOException If an error occurs writing to the output stream.
95    * @exception ProcessingException If an error occurs while processing the object.
96    */

97   public void process(Object JavaDoc anObject, OutputStream JavaDoc anOutput)
98     throws IOException JavaDoc, ProcessingException {
99     if (anObject == null) {
100       throw new IllegalArgumentException JavaDoc("The object passed in is null");
101     }
102
103     try {
104       XmlBuffer anXmlBuffer = new XmlBuffer("UTF-8");
105       SerializationContext aContext = new SerializationContext(_theSerializerFactory,
106           _theNamespaceFactory, anXmlBuffer);
107
108       SerializerIF aSerializer = _theSerializerFactory.getSerializer(anObject.getClass());
109       aSerializer.serialize(anObject, aContext);
110
111       OutputStreamWriter JavaDoc aWriter = new OutputStreamWriter JavaDoc(new BufferedOutputStream JavaDoc(
112             anOutput), "UTF-8");
113       aWriter.write(aContext.getXmlBuffer().toString());
114       aWriter.flush();
115     } catch (SerializerNotFoundException sne) {
116       String JavaDoc aMessage = "Could not process the object: " + anObject;
117
118       // _theLogger.error(aMessage, sne);
119
throw new ProcessingException(aMessage, sne);
120     } catch (SerializationException se) {
121       String JavaDoc aMessage = "Could not serialize the object: " + anObject;
122
123       // _theLogger.error(aMessage, se);
124
throw new ProcessingException(aMessage, se);
125     } catch (IOException JavaDoc ioe) {
126       String JavaDoc aMessage = "Could not process the object: " + anObject;
127
128       //_theLogger.error(aMessage, ioe);
129
throw new ProcessingException(aMessage, ioe);
130     } catch (RuntimeException JavaDoc re) {
131       String JavaDoc aMessage = "System error processing the object: " + anObject;
132
133       //_theLogger.error(aMessage, re);
134
throw new ProcessingException(aMessage, re);
135     }
136   }
137 }
138
Popular Tags