001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: *
022: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023: * in the United States and other countries.]
024: *
025: * ------------
026: * EncodingSupport.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.util;
030:
031: import java.io.UnsupportedEncodingException;
032: import java.util.HashMap;
033:
034: import org.jfree.report.JFreeReportBoot;
035: import org.jfree.util.Log;
036:
037: /**
038: * A global registry for all supported encodings.
039: * @deprecated use libfonts instead.
040: * @author Thomas Morgner.
041: */
042: public final class EncodingSupport {
043: /**
044: * Default Constructor.
045: */
046: private EncodingSupport() {
047: }
048:
049: /**
050: * Storage for the known encodings.
051: */
052: private static HashMap knownEncodings;
053:
054: /**
055: * the string that should be encoded.
056: */
057: private static final String TEST_STRING = " ";
058:
059: /**
060: * Returns <code>true</code> if the encoding is valid, and <code>false</code>
061: * otherwise.
062: *
063: * @param encoding the encoding (name).
064: * @return A boolean.
065: * @noinspection ResultOfMethodCallIgnored
066: */
067: public static boolean isSupportedEncoding(final String encoding) {
068: if (encoding == null) {
069: throw new NullPointerException();
070: }
071: if (knownEncodings == null) {
072: knownEncodings = new HashMap();
073: }
074:
075: final Boolean value = (Boolean) knownEncodings.get(encoding);
076: if (value != null) {
077: return value.booleanValue();
078: }
079:
080: try {
081: TEST_STRING.getBytes(encoding);
082: knownEncodings.put(encoding, Boolean.TRUE);
083: return true;
084: } catch (UnsupportedEncodingException ue) {
085: knownEncodings.put(encoding, Boolean.FALSE);
086: Log.info(new Log.SimpleMessage("Encoding ", encoding,
087: " is not supported."));
088: return false;
089: } catch (Exception e) {
090: knownEncodings.put(encoding, Boolean.FALSE);
091: Log.info(new Log.SimpleMessage("Encoding ", encoding,
092: " is buggy. Blame Sun for it."));
093: return false;
094: }
095: }
096:
097: /**
098: * Helper method to read the platform default encoding from the VM's system properties.
099: *
100: * @return the contents of the system property "file.encoding".
101: */
102: public static String getPlatformDefaultEncoding() {
103: return JFreeReportBoot.getInstance().getGlobalConfig()
104: .getConfigProperty("file.encoding", "Cp1252");
105: }
106:
107: }
|