1
/*****************************************************************************
2
* Copyright (C) 1999 The Apache Software Foundation. All rights reserved. *
3
* ------------------------------------------------------------------------- *
4
* This software is published under the terms of the Apache Software License *
5
* version 1.1, a copy of wich has been included with this distribution in *
7
*****************************************************************************/
8
package org.apache.stylebook;
10
import java.io.IOException;
11
import java.io.OutputStream;
13
import java.util.Enumeration;
14
import java.util.Hashtable;
15
import org.w3c.dom.Document;
16
import org.w3c.dom.Element;
17
import org.w3c.dom.Node;
18
import org.w3c.dom.NodeList;
23
* @author <a href="mailto:pier@apache.org">Pierpaolo Fumagalli</a>
24
* @author Copyright 1999 © <a href="http://www.apache.org">The Apache
25
* Software Foundation</a>. All rights reserved.
26
* @version CVS $Revision: 313221 $ $Date: 1999-12-01 19:39:14 +0100 (ons, 01 dec 1999) $
28
public class Project {
29
private Hashtable entries=new Hashtable();
30
private Engine engine=null;
31
private URL urlbase=null;
33
public Project(Engine engine, Document project, Parameters parambase, URL u)
34
throws LoadingException, IOException {
37
Element root=project.getDocumentElement();
38
if(!root.getTagName().equals("project"))
39
throw new LoadingException("Project does not start with <project>");
40
NodeList l=root.getChildNodes();
41
Parameters parameters=new NodeListParameters(l).merge(parambase);
42
processNodeList(l,this.urlbase,parameters);
45
private void processNodeList(NodeList l, URL urlbase, Parameters parambase)
46
throws LoadingException, IOException {
47
for(int x=0;x<l.getLength();x++) {
48
if(l.item(x).getNodeType()!=Node.ELEMENT_NODE) continue;
49
Element e=(Element)l.item(x);
50
String name=e.getTagName();
51
if(name.equals("parameter")) continue;
52
else if(name.equals("create")) setCreateEntry(e,urlbase,parambase);
53
else if(name.equals("resource")) setResourceEntry(e,urlbase);
54
else if(name.equals("process")) processEntry(e,urlbase,parambase);
55
else throw new LoadingException("Invalid element <"+name+">");
59
public Enumeration getEntryNames() {
60
return(this.entries.keys());
63
public void create(String name, OutputStream out)
64
throws IOException, CreationException {
65
this.engine.log(this,"Creating \""+name+"\"");
66
Entry e=(Entry)this.entries.get(name);
67
if(e==null) throw new CreationException("No Entry \""+name+"\"");
71
private void setCreateEntry(Element e, URL urlbase, Parameters parambase)
72
throws LoadingException, IOException {
73
URL source=new URL(urlbase,e.getAttribute("source"));
74
String target=e.getAttribute("target");
75
Entry entry=new BasicEntry(this.engine,source,target,e,parambase);
76
Entry old=(Entry)this.entries.put(target,entry);
77
if(old!=null) if(!old.equals(entry))
78
throw new LoadingException("Duplicate Entry \""+target+"\"");
81
private void setResourceEntry(Element e, URL urlbase)
82
throws LoadingException, IOException {
83
URL source=new URL(urlbase,e.getAttribute("source"));
84
String target=e.getAttribute("target");
85
Entry entry=new ResourceEntry(this.engine,source,target);
86
Entry old=(Entry)this.entries.put(target,entry);
87
if(old!=null) if(!old.equals(entry))
88
throw new LoadingException("Duplicate Entry \""+target+"\"");
91
private void processEntry(Element e, URL urlbase, Parameters parambase)
92
throws LoadingException, IOException {
93
// Get the producer and the source, create a temporary context
94
String prod=e.getAttribute("producer");
95
URL source=new URL(urlbase,e.getAttribute("source"));
96
CreationContext ctx=new BasicContext(source,"");
98
// Try to load the producer
99
this.engine.debug(this,"Processing Source=\""+source+"\" ["+prod+"]");
100
Producer producer=this.engine.getProducer(prod);
102
throw new LoadingException("Invalid Producer \""+prod+"\"");
103
// Get local parameters
104
NodeList l=e.getChildNodes();
105
ctx.merge(new NodeListParameters(l));
106
// Try to create the subproject
109
// Produce the document
110
doc=producer.produce(ctx);
111
// For every processor, generate the project
112
for(int x=0;x<l.getLength();x++) {
113
if(l.item(x).getNodeType()!=Node.ELEMENT_NODE) continue;
114
Element el=(Element)l.item(x);
115
if(el.getTagName().equals("parameter")) continue;
116
if(!el.getTagName().equals("processor"))
117
throw new LoadingException("Invalid <"+el.getTagName()+">");
118
Processor proc=this.engine.getProcessor(el.getAttribute("name"));
120
throw new LoadingException("No Processr \""+proc+"\"");
121
Parameters p=new NodeListParameters(el.getChildNodes()).merge(ctx);
122
doc=proc.process(doc,ctx,p);
124
} catch (CreationException x) {
125
x.printStackTrace(System.out);
126
throw new LoadingException("Processing Entry ("+x.getMessage()+")");
128
// Check for proper document
129
if (doc==null) return;
130
// Do as a top-level project recursively
131
Element root=doc.getDocumentElement();
132
if(!root.getTagName().equals("project"))
133
throw new LoadingException("Result of process is not <project>");
134
l=root.getChildNodes();
135
Parameters p=new NodeListParameters(l).merge(ctx);
136
processNodeList(l,source,p);