public void instantiate(Publication template, String newPublicationId, String name)
throws Exception {
SourceResolver resolver = null;
Source publicationsSource = null;
ModifiableSource metaSource = null;
ModifiableSource configSource = null;
try {
resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE);
publicationsSource = resolver.resolveURI("context://"
+ PublicationImpl.PUBLICATION_PREFIX_URI);
String publicationsUri = publicationsSource.getURI();
for (int i = 0; i < sourcesToCopy.length; i++) {
String source = sourcesToCopy[i];
if (source.endsWith("/")) {
copyDirSource(template, newPublicationId, resolver, publicationsUri, source);
} else {
copySource(template, newPublicationId, resolver, publicationsUri, source);
}
}
metaSource = (ModifiableSource) resolver.resolveURI(publicationsUri + "/"
+ newPublicationId + "/publication.xml");
Document metaDoc = DocumentHelper.readDocument(metaSource.getInputStream());
NamespaceHelper helper = new NamespaceHelper(
"http://apache.org/cocoon/lenya/publication/1.0", "lenya", metaDoc);
Element nameElement = helper.getFirstChild(metaDoc.getDocumentElement(), "name");
DocumentHelper.setSimpleElementText(nameElement, name);
save(metaDoc, metaSource);
configSource = (ModifiableSource) resolver.resolveURI(publicationsUri + "/"
+ newPublicationId + "/config/publication.xconf");
DefaultConfiguration config = (DefaultConfiguration) new DefaultConfigurationBuilder()
.build(configSource.getInputStream());
DefaultConfiguration templatesConfig = new DefaultConfiguration("templates");
DefaultConfiguration templateConfig = new DefaultConfiguration("template");
templateConfig.setAttribute("id", template.getId());
templatesConfig.addChild(templateConfig);
config.addChild(templatesConfig);
OutputStream oStream = configSource.getOutputStream();
new DefaultConfigurationSerializer().serialize(oStream, config);
if (oStream != null) {
oStream.flush();
try {
oStream.close();