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

Print this page

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

      11  * This code is distributed in the hope that it will be useful, but WITHOUT
      12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
      13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      14  * version 2 for more details (a copy is included in the LICENSE file that
      15  * accompanied this code).
      16  *
      17  * You should have received a copy of the GNU General Public License version
      18  * 2 along with this work; if not, write to the Free Software Foundation,
      19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
      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.Utils;
      29 import com.sun.javafx.css.Size;
      30 import com.sun.javafx.css.StyleConverterImpl;

      31 import java.util.Map;
      32 import java.util.Map.Entry;
      33 import javafx.css.CssMetaData;
      34 import javafx.css.ParsedValue;
      35 import javafx.css.StyleConverter;
      36 import javafx.css.Styleable;
      37 import javafx.scene.text.Font;
      38 import javafx.scene.text.FontPosture;
      39 import javafx.scene.text.FontWeight;
      40
      41 public final class FontConverter extends StyleConverterImpl<ParsedValue[], Font> {
      42
      43     // lazy, thread-safe instatiation
      44     private static class Holder {
      45         static FontConverter INSTANCE = new FontConverter();
      46     }
      47
      48     public static StyleConverter<ParsedValue[], Font> getInstance() {
      49         return Holder.INSTANCE;
      50     }
     115
     116         public static FontStyleConverter getInstance() {
     117             return Holder.INSTANCE;
     118         }
     119
     120         private FontStyleConverter() {
     121             super();
     122         }
     123
     124         @Override
     125         public FontPosture convert(ParsedValue<String, FontPosture> value, Font font) {
     126
     127             // Testing for RT-31022 exposed a ClassCastException where value
     128             // wraps a String (e.g., "ITALIC", not a FontUnits.Style).
     129             final Object val = value.getValue();
     130
     131             FontPosture style = null;
     132
     133             if (val instanceof String) {
     134                 try {
     135                     String sval = ((String)val).toUpperCase();
     136                     style = Enum.valueOf(FontPosture.class, sval);
     137                 } catch (IllegalArgumentException iae) {
     138                     style =  FontPosture.REGULAR;
     139                 } catch (NullPointerException npe) {
     140                     style =  FontPosture.REGULAR;
     141                 }
     142
     143             } else if (val instanceof FontPosture) {
     144                 style = (FontPosture)val;
     145             }
     146             return style;
     147         }
     148
     149         @Override
     150         public String toString() {
     151             return "FontConverter.StyleConverter";
     152         }
     153     }
     154
     155     public static final class FontWeightConverter extends StyleConverterImpl<String, FontWeight> {
     161
     162         public static FontWeightConverter getInstance() {
     163             return Holder.INSTANCE;
     164         }
     165
     166         private FontWeightConverter() {
     167             super();
     168         }
     169
     170         @Override
     171         public FontWeight convert(ParsedValue<String, FontWeight> value, Font font) {
     172
     173             // Testing for RT-31022 exposed a ClassCastException where value
     174             // wraps a String (e.g., "ITALIC", not a FontUnits.Style).
     175             final Object val = value.getValue();
     176
     177             FontWeight weight = null;
     178
     179             if (val instanceof String) {
     180                 try {
     181                     String sval = ((String)val).toUpperCase();
     182                     weight = Enum.valueOf(FontWeight.class, sval);
     183                 } catch (IllegalArgumentException iae) {
     184                     weight =  FontWeight.NORMAL;
     185                 } catch (NullPointerException npe) {
     186                     weight =  FontWeight.NORMAL;
     187                 }
     188
     189             } else if (val instanceof FontWeight) {
     190                 weight = (FontWeight)val;
     191             }
     192
     193             return weight;
     194         }
     195
     196         @Override
     197         public String toString() {
     198             return "FontConverter.WeightConverter";
     199         }
     200     }
     201
      11  * This code is distributed in the hope that it will be useful, but WITHOUT
      12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
      13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      14  * version 2 for more details (a copy is included in the LICENSE file that
      15  * accompanied this code).
      16  *
      17  * You should have received a copy of the GNU General Public License version
      18  * 2 along with this work; if not, write to the Free Software Foundation,
      19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
      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.Utils;
      29 import com.sun.javafx.css.Size;
      30 import com.sun.javafx.css.StyleConverterImpl;
      31 import java.util.Locale;
      32 import java.util.Map;
      33 import java.util.Map.Entry;
      34 import javafx.css.CssMetaData;
      35 import javafx.css.ParsedValue;
      36 import javafx.css.StyleConverter;
      37 import javafx.css.Styleable;
      38 import javafx.scene.text.Font;
      39 import javafx.scene.text.FontPosture;
      40 import javafx.scene.text.FontWeight;
      41
      42 public final class FontConverter extends StyleConverterImpl<ParsedValue[], Font> {
      43
      44     // lazy, thread-safe instatiation
      45     private static class Holder {
      46         static FontConverter INSTANCE = new FontConverter();
      47     }
      48
      49     public static StyleConverter<ParsedValue[], Font> getInstance() {
      50         return Holder.INSTANCE;
      51     }
     116
     117         public static FontStyleConverter getInstance() {
     118             return Holder.INSTANCE;
     119         }
     120
     121         private FontStyleConverter() {
     122             super();
     123         }
     124
     125         @Override
     126         public FontPosture convert(ParsedValue<String, FontPosture> value, Font font) {
     127
     128             // Testing for RT-31022 exposed a ClassCastException where value
     129             // wraps a String (e.g., "ITALIC", not a FontUnits.Style).
     130             final Object val = value.getValue();
     131
     132             FontPosture style = null;
     133
     134             if (val instanceof String) {
     135                 try {
     136                     String sval = ((String)val).toUpperCase(Locale.ROOT);
     137                     style = Enum.valueOf(FontPosture.class, sval);
     138                 } catch (IllegalArgumentException iae) {
     139                     style =  FontPosture.REGULAR;
     140                 } catch (NullPointerException npe) {
     141                     style =  FontPosture.REGULAR;
     142                 }
     143
     144             } else if (val instanceof FontPosture) {
     145                 style = (FontPosture)val;
     146             }
     147             return style;
     148         }
     149
     150         @Override
     151         public String toString() {
     152             return "FontConverter.StyleConverter";
     153         }
     154     }
     155
     156     public static final class FontWeightConverter extends StyleConverterImpl<String, FontWeight> {
     162
     163         public static FontWeightConverter getInstance() {
     164             return Holder.INSTANCE;
     165         }
     166
     167         private FontWeightConverter() {
     168             super();
     169         }
     170
     171         @Override
     172         public FontWeight convert(ParsedValue<String, FontWeight> value, Font font) {
     173
     174             // Testing for RT-31022 exposed a ClassCastException where value
     175             // wraps a String (e.g., "ITALIC", not a FontUnits.Style).
     176             final Object val = value.getValue();
     177
     178             FontWeight weight = null;
     179
     180             if (val instanceof String) {
     181                 try {
     182                     String sval = ((String)val).toUpperCase(Locale.ROOT);
     183                     weight = Enum.valueOf(FontWeight.class, sval);
     184                 } catch (IllegalArgumentException iae) {
     185                     weight =  FontWeight.NORMAL;
     186                 } catch (NullPointerException npe) {
     187                     weight =  FontWeight.NORMAL;
     188                 }
     189
     190             } else if (val instanceof FontWeight) {
     191                 weight = (FontWeight)val;
     192             }
     193
     194             return weight;
     195         }
     196
     197         @Override
     198         public String toString() {
     199             return "FontConverter.WeightConverter";
     200         }
     201     }
     202