jaxp/src/com/sun/org/apache/xml/internal/serialize/SerializerFactory.java
author joehw
Thu, 12 Apr 2012 08:38:26 -0700
changeset 12457 c348e06f0e82
parent 6 jaxp/src/share/classes/com/sun/org/apache/xml/internal/serialize/SerializerFactory.java@7f561c08de6b
child 12458 d601e4bba306
permissions -rw-r--r--
7160496: Rename JDK8 JAXP source directory Summary: moving src/share/classes to src Reviewed-by: ohair
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
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     6
 * Copyright 1999-2002,2004 The Apache Software Foundation.
7f561c08de6b Initial load
duke
parents:
diff changeset
     7
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
     8
 * Licensed under the Apache License, Version 2.0 (the "License");
7f561c08de6b Initial load
duke
parents:
diff changeset
     9
 * you may not use this file except in compliance with the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    10
 * You may obtain a copy of the License at
7f561c08de6b Initial load
duke
parents:
diff changeset
    11
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *      http://www.apache.org/licenses/LICENSE-2.0
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * limitations under the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
package com.sun.org.apache.xml.internal.serialize;
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
import java.io.OutputStream;
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
import java.io.Writer;
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import java.io.UnsupportedEncodingException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import java.util.Hashtable;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import java.util.StringTokenizer;
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
 * @author <a href="mailto:Scott_Boag/CAM/Lotus@lotus.com">Scott Boag</a>
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
 * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a>
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
public abstract class SerializerFactory
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
{
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
    public static final String FactoriesProperty = "com.sun.org.apache.xml.internal.serialize.factories";
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
    private static Hashtable  _factories = new Hashtable();
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
    static
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
        SerializerFactory factory;
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
        String            list;
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
        StringTokenizer   token;
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
        String            className;
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
        // The default factories are always registered first,
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
        // any factory specified in the properties file and supporting
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
        // the same method will override the default factory.
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
        factory =  new SerializerFactoryImpl( Method.XML );
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
        registerSerializerFactory( factory );
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
        factory =  new SerializerFactoryImpl( Method.HTML );
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
        registerSerializerFactory( factory );
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
        factory =  new SerializerFactoryImpl( Method.XHTML );
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
        registerSerializerFactory( factory );
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
        factory =  new SerializerFactoryImpl( Method.TEXT );
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
        registerSerializerFactory( factory );
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
        list = System.getProperty( FactoriesProperty );
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
        if ( list != null ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
            token = new StringTokenizer( list, " ;,:" );
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
            while ( token.hasMoreTokens() ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
                className = token.nextToken();
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
                try {
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
                    factory = (SerializerFactory) ObjectFactory.newInstance( className,
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
                        SerializerFactory.class.getClassLoader(), true);
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
                    if ( _factories.containsKey( factory.getSupportedMethod() ) )
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
                        _factories.put( factory.getSupportedMethod(), factory );
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
                } catch ( Exception except ) { }
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
     * Register a serializer factory, keyed by the given
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
     * method string.
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
    public static void registerSerializerFactory( SerializerFactory factory )
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
        String method;
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
        synchronized ( _factories ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
            method = factory.getSupportedMethod();
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
            _factories.put( method, factory );
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
     * Register a serializer factory, keyed by the given
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
     * method string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
    public static SerializerFactory getSerializerFactory( String method )
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
        return (SerializerFactory) _factories.get( method );
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
     * Returns the method supported by this factory and used to register
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
     * the factory. This call is required so factories can be added from
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
     * a properties file by knowing only the class name. This method is
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
     * protected, it is only required by this class but must be implemented
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
     * in derived classes.
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
    protected abstract String getSupportedMethod();
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
     * Create a new serializer based on the {@link OutputFormat}.
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
     * If this method is used to create the serializer, the {@link
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
     * Serializer#setOutputByteStream} or {@link Serializer#setOutputCharStream}
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
     * methods must be called before serializing a document.
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
    public abstract Serializer makeSerializer(OutputFormat format);
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
     * Create a new serializer, based on the {@link OutputFormat} and
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
     * using the writer as the output character stream.  If this
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
     * method is used, the encoding property will be ignored.
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
    public abstract Serializer makeSerializer( Writer writer,
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
                                               OutputFormat format );
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
     * Create a new serializer, based on the {@link OutputFormat} and
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
     * using the output byte stream and the encoding specified in the
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
     * output format.
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
     * @throws UnsupportedEncodingException The specified encoding is
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
     *   not supported
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
    public abstract Serializer makeSerializer( OutputStream output,
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
                                               OutputFormat format )
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
        throws UnsupportedEncodingException;
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
}