SearchFormParser.java
4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* Decompiled with CFR 0_118.
*/
package com.day.cq.searchpromote.xml.form;
import com.day.cq.searchpromote.SearchPromoteException;
import com.day.cq.searchpromote.xml.DOMParser;
import com.day.cq.searchpromote.xml.form.AutoComplete;
import com.day.cq.searchpromote.xml.form.Form;
import com.day.cq.searchpromote.xml.form.Input;
import com.day.cq.searchpromote.xml.form.SearchForm;
import com.day.cq.searchpromote.xml.form.Tnt;
import java.util.ArrayList;
import java.util.List;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class SearchFormParser
extends DOMParser {
private static final String AUTOCOMPLETE_ELEMENT = "autocomplete";
private static final String TNT_ELEMENT = "tnt";
private static final String FORM_ELEMENT = "form";
private static final String CSS_ELEMENT = "css";
private static final String ENABLED_ELEMENT = "enabled";
private static final String JAVASCRIPT_ELEMENT = "javascript";
private static final String FORMCONTENT_ELEMENT = "form-content";
private static final String NAME_ELEMENT = "name";
private static final String ID_ELEMENT = "id";
private static final String ACTION_ELEMENT = "action";
private static final String TYPE_ELEMENT = "type";
private static final String VALUE_ELEMENT = "value";
private static final String INPUTS_ELEMENT = "inputs";
public SearchForm parse(InputSource xml) throws SearchPromoteException {
Element root = this.parseXML(xml);
Element autocompleteElement = this.getElement(root, "autocomplete", false);
AutoComplete autocomplete = autocompleteElement != null ? this.parseAutocomplete(autocompleteElement) : null;
Element tntElement = this.getElement(root, "tnt", false);
Tnt tnt = tntElement != null ? this.parseTnt(tntElement) : null;
Element formElement = this.getElement(root, "form", true);
Form form = this.parseForm(formElement);
return new SearchForm(autocomplete, tnt, form);
}
protected AutoComplete parseAutocomplete(Element element) throws SearchPromoteException {
Boolean isEnabled = this.getEnabled(element);
String formContent = this.getElement(element, "form-content", true).getTextContent();
String javaScript = this.getElement(element, "javascript", true).getTextContent();
String css = this.getElement(element, "css", true).getTextContent();
return new AutoComplete(isEnabled, formContent, javaScript, css);
}
protected Tnt parseTnt(Element element) throws SearchPromoteException {
Boolean isEnabled = this.getEnabled(element);
String formContent = this.getElement(element, "form-content", true).getTextContent();
String javaScript = this.getElement(element, "javascript", true).getTextContent();
return new Tnt(isEnabled, formContent, javaScript);
}
protected Form parseForm(Element element) throws SearchPromoteException {
String name = this.getElement(element, "name", true).getTextContent();
String id = this.getElement(element, "id", true).getTextContent();
String action = this.getElement(element, "action", true).getTextContent();
Element inputs = this.getElement(element, "inputs", false);
List<Input> input = this.parseInputs(inputs);
return new Form(name, id, action, input);
}
protected List<Input> parseInputs(Element element) throws SearchPromoteException {
ArrayList<Input> inputs = new ArrayList<Input>();
NodeList list = element.getChildNodes();
for (int i = 0; i < list.getLength(); ++i) {
Node n = list.item(i);
if (n.getNodeType() != 1) continue;
String type = this.getElement((Element)n, "type", true).getTextContent();
Element valueEl = this.getElement((Element)n, "value", false);
String value = valueEl != null ? valueEl.getTextContent() : null;
Element idEl = this.getElement((Element)n, "id", false);
String id = idEl != null ? idEl.getTextContent() : null;
Element nameEl = this.getElement((Element)n, "name", false);
String name = nameEl != null ? nameEl.getTextContent() : null;
inputs.add(new Input(type, id, name, value));
}
return inputs;
}
private Boolean getEnabled(Element element) throws SearchPromoteException {
String enabled = this.getElement(element, "enabled", true).getTextContent();
Boolean isEnabled = Integer.parseInt(enabled) == 1 ? Boolean.TRUE : Boolean.FALSE;
return isEnabled;
}
}