Coverage Report - de.smartics.ci.config.load.LoaderPlanLoader
 
Classes in this File Line Coverage Branch Coverage Complexity
LoaderPlanLoader
0%
0/25
0%
0/4
2.333
 
 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  0
   {
 60  0
     this.mavenConfig = mavenConfig;
 61  0
   }
 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  0
     Arguments.checkNotNull("inputSource", inputSource);
 86  
 
 87  0
     final List<LoaderPlan> loaderPlans = new ArrayList<LoaderPlan>();
 88  
 
 89  0
     final SAXBuilder builder = new SAXBuilder();
 90  
     try
 91  
     {
 92  0
       final Document ciConfigXml = builder.build(inputSource);
 93  0
       load(loaderPlans, ciConfigXml);
 94  
     }
 95  0
     catch (final Exception e)
 96  
     {
 97  0
       throw new IllegalArgumentException("Cannot load loader plan from '"
 98  
                                          + inputSource.getSystemId() + "'.", e);
 99  0
     }
 100  
 
 101  0
     return loaderPlans;
 102  
   }
 103  
 
 104  
   @SuppressWarnings("unchecked")
 105  
   private void load(final List<LoaderPlan> loaderPlans,
 106  
       final Document ciConfigXml) throws JDOMException
 107  
   {
 108  0
     final String xPathString = "ci-config/import-definitions/import-definition";
 109  0
     final List<Element> importDefinitions =
 110  
         XPath.selectNodes(ciConfigXml, xPathString);
 111  0
     for (final Element importDefinition : importDefinitions)
 112  
     {
 113  0
       final String id = importDefinition.getAttributeValue("id");
 114  0
       final List<Element> imports = importDefinition.getChildren("import");
 115  
 
 116  0
       final LoaderPlan plan = new LoaderPlan(id, mavenConfig);
 117  0
       for (final Element singleImport : imports)
 118  
       {
 119  0
         final String configurationName = singleImport.getTextTrim();
 120  0
         plan.addConfigurationName(configurationName);
 121  0
       }
 122  0
       loaderPlans.add(plan);
 123  0
     }
 124  0
   }
 125  
 
 126  
   // --- object basics --------------------------------------------------------
 127  
 
 128  
 }