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