commit-classpath
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[commit-cp] Re: [bug #13111] Classes in gnu.java.nio.charset.iconv shoul


From: Ito Kazumitsu
Subject: [commit-cp] Re: [bug #13111] Classes in gnu.java.nio.charset.iconv should be extendable
Date: Wed, 18 May 2005 07:30:13 +0900 (JST)

> So I suggest that classes in gnu.java.nio.charset.iconv should be
> extendable so that we can write slightly modified subclasses.
> 
> I think I will submit my suggested patch to the mailing list.

This is my patch:

diff -u -r orig/gnu/java/nio/charset/iconv/IconvCharset.java 
gnu/java/nio/charset/iconv/IconvCharset.java
--- orig/gnu/java/nio/charset/iconv/IconvCharset.java   Sat May  7 01:15:51 2005
+++ gnu/java/nio/charset/iconv/IconvCharset.java        Tue May 17 07:18:43 2005
@@ -42,16 +42,30 @@
 import java.nio.charset.CharsetDecoder;
 import java.nio.charset.CharsetEncoder;
 
-public final class IconvCharset extends Charset
+public class IconvCharset extends Charset
 {
   private IconvMetaData info;
 
+  public IconvCharset(IconvMetaData info, boolean isFinal)
+  {
+    super(info.nioCanonical(), info.aliases());
+    init(isFinal, info);
+  }
+    
   public IconvCharset(IconvMetaData info)
   {
     super(info.nioCanonical(), info.aliases());
-    this.info = info;
-    if (newEncoder() == null || newDecoder() == null)
-      throw new IllegalArgumentException();
+    init(true, info);
+  }
+
+  private void init(boolean isFinal, IconvMetaData info)
+  {
+    if (isFinal)
+    {
+      this.info = info;
+      if (newEncoder() == null || newDecoder() == null)
+        throw new IllegalArgumentException();
+    }
   }
 
   public boolean contains(Charset cs)
diff -u -r orig/gnu/java/nio/charset/iconv/IconvDecoder.java 
gnu/java/nio/charset/iconv/IconvDecoder.java
--- orig/gnu/java/nio/charset/iconv/IconvDecoder.java   Wed Apr 27 00:46:40 2005
+++ gnu/java/nio/charset/iconv/IconvDecoder.java        Thu May 12 07:06:17 2005
@@ -47,7 +47,7 @@
 import java.nio.charset.CharsetEncoder;
 import java.nio.charset.CoderResult;
 
-final class IconvDecoder extends CharsetDecoder
+class IconvDecoder extends CharsetDecoder
 {
   IconvDecoder(Charset cs, IconvMetaData info)
   {
diff -u -r orig/gnu/java/nio/charset/iconv/IconvEncoder.java 
gnu/java/nio/charset/iconv/IconvEncoder.java
--- orig/gnu/java/nio/charset/iconv/IconvEncoder.java   Wed Apr 27 00:46:40 2005
+++ gnu/java/nio/charset/iconv/IconvEncoder.java        Mon May 16 06:44:12 2005
@@ -47,7 +47,7 @@
 import java.nio.charset.CharsetEncoder;
 import java.nio.charset.CoderResult;
 
-final class IconvEncoder extends CharsetEncoder
+class IconvEncoder extends CharsetEncoder
 {
   private RawData data;
   private int inremaining;
diff -u -r orig/gnu/java/nio/charset/iconv/IconvMetaData.java 
gnu/java/nio/charset/iconv/IconvMetaData.java
--- orig/gnu/java/nio/charset/iconv/IconvMetaData.java  Mon Apr 18 20:35:14 2005
+++ gnu/java/nio/charset/iconv/IconvMetaData.java       Wed May 11 07:32:42 2005
@@ -97,10 +97,33 @@
    */
   private String[] aliases;
 
+  /**
+   * Name of subclass extending IconvCharset
+   */
+  private String subClassName;
+
   IconvMetaData(String nioCanonical, float averageBperC, float maxBperC,
                 float averageCperB, float maxCperB, String[] aliases,
                 String iconvName)
   {
+    init(nioCanonical, averageBperC, maxBperC,
+                averageCperB, maxCperB, aliases,
+                iconvName, null);
+  }
+
+  IconvMetaData(String nioCanonical, float averageBperC, float maxBperC,
+                float averageCperB, float maxCperB, String[] aliases,
+                String iconvName, String subClassName)
+  {
+    init(nioCanonical, averageBperC, maxBperC,
+                averageCperB, maxCperB, aliases,
+                iconvName, subClassName);
+  }
+
+  private void init(String nioCanonical, float averageBperC, float maxBperC,
+                float averageCperB, float maxCperB, String[] aliases,
+                String iconvName, String subClassName)
+  {
     this.nioCanonical = nioCanonical;
     this.iconvName = iconvName;
 
@@ -109,6 +132,7 @@
     this.averageCperB = averageCperB;
     this.maxCperB = maxCperB;
     this.aliases = aliases;
+    this.subClassName = subClassName;
 
     names.put(nioCanonical, this);
     names.put(iconvName, this);
@@ -157,6 +181,11 @@
     return averageCperB;
   }
 
+  String subClassName()
+  {
+    return subClassName;
+  }
+
   static IconvMetaData get(String s)
   {
     return (IconvMetaData) names.get(s);
@@ -327,7 +356,8 @@
                       {
                         "shift-jis", "x-sjis", "ms_kanji", "shift_jis",
                         "csShiftJIS", "sjis", "pck",
-                      }, "Shift_JIS");
+                      }, "Shift_JIS",
+                     "gnu.java.nio.charset.iconv.Shift_JISCharset");
 
     new IconvMetaData("TIS-620", 1.0f, 1.0f, 1.0f, 1.0f, new String[] {  },
                       "TIS-620");
diff -u -r orig/gnu/java/nio/charset/iconv/IconvProvider.java 
gnu/java/nio/charset/iconv/IconvProvider.java
--- orig/gnu/java/nio/charset/iconv/IconvProvider.java  Mon Apr 18 20:35:14 2005
+++ gnu/java/nio/charset/iconv/IconvProvider.java       Tue May 17 07:21:33 2005
@@ -38,6 +38,7 @@
 
 package gnu.java.nio.charset.iconv;
 
+import java.lang.reflect.Constructor;
 import java.nio.charset.Charset;
 import java.nio.charset.spi.CharsetProvider;
 import java.util.Collections;
@@ -94,6 +95,23 @@
        if (info == null)
          info = new IconvMetaData(charsetName, 2.0f, 2.0f, 2.0f, 2.0f,
                                   new String[] {  }, charsetName);
+       String subClassName = info.subClassName();
+       if (subClassName != null)
+       {
+         Class c = null;
+         Constructor co = null;
+         try
+           {
+             c = Class.forName(subClassName);
+             co = c.getConstructor(
+               new Class[] {IconvMetaData.class});
+             return (Charset)(co.newInstance(new Object[] {info}));
+           }
+         catch (Exception e)
+           {
+             throw new IllegalArgumentException(e.toString());
+           }
+       }
        return new IconvCharset(info);
       }
     catch (IllegalArgumentException e)
diff -u -r orig/gnu/java/nio/charset/iconv/Shift_JISCharset.java 
gnu/java/nio/charset/iconv/Shift_JISCharset.java
--- orig/gnu/java/nio/charset/iconv/Shift_JISCharset.java       Thu May 12 
06:56:53 2005
+++ gnu/java/nio/charset/iconv/Shift_JISCharset.java    Tue May 17 07:22:02 2005
@@ -0,0 +1,84 @@
+/* Shift_JISCharset.java -- Wrapper for iconv charsets.
+   Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+02111-1307 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package gnu.java.nio.charset.iconv;
+
+import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+import java.nio.charset.Charset;
+import java.nio.charset.CharsetDecoder;
+import java.nio.charset.CharsetEncoder;
+import java.nio.charset.CoderResult;
+
+public final class Shift_JISCharset extends IconvCharset
+{
+
+  private IconvMetaData info;
+
+  public Shift_JISCharset(IconvMetaData info)
+  {
+    super(info, false);
+    this.info = info;
+    if (newEncoder() == null || newDecoder() == null)
+      throw new IllegalArgumentException();
+  }
+
+  public CharsetDecoder newDecoder()
+  {
+    try
+      {
+       return new Shift_JISDecoder(this, info);
+      }
+    catch (IllegalArgumentException e)
+      {
+       return null;
+      }
+  }
+
+  public CharsetEncoder newEncoder()
+  {
+    try
+      {
+       return new Shift_JISEncoder(this, info);
+      }
+    catch (IllegalArgumentException e)
+      {
+       return null;
+      }
+  }
+}
diff -u -r orig/gnu/java/nio/charset/iconv/Shift_JISDecoder.java 
gnu/java/nio/charset/iconv/Shift_JISDecoder.java
--- orig/gnu/java/nio/charset/iconv/Shift_JISDecoder.java       Thu May 12 
06:56:53 2005
+++ gnu/java/nio/charset/iconv/Shift_JISDecoder.java    Tue May 17 07:27:37 2005
@@ -0,0 +1,76 @@
+/* Shift_JISDecoder.java --
+   Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+02111-1307 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package gnu.java.nio.charset.iconv;
+
+import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+import java.nio.charset.Charset;
+import java.nio.charset.CoderResult;
+
+final class Shift_JISDecoder extends IconvDecoder
+{
+
+  private IconvMetaData info;
+
+  Shift_JISDecoder(Charset cs, IconvMetaData info)
+  {
+    super(cs, info);
+    this.info = info;
+  }
+
+  protected CoderResult decodeLoop(ByteBuffer in, CharBuffer out)
+  {
+    int p0 = out.position();
+    CoderResult result = super.decodeLoop(in, out);
+    if (! result.isError())
+    {
+      int p = out.position();
+      for (int i = p0; i < p; i++)
+      {
+        char c = out.get(i);
+       if (c == (char)0x00a5)
+         out.put(i, (char)'\\');
+       else if (c == (char)0x203e)
+         out.put(i, (char)'~');
+      }
+    }
+    return result;
+  }
+
+}
diff -u -r orig/gnu/java/nio/charset/iconv/Shift_JISEncoder.java 
gnu/java/nio/charset/iconv/Shift_JISEncoder.java
--- orig/gnu/java/nio/charset/iconv/Shift_JISEncoder.java       Thu May 12 
06:56:53 2005
+++ gnu/java/nio/charset/iconv/Shift_JISEncoder.java    Tue May 17 07:27:55 2005
@@ -0,0 +1,92 @@
+/* Shift_JISEncoder.java --
+   Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+02111-1307 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package gnu.java.nio.charset.iconv;
+
+import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+import java.nio.BufferOverflowException;
+import java.nio.charset.Charset;
+import java.nio.charset.CoderResult;
+
+final class Shift_JISEncoder extends IconvEncoder
+{
+
+  Shift_JISEncoder(Charset cs, IconvMetaData info)
+  {
+    super(cs, info);
+  }
+
+  protected CoderResult encodeLoop(CharBuffer in, ByteBuffer out)
+  {
+    CoderResult result = super.encodeLoop(in, out);
+    if (result.isError())
+    {
+      int l = result.length();
+      int p = in.position();
+      for (int i = 0; i < l; i++)
+      {
+        char c = in.get(p);
+       if (c == '\\' || c == '~')
+       {
+         try
+         {
+           out.put((byte)c);
+            p++;
+            in.position(p);
+         }
+         catch (BufferOverflowException _)
+         {
+           return CoderResult.OVERFLOW;
+         }
+       }
+       else
+       {
+         return CoderResult.unmappableForLength(1);
+       }
+      }
+      return this.encodeLoop(in, out);
+    }
+    else
+    {
+      return result;
+    }
+
+  }
+
+}




reply via email to

[Prev in Thread] Current Thread [Next in Thread]