1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xerces" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, International
  53. * Business Machines, Inc., http://www.apache.org. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package com.sun.org.apache.xml.internal.serialize;
  58. import java.io.OutputStream;
  59. import java.io.Writer;
  60. import java.io.UnsupportedEncodingException;
  61. import java.util.Hashtable;
  62. import java.util.StringTokenizer;
  63. /**
  64. *
  65. *
  66. * @version $Revision: 1.9 $ $Date: 2004/02/17 07:14:49 $
  67. * @author <a href="mailto:Scott_Boag/CAM/Lotus@lotus.com">Scott Boag</a>
  68. * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a>
  69. */
  70. public abstract class SerializerFactory
  71. {
  72. public static final String FactoriesProperty = "com.sun.org.apache.xml.internal.serialize.factories";
  73. private static Hashtable _factories = new Hashtable();
  74. static
  75. {
  76. SerializerFactory factory;
  77. String list;
  78. StringTokenizer token;
  79. String className;
  80. // The default factories are always registered first,
  81. // any factory specified in the properties file and supporting
  82. // the same method will override the default factory.
  83. factory = new SerializerFactoryImpl( Method.XML );
  84. registerSerializerFactory( factory );
  85. factory = new SerializerFactoryImpl( Method.HTML );
  86. registerSerializerFactory( factory );
  87. factory = new SerializerFactoryImpl( Method.XHTML );
  88. registerSerializerFactory( factory );
  89. factory = new SerializerFactoryImpl( Method.TEXT );
  90. registerSerializerFactory( factory );
  91. list = System.getProperty( FactoriesProperty );
  92. if ( list != null ) {
  93. token = new StringTokenizer( list, " ;,:" );
  94. while ( token.hasMoreTokens() ) {
  95. className = token.nextToken();
  96. try {
  97. factory = (SerializerFactory) ObjectFactory.newInstance( className,
  98. SerializerFactory.class.getClassLoader(), true);
  99. if ( _factories.containsKey( factory.getSupportedMethod() ) )
  100. _factories.put( factory.getSupportedMethod(), factory );
  101. } catch ( Exception except ) { }
  102. }
  103. }
  104. }
  105. /**
  106. * Register a serializer factory, keyed by the given
  107. * method string.
  108. */
  109. public static void registerSerializerFactory( SerializerFactory factory )
  110. {
  111. String method;
  112. synchronized ( _factories ) {
  113. method = factory.getSupportedMethod();
  114. _factories.put( method, factory );
  115. }
  116. }
  117. /**
  118. * Register a serializer factory, keyed by the given
  119. * method string.
  120. */
  121. public static SerializerFactory getSerializerFactory( String method )
  122. {
  123. return (SerializerFactory) _factories.get( method );
  124. }
  125. /**
  126. * Returns the method supported by this factory and used to register
  127. * the factory. This call is required so factories can be added from
  128. * a properties file by knowing only the class name. This method is
  129. * protected, it is only required by this class but must be implemented
  130. * in derived classes.
  131. */
  132. protected abstract String getSupportedMethod();
  133. /**
  134. * Create a new serializer based on the {@link OutputFormat}.
  135. * If this method is used to create the serializer, the {@link
  136. * Serializer#setOutputByteStream} or {@link Serializer#setOutputCharStream}
  137. * methods must be called before serializing a document.
  138. */
  139. public abstract Serializer makeSerializer(OutputFormat format);
  140. /**
  141. * Create a new serializer, based on the {@link OutputFormat} and
  142. * using the writer as the output character stream. If this
  143. * method is used, the encoding property will be ignored.
  144. */
  145. public abstract Serializer makeSerializer( Writer writer,
  146. OutputFormat format );
  147. /**
  148. * Create a new serializer, based on the {@link OutputFormat} and
  149. * using the output byte stream and the encoding specified in the
  150. * output format.
  151. *
  152. * @throws UnsupportedEncodingException The specified encoding is
  153. * not supported
  154. */
  155. public abstract Serializer makeSerializer( OutputStream output,
  156. OutputFormat format )
  157. throws UnsupportedEncodingException;
  158. }