1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.ci.config.hudson;
17
18 import static de.smartics.ci.config.utils.JDomUtils.calcXPath;
19
20 import org.jdom.Element;
21 import org.jdom.JDOMException;
22 import org.jdom.Text;
23 import org.jdom.xpath.XPath;
24
25
26
27
28 public enum HudsonConfigElement
29 {
30
31
32
33
34
35 ENTRY("entry")
36 {
37 @Override
38 public Element findElementWithin(final Element rootElement,
39 final Element element, final Element targetRootElement)
40 throws JDOMException
41 {
42 final XPath xpath = XPath.newInstance("string/text()");
43 final Text entryIdText = (Text) xpath.selectSingleNode(element);
44 if (entryIdText == null)
45 {
46 return null;
47 }
48
49 final String id = entryIdText.getTextTrim();
50 final String xPathString =
51 "/maven2-moduleset/project-properties/entry/string[text()='" + id
52 + "']";
53 final XPath xPath = XPath.newInstance(xPathString);
54 final Object object = xPath.selectSingleNode(targetRootElement);
55 if (object != null)
56 {
57 final Element parent = ((Element) object).getParentElement();
58 return parent;
59 }
60 return null;
61 }
62 };
63
64
65
66
67
68
69
70
71
72
73 private final String name;
74
75
76
77
78
79
80
81
82 private HudsonConfigElement(final String name)
83 {
84 this.name = name;
85 }
86
87
88
89
90
91
92
93
94
95
96
97
98 public String getName()
99 {
100 return name;
101 }
102
103
104
105
106
107
108
109
110
111
112
113 public static HudsonConfigElement valueFor(final String elementName)
114 throws IllegalArgumentException
115 {
116 for (final HudsonConfigElement element : values())
117 {
118 if (element.name.equals(elementName))
119 {
120 return element;
121 }
122 }
123
124 throw new IllegalArgumentException("Cannot find element for name '"
125 + elementName + "'.");
126 }
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144 public static Element findElement(final Element rootElement,
145 final Element element, final Element targetRootElement)
146 throws JDOMException
147 {
148 final String elementName = element.getName();
149 try
150 {
151 final HudsonConfigElement config = valueFor(elementName);
152 return config.findElementWithin(rootElement, element, targetRootElement);
153 }
154 catch (final IllegalArgumentException e)
155 {
156 return defaultFindElement(rootElement, element, targetRootElement);
157 }
158 }
159
160 private static Element defaultFindElement(final Element rootElement,
161 final Element element, final Element targetRootElement)
162 throws JDOMException
163 {
164 final String path = calcXPath(rootElement, element);
165 final XPath xPath = XPath.newInstance(path);
166 final Object object = xPath.selectSingleNode(targetRootElement);
167 return (Element) object;
168 }
169
170 abstract Element findElementWithin(final Element rootElement,
171 final Element element, final Element targetRootElement)
172 throws JDOMException;
173
174
175
176 }