1 package org.sapia.util.xml.idefix; 2 3 4 import org.sapia.util.xml.ProcessingException; 6 7 import java.io.BufferedOutputStream ; 8 import java.io.IOException ; 9 import java.io.OutputStream ; 10 import java.io.OutputStreamWriter ; 11 12 13 23 public class DefaultProcessor implements IdefixProcessorIF { 24 25 26 28 29 30 private SerializerFactoryIF _theSerializerFactory; 31 32 33 private NamespaceFactoryIF _theNamespaceFactory; 34 35 40 public DefaultProcessor(SerializerFactoryIF aSerializerFactory, 41 NamespaceFactoryIF aNamespaceFactory) { 42 _theSerializerFactory = aSerializerFactory; 43 _theNamespaceFactory = aNamespaceFactory; 44 } 45 46 55 public String process(Object anObject) throws ProcessingException { 56 if (anObject == null) { 57 throw new IllegalArgumentException ("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 aMessage = "Could not process the object: " + anObject; 71 72 throw new ProcessingException(aMessage, sne); 74 } catch (SerializationException se) { 75 String aMessage = "Could not serialize the object: " + anObject; 76 77 throw new ProcessingException(aMessage, se); 79 } catch (RuntimeException re) { 80 String aMessage = "System error processing the object: " + anObject; 81 82 throw new ProcessingException(aMessage, re); 84 } 85 } 86 87 97 public void process(Object anObject, OutputStream anOutput) 98 throws IOException , ProcessingException { 99 if (anObject == null) { 100 throw new IllegalArgumentException ("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 aWriter = new OutputStreamWriter (new BufferedOutputStream ( 112 anOutput), "UTF-8"); 113 aWriter.write(aContext.getXmlBuffer().toString()); 114 aWriter.flush(); 115 } catch (SerializerNotFoundException sne) { 116 String aMessage = "Could not process the object: " + anObject; 117 118 throw new ProcessingException(aMessage, sne); 120 } catch (SerializationException se) { 121 String aMessage = "Could not serialize the object: " + anObject; 122 123 throw new ProcessingException(aMessage, se); 125 } catch (IOException ioe) { 126 String aMessage = "Could not process the object: " + anObject; 127 128 throw new ProcessingException(aMessage, ioe); 130 } catch (RuntimeException re) { 131 String aMessage = "System error processing the object: " + anObject; 132 133 throw new ProcessingException(aMessage, re); 135 } 136 } 137 } 138 | Popular Tags |