## modules/graphics/src/main/java/com/sun/javafx/css/converters/EnumConverter.java

Print this page

    rev 4839 : RT-2529: Locale specific runtime errors (Turkish)

      20  *
      21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
      22  * or visit www.oracle.com if you need additional information or have any
      23  * questions.
      24  */
      25
      26 package com.sun.javafx.css.converters;
      27
      28 import com.sun.javafx.css.StringStore;
      29 import com.sun.javafx.css.StyleConverterImpl;
      30 import java.io.DataInputStream;
      31 import java.io.DataOutputStream;
      32 import java.io.IOException;
      33
      34 import com.sun.javafx.scene.layout.region.RepeatStructConverter;
      35 import javafx.css.ParsedValue;
      36 import javafx.css.StyleConverter;
      37 import javafx.scene.text.Font;
      38
      39 import java.util.HashMap;

      40 import java.util.Map;
      41
      42 import com.sun.javafx.Logging;
      43 import sun.util.logging.PlatformLogger;
      44 import sun.util.logging.PlatformLogger.Level;
      45
      46 public final class EnumConverter<E extends Enum<E>> extends StyleConverterImpl<String, E> {
      47
      48     // package for unit testing
      49     final Class<E> enumClass;
      50
      51     public EnumConverter(Class<E> enumClass) {
      52         this.enumClass = enumClass;
      53     }
      54
      55     @Override
      56     public E convert(ParsedValue<String, E> value, Font not_used) {
      57         if (enumClass == null) {
      58             return null;
      59         }
      60         String string = value.getValue();
      61         final int dotPos = string.lastIndexOf('.');
      62         if (dotPos > -1) {
      63             string = string.substring(dotPos + 1);
      64         }
      65         try {
      66             string = string.replace('-', '_');
      67             return Enum.valueOf(enumClass, string.toUpperCase());
      68         } catch (IllegalArgumentException e) {
      69             // may throw another IllegalArgumentException
      70             return Enum.valueOf(enumClass, string);
      71         }
      72     }
      73
      74     @Override
      75     public void writeBinary(DataOutputStream os, StringStore sstore) throws IOException {
      76         super.writeBinary(os,sstore);
      77         String ename = enumClass.getName();
      78         int index = sstore.addString(ename);
      79         os.writeShort(index);
      80     }
      81
      82
      83     public static StyleConverter<?,?> readBinary(DataInputStream is, String[] strings)
      84             throws IOException {
      85
      86         short index = is.readShort();
      87         String ename = 0 <= index && index <= strings.length ? strings[index] : null;
      20  *
      21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
      22  * or visit www.oracle.com if you need additional information or have any
      23  * questions.
      24  */
      25
      26 package com.sun.javafx.css.converters;
      27
      28 import com.sun.javafx.css.StringStore;
      29 import com.sun.javafx.css.StyleConverterImpl;
      30 import java.io.DataInputStream;
      31 import java.io.DataOutputStream;
      32 import java.io.IOException;
      33
      34 import com.sun.javafx.scene.layout.region.RepeatStructConverter;
      35 import javafx.css.ParsedValue;
      36 import javafx.css.StyleConverter;
      37 import javafx.scene.text.Font;
      38
      39 import java.util.HashMap;
      40 import java.util.Locale;
      41 import java.util.Map;
      42
      43 import com.sun.javafx.Logging;
      44 import sun.util.logging.PlatformLogger;
      45 import sun.util.logging.PlatformLogger.Level;
      46
      47 public final class EnumConverter<E extends Enum<E>> extends StyleConverterImpl<String, E> {
      48
      49     // package for unit testing
      50     final Class<E> enumClass;
      51
      52     public EnumConverter(Class<E> enumClass) {
      53         this.enumClass = enumClass;
      54     }
      55
      56     @Override
      57     public E convert(ParsedValue<String, E> value, Font not_used) {
      58         if (enumClass == null) {
      59             return null;
      60         }
      61         String string = value.getValue();
      62         final int dotPos = string.lastIndexOf('.');
      63         if (dotPos > -1) {
      64             string = string.substring(dotPos + 1);
      65         }
      66         try {
      67             string = string.replace('-', '_');
      68             return Enum.valueOf(enumClass, string.toUpperCase(Locale.ROOT));
      69         } catch (IllegalArgumentException e) {
      70             // may throw another IllegalArgumentException
      71             return Enum.valueOf(enumClass, string);
      72         }
      73     }
      74
      75     @Override
      76     public void writeBinary(DataOutputStream os, StringStore sstore) throws IOException {
      77         super.writeBinary(os,sstore);
      78         String ename = enumClass.getName();
      79         int index = sstore.addString(ename);
      80         os.writeShort(index);
      81     }
      82
      83
      84     public static StyleConverter<?,?> readBinary(DataInputStream is, String[] strings)
      85             throws IOException {
      86
      87         short index = is.readShort();
      88         String ename = 0 <= index && index <= strings.length ? strings[index] : null;