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