2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mielecloud.internal.util;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
18 * Helper class for testing websites. Allows for easy access to the document contents.
20 * @author Björn Lange - Initial Contribution
23 public final class Website {
24 private String content;
26 protected Website(String content) {
27 this.content = content;
31 * Gets the part of the content representing the element that surrounds the given text.
33 private String getElementSurrounding(String text) {
34 int index = content.indexOf(text);
36 throw new IllegalStateException("Could not find \"" + text + "\" in \"" + content + "\"");
39 int elementBegin = content.lastIndexOf('<', index);
40 if (elementBegin == -1) {
41 throw new IllegalStateException("\"" + text + "\" is not contained in \"" + content + "\"");
44 int elementEnd = content.indexOf('>', index);
45 if (elementEnd == -1) {
46 throw new IllegalStateException("Malformatted HTML content: " + content);
49 return content.substring(elementBegin, elementEnd + 1);
53 * Gets the value of an attribute from an element.
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);
61 int valueEnd = element.indexOf('\"', valueStart + attribute.length() + 2);
63 throw new IllegalStateException("Malformatted HTML content in element: " + element);
66 return element.substring(valueStart + attribute.length() + 2, valueEnd);
70 * Gets the value of the input field with the given name.
72 * @param inputName Name of the input field.
73 * @return The value of the input field.
75 public String getValueOfInput(String inputName) {
76 return getAttributeFromElement(getElementSurrounding("name=\"" + inputName + "\""), "value");
80 * Gets the value of the href attribute of the link with the given title text.
82 public String getTargetOfLink(String linkTitle) {
83 return getAttributeFromElement(getElementSurrounding(linkTitle), "href");
87 * Checks whether the given raw text is contained in the raw website code.
89 public boolean contains(String expectedContent) {
90 return this.content.contains(expectedContent);
94 * Gets the value of the action attribute of the first form found in the website body.
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 + "\"");
102 int formActionEnd = content.indexOf('\"', formActionStart + 15);
103 if (formActionEnd == -1) {
104 throw new IllegalStateException("Malformatted HTML content in form: " + content);
107 return content.substring(formActionStart + 14, formActionEnd);
110 public String getContent() {