001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/framework/util/CharsetUtils.java $
002    /*----------------------------------------------------------------------------
003     This file is part of deegree, http://deegree.org/
004     Copyright (C) 2001-2009 by:
005       Department of Geography, University of Bonn
006     and
007       lat/lon GmbH
008    
009     This library is free software; you can redistribute it and/or modify it under
010     the terms of the GNU Lesser General Public License as published by the Free
011     Software Foundation; either version 2.1 of the License, or (at your option)
012     any later version.
013     This library is distributed in the hope that it will be useful, but WITHOUT
014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016     details.
017     You should have received a copy of the GNU Lesser General Public License
018     along with this library; if not, write to the Free Software Foundation, Inc.,
019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020    
021     Contact information:
022    
023     lat/lon GmbH
024     Aennchenstr. 19, 53177 Bonn
025     Germany
026     http://lat-lon.de/
027    
028     Department of Geography, University of Bonn
029     Prof. Dr. Klaus Greve
030     Postfach 1147, 53001 Bonn
031     Germany
032     http://www.geographie.uni-bonn.de/deegree/
033    
034     e-mail: info@deegree.org
035    ----------------------------------------------------------------------------*/
036    package org.deegree.framework.util;
037    
038    import java.nio.ByteBuffer;
039    import java.nio.CharBuffer;
040    import java.nio.charset.CharacterCodingException;
041    import java.nio.charset.Charset;
042    import java.nio.charset.CharsetDecoder;
043    import java.nio.charset.CharsetEncoder;
044    
045    import org.deegree.framework.log.ILogger;
046    import org.deegree.framework.log.LoggerFactory;
047    
048    /**
049     *
050     *
051     * @version $Revision: 18195 $
052     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
053     * @author last edited by: $Author: mschneider $
054     *
055     * @version 1.0. $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
056     *
057     * @since 2.0
058     */
059    public final class CharsetUtils {
060    
061        private static ILogger LOG = LoggerFactory.getLogger( CharsetUtils.class );
062    
063        private static final String DEFAULT_CHARSET = "UTF-8";
064    
065        private CharsetUtils() {
066            //nottin
067        }
068    
069        /**
070         * returns the name of the charset that is passed to the JVM as system property -DCHARSET=... If
071         * no charset has been defined UTF-8 will be returned as default.
072         *
073         * @return the name of the charset that is passed to the JVM as system property -DCHARSET=... If
074         *         no charset has been defined UTF-8 will be returned as default.
075         */
076        public static String getSystemCharset() {
077            String charset = null;
078            try {
079                charset = System.getProperty( "CHARSET" );
080            } catch ( Exception exc ) {
081                LOG.logError( "Error retrieving system property CHARSET", exc );
082            }
083            if ( charset == null ) {
084                charset = DEFAULT_CHARSET;
085            }
086            LOG.logDebug( "Using system charset: " + charset );
087            return charset;
088        }
089    
090        /**
091         * A method to convert an input string from a given charset to UTF-8 by using {@link java.nio.charset.CharsetEncoder}
092         * @param input
093         * @param inCharset
094         * @return the charset encoded String
095         */
096        public static String convertToUnicode( String input, String inCharset ) {
097            // Create the encoder and decoder for inCharset
098            Charset charset = Charset.forName( inCharset );
099            CharsetEncoder encoder = charset.newEncoder();
100    
101            ByteBuffer bbuf = null;
102            try {
103                // Convert a string to ISO-LATIN-1 bytes in a ByteBuffer
104                // The new ByteBuffer is ready to be read.
105                bbuf = encoder.encode( CharBuffer.wrap( input ) );
106    
107            } catch ( CharacterCodingException e ) {
108                LOG.logError( e.getMessage(), e );
109            }
110            return bbuf.toString();
111        }
112    
113        /**
114         * A method to convert an input string in UTF-8 to a given charset by using {@link java.nio.charset.CharsetDecoder}
115         * @param input
116         * @param targetCharset
117         * @return the decoded string
118         */
119        public static String convertFromUnicode( String input, String targetCharset ) {
120            // Create the encoder and decoder for inCharset
121            Charset charset = Charset.forName( targetCharset );
122            CharsetDecoder decoder = charset.newDecoder();
123    
124            CharBuffer cbuf = null;
125            try {
126                // Convert ISO-LATIN-1 bytes in a ByteBuffer to a character ByteBuffer and then to a
127                // string.
128                // The new ByteBuffer is ready to be read.
129                cbuf = decoder.decode( ByteBuffer.wrap( input.getBytes() ) );
130            } catch ( CharacterCodingException e ) {
131                LOG.logError( e.getMessage(), e );
132            }
133            return cbuf.toString();
134        }
135    
136    }