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;
16  
17  import java.util.ArrayList;
18  import java.util.List;
19  
20  import org.htmlunit.corejs.javascript.Scriptable;
21  import org.htmlunit.javascript.HtmlUnitScriptable;
22  import org.htmlunit.javascript.JavaScriptEngine;
23  import org.htmlunit.javascript.configuration.JsxClass;
24  import org.htmlunit.javascript.configuration.JsxConstructor;
25  import org.htmlunit.javascript.configuration.JsxFunction;
26  import org.htmlunit.javascript.configuration.JsxGetter;
27  import org.htmlunit.javascript.configuration.JsxSymbol;
28  
29  /**
30   * A JavaScript object for {@code MimeTypeArray}.
31   *
32   * @author Marc Guillemot
33   * @author Ahmed Ashour
34   * @author Ronald Brill
35   *
36   * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/MimeTypeArray">MimeTypeArray</a>
37   */
38  @JsxClass
39  public class MimeTypeArray extends HtmlUnitScriptable {
40  
41      private final List<MimeType> elements_ = new ArrayList<>();
42  
43      /**
44       * JavaScript constructor.
45       */
46      @JsxConstructor
47      public void jsConstructor() {
48          // nothing to do
49      }
50  
51      /**
52       * Gets the name of the mime type.
53       * @param element a {@link MimeType}
54       * @return the name
55       */
56      protected String getItemName(final Object element) {
57          return ((MimeType) element).getType();
58      }
59  
60      /**
61       * Returns the item at the given index.
62       * @param index the index
63       * @return the item at the given position
64       */
65      @JsxFunction
66      public MimeType item(final int index) {
67          return elements_.get(index);
68      }
69  
70      /**
71       * {@inheritDoc}
72       */
73      @Override
74      protected Object getWithPreemption(final String name) {
75          final MimeType response = namedItem(name);
76          if (response != null) {
77              return response;
78          }
79          return NOT_FOUND;
80      }
81  
82      /**
83       * {@inheritDoc}.
84       */
85      @Override
86      public boolean has(final String name, final Scriptable start) {
87          if (NOT_FOUND != getWithPreemption(name)) {
88              return true;
89          }
90  
91          return super.has(name, start);
92      }
93  
94      /**
95       * Returns the element at the specified index, or {@code null} if the index is invalid.
96       * {@inheritDoc}
97       */
98      @Override
99      public final MimeType get(final int index, final Scriptable start) {
100         final MimeTypeArray array = (MimeTypeArray) start;
101         final List<MimeType> elements = array.elements_;
102 
103         if (index >= 0 && index < elements.size()) {
104             return elements.get(index);
105         }
106         return null;
107     }
108 
109     /**
110      * Returns the item at the given index.
111      * @param name the item name
112      * @return the item with the given name
113      */
114     @JsxFunction
115     public MimeType namedItem(final String name) {
116         for (final MimeType element : elements_) {
117             if (name.equals(getItemName(element))) {
118                 return element;
119             }
120         }
121         return null;
122     }
123 
124     /**
125      * Gets the array size.
126      * @return the number elements
127      */
128     @JsxGetter
129     public int getLength() {
130         return elements_.size();
131     }
132 
133     /**
134      * Adds an element.
135      * @param element the element to add
136      */
137     void add(final MimeType element) {
138         elements_.add(element);
139     }
140 
141     /**
142      * @return the Iterator symbol
143      */
144     @JsxSymbol
145     public Scriptable iterator() {
146         return JavaScriptEngine.newArrayIteratorTypeValues(getParentScope(), this);
147     }
148 }