/
ParserContext.java
82 lines (69 loc) · 2.41 KB
/
ParserContext.java
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
package org.jboss.windup.config.parser;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jboss.windup.config.exception.ConfigurationException;
import org.jboss.windup.util.Annotations;
import static org.joox.JOOX.$;
import org.ocpsoft.common.services.ServiceLoader;
import org.ocpsoft.common.util.Iterators;
import org.ocpsoft.rewrite.config.ConfigurationBuilder;
import org.ocpsoft.rewrite.config.ConfigurationRuleBuilderPerform;
import org.ocpsoft.rewrite.config.ConfigurationRuleParameterWhere;
import org.w3c.dom.Element;
public class ParserContext
{
private final Map<HandlerId, ElementHandler<?>> handlers = new HashMap<>();
private final ConfigurationBuilder builder;
private ConfigurationRuleBuilderPerform rule;
private ConfigurationRuleParameterWhere where;
@SuppressWarnings({ "rawtypes", "unchecked" })
public ParserContext(ConfigurationBuilder builder)
{
List<ElementHandler> loadedHandlers = Iterators.asList(ServiceLoader.load(ElementHandler.class));
for (ElementHandler handler : loadedHandlers)
{
NamespaceElementHandler annotation = Annotations.getAnnotation(handler.getClass(),
NamespaceElementHandler.class);
if (annotation != null)
{
handlers.put(new HandlerId(annotation.namespace(), annotation.elementName()), handler);
}
}
this.builder = builder;
}
@SuppressWarnings("unchecked")
public <T> T processElement(Element element) throws ConfigurationException
{
String namespace = $(element).namespaceURI();
String tagName = $(element).tag();
ElementHandler<?> handler = handlers.get(new HandlerId(namespace, tagName));
if (handler != null)
{
Object o = handler.processElement(this, element);
return (T) o;
}
throw new ConfigurationException("No Handler registered for element named [" + tagName
+ "] in namespace: [" + namespace + "]");
}
public ConfigurationBuilder getBuilder()
{
return builder;
}
public void setRuleBuilder(ConfigurationRuleBuilderPerform perform)
{
this.rule = perform;
}
public ConfigurationRuleBuilderPerform getRule()
{
return rule;
}
public void setWhereBuilder(ConfigurationRuleParameterWhere where)
{
this.where = where;
}
public ConfigurationRuleParameterWhere getWhere()
{
return where;
}
}