]> git.basschouten.com Git - openhab-addons.git/blob
3270c1e5d22fb4909e472e410436daa881a0787d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.tacmi.internal.schema;
14
15 import java.util.LinkedHashMap;
16 import java.util.Map;
17
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;
25
26 /**
27  * The {@link ApiPageParser} class parses the 'changerx2' page from the CMI and
28  * maps it to the results
29  *
30  * @author Christian Niessner - Initial contribution
31  */
32 @NonNullByDefault
33 public class ChangerX2Parser extends AbstractSimpleMarkupHandler {
34
35     private final Logger logger = LoggerFactory.getLogger(ChangerX2Parser.class);
36
37     static enum ParserState {
38         INIT,
39         INPUT,
40         INPUT_DATA,
41         SELECT,
42         SELECT_OPTION,
43         UNKNOWN
44     }
45
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;
55
56     public ChangerX2Parser(String channelName) {
57         super();
58         this.options = new LinkedHashMap<>();
59         this.channelName = channelName;
60     }
61
62     @Override
63     public void handleDocumentStart(final long startTimeNanos, final int line, final int col) throws ParseException {
64         this.parserState = ParserState.INIT;
65         this.options.clear();
66     }
67
68     @Override
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);
73         }
74     }
75
76     @Override
77     @NonNullByDefault({})
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
81         logger.debug("Error parsing options for {}: Unexpected StandaloneElement in {}{}: {} [{}]", channelName, line,
82                 col, elementName, attributes);
83     }
84
85     @Override
86     @NonNullByDefault({})
87     public void handleOpenElement(final String elementName, final Map<String, String> attributes, final int line,
88             final int col) throws ParseException {
89
90         String id = attributes == null ? null : attributes.get("id");
91
92         if (this.parserState == ParserState.INIT && "input".equals(elementName) && "changeadr".equals(id)) {
93             this.parserState = ParserState.INPUT;
94             if (attributes == null) {
95                 this.address = null;
96                 this.addressFieldName = null;
97             } else {
98                 this.addressFieldName = attributes.get("name");
99                 this.address = attributes.get("value");
100             }
101         } else if ((this.parserState == ParserState.INIT || this.parserState == ParserState.INPUT)
102                 && "select".equals(elementName)) {
103             this.parserState = ParserState.SELECT;
104             this.optionFieldName = attributes == null ? null : attributes.get("name");
105         } else if ((this.parserState == ParserState.INIT || this.parserState == ParserState.INPUT)
106                 && "br".equals(elementName)) {
107             // ignored
108         } else if ((this.parserState == ParserState.INIT || this.parserState == ParserState.INPUT)
109                 && "input".equals(elementName) && "changeto".equals(id)) {
110             this.parserState = ParserState.INPUT_DATA;
111             if (attributes != null) {
112                 this.optionFieldName = attributes.get("name");
113                 String type = attributes.get("type");
114                 if ("number".equals(type)) {
115                     this.optionType = OptionType.NUMBER;
116                     // we transfer the limits from the input elemnt...
117                     this.options.put(ChangerX2Entry.NUMBER_MIN, attributes.get(ChangerX2Entry.NUMBER_MIN));
118                     this.options.put(ChangerX2Entry.NUMBER_MAX, attributes.get(ChangerX2Entry.NUMBER_MAX));
119                     this.options.put(ChangerX2Entry.NUMBER_STEP, attributes.get(ChangerX2Entry.NUMBER_STEP));
120                 } else {
121                     logger.warn("Error parsing options for {}: Unhandled input field in {}:{}: {}", channelName, line,
122                             col, attributes);
123                 }
124             }
125         } else if (this.parserState == ParserState.SELECT && "option".equals(elementName)) {
126             this.parserState = ParserState.SELECT_OPTION;
127             this.optionType = OptionType.SELECT;
128             this.curOptionValue = new StringBuilder();
129             this.curOptionId = attributes == null ? null : attributes.get("value");
130         } else {
131             logger.debug("Error parsing options for {}: Unexpected OpenElement in {}:{}: {} [{}]", channelName, line,
132                     col, elementName, attributes);
133         }
134     }
135
136     @Override
137     public void handleCloseElement(final @Nullable String elementName, final int line, final int col)
138             throws ParseException {
139         if (this.parserState == ParserState.INPUT && "input".equals(elementName)) {
140             this.parserState = ParserState.INIT;
141         } else if (this.parserState == ParserState.SELECT && "select".equals(elementName)) {
142             this.parserState = ParserState.INIT;
143         } else if (this.parserState == ParserState.SELECT_OPTION && "option".equals(elementName)) {
144             this.parserState = ParserState.SELECT;
145             StringBuilder sb = this.curOptionValue;
146             String value = sb != null && sb.length() > 0 ? sb.toString().trim() : null;
147             this.curOptionValue = null;
148             String id = this.curOptionId;
149             this.curOptionId = null;
150             if (value != null) {
151                 if (id == null || id.trim().isEmpty()) {
152                     logger.debug("Error parsing options for {}: Got option with empty 'value' in {}:{}: [{}]",
153                             channelName, line, col, value);
154                     return;
155                 }
156                 // we use the value as key and the id as value, as we have to map from the value to the id...
157                 @Nullable
158                 String prev = this.options.put(value, id);
159                 if (prev != null && !prev.equals(value)) {
160                     logger.debug("Error parsing options for {}: Got duplicate options in {}:{} for {}: {} and {}",
161                             channelName, line, col, value, prev, id);
162                 }
163             }
164         } else {
165             logger.debug("Error parsing options for {}: Unexpected CloseElement in {}:{}: {}", channelName, line, col,
166                     elementName);
167         }
168     }
169
170     @Override
171     public void handleAutoCloseElement(final @Nullable String elementName, final int line, final int col)
172             throws ParseException {
173         logger.debug("Unexpected AutoCloseElement in {}:{}: {}", line, col, elementName);
174     }
175
176     @Override
177     public void handleUnmatchedCloseElement(final @Nullable String elementName, final int line, final int col)
178             throws ParseException {
179         logger.debug("Unexpected UnmatchedCloseElement in {}:{}: {}", line, col, elementName);
180     }
181
182     @Override
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,
187                 internalSubset);
188     }
189
190     @Override
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, new String(buffer, offset, len));
194     }
195
196     @Override
197     public void handleCDATASection(final char @Nullable [] buffer, final int offset, final int len, final int line,
198             final int col) throws ParseException {
199         logger.debug("Unexpected CDATA in {}:{}: {}", line, col, new String(buffer, offset, len));
200     }
201
202     @Override
203     public void handleText(final char @Nullable [] buffer, final int offset, final int len, final int line,
204             final int col) throws ParseException {
205
206         if (buffer == null) {
207             return;
208         }
209
210         if (this.parserState == ParserState.SELECT_OPTION) {
211             // logger.debug("Text {}:{}: {}", line, col, new String(buffer, offset, len));
212             StringBuilder sb = this.curOptionValue;
213             if (sb != null) {
214                 sb.append(buffer, offset, len);
215             }
216         } else if (this.parserState == ParserState.INIT && len == 1 && buffer[offset] == '\n') {
217             // single newline - ignore/drop it...
218         } else if (this.parserState == ParserState.INPUT) {
219             // this is a label next to the value input field - we currently have no use for it so
220             // it's dropped...
221         } else {
222             logger.debug("Error parsing options for {}: Unexpected Text {}:{}: (ctx: {} len: {}) '{}' ",
223                     this.channelName, line, col, this.parserState, len, new String(buffer, offset, len));
224         }
225     }
226
227     @Override
228     public void handleXmlDeclaration(final @Nullable String version, final @Nullable String encoding,
229             final @Nullable String standalone, final int line, final int col) throws ParseException {
230         logger.debug("Unexpected XML Declaration {}:{}: {} {} {}", line, col, version, encoding, standalone);
231     }
232
233     @Override
234     public void handleProcessingInstruction(final @Nullable String target, final @Nullable String content,
235             final int line, final int col) throws ParseException {
236         logger.debug("Unexpected ProcessingInstruction {}:{}: {} {}", line, col, target, content);
237     }
238
239     @Nullable
240     protected ChangerX2Entry getParsedEntry() {
241         String addressFieldName = this.addressFieldName;
242         String address = this.address;
243         String optionFieldName = this.optionFieldName;
244         OptionType optionType = this.optionType;
245         if (address == null || addressFieldName == null || optionType == null || optionFieldName == null) {
246             return null;
247         }
248         return new ChangerX2Entry(addressFieldName, address, optionFieldName, optionType, this.options);
249     }
250 }