1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.ci.config.load;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.jdom.Document;
22 import org.jdom.Element;
23 import org.jdom.JDOMException;
24 import org.jdom.input.SAXBuilder;
25 import org.jdom.xpath.XPath;
26 import org.xml.sax.InputSource;
27
28 import de.smartics.ci.config.maven.MavenConfig;
29 import de.smartics.util.lang.Arguments;
30 import de.smartics.util.lang.NullArgumentException;
31
32
33
34
35 public final class LoaderPlanLoader
36 {
37
38
39
40
41
42
43
44
45
46 private final MavenConfig mavenConfig;
47
48
49
50
51
52
53
54
55
56
57
58 public LoaderPlanLoader(final MavenConfig mavenConfig)
59 {
60 this.mavenConfig = mavenConfig;
61 }
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82 public List<LoaderPlan> load(final InputSource inputSource)
83 throws NullArgumentException
84 {
85 Arguments.checkNotNull("inputSource", inputSource);
86
87 final List<LoaderPlan> loaderPlans = new ArrayList<LoaderPlan>();
88
89 final SAXBuilder builder = new SAXBuilder();
90 try
91 {
92 final Document ciConfigXml = builder.build(inputSource);
93 load(loaderPlans, ciConfigXml);
94 }
95 catch (final Exception e)
96 {
97 throw new IllegalArgumentException("Cannot load loader plan from '"
98 + inputSource.getSystemId() + "'.", e);
99 }
100
101 return loaderPlans;
102 }
103
104 @SuppressWarnings("unchecked")
105 private void load(final List<LoaderPlan> loaderPlans,
106 final Document ciConfigXml) throws JDOMException
107 {
108 final String xPathString = "ci-config/import-definitions/import-definition";
109 final List<Element> importDefinitions =
110 XPath.selectNodes(ciConfigXml, xPathString);
111 for (final Element importDefinition : importDefinitions)
112 {
113 final String id = importDefinition.getAttributeValue("id");
114 final List<Element> imports = importDefinition.getChildren("import");
115
116 final LoaderPlan plan = new LoaderPlan(id, mavenConfig);
117 for (final Element singleImport : imports)
118 {
119 final String configurationName = singleImport.getTextTrim();
120 plan.addConfigurationName(configurationName);
121 }
122 loaderPlans.add(plan);
123 }
124 }
125
126
127
128 }