1 16 19 package org.apache.xml.serializer; 20 21 import java.util.Hashtable ; 22 import java.util.Properties ; 23 24 import javax.xml.transform.OutputKeys ; 25 26 import org.apache.xml.res.XMLErrorResources; 27 import org.apache.xml.res.XMLMessages; 28 import org.xml.sax.ContentHandler ; 29 30 33 public abstract class SerializerFactory 34 { 35 38 private static Hashtable m_formats = new Hashtable (); 39 40 54 public static Serializer getSerializer(Properties format) 55 { 56 Serializer ser; 57 58 try 59 { 60 String method = format.getProperty(OutputKeys.METHOD); 61 62 if (method == null) 63 throw new IllegalArgumentException ( 64 "The output format has a null method name"); 65 66 String className = 67 format.getProperty(OutputPropertiesFactory.S_KEY_CONTENT_HANDLER); 68 69 70 if (null == className) 71 { 72 Properties methodDefaults = 74 OutputPropertiesFactory.getDefaultMethodProperties(method); 75 className = 76 methodDefaults.getProperty(OutputPropertiesFactory.S_KEY_CONTENT_HANDLER); 77 if (null == className) 78 throw new IllegalArgumentException ( 79 "The output format must have a '" 80 + OutputPropertiesFactory.S_KEY_CONTENT_HANDLER + "' property!"); 81 } 82 83 84 85 ClassLoader loader = ObjectFactory.findClassLoader(); 86 87 Class cls = ObjectFactory.findProviderClass(className, loader, true); 88 89 91 Object obj = cls.newInstance(); 92 93 if (obj instanceof SerializationHandler) 94 { 95 ser = (Serializer) cls.newInstance(); 97 ser.setOutputFormat(format); 98 } 99 else 100 { 101 105 if (obj instanceof ContentHandler ) 106 { 107 108 114 className = SerializerConstants.DEFAULT_SAX_SERIALIZER; 115 cls = ObjectFactory.findProviderClass(className, loader, true); 116 SerializationHandler sh = 117 (SerializationHandler) cls.newInstance(); 118 sh.setContentHandler( (ContentHandler ) obj); 119 sh.setOutputFormat(format); 120 121 ser = sh; 122 } 123 else 124 { 125 throw new Exception ( 128 XMLMessages.createXMLMessage( 129 XMLErrorResources.ER_SERIALIZER_NOT_CONTENTHANDLER, 130 new Object [] { className})); 131 } 132 133 } 134 } 135 catch (Exception e) 136 { 137 throw new org.apache.xml.utils.WrappedRuntimeException(e); 138 } 139 140 return ser; 142 } 143 } 144 | Popular Tags |