Coverage report

  %line %branch
nl.toolforge.karma.core.manifest.ManifestLoader
83% 
85% 

 1  
 /*
 2  
 Karma core - Core of the Karma application
 3  
 Copyright (C) 2004  Toolforge <www.toolforge.nl>
 4  
 
 5  
 This library is free software; you can redistribute it and/or
 6  
 modify it under the terms of the GNU Lesser General Public
 7  
 License as published by the Free Software Foundation; either
 8  
 version 2.1 of the License, or (at your option) any later version.
 9  
 
 10  
 This library is distributed in the hope that it will be useful,
 11  
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  
 Lesser General Public License for more details.
 14  
 
 15  
 You should have received a copy of the GNU Lesser General Public
 16  
 License along with this library; if not, write to the Free Software
 17  
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 18  
 */
 19  
 package nl.toolforge.karma.core.manifest;
 20  
 
 21  
 import nl.toolforge.karma.core.boot.WorkingContext;
 22  
 import nl.toolforge.karma.core.manifest.digester.ModuleDescriptorCreationFactory;
 23  
 import org.apache.commons.digester.Digester;
 24  
 import org.apache.commons.logging.Log;
 25  
 import org.apache.commons.logging.LogFactory;
 26  
 import org.xml.sax.SAXException;
 27  
 
 28  
 import java.io.File;
 29  
 import java.io.FileInputStream;
 30  
 import java.io.FileNotFoundException;
 31  
 import java.io.IOException;
 32  
 import java.io.InputStream;
 33  
 import java.util.Iterator;
 34  
 
 35  
 /**
 36  
  * Factory class to create {@link nl.toolforge.karma.core.manifest.ManifestStructure} instances.
 37  
  *
 38  
  * @author D.A. Smedes
 39  
  * @version $Id: ManifestLoader.java,v 1.6 2004/11/10 22:33:25 asmedes Exp $
 40  
  */
 41  
 public final class ManifestLoader {
 42  
 
 43  208
   private static Log logger = LogFactory.getLog(ManifestLoader.class);
 44  
 
 45  130
   private WorkingContext workingContext = null;
 46  
 
 47  130
   public ManifestLoader(WorkingContext workingContext) {
 48  130
     this.workingContext = workingContext;
 49  130
   }
 50  
 
 51  
   /**
 52  
    * This method builds a tree of <code>ManifestStructure</code> instances, representing the root manifest and all its
 53  
    * included manifests.
 54  
    *
 55  
    * @param manifest The manifest name.
 56  
    */
 57  
   public ManifestStructure load(String manifest) throws ManifestException {
 58  
 
 59  286
     Digester digester = new Digester();
 60  
 
 61  
     // The <manifest>-element
 62  
     //
 63  286
     digester.addObjectCreate("manifest", ManifestStructure.class);
 64  286
     digester.addSetProperties("manifest");
 65  286
     digester.addCallMethod("manifest/description", "setDescription", 0);
 66  
 
 67  
     // All <module>-elements
 68  
     //
 69  286
     digester.addFactoryCreate("*/module", ModuleDescriptorCreationFactory.class);
 70  286
     digester.addSetProperties("*/module");
 71  286
     digester.addSetNext("*/module", "addModule", "nl.toolforge.karma.core.module.ModuleDigester");
 72  
 
 73  
     // All <include-manifest>-elements
 74  
     //
 75  286
     digester.addObjectCreate("*/include-manifest", "nl.toolforge.karma.core.manifest.ManifestStructure");
 76  286
     digester.addSetProperties("*/include-manifest");
 77  286
     digester.addSetNext("*/include-manifest", "addChild");
 78  
 
 79  286
     ManifestStructure structure = null;
 80  
     try {
 81  286
       structure = (ManifestStructure) digester.parse(getManifestFileAsStream(manifest));
 82  63
       structure.setName(manifest);
 83  0
     } catch (IOException e) {
 84  38
       throw new ManifestException(e, ManifestException.MANIFEST_LOAD_ERROR, class="keyword">new Object[]{manifest});
 85  52
     } catch (SAXException e) {
 86  52
       if (e.getException() instanceof Manclass="keyword">ifestException) {
 87  14
         throw new ManifestException(
 88  
             ((ManifestException) e.getException()).getErrorCode(),
 89  
             ((ManifestException) e.getException()).getMessageArguments()
 90  
         );
 91  
       } else {
 92  0
         throw new ManifestException(e, ManifestException.MANIFEST_LOAD_ERROR, class="keyword">new Object[]{manifest});
 93  171
       }
 94  63
     }
 95  171
 
 96  63
     Iterator i = structure.getChilds().values().iterator();
 97  285
 
 98  219
     while (i.hasNext()) {
 99  156
       String childName = ((ManifestStructure) i.next()).getName();
 100  156
       ManifestStructure child = load(childName);
 101  42
       structure.getChild(childName).update(child);
 102  
     }
 103  171
 
 104  63
     return structure;
 105  
   }
 106  
 
 107  
   private InputStream getManifestFileAsStream(String id) throws ManifestException {
 108  
 
 109  209
     try {
 110  77
       String fileName = (id.endsWith(".xml") ? id : id.concat(".xml"));
 111  209
 
 112  77
       if (fileName.endsWith(File.separator)) {
 113  0
         fileName = fileName.substring(0, fileName.length() - 1);
 114  209
       }
 115  77
       fileName = fileName.substring(fileName.lastIndexOf(File.separator) + 1);
 116  209
 
 117  77
       String mStorePath = workingContext.getConfiguration().getManifestStore().getModule().getBaseDir().getPath();
 118  209
 
 119  77
       logger.debug("Loading manifest `" + fileName + "` from `" + mStorePath + File.separator + fileName + "`.");
 120  99
 
 121  77
       return new FileInputStream(mStorePath + File.separator + fileName);
 122  
 
 123  0
     } catch (FileNotFoundException f) {
 124  0
       throw new ManifestException(f, ManifestException.MANIFEST_FILE_NOT_FOUND, class="keyword">new Object[]{id});
 125  0
     } catch (NullPointerException n) {
 126  0
       throw new ManifestException(n, ManifestException.MANIFEST_FILE_NOT_FOUND, class="keyword">new Object[]{id});
 127  
     }
 128  
   }
 129  
 
 130  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.