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.file;
16  
17  import java.util.ArrayList;
18  
19  import org.htmlunit.corejs.javascript.Scriptable;
20  import org.htmlunit.corejs.javascript.VarScope;
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 FileList}.
31   *
32   * @author Ahmed Ashour
33   * @author Ronald Brill
34   */
35  @JsxClass
36  public class FileList extends HtmlUnitScriptable {
37  
38      private ArrayList<File> files_;
39  
40      /**
41       * Creates an instance.
42       */
43      public FileList() {
44          super();
45      }
46  
47      /**
48       * JavaScript constructor.
49       */
50      @JsxConstructor
51      public void jsConstructor() {
52          // nothing to do
53      }
54  
55      /**
56       * Creates a new instance.
57       * @param array the array of files
58       */
59      public FileList(final java.io.File[] array) {
60          super();
61          files_ = new ArrayList<>();
62  
63          for (final java.io.File f : array) {
64              files_.add(new File(f.getAbsolutePath()));
65          }
66      }
67  
68      /**
69       * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span>
70       * Update the backing file array.
71       * @param files the new files list
72       */
73      public void updateFiles(final ArrayList<File> files) {
74          files_ = files;
75      }
76  
77      /**
78       * {@inheritDoc}
79       */
80      @Override
81      public void setParentScope(final VarScope scope) {
82          super.setParentScope(scope);
83          if (files_ != null) {
84              for (final File file : files_) {
85                  file.setParentScope(scope);
86                  file.setPrototype(getPrototype(file.getClass()));
87              }
88          }
89      }
90  
91      /**
92       * Returns the {@code length} property.
93       * @return the {@code length} property
94       */
95      @JsxGetter
96      public int getLength() {
97          if (files_ == null) {
98              return 0; // prototype
99          }
100         return files_.size();
101     }
102 
103     /**
104      * Returns a {@code File} object representing the file at the specified index in the file list.
105      * @param index The zero-based index of the file to retrieve from the list
106      * @return The {@code File} representing the requested file
107      */
108     @JsxFunction
109     public File item(final int index) {
110         if (index >= 0 && index < getIndexedPropertyCount()) {
111             return files_.get(index);
112         }
113         return null;
114     }
115 
116     /**
117      * {@inheritDoc}
118      */
119     @Override
120     protected int getIndexedPropertyCount() {
121         if (files_ == null) {
122             return 0;
123         }
124         return getLength();
125     }
126 
127     /**
128      * {@inheritDoc}
129      */
130     @Override
131     public Object get(final int index, final Scriptable start) {
132         if (this == start) {
133             // the prototype has no files_
134             if (index >= 0 && index < getIndexedPropertyCount()) {
135                 return files_.get(index);
136             }
137         }
138         return super.get(index, start);
139     }
140 
141     /**
142      * Returns an Iterator allowing to go through all keys contained in this object.
143      * @return a NativeArrayIterator
144      */
145     @JsxSymbol(symbolName = "iterator")
146     public Scriptable values() {
147         return JavaScriptEngine.newArrayIteratorTypeValues(getParentScope(), this);
148     }
149 }