001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.desktop.encode;
006:
007: import java.util.Map;
008: import java.util.HashMap;
009: import java.util.Collections;
010:
011: /**
012: * This class provides a device-unaware means of decoding markup and
013: * other text. It is the front end for an decoder SPI, allowing different
014: * encoding schemes to be plugged in to the system.
015: * <br><br>
016: * To encode text, use the <code>Encoder</code> class.
017: * <br><br>
018: * The pluggable components used by this class are called
019: * type decoders. A type decoder is a class that implements
020: * the simple <code>TypeDecoder</code> interface.
021: * <br><br>
022: * For cases where the encoding type is fixed and does not
023: * change with the markup type, the static type decoder may be used:
024: * COOKIE_DECODER</code>.
025: * <br><br>
026: * To add a new type decoder to the system, do the following:
027: * <ul>
028: * <li> Author a class that implements the <code>TypeDecoder</code>
029: * interface.
030: * <li> Add the class to the web container's class path.
031: * </ul>
032: * Now, the new type decoder may be referenced by naming its
033: * class name in either of the two methods in this class.
034: * <br><br>
035: * The public methods and members in this class are static. This class
036: * may not be instantiated.
037: *
038: * @see com.sun.portal.desktop.encode.TypeDecoder
039: * @see com.sun.portal.desktop.encode.Encoder
040: */
041:
042: public class Decoder {
043: private static Map decoders = Collections
044: .synchronizedMap(new HashMap());
045:
046: /**
047: * Cookie type decoder.
048: */
049: public static TypeDecoder COOKIE_DECODER = null;
050:
051: static {
052: try {
053: COOKIE_DECODER = get(EncoderClassNames.DECODER_COOKIE);
054: } catch (EncoderException ee) {
055: throw new EncoderError(
056: "Decoder.<init>: error initializing standard decoder types",
057: ee);
058: }
059: }
060:
061: private Decoder() {
062: }
063:
064: /**
065: * Gets an instance for the named type decoder.
066: *
067: * @param decoderClassName a <code>String</code> value indicating
068: * the type decoder class name. The named class must implement
069: * the <code>TypeDecoder</code> interface.
070: * @return a <code>TypeDecoder</code> value, the type decoder instance.
071: * @exception EncoderException if there is an error instantiating the
072: * type decoder object.
073: */
074: public static TypeDecoder get(String decoderClassName)
075: throws EncoderException {
076: TypeDecoder decoder = (TypeDecoder) decoders
077: .get(decoderClassName);
078: if (decoder == null) {
079: try {
080: decoder = (TypeDecoder) Class.forName(decoderClassName)
081: .newInstance();
082: } catch (ClassNotFoundException cnfe) {
083: throw new EncoderException("Decoder.decode()", cnfe);
084: } catch (InstantiationException ie) {
085: throw new EncoderException("Decoder.decode()", ie);
086: } catch (IllegalAccessException iae) {
087: throw new EncoderException("Decoder.decode()", iae);
088: }
089: decoders.put(decoderClassName, decoder);
090: }
091:
092: return decoder;
093: }
094:
095: /**
096: * Encodes text with the named type encoder. This method is a
097: * convenience wrapper for calling <code>get()</code> and
098: * <code>TypeDecoder.decode()</code>.
099: *
100: * @param decoderClassName a <code>String</code> value indicating
101: * the type decoder class name. The named class must implement
102: * the <code>TypeDecoder</code> interface.
103: * @param text a <code>String</code> value, the text to be
104: * decoded.
105: * @return a <code>String</code> value, the decoded text.
106: * @exception EncoderException if an error occurs instantiating the named
107: * type decoder.
108: */
109: public static String decode(String decoderClassName, String text)
110: throws EncoderException {
111: TypeDecoder decoder = get(decoderClassName);
112: return decoder.decode(text);
113: }
114: }
|