View Javadoc

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    private static Log logger = LogFactory.getLog(ManifestLoader.class);
44  
45    private WorkingContext workingContext = null;
46  
47    public ManifestLoader(WorkingContext workingContext) {
48      this.workingContext = workingContext;
49    }
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      Digester digester = new Digester();
60  
61      // The <manifest>-element
62      //
63      digester.addObjectCreate("manifest", ManifestStructure.class);
64      digester.addSetProperties("manifest");
65      digester.addCallMethod("manifest/description", "setDescription", 0);
66  
67      // All <module>-elements
68      //
69      digester.addFactoryCreate("*/module", ModuleDescriptorCreationFactory.class);
70      digester.addSetProperties("*/module");
71      digester.addSetNext("*/module", "addModule", "nl.toolforge.karma.core.module.ModuleDigester");
72  
73      // All <include-manifest>-elements
74      //
75      digester.addObjectCreate("*/include-manifest", "nl.toolforge.karma.core.manifest.ManifestStructure");
76      digester.addSetProperties("*/include-manifest");
77      digester.addSetNext("*/include-manifest", "addChild");
78  
79      ManifestStructure structure = null;
80      try {
81        structure = (ManifestStructure) digester.parse(getManifestFileAsStream(manifest));
82        structure.setName(manifest);
83      } catch (IOException e) {
84        throw new ManifestException(e, ManifestException.MANIFEST_LOAD_ERROR, new Object[]{manifest});
85      } catch (SAXException e) {
86        if (e.getException() instanceof ManifestException) {
87          throw new ManifestException(
88              ((ManifestException) e.getException()).getErrorCode(),
89              ((ManifestException) e.getException()).getMessageArguments()
90          );
91        } else {
92          throw new ManifestException(e, ManifestException.MANIFEST_LOAD_ERROR, new Object[]{manifest});
93        }
94      }
95  
96      Iterator i = structure.getChilds().values().iterator();
97  
98      while (i.hasNext()) {
99        String childName = ((ManifestStructure) i.next()).getName();
100       ManifestStructure child = load(childName);
101       structure.getChild(childName).update(child);
102     }
103 
104     return structure;
105   }
106 
107   private InputStream getManifestFileAsStream(String id) throws ManifestException {
108 
109     try {
110       String fileName = (id.endsWith(".xml") ? id : id.concat(".xml"));
111 
112       if (fileName.endsWith(File.separator)) {
113         fileName = fileName.substring(0, fileName.length() - 1);
114       }
115       fileName = fileName.substring(fileName.lastIndexOf(File.separator) + 1);
116 
117       String mStorePath = workingContext.getConfiguration().getManifestStore().getModule().getBaseDir().getPath();
118 
119       logger.debug("Loading manifest `" + fileName + "` from `" + mStorePath + File.separator + fileName + "`.");
120 
121       return new FileInputStream(mStorePath + File.separator + fileName);
122 
123     } catch (FileNotFoundException f) {
124       throw new ManifestException(f, ManifestException.MANIFEST_FILE_NOT_FOUND, new Object[]{id});
125     } catch (NullPointerException n) {
126       throw new ManifestException(n, ManifestException.MANIFEST_FILE_NOT_FOUND, new Object[]{id});
127     }
128   }
129 
130 }