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;
16
17 import java.io.File;
18 import java.io.IOException;
19 import java.nio.charset.Charset;
20 import java.nio.file.Files;
21 import java.nio.file.Path;
22
23 /**
24 * A generic page that will be returned for any text related content.
25 * Specifically any content types that start with {@code text/}
26 *
27 * @author Mike Bowler
28 * @author David K. Taylor
29 * @author Ronald Brill
30 * @author Ahmed Ashour
31 */
32 public class TextPage extends AbstractPage {
33
34 /**
35 * Creates an instance.
36 *
37 * @param webResponse the response from the server
38 * @param enclosingWindow the window that holds the page
39 */
40 public TextPage(final WebResponse webResponse, final WebWindow enclosingWindow) {
41 super(webResponse, enclosingWindow);
42 }
43
44 /**
45 * Returns the content of this page.
46 *
47 * @return the content of this page
48 */
49 public String getContent() {
50 return getWebResponse().getContentAsString();
51 }
52
53 /**
54 * Saves the content of this page to a text file.
55 *
56 * @param file file to write this page into
57 * @throws IOException If an error occurs
58 */
59 public void save(final File file) throws IOException {
60 final Path savePath = file.toPath();
61 final String text = getContent();
62 final Charset charset = getWebResponse().getContentCharset();
63 Files.write(savePath, text.getBytes(charset));
64 }
65 }