001: /*
002: * :tabSize=8:indentSize=8:noTabs=false:
003: * :folding=explicit:collapseFolds=1:
004: *
005: * Copyright (C) 2007 Kazutoshi Satoda
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or any later version.
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: */
020:
021: package org.gjt.sp.jedit.io;
022:
023: //{{{ Imports
024: import java.io.InputStream;
025: import java.io.OutputStream;
026: import java.io.Reader;
027: import java.io.Writer;
028: import java.io.IOException;
029: import java.nio.charset.Charset;
030: import java.nio.charset.IllegalCharsetNameException;
031: import java.nio.charset.UnsupportedCharsetException;
032: import java.util.Set;
033: import java.util.HashSet;
034: import java.util.Iterator;
035: import java.util.Arrays;
036:
037: import org.gjt.sp.jedit.jEdit;
038: import org.gjt.sp.jedit.ServiceManager;
039:
040: //}}}
041:
042: /**
043: * A class for some static methods to deal with encodings.
044: *
045: * @since 4.3pre10
046: * @author Kazutoshi Satoda
047: */
048: public class EncodingServer {
049: //{{{ getEncoding() method
050: /**
051: * Returns an instance of Encoding for specified name.
052: * The name is used for search the following domains in the
053: * listed order.
054: * - java.nio.charset.Charset
055: * - jEdit ServiceManager
056: */
057: public static Encoding getEncoding(String name) {
058: try {
059: return new CharsetEncoding(name);
060: } catch (IllegalCharsetNameException e) {
061: // just failed
062: } catch (UnsupportedCharsetException e) {
063: // just failed
064: }
065:
066: Object namedService = ServiceManager.getService(serviceClass,
067: name);
068: if (namedService != null && namedService instanceof Encoding) {
069: return (Encoding) namedService;
070: }
071:
072: // UnsupportedCharsetException is for java.nio.charset,
073: // but throw this here too so that this can be caught as
074: // an encoding error by catch clause for general I/O code.
075: throw new UnsupportedCharsetException("No such encoding: \""
076: + name + "\"");
077: } //}}}
078:
079: //{{{ getAvailableNames() method
080: /**
081: * Returns the set of all available encoding names.
082: */
083: public static Set<String> getAvailableNames() {
084: Set<String> set = new HashSet<String>();
085: set.addAll(Charset.availableCharsets().keySet());
086: set.addAll(Arrays.asList(ServiceManager
087: .getServiceNames(serviceClass)));
088: return set;
089: } //}}}
090:
091: //{{{ getSelectedNames() method
092: /**
093: * Returns the set of user selected encoding names.
094: */
095: public static Set<String> getSelectedNames() {
096: Set<String> set = getAvailableNames();
097: Iterator<String> i = set.iterator();
098: while (i.hasNext()) {
099: String name = i.next();
100: if (jEdit.getBooleanProperty("encoding.opt-out." + name,
101: false)) {
102: i.remove();
103: }
104: }
105: return set;
106: } //}}}
107:
108: //{{{ getTextReader() method
109: /**
110: * Returns a Reader object that reads the InputStream with
111: * the encoding. This method is same with
112: * "getEncoding(encoding).getTextReader(in)".
113: */
114: public static Reader getTextReader(InputStream in, String encoding)
115: throws IOException {
116: return getEncoding(encoding).getTextReader(in);
117: } //}}}
118:
119: //{{{ getTextWriter() method
120: /**
121: * Returns a Writer object that writes to the OutputStream with
122: * the encoding. This method is same with
123: * "getEncoding(encoding).getTextWriter(out)".
124: */
125: public static Writer getTextWriter(OutputStream out, String encoding)
126: throws IOException {
127: return getEncoding(encoding).getTextWriter(out);
128: } //}}}
129:
130: //{{{ hasEncoding() method
131: /**
132: * Returns if the specified name is supported as a name for an Encoding.
133: */
134: public static boolean hasEncoding(String name) {
135: try {
136: if (Charset.isSupported(name)) {
137: return true;
138: }
139: } catch (IllegalCharsetNameException e) {
140: // The name is illegal for java.nio.charset.Charset.
141: // But it may be legal for service name.
142: }
143:
144: return Arrays.asList(
145: ServiceManager.getServiceNames(serviceClass)).contains(
146: name);
147: } //}}}
148:
149: //{{{ Private members
150: private static final String serviceClass = "org.gjt.sp.jedit.io.Encoding";
151: //}}}
152: }
|