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