001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019: package de.schlund.pfixcore.util;
020:
021: import java.io.File;
022:
023: import javax.xml.parsers.DocumentBuilder;
024: import javax.xml.parsers.DocumentBuilderFactory;
025:
026: import org.apache.tools.ant.BuildException;
027: import org.apache.tools.ant.Project;
028: import org.w3c.dom.Document;
029: import org.w3c.dom.Element;
030: import org.w3c.dom.NodeList;
031:
032: /**
033: *
034: * the following attributes have to be set when calling this task:
035: * <ul>
036: * <li>projects: relative (to ${dir.projects}) path to the projects.xml file </li>
037: * <li>corewebxml: relative (to ${dir.projects}) path to the core web.xml file,
038: * which defines a minimum web.xml file</li>
039: * </ul>
040: *
041: * @author <a href="mailto:benjamin@schlund.de">Benjamin Reitzammer</a>
042: * @version $Id: XsltWebXmlTask.java 3058 2007-02-12 12:08:46Z mtld $
043: */
044: public class XsltWebXmlTask extends XsltGenericTask {
045:
046: protected DocumentBuilderFactory dbFactory;
047:
048: /** contains the location of projects.xml file, relative to srcdirResolved
049: */
050: protected String projects;
051:
052: /** contains the location of core web.xml template, which is used, when a
053: * project has no custom web.xml specified, relative to srcdirResolved
054: */
055: protected String corewebxml;
056:
057: public void init() throws BuildException {
058: super .init();
059:
060: this .dbFactory = DocumentBuilderFactory.newInstance();
061: dbFactory.setNamespaceAware(true);
062: dbFactory.setValidating(false);
063: }
064:
065: protected void doTransformations() {
066: StringBuffer tmp = new StringBuffer(100);
067: tmp.append("Created web.xml for ");
068: int created = 0;
069: try {
070: if (infile != null || outfile != null) {
071: throw new BuildException("don't use " + ATTR_INFILE
072: + "/" + ATTR_OUTFILE + ", use " + ATTR_SRCDIR
073: + "/" + ATTR_DESTDIR + " etc. instead");
074: }
075: if (projects == null || "".equals(projects)
076: || corewebxml == null || "".equals(corewebxml)) {
077: throw new BuildException(
078: "Attributes 'projects' and 'corewebxml' are not allowed to be null or empty");
079: }
080:
081: File coreWebXml = new File(srcdirResolved, corewebxml);
082: if (!coreWebXml.exists() || !coreWebXml.canRead()) {
083: throw new BuildException("File '" + corewebxml
084: + "' can't be found or can't be read");
085: }
086:
087: DocumentBuilder domp;
088: Document doc;
089: File projectsFile = new File(srcdirResolved, projects);
090: try {
091: log("Parsing " + projectsFile, Project.MSG_DEBUG);
092: domp = dbFactory.newDocumentBuilder();
093: doc = domp.parse(projectsFile);
094: } catch (Exception e) {
095: throw new BuildException("Could not parse file "
096: + projectsFile, e);
097: }
098:
099: int count;
100: NodeList nl = doc.getElementsByTagName("project");
101: for (int i = 0; i < nl.getLength(); i++) {
102: Element currproject = (Element) nl.item(i);
103: String name = currproject.getAttribute("name");
104: log("found project " + name + " in " + projectsFile,
105: Project.MSG_DEBUG);
106:
107: File cusWebXml = new File(srcdirResolved + "/" + name
108: + "/conf", "web.xml");
109: if (cusWebXml.exists() && cusWebXml.canRead()) {
110: in = cusWebXml;
111: } else {
112: in = coreWebXml;
113: }
114:
115: log("Using " + in
116: + " as sourcefile for web.xml transformation",
117: Project.MSG_DEBUG);
118: getTransformer().setParameter(
119: new XsltParam("prjname", name));
120: getTransformer().setParameter(
121: new XsltParam("projectsxmlfile", projectsFile
122: .getPath()));
123: String outdir = "webapps/" + name + "/WEB-INF";
124: File webinfdir = new File(getDestdir(), outdir);
125: webinfdir.mkdirs();
126:
127: NodeList webappNodes = currproject
128: .getElementsByTagName("webapps");
129: if (webappNodes != null && webappNodes.getLength() > 0) {
130: Element webappElem = (Element) webappNodes.item(0);
131: String hostBasedAttr = webappElem
132: .getAttribute("hostbased");
133: if (hostBasedAttr != null
134: && hostBasedAttr.equals("true")) {
135: File hostWebappsDir = new File(getDestdir(),
136: "webapps_" + name);
137: hostWebappsDir.mkdirs();
138: }
139: }
140:
141: outname = "web.xml";
142: out = new File(webinfdir, outname);
143: count = doTransformationMaybe();
144: if (count == 0
145: && (out.lastModified() < projectsFile
146: .lastModified())) {
147: doTransformation();
148: count = 1;
149: }
150:
151: if (count > 0) {
152: if (created > 0) {
153: tmp.append(", ");
154: }
155: tmp.append(name);
156: created = created + count;
157: }
158: }
159: } finally {
160: if (created > 0) {
161: log(tmp.toString(), Project.MSG_INFO);
162: }
163: scanner = null;
164: inname = null; /* filename relative to srcdirResolved */
165: infilenameNoExt = null; /* filename relative to srcdirResolved without extension */
166: outname = null; /* filename relative to srcdirResolved */
167: in = null;
168: out = null;
169: inLastModified = 0;
170: outLastModified = 0;
171: styleLastModified = 0;
172: }
173: }
174:
175: public String getProjects() {
176: return projects;
177: }
178:
179: public void setProjects(String newProjects) {
180: this .projects = newProjects;
181: }
182:
183: public String getCorewebxml() {
184: return corewebxml;
185: }
186:
187: public void setCorewebxml(String newCorewebxml) {
188: this.corewebxml = newCorewebxml;
189: }
190:
191: }
|