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