View Javadoc
1   /*
2    * Copyright (c) 2002-2025 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    * https://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 org.htmlunit.javascript.host.intl;
16  
17  import org.htmlunit.corejs.javascript.Context;
18  import org.htmlunit.corejs.javascript.Function;
19  import org.htmlunit.corejs.javascript.NativeArray;
20  import org.htmlunit.corejs.javascript.Scriptable;
21  import org.htmlunit.javascript.HtmlUnitScriptable;
22  import org.htmlunit.javascript.JavaScriptEngine;
23  import org.htmlunit.javascript.RecursiveFunctionObject;
24  import org.htmlunit.javascript.configuration.JsxClass;
25  import org.htmlunit.javascript.configuration.JsxConstructor;
26  import org.htmlunit.javascript.host.Window;
27  
28  /**
29   * A JavaScript object for {@code Collator}.
30   *
31   * @author Ahmed Ashour
32   */
33  @JsxClass
34  public class Collator extends HtmlUnitScriptable {
35  
36      /**
37       * JavaScript constructor.
38       * @param cx the current context
39       * @param scope the scope
40       * @param args the arguments to the WebSocket constructor
41       * @param ctorObj the function object
42       * @param inNewExpr Is new or not
43       * @return the java object to allow JavaScript to access
44       */
45      @JsxConstructor
46      public static Scriptable jsConstructor(final Context cx, final Scriptable scope,
47              final Object[] args, final Function ctorObj, final boolean inNewExpr) {
48          final String[] locales;
49          if (args.length != 0) {
50              if (args[0] instanceof NativeArray) {
51                  final NativeArray array = (NativeArray) args[0];
52                  locales = new String[(int) array.getLength()];
53                  for (int i = 0; i < locales.length; i++) {
54                      locales[i] = JavaScriptEngine.toString(array.get(i));
55                  }
56              }
57              else {
58                  locales = new String[] {JavaScriptEngine.toString(args[0])};
59              }
60          }
61          else {
62              locales = new String[] {""};
63          }
64          final Window window = getWindow(ctorObj);
65          final Collator format = new Collator(/*locales, window.getBrowserVersion()*/);
66          format.setParentScope(window);
67          format.setPrototype(((RecursiveFunctionObject) ctorObj).getClassPrototype());
68          return format;
69      }
70  }