1 /*
   2  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  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 sun.util.locale.provider;
  27 
  28 import java.text.DateFormat;
  29 import java.text.SimpleDateFormat;
  30 import java.text.spi.DateFormatProvider;
  31 import java.util.Locale;
  32 
  33 /**
  34  * LocaleProviderAdapter implementation for the Unix locale data
  35  *
  36  * @author Naoto Sato
  37  */
  38 public class HostLocaleProviderAdapterImpl {
  39     static Locale[] supported = {Locale.getDefault(Locale.Category.FORMAT)};
  40 
  41     public static DateFormatProvider getDateFormatProvider() {
  42         return new DateFormatProvider() {
  43             String posixPattern;
  44 
  45             @Override
  46             public Locale[] getAvailableLocales() {
  47                 // not implemented yet
  48                 return new Locale[0];
  49             }
  50             @Override
  51             public DateFormat getDateInstance(int style, Locale locale) {
  52                 posixPattern = getPattern(style, -1, locale.toLanguageTag());
  53                 return new SimpleDateFormat(convertPosixToJava(posixPattern), locale);
  54             }
  55             @Override
  56             public DateFormat getTimeInstance(int style, Locale locale) {
  57                 posixPattern = getPattern(-1, style, locale.toLanguageTag());
  58                 return new SimpleDateFormat(convertPosixToJava(posixPattern), locale);
  59             }
  60             @Override
  61             public DateFormat getDateTimeInstance(int dateStyle,
  62                     int timeStyle, Locale locale) {
  63                 posixPattern = getPattern(dateStyle, timeStyle, locale.toLanguageTag());
  64                 return new SimpleDateFormat(convertPosixToJava(posixPattern), locale);
  65             }
  66         };
  67     }
  68 
  69     private static String convertPosixToJava(String posixPattern) {
  70         StringBuilder sb = new StringBuilder();
  71         boolean conversion = false;
  72 
  73         for (int index = 0; index < posixPattern.length(); index++) {
  74             char c = posixPattern.charAt(index);
  75             if (conversion) {
  76                 switch (c) {
  77                 case 'a':
  78                     sb.append("EEE");
  79                     break;
  80                 case 'b':
  81                     sb.append("MMM");
  82                     break;
  83                 case 'e':
  84                     sb.append("dd");
  85                     break;
  86                 case 'H':
  87                     sb.append("kk");
  88                     break;
  89                 case 'M':
  90                     sb.append("mm");
  91                     break;
  92                 case 'S':
  93                     sb.append("ss");
  94                     break;
  95                 case 'Y':
  96                     sb.append("yyyy");
  97                     break;
  98                 case 'm':
  99                     sb.append("MM");
 100                     break;
 101                 case 'd':
 102                     sb.append("dd");
 103                     break;
 104                 case 'r':
 105                     sb.append("hh:mm:ss aa");
 106                     break;
 107                 case 'Z':
 108                     sb.append("zzz");
 109                     break;
 110                 }
 111                 conversion = false;
 112             } else {
 113                 if (c == '%') {
 114                     conversion = true;
 115                 } else {
 116                     sb.append(c);
 117                 }
 118             }
 119         }
 120         return sb.toString();
 121     }
 122 
 123     private static native String getPattern(int dateStyle, int timeStyle, String langtag);
 124 }