2 * Copyright (c) 2010-2024 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.tacmi.internal.schema;
15 import java.util.LinkedHashMap;
18 import org.attoparser.ParseException;
19 import org.attoparser.simple.AbstractSimpleMarkupHandler;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.tacmi.internal.schema.ChangerX2Entry.OptionType;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * The {@link ApiPageParser} class parses the 'changerx2' page from the CMI and
28 * maps it to the results
30 * @author Christian Niessner - Initial contribution
33 public class ChangerX2Parser extends AbstractSimpleMarkupHandler {
35 private final Logger logger = LoggerFactory.getLogger(ChangerX2Parser.class);
37 static enum ParserState {
46 private final String channelName;
47 private @Nullable String curOptionId;
48 private ParserState parserState = ParserState.INIT;
49 private @Nullable String address;
50 private @Nullable String addressFieldName;
51 private @Nullable String optionFieldName;
52 private @Nullable OptionType optionType;
53 private @Nullable StringBuilder curOptionValue;
54 private Map<String, @Nullable String> options;
56 public ChangerX2Parser(String channelName) {
58 this.options = new LinkedHashMap<>();
59 this.channelName = channelName;
63 public void handleDocumentStart(final long startTimeNanos, final int line, final int col) throws ParseException {
64 this.parserState = ParserState.INIT;
69 public void handleDocumentEnd(final long endTimeNanos, final long totalTimeNanos, final int line, final int col)
70 throws ParseException {
71 if (this.parserState != ParserState.INIT) {
72 logger.debug("Parserstate == Init expected, but is {}", this.parserState);
78 public void handleStandaloneElement(final String elementName, final Map<String, String> attributes,
79 final boolean minimized, final int line, final int col) throws ParseException {
80 logger.debug("Error parsing options for {}: Unexpected StandaloneElement in {}{}: {} [{}]", channelName, line,
81 col, elementName, attributes);
86 public void handleOpenElement(final String elementName, final Map<String, String> attributes, final int line,
87 final int col) throws ParseException {
88 String id = attributes == null ? null : attributes.get("id");
90 if (this.parserState == ParserState.INIT && "input".equals(elementName) && "changeadr".equals(id)) {
91 this.parserState = ParserState.INPUT;
92 if (attributes == null) {
94 this.addressFieldName = null;
96 this.addressFieldName = attributes.get("name");
97 this.address = attributes.get("value");
99 } else if ((this.parserState == ParserState.INIT || this.parserState == ParserState.INPUT)
100 && "select".equals(elementName)) {
101 this.parserState = ParserState.SELECT;
102 this.optionFieldName = attributes == null ? null : attributes.get("name");
103 } else if ((this.parserState == ParserState.INIT || this.parserState == ParserState.INPUT)
104 && "br".equals(elementName)) {
106 } else if ((this.parserState == ParserState.INIT || this.parserState == ParserState.INPUT)
107 && "input".equals(elementName) && "changeto".equals(id)) {
108 this.parserState = ParserState.INPUT_DATA;
109 if (attributes != null) {
110 this.optionFieldName = attributes.get("name");
111 String type = attributes.get("type");
112 if ("number".equals(type)) {
113 this.optionType = OptionType.NUMBER;
114 // we transfer the limits from the input elemnt...
115 this.options.put(ChangerX2Entry.NUMBER_MIN, attributes.get(ChangerX2Entry.NUMBER_MIN));
116 this.options.put(ChangerX2Entry.NUMBER_MAX, attributes.get(ChangerX2Entry.NUMBER_MAX));
117 this.options.put(ChangerX2Entry.NUMBER_STEP, attributes.get(ChangerX2Entry.NUMBER_STEP));
119 logger.warn("Error parsing options for {}: Unhandled input field in {}:{}: {}", channelName, line,
123 } else if (this.parserState == ParserState.SELECT && "option".equals(elementName)) {
124 this.parserState = ParserState.SELECT_OPTION;
125 this.optionType = OptionType.SELECT;
126 this.curOptionValue = new StringBuilder();
127 this.curOptionId = attributes == null ? null : attributes.get("value");
129 logger.debug("Error parsing options for {}: Unexpected OpenElement in {}:{}: {} [{}]", channelName, line,
130 col, elementName, attributes);
135 public void handleCloseElement(final @Nullable String elementName, final int line, final int col)
136 throws ParseException {
137 if (this.parserState == ParserState.INPUT && "input".equals(elementName)) {
138 this.parserState = ParserState.INIT;
139 } else if (this.parserState == ParserState.SELECT && "select".equals(elementName)) {
140 this.parserState = ParserState.INIT;
141 } else if (this.parserState == ParserState.SELECT_OPTION && "option".equals(elementName)) {
142 this.parserState = ParserState.SELECT;
143 StringBuilder sb = this.curOptionValue;
144 String value = sb != null && sb.length() > 0 ? sb.toString().trim() : null;
145 this.curOptionValue = null;
146 String id = this.curOptionId;
147 this.curOptionId = null;
149 if (id == null || id.trim().isEmpty()) {
150 logger.debug("Error parsing options for {}: Got option with empty 'value' in {}:{}: [{}]",
151 channelName, line, col, value);
154 // we use the value as key and the id as value, as we have to map from the value to the id...
156 String prev = this.options.put(value, id);
157 if (prev != null && !prev.equals(value)) {
158 logger.debug("Error parsing options for {}: Got duplicate options in {}:{} for {}: {} and {}",
159 channelName, line, col, value, prev, id);
163 logger.debug("Error parsing options for {}: Unexpected CloseElement in {}:{}: {}", channelName, line, col,
169 public void handleAutoCloseElement(final @Nullable String elementName, final int line, final int col)
170 throws ParseException {
171 logger.debug("Unexpected AutoCloseElement in {}:{}: {}", line, col,
172 elementName == null ? "<null>" : elementName);
176 public void handleUnmatchedCloseElement(final @Nullable String elementName, final int line, final int col)
177 throws ParseException {
178 logger.debug("Unexpected UnmatchedCloseElement in {}:{}: {}", line, col,
179 elementName == null ? "<null>" : elementName);
183 public void handleDocType(final @Nullable String elementName, final @Nullable String publicId,
184 final @Nullable String systemId, final @Nullable String internalSubset, final int line, final int col)
185 throws ParseException {
186 logger.debug("Unexpected DocType in {}:{}: {}/{}/{}/{}", line, col, elementName, publicId, systemId,
191 public void handleComment(final char @Nullable [] buffer, final int offset, final int len, final int line,
192 final int col) throws ParseException {
193 logger.debug("Unexpected comment in {}:{}: {}", line, col,
194 buffer == null ? "<null>" : new String(buffer, offset, len));
198 public void handleCDATASection(final char @Nullable [] buffer, final int offset, final int len, final int line,
199 final int col) throws ParseException {
200 logger.debug("Unexpected CDATA in {}:{}: {}", line, col,
201 buffer == null ? "<null>" : new String(buffer, offset, len));
205 public void handleText(final char @Nullable [] buffer, final int offset, final int len, final int line,
206 final int col) throws ParseException {
207 if (buffer == null) {
211 if (this.parserState == ParserState.SELECT_OPTION) {
212 // logger.debug("Text {}:{}: {}", line, col, new String(buffer, offset, len));
213 StringBuilder sb = this.curOptionValue;
215 sb.append(buffer, offset, len);
217 } else if (this.parserState == ParserState.INIT && len == 1 && buffer[offset] == '\n') {
218 // single newline - ignore/drop it...
219 } else if (this.parserState == ParserState.INPUT) {
220 // this is a label next to the value input field - we currently have no use for it so
223 logger.debug("Error parsing options for {}: Unexpected Text {}:{}: (ctx: {} len: {}) '{}' ",
224 this.channelName, line, col, this.parserState, len, new String(buffer, offset, len));
229 public void handleXmlDeclaration(final @Nullable String version, final @Nullable String encoding,
230 final @Nullable String standalone, final int line, final int col) throws ParseException {
231 logger.debug("Unexpected XML Declaration {}:{}: {} {} {}", line, col, version, encoding, standalone);
235 public void handleProcessingInstruction(final @Nullable String target, final @Nullable String content,
236 final int line, final int col) throws ParseException {
237 logger.debug("Unexpected ProcessingInstruction {}:{}: {} {}", line, col, target, content);
241 protected ChangerX2Entry getParsedEntry() {
242 String addressFieldName = this.addressFieldName;
243 String address = this.address;
244 String optionFieldName = this.optionFieldName;
245 OptionType optionType = this.optionType;
246 if (address == null || addressFieldName == null || optionType == null || optionFieldName == null) {
249 return new ChangerX2Entry(addressFieldName, address, optionFieldName, optionType, this.options);