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