| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 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 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 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 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
public ManifestStructure load(String manifest) throws ManifestException { |
| 58 |
|
|
| 59 |
286 |
Digester digester = new Digester(); |
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
286 |
digester.addObjectCreate("manifest", ManifestStructure.class); |
| 64 |
286 |
digester.addSetProperties("manifest"); |
| 65 |
286 |
digester.addCallMethod("manifest/description", "setDescription", 0); |
| 66 |
|
|
| 67 |
|
|
| 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 |
|
|
| 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 |
|
} |