View Javadoc
1   /*
2    * Copyright (c) 2002-2017 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package com.gargoylesoftware.htmlunit.javascript.host.intl;
16  
17  import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_DATE_AR_DZ_ASCII_DIGITS;
18  
19  import java.text.DateFormat;
20  import java.text.DecimalFormat;
21  import java.text.DecimalFormatSymbols;
22  import java.text.SimpleDateFormat;
23  import java.util.Date;
24  
25  import com.gargoylesoftware.htmlunit.BrowserVersion;
26  
27  /**
28   * An implementation of {@code DateTimeFormatter}, supporting Google App Engine.
29   *
30   * @author Ahmed Ashour
31   */
32  class GAEDateTimeFormatter implements AbstractDateTimeFormatter {
33  
34      private DateFormat format_;
35  
36      /**
37       * Default constructor.
38       */
39      GAEDateTimeFormatter(final String locale, final BrowserVersion browserVersion, final String pattern) {
40          format_ = new SimpleDateFormat(pattern);
41          if (locale.startsWith("ar")
42                  && (!browserVersion.hasFeature(JS_DATE_AR_DZ_ASCII_DIGITS)
43                          || (!"ar-DZ".equals(locale)
44                                  && !"ar-LY".equals(locale)
45                                  && !"ar-MA".equals(locale)
46                                  && !"ar-TN".equals(locale)))) {
47              setZeroDigit('\u0660');
48          }
49      }
50  
51      private void setZeroDigit(final char zeroDigit) {
52          final DecimalFormat df = (DecimalFormat) format_.getNumberFormat();
53          final DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();
54          dfs.setZeroDigit(zeroDigit);
55          df.setDecimalFormatSymbols(dfs);
56      }
57  
58      /**
59       * {@inheritDoc}
60       */
61      @Override
62      public String format(final Date date) {
63          return format_.format(date);
64      }
65  
66  }