1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.html;
16
17 import java.io.File;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.OutputStream;
21 import java.net.URL;
22 import java.nio.file.Files;
23 import java.util.Map;
24
25 import org.apache.commons.io.IOUtils;
26 import org.apache.commons.lang3.StringUtils;
27 import org.htmlunit.BrowserVersion;
28 import org.htmlunit.ElementNotFoundException;
29 import org.htmlunit.Page;
30 import org.htmlunit.SgmlPage;
31 import org.htmlunit.WebClient;
32 import org.htmlunit.WebRequest;
33 import org.htmlunit.WebResponse;
34 import org.htmlunit.javascript.host.event.Event;
35 import org.htmlunit.util.NameValuePair;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public class HtmlImageInput extends HtmlInput implements LabelableElement {
51
52
53 private boolean wasPositionSpecified_;
54 private int xPosition_;
55 private int yPosition_;
56 private WebResponse imageWebResponse_;
57 private boolean downloaded_;
58
59
60
61
62
63
64
65
66 HtmlImageInput(final String qualifiedName, final SgmlPage page, final Map<String, DomAttr> attributes) {
67 super(qualifiedName, page, attributes);
68 }
69
70
71
72
73 @Override
74 public NameValuePair[] getSubmitNameValuePairs() {
75 final String name = getNameAttribute();
76 final String prefix;
77
78 if (StringUtils.isEmpty(name)) {
79 prefix = "";
80 }
81 else {
82 prefix = name + ".";
83 }
84
85 if (wasPositionSpecified_) {
86 final NameValuePair valueX = new NameValuePair(prefix + 'x', Integer.toString(xPosition_));
87 final NameValuePair valueY = new NameValuePair(prefix + 'y', Integer.toString(yPosition_));
88 return new NameValuePair[] {valueX, valueY};
89 }
90 return new NameValuePair[]{new NameValuePair(getNameAttribute(), getRawValue())};
91 }
92
93
94
95
96
97
98
99
100
101 @Override
102 @SuppressWarnings("unchecked")
103 public Page click() throws IOException {
104 return click(0, 0);
105 }
106
107
108
109
110
111 @Override
112 protected boolean doClickStateUpdate(final boolean shiftKey, final boolean ctrlKey) throws IOException {
113 final HtmlForm form = getEnclosingForm();
114 if (form != null) {
115 form.submit(this);
116 return false;
117 }
118 super.doClickStateUpdate(shiftKey, ctrlKey);
119 return false;
120 }
121
122
123
124
125
126
127
128
129
130
131
132
133 public <P extends Page> P click(final int x, final int y) throws IOException, ElementNotFoundException {
134 wasPositionSpecified_ = true;
135 xPosition_ = x;
136 yPosition_ = y;
137 return super.click();
138 }
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154 @Override
155 public <P extends Page> P click(final Event event,
156 final boolean shiftKey, final boolean ctrlKey, final boolean altKey,
157 final boolean ignoreVisibility) throws IOException {
158 wasPositionSpecified_ = true;
159 return super.click(event, shiftKey, ctrlKey, altKey, ignoreVisibility);
160 }
161
162
163
164
165 @Override
166 public void setValue(final String newValue) {
167 unmarkValueDirty();
168 setDefaultValue(newValue);
169 }
170
171
172
173
174 @Override
175 public void setDefaultChecked(final boolean defaultChecked) {
176
177 }
178
179
180
181
182
183 @Override
184 public void setDefaultValue(final String defaultValue) {
185 super.setDefaultValue(defaultValue);
186 setRawValue(defaultValue);
187 }
188
189
190
191
192 @Override
193 protected boolean isRequiredSupported() {
194 return false;
195 }
196
197
198
199
200 @Override
201 public void setSrcAttribute(final String src) {
202 super.setSrcAttribute(src);
203 downloaded_ = false;
204 imageWebResponse_ = null;
205 }
206
207
208
209
210
211
212
213
214 private void downloadImageIfNeeded() throws IOException {
215 if (!downloaded_) {
216 final String src = getSrc();
217 if (!org.htmlunit.util.StringUtils.isEmptyString(src)) {
218 final HtmlPage page = (HtmlPage) getPage();
219 final WebClient webClient = page.getWebClient();
220
221 final BrowserVersion browser = webClient.getBrowserVersion();
222 final WebRequest request = new WebRequest(new URL(src), browser.getImgAcceptHeader(),
223 browser.getAcceptEncodingHeader());
224 request.setCharset(page.getCharset());
225 request.setRefererHeader(page.getUrl());
226 imageWebResponse_ = webClient.loadWebResponse(request);
227 }
228
229 downloaded_ = true;
230 }
231 }
232
233
234
235
236
237
238 public void saveAs(final File file) throws IOException {
239 downloadImageIfNeeded();
240 if (null != imageWebResponse_) {
241 try (OutputStream fos = Files.newOutputStream(file.toPath());
242 InputStream inputStream = imageWebResponse_.getContentAsStream()) {
243 IOUtils.copy(inputStream, fos);
244 }
245 }
246 }
247 }