1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  */
   4 /*
   5  * Licensed to the Apache Software Foundation (ASF) under one or more
   6  * contributor license agreements.  See the NOTICE file distributed with
   7  * this work for additional information regarding copyright ownership.
   8  * The ASF licenses this file to You under the Apache License, Version 2.0
   9  * (the "License"); you may not use this file except in compliance with
  10  * the License.  You may obtain a copy of the License at
  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 
  21 
  22 package com.sun.org.apache.xml.internal.serialize;
  23 
  24 import com.sun.org.apache.xerces.internal.utils.ObjectFactory;
  25 import com.sun.org.apache.xerces.internal.utils.SecuritySupport;
  26 import java.io.OutputStream;
  27 import java.io.UnsupportedEncodingException;
  28 import java.io.Writer;
  29 import java.util.Collections;
  30 import java.util.HashMap;
  31 import java.util.Map;
  32 import java.util.StringTokenizer;
  33 
  34 /**
  35  *
  36  *
  37  * @author <a href="mailto:Scott_Boag/CAM/Lotus@lotus.com">Scott Boag</a>
  38  * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a>
  39  *
  40  * @deprecated As of JDK 9, Xerces 2.9.0, Xerces DOM L3 Serializer implementation
  41  * is replaced by that of Xalan. Main class
  42  * {@link com.sun.org.apache.xml.internal.serialize.DOMSerializerImpl} is replaced
  43  * by {@link com.sun.org.apache.xml.internal.serializer.dom3.LSSerializerImpl}.
  44  */
  45 public abstract class SerializerFactory
  46 {
  47 
  48 
  49     public static final String FactoriesProperty = "com.sun.org.apache.xml.internal.serialize.factories";
  50 
  51 
  52     private static final Map<String, SerializerFactory>  _factories = Collections.synchronizedMap(new HashMap());
  53 
  54 
  55     static
  56     {
  57         SerializerFactory factory;
  58         String            list;
  59         StringTokenizer   token;
  60         String            className;
  61 
  62         // The default factories are always registered first,
  63         // any factory specified in the properties file and supporting
  64         // the same method will override the default factory.
  65         factory =  new SerializerFactoryImpl( Method.XML );
  66         registerSerializerFactory( factory );
  67         factory =  new SerializerFactoryImpl( Method.HTML );
  68         registerSerializerFactory( factory );
  69         factory =  new SerializerFactoryImpl( Method.XHTML );
  70         registerSerializerFactory( factory );
  71         factory =  new SerializerFactoryImpl( Method.TEXT );
  72         registerSerializerFactory( factory );
  73 
  74         list = SecuritySupport.getSystemProperty( FactoriesProperty );
  75         if ( list != null ) {
  76             token = new StringTokenizer( list, " ;,:" );
  77             while ( token.hasMoreTokens() ) {
  78                 className = token.nextToken();
  79                 try {
  80                     factory = (SerializerFactory) ObjectFactory.newInstance( className, true);
  81                     if ( _factories.containsKey( factory.getSupportedMethod() ) )
  82                         _factories.put( factory.getSupportedMethod(), factory );
  83                 } catch ( Exception except ) { }
  84             }
  85         }
  86     }
  87 
  88 
  89     /**
  90      * Register a serializer factory, keyed by the given
  91      * method string.
  92      */
  93     public static void registerSerializerFactory( SerializerFactory factory )
  94     {
  95         String method;
  96 
  97         synchronized ( _factories ) {
  98         method = factory.getSupportedMethod();
  99         _factories.put( method, factory );
 100     }
 101     }
 102 
 103 
 104     /**
 105      * Register a serializer factory, keyed by the given
 106      * method string.
 107      */
 108     public static SerializerFactory getSerializerFactory( String method )
 109     {
 110         return _factories.get( method );
 111     }
 112 
 113 
 114     /**
 115      * Returns the method supported by this factory and used to register
 116      * the factory. This call is required so factories can be added from
 117      * a properties file by knowing only the class name. This method is
 118      * protected, it is only required by this class but must be implemented
 119      * in derived classes.
 120      */
 121     protected abstract String getSupportedMethod();
 122 
 123 
 124     /**
 125      * Create a new serializer based on the {@link OutputFormat}.
 126      * If this method is used to create the serializer, the {@link
 127      * Serializer#setOutputByteStream} or {@link Serializer#setOutputCharStream}
 128      * methods must be called before serializing a document.
 129      */
 130     public abstract Serializer makeSerializer(OutputFormat format);
 131 
 132 
 133     /**
 134      * Create a new serializer, based on the {@link OutputFormat} and
 135      * using the writer as the output character stream.  If this
 136      * method is used, the encoding property will be ignored.
 137      */
 138     public abstract Serializer makeSerializer( Writer writer,
 139                                                OutputFormat format );
 140 
 141 
 142     /**
 143      * Create a new serializer, based on the {@link OutputFormat} and
 144      * using the output byte stream and the encoding specified in the
 145      * output format.
 146      *
 147      * @throws UnsupportedEncodingException The specified encoding is
 148      *   not supported
 149      */
 150     public abstract Serializer makeSerializer( OutputStream output,
 151                                                OutputFormat format )
 152         throws UnsupportedEncodingException;
 153 
 154 
 155 }