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          return files_.size();
98      }
99  
100     /**
101      * Returns a {@code File} object representing the file at the specified index in the file list.
102      * @param index The zero-based index of the file to retrieve from the list
103      * @return The {@code File} representing the requested file
104      */
105     @JsxFunction
106     public File item(final int index) {
107         if (index >= 0 && index < files_.size()) {
108             return files_.get(index);
109         }
110         return null;
111     }
112 
113     /**
114      * {@inheritDoc}
115      */
116     @Override
117     public Object get(final int index, final Scriptable start) {
118         if (this == start) {
119             if (index >= 0 && index < files_.size()) {
120                 return files_.get(index);
121             }
122         }
123         return super.get(index, start);
124     }
125 
126     /**
127      * Returns an Iterator allowing to go through all keys contained in this object.
128      * @return a NativeArrayIterator
129      */
130     @JsxSymbol(symbolName = "iterator")
131     public Scriptable values() {
132         return JavaScriptEngine.newArrayIteratorTypeValues(getParentScope(), this);
133     }
134 }