View Javadoc
1   /*
2    * Copyright (c) 2002-2017 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    * http://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 com.gargoylesoftware.htmlunit.javascript.host.file;
16  
17  import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_FILE_SHORT_DATE_FORMAT;
18  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.CHROME;
19  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.EDGE;
20  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF;
21  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF52;
22  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.IE;
23  
24  import java.util.Date;
25  import java.util.Locale;
26  import java.util.TimeZone;
27  
28  import org.apache.commons.lang3.time.FastDateFormat;
29  
30  import com.gargoylesoftware.htmlunit.BrowserVersion;
31  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass;
32  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor;
33  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction;
34  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter;
35  
36  /**
37   * A JavaScript object for {@code File}.
38   *
39   * @author Ahmed Ashour
40   */
41  @JsxClass
42  public class File extends Blob {
43      private static final String LAST_MODIFIED_DATE_FORMAT = "EEE MMM dd yyyy HH:mm:ss 'GMT'Z (zzzz)";
44      private static final String LAST_MODIFIED_DATE_FORMAT_FF = "EEE MMM dd yyyy HH:mm:ss 'GMT'Z";
45  
46      private java.io.File file_;
47  
48      /**
49       * Creates an instance.
50       */
51      @JsxConstructor({CHROME, FF, EDGE})
52      public File() {
53      }
54  
55      File(final String pathname) {
56          file_ = new java.io.File(pathname);
57      }
58  
59      /**
60       * Returns the {@code name} property.
61       * @return the {@code name} property
62       */
63      @JsxGetter
64      public String getName() {
65          return file_.getName();
66      }
67  
68      /**
69       * Returns the {@code lastModifiedDate} property.
70       * @return the {@code lastModifiedDate} property
71       */
72      @JsxGetter
73      public String getLastModifiedDate() {
74          final Date date = new Date(getLastModified());
75          final BrowserVersion browser = getBrowserVersion();
76          final Locale locale = new Locale(browser.getSystemLanguage());
77          final TimeZone timezone = browser.getSystemTimezone();
78  
79          if (browser.hasFeature(JS_FILE_SHORT_DATE_FORMAT)) {
80              final FastDateFormat format = FastDateFormat.getInstance(LAST_MODIFIED_DATE_FORMAT_FF, timezone, locale);
81              return format.format(date);
82          }
83  
84          final FastDateFormat format = FastDateFormat.getInstance(LAST_MODIFIED_DATE_FORMAT, timezone, locale);
85          return format.format(date);
86      }
87  
88      /**
89       * Returns the {@code lastModified} property.
90       * @return the {@code lastModified} property
91       */
92      @JsxGetter({CHROME, FF})
93      public long getLastModified() {
94          return file_.lastModified();
95      }
96  
97      /**
98       * Returns the {@code webkitRelativePath} property.
99       * @return the {@code webkitRelativePath} property
100      */
101     @JsxGetter({CHROME, FF52})
102     public String getWebkitRelativePath() {
103         return "";
104     }
105 
106     /**
107      * Returns the {@code size} property.
108      * @return the {@code size} property
109      */
110     @JsxGetter
111     public long getSize() {
112         return file_.length();
113     }
114 
115     /**
116      * Returns the {@code type} property.
117      * @return the {@code type} property
118      */
119     @JsxGetter
120     public String getType() {
121         return getBrowserVersion().getUploadMimeType(file_);
122     }
123 
124     /**
125      * Slices the file.
126      */
127     @JsxFunction
128     public void slice() {
129     }
130 
131     /**
132      * Closes the file.
133      */
134     @JsxFunction(IE)
135     public void msClose() {
136     }
137 
138     /**
139      * Returns the underlying file.
140      * @return the underlying file
141      */
142     public java.io.File getFile() {
143         return file_;
144     }
145 }