]> git.basschouten.com Git - openhab-addons.git/blob
77f8eaef9d68e36d277a91579a9995bdafd1b03d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mielecloud.internal.util;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16
17 /**
18  * Helper class for testing websites. Allows for easy access to the document contents.
19  *
20  * @author Björn Lange - Initial Contribution
21  */
22 @NonNullByDefault
23 public final class Website {
24     private String content;
25
26     protected Website(String content) {
27         this.content = content;
28     }
29
30     /**
31      * Gets the part of the content representing the element that surrounds the given text.
32      */
33     private String getElementSurrounding(String text) {
34         int index = content.indexOf(text);
35         if (index == -1) {
36             throw new IllegalStateException("Could not find \"" + text + "\" in \"" + content + "\"");
37         }
38
39         int elementBegin = content.lastIndexOf('<', index);
40         if (elementBegin == -1) {
41             throw new IllegalStateException("\"" + text + "\" is not contained in \"" + content + "\"");
42         }
43
44         int elementEnd = content.indexOf('>', index);
45         if (elementEnd == -1) {
46             throw new IllegalStateException("Malformatted HTML content: " + content);
47         }
48
49         return content.substring(elementBegin, elementEnd + 1);
50     }
51
52     /**
53      * Gets the value of an attribute from an element.
54      */
55     private String getAttributeFromElement(String element, String attribute) {
56         int valueStart = element.indexOf(attribute + "=\"");
57         if (valueStart == -1) {
58             throw new IllegalStateException("Element \"" + element + "\" has no " + attribute);
59         }
60
61         int valueEnd = element.indexOf('\"', valueStart + attribute.length() + 2);
62         if (valueEnd == -1) {
63             throw new IllegalStateException("Malformatted HTML content in element: " + element);
64         }
65
66         return element.substring(valueStart + attribute.length() + 2, valueEnd);
67     }
68
69     /**
70      * Gets the value of the input field with the given name.
71      *
72      * @param inputName Name of the input field.
73      * @return The value of the input field.
74      */
75     public String getValueOfInput(String inputName) {
76         return getAttributeFromElement(getElementSurrounding("name=\"" + inputName + "\""), "value");
77     }
78
79     /**
80      * Gets the value of the href attribute of the link with the given title text.
81      */
82     public String getTargetOfLink(String linkTitle) {
83         return getAttributeFromElement(getElementSurrounding(linkTitle), "href");
84     }
85
86     /**
87      * Checks whether the given raw text is contained in the raw website code.
88      */
89     public boolean contains(String expectedContent) {
90         return this.content.contains(expectedContent);
91     }
92
93     /**
94      * Gets the value of the action attribute of the first form found in the website body.
95      */
96     public String getFormAction() {
97         int formActionStart = content.indexOf("<form action=\"");
98         if (formActionStart == -1) {
99             throw new IllegalStateException("Could not find a form in \"" + content + "\"");
100         }
101
102         int formActionEnd = content.indexOf('\"', formActionStart + 15);
103         if (formActionEnd == -1) {
104             throw new IllegalStateException("Malformatted HTML content in form: " + content);
105         }
106
107         return content.substring(formActionStart + 14, formActionEnd);
108     }
109
110     public String getContent() {
111         return content;
112     }
113 }