001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    /*
019     * This code has been borrowed from the Apache Xerces project. We're copying the code to
020     * keep from adding a dependency on Xerces in the Geronimo kernel.
021     */
022    
023    package org.apache.geronimo.system.configuration;
024    
025    import java.io.OutputStream;
026    import java.io.OutputStreamWriter;
027    import java.io.UnsupportedEncodingException;
028    import java.io.Writer;
029    
030    /**
031     * This class represents an encoding.
032     *
033     * @version $Id: EncodingInfo.java 482336 2006-12-04 20:12:19Z kevan $
034     */
035    public class EncodingInfo {
036    
037        String name;
038        String javaName;
039        int lastPrintable;
040    
041        /**
042         * Creates new <code>EncodingInfo</code> instance.
043         */
044        public EncodingInfo(String mimeName, String javaName, int lastPrintable) {
045            this.name = mimeName;
046            this.javaName = javaName == null ? mimeName : javaName;
047            this.lastPrintable = lastPrintable;
048        }
049    
050        /**
051         * Creates new <code>EncodingInfo</code> instance.
052         */
053        public EncodingInfo(String mimeName, int lastPrintable) {
054            this(mimeName, mimeName, lastPrintable);
055        }
056    
057        /**
058         * Returns a MIME charset name of this encoding.
059         */
060        public String getName() {
061            return this.name;
062        }
063    
064        /**
065         * Returns a writer for this encoding based on
066         * an output stream.
067         *
068         * @return A suitable writer
069         * @exception UnsupportedEncodingException There is no convertor
070         *  to support this encoding
071         */
072        public Writer getWriter(OutputStream output)
073            throws UnsupportedEncodingException {
074            if (this.javaName == null)
075                return new OutputStreamWriter(output);
076            return new OutputStreamWriter(output, this.javaName);
077        }
078        /**
079         * Checks whether the specified character is printable or not.
080         *
081         * @param ch a code point (0-0x10ffff)
082         */
083        public boolean isPrintable(int ch) {
084            return ch <= this.lastPrintable;
085        }
086    }