1 /*
   2  * Copyright (c) 2015, 2017 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 @Deprecated
  46 public abstract class SerializerFactory
  47 {
  48 
  49 
  50     public static final String FactoriesProperty = "com.sun.org.apache.xml.internal.serialize.factories";
  51 
  52 
  53     private static final Map<String, SerializerFactory>  _factories = Collections.synchronizedMap(new HashMap());
  54 
  55 
  56     static
  57     {
  58         SerializerFactory factory;
  59         String            list;
  60         StringTokenizer   token;
  61         String            className;
  62 
  63         // The default factories are always registered first,
  64         // any factory specified in the properties file and supporting
  65         // the same method will override the default factory.
  66         factory =  new SerializerFactoryImpl( Method.XML );
  67         registerSerializerFactory( factory );
  68         factory =  new SerializerFactoryImpl( Method.HTML );
  69         registerSerializerFactory( factory );
  70         factory =  new SerializerFactoryImpl( Method.XHTML );
  71         registerSerializerFactory( factory );
  72         factory =  new SerializerFactoryImpl( Method.TEXT );
  73         registerSerializerFactory( factory );
  74 
  75         list = SecuritySupport.getSystemProperty( FactoriesProperty );
  76         if ( list != null ) {
  77             token = new StringTokenizer( list, " ;,:" );
  78             while ( token.hasMoreTokens() ) {
  79                 className = token.nextToken();
  80                 try {
  81                     factory = (SerializerFactory) ObjectFactory.newInstance( className, true);
  82                     if ( _factories.containsKey( factory.getSupportedMethod() ) )
  83                         _factories.put( factory.getSupportedMethod(), factory );
  84                 } catch ( Exception except ) { }
  85             }
  86         }
  87     }
  88 
  89 
  90     /**
  91      * Register a serializer factory, keyed by the given
  92      * method string.
  93      */
  94     public static void registerSerializerFactory( SerializerFactory factory )
  95     {
  96         String method;
  97 
  98         synchronized ( _factories ) {
  99         method = factory.getSupportedMethod();
 100         _factories.put( method, factory );
 101     }
 102     }
 103 
 104 
 105     /**
 106      * Register a serializer factory, keyed by the given
 107      * method string.
 108      */
 109     public static SerializerFactory getSerializerFactory( String method )
 110     {
 111         return _factories.get( method );
 112     }
 113 
 114 
 115     /**
 116      * Returns the method supported by this factory and used to register
 117      * the factory. This call is required so factories can be added from
 118      * a properties file by knowing only the class name. This method is
 119      * protected, it is only required by this class but must be implemented
 120      * in derived classes.
 121      */
 122     protected abstract String getSupportedMethod();
 123 
 124 
 125     /**
 126      * Create a new serializer based on the {@link OutputFormat}.
 127      * If this method is used to create the serializer, the {@link
 128      * Serializer#setOutputByteStream} or {@link Serializer#setOutputCharStream}
 129      * methods must be called before serializing a document.
 130      */
 131     public abstract Serializer makeSerializer(OutputFormat format);
 132 
 133 
 134     /**
 135      * Create a new serializer, based on the {@link OutputFormat} and
 136      * using the writer as the output character stream.  If this
 137      * method is used, the encoding property will be ignored.
 138      */
 139     public abstract Serializer makeSerializer( Writer writer,
 140                                                OutputFormat format );
 141 
 142 
 143     /**
 144      * Create a new serializer, based on the {@link OutputFormat} and
 145      * using the output byte stream and the encoding specified in the
 146      * output format.
 147      *
 148      * @throws UnsupportedEncodingException The specified encoding is
 149      *   not supported
 150      */
 151     public abstract Serializer makeSerializer( OutputStream output,
 152                                                OutputFormat format )
 153         throws UnsupportedEncodingException;
 154 
 155 
 156 }