1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: SerializerFactory.java,v 1.6 2004/02/23 10:29:37 aruny Exp $
  18. */
  19. package com.sun.org.apache.xml.internal.serializer;
  20. import java.util.Hashtable;
  21. import java.util.Properties;
  22. import javax.xml.transform.OutputKeys;
  23. import com.sun.org.apache.xml.internal.res.XMLErrorResources;
  24. import com.sun.org.apache.xml.internal.res.XMLMessages;
  25. import org.xml.sax.ContentHandler;
  26. /**
  27. * Factory for creating serializers.
  28. */
  29. public abstract class SerializerFactory
  30. {
  31. /**
  32. * Associates output methods to default output formats.
  33. */
  34. private static Hashtable m_formats = new Hashtable();
  35. /**
  36. * Returns a serializer for the specified output method.
  37. * If no implementation exists that supports the specified output method
  38. * an exception of some type will be thrown.
  39. * For a list of the default output methods see {@link Method}.
  40. *
  41. * @param format The output format, minimally the "method" property must be set.
  42. * @return A suitable serializer.
  43. * @throws IllegalArgumentException if method is
  44. * null or an appropriate serializer can't be found
  45. * @throws Exception if the class for the serializer is found but does not
  46. * implement ContentHandler.
  47. * @throws WrappedRuntimeException if an exception is thrown while trying to find serializer
  48. */
  49. public static Serializer getSerializer(Properties format)
  50. {
  51. Serializer ser;
  52. try
  53. {
  54. String method = format.getProperty(OutputKeys.METHOD);
  55. if (method == null)
  56. throw new IllegalArgumentException(
  57. "The output format has a null method name");
  58. String className =
  59. format.getProperty(OutputPropertiesFactory.S_KEY_CONTENT_HANDLER);
  60. if (null == className)
  61. {
  62. // Missing Content Handler property, load default using OutputPropertiesFactory
  63. Properties methodDefaults =
  64. OutputPropertiesFactory.getDefaultMethodProperties(method);
  65. className =
  66. methodDefaults.getProperty(OutputPropertiesFactory.S_KEY_CONTENT_HANDLER);
  67. if (null == className)
  68. throw new IllegalArgumentException(
  69. "The output format must have a '"
  70. + OutputPropertiesFactory.S_KEY_CONTENT_HANDLER + "' property!");
  71. }
  72. ClassLoader loader = ObjectFactory.findClassLoader();
  73. Class cls = ObjectFactory.findProviderClass(className, loader, true);
  74. // _serializers.put(method, cls);
  75. Object obj = cls.newInstance();
  76. if (obj instanceof SerializationHandler)
  77. {
  78. // this is one of the supplied serializers
  79. ser = (Serializer) cls.newInstance();
  80. ser.setOutputFormat(format);
  81. }
  82. else
  83. {
  84. /*
  85. * This must be a user defined Serializer.
  86. * It had better implement ContentHandler.
  87. */
  88. if (obj instanceof ContentHandler)
  89. {
  90. /*
  91. * The user defined serializer defines ContentHandler,
  92. * but we need to wrap it with ToXMLSAXHandler which
  93. * will collect SAX-like events and emit true
  94. * SAX ContentHandler events to the users handler.
  95. */
  96. className = SerializerConstants.DEFAULT_SAX_SERIALIZER;
  97. cls = ObjectFactory.findProviderClass(className, loader, true);
  98. SerializationHandler sh =
  99. (SerializationHandler) cls.newInstance();
  100. sh.setContentHandler( (ContentHandler) obj);
  101. sh.setOutputFormat(format);
  102. ser = sh;
  103. }
  104. else
  105. {
  106. // user defined serializer does not implement
  107. // ContentHandler, ... very bad
  108. throw new Exception(
  109. XMLMessages.createXMLMessage(
  110. XMLErrorResources.ER_SERIALIZER_NOT_CONTENTHANDLER,
  111. new Object[] { className}));
  112. }
  113. }
  114. }
  115. catch (Exception e)
  116. {
  117. throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(e);
  118. }
  119. // If we make it to here ser is not null.
  120. return ser;
  121. }
  122. }