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 Plugin}.
31   *
32   * @author Marc Guillemot
33   * @author Ahmed Ashour
34   *
35   * @see <a href="http://www.xulplanet.com/references/objref/MimeTypeArray.html">XUL Planet</a>
36   */
37  @JsxClass
38  public class Plugin extends HtmlUnitScriptable {
39      private String description_;
40      private String filename_;
41      private String name_;
42  
43      private final List<MimeType> elements_ = new ArrayList<>();
44  
45      /**
46       * Creates an instance.
47       */
48      public Plugin() {
49          super();
50      }
51  
52      /**
53       * JavaScript constructor.
54       */
55      @JsxConstructor
56      public void jsConstructor() {
57          // nothing to do
58      }
59  
60      /**
61       * Ctor initializing fields.
62       *
63       * @param name the plugin name
64       * @param description the plugin description
65       * @param filename the plugin filename
66       */
67      public Plugin(final String name, final String description, final String filename) {
68          super();
69          name_ = name;
70          description_ = description;
71          filename_ = filename;
72      }
73  
74      /**
75       * Returns the item at the given index.
76       * @param index the index
77       * @return the item at the given position
78       */
79      @JsxFunction
80      public MimeType item(final int index) {
81          if (index >= 0 && index < elements_.size()) {
82              return elements_.get(index);
83          }
84          return null;
85      }
86  
87      /**
88       * {@inheritDoc}
89       */
90      @Override
91      protected Object getWithPreemption(final String name) {
92          final MimeType response = namedItem(name);
93          if (response != null) {
94              return response;
95          }
96          return NOT_FOUND;
97      }
98  
99      /**
100      * Returns the element at the specified index, or {@code null} if the index is invalid.
101      * {@inheritDoc}
102      */
103     @Override
104     public final Object get(final int index, final Scriptable start) {
105         final Plugin plugin = (Plugin) start;
106         final List<MimeType> elements = plugin.elements_;
107 
108         if (index >= 0 && index < elements.size()) {
109             return elements.get(index);
110         }
111         return NOT_FOUND;
112     }
113 
114     /**
115      * Returns the item at the given index.
116      * @param name the item name
117      * @return the item with the given name
118      */
119     @JsxFunction
120     public MimeType namedItem(final String name) {
121         for (final MimeType element : elements_) {
122             if (name.equals(element.getType())) {
123                 return element;
124             }
125         }
126         return null;
127     }
128 
129     /**
130      * Gets the array size.
131      * @return the number elements
132      */
133     @JsxGetter
134     public int getLength() {
135         return elements_.size();
136     }
137 
138     /**
139      * Adds an element.
140      * @param element the element to add
141      */
142     void add(final MimeType element) {
143         elements_.add(element);
144     }
145 
146     /**
147      * Gets the plugin's description.
148      * @return the description
149      */
150     @JsxGetter
151     public String getDescription() {
152         return description_;
153     }
154 
155     /**
156      * Gets the plugin's file name.
157      * @return the file name
158      */
159     @JsxGetter
160     public String getFilename() {
161         return filename_;
162     }
163 
164     /**
165      * Gets the plugin's name.
166      * @return the name
167      */
168     @JsxGetter
169     public String getName() {
170         return name_;
171     }
172 
173     /**
174      * @return the Iterator symbol
175      */
176     @JsxSymbol
177     public Scriptable iterator() {
178         return JavaScriptEngine.newArrayIteratorTypeValues(getParentScope(), this);
179     }
180 }