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