View Javadoc

1   /*
2    * Copyright 2012 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Loads {@link LoaderPlan}s from XML files.
34   */
35  public final class LoaderPlanLoader
36  {
37    // ********************************* Fields *********************************
38  
39    // --- constants ------------------------------------------------------------
40  
41    // --- members --------------------------------------------------------------
42  
43    /**
44     * The Maven configuration to add to every generated loader.
45     */
46    private final MavenConfig mavenConfig;
47  
48    // ****************************** Initializer *******************************
49  
50    // ****************************** Constructors ******************************
51  
52    /**
53     * Default constructor.
54     *
55     * @param mavenConfig the Maven configuration to add to every generated
56     *          loader.
57     */
58    public LoaderPlanLoader(final MavenConfig mavenConfig)
59    {
60      this.mavenConfig = mavenConfig;
61    }
62  
63    // ****************************** Inner Classes *****************************
64  
65    // ********************************* Methods ********************************
66  
67    // --- init -----------------------------------------------------------------
68  
69    // --- get&set --------------------------------------------------------------
70  
71    // --- business -------------------------------------------------------------
72  
73    /**
74     * Loads loader plans read from the given stream.
75     *
76     * @param inputSource contains an <code>ci-config</code> XML document with
77     *          loader information.
78     * @return the loader information from the stream. May be empty, if no loader
79     *         information has been found, but is never <code>null</code>.
80     * @throws NullArgumentException if {@code inputSource} is <code>null</code>.
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   // --- object basics --------------------------------------------------------
127 
128 }