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.basicapp.basics;
020:
021: import java.io.File;
022: import java.io.IOException;
023:
024: import javax.xml.parsers.DocumentBuilder;
025: import javax.xml.parsers.DocumentBuilderFactory;
026: import javax.xml.parsers.ParserConfigurationException;
027: import javax.xml.transform.Result;
028: import javax.xml.transform.Source;
029: import javax.xml.transform.Transformer;
030: import javax.xml.transform.TransformerConfigurationException;
031: import javax.xml.transform.TransformerException;
032: import javax.xml.transform.TransformerFactory;
033: import javax.xml.transform.dom.DOMSource;
034: import javax.xml.transform.stream.StreamResult;
035:
036: import org.apache.log4j.Logger;
037: import org.w3c.dom.Document;
038: import org.xml.sax.SAXException;
039:
040: import de.schlund.pfixcore.util.basicapp.helper.AppValues;
041: import de.schlund.pfixcore.util.basicapp.objects.Project;
042: import de.schlund.pfixcore.util.basicapp.objects.ServletObject;
043: import de.schlund.pfixcore.util.basicapp.projectdom.ConfigXmlDom;
044: import de.schlund.pfixcore.util.basicapp.projectdom.DependXmlDom;
045: import de.schlund.pfixcore.util.basicapp.projectdom.FrameXmlDom;
046: import de.schlund.pfixcore.util.basicapp.projectdom.PageXmlDom;
047: import de.schlund.pfixcore.util.basicapp.projectdom.ProjectXmlDom;
048:
049: /**
050: * A class responsible for handling all xml files.
051: * This means that editing and also to copy a template
052: * will start here.
053: *
054: * @author <a href="mailto:rapude@schlund.de">Ralf Rapude</a>
055: * @version $Id: HandleXMLFiles.java 3302 2007-11-30 16:56:16Z jenstl $
056: */
057: public class HandleXMLFiles {
058: private static final Logger LOG = Logger
059: .getLogger(HandleXMLFiles.class);
060: /** The project settings */
061: private Project project = null;
062: /** Path to the template folder */
063: private String pathToTmpl = null;
064: /** A path to the conf folder of the new project */
065: private String pathToConf = null;
066: /** The project name */
067: private String projectName = null;
068: /** The path to the pages folder */
069: private String pathToPages = null;
070:
071: /**
072: * Constructor just initializes an Project Object
073: * in order to get the project settings given by
074: * the user in the steps before
075: */
076: public HandleXMLFiles(Project project) {
077: this .project = project;
078: }
079:
080: /**
081: * The "starter method for this class".
082: * @param project An object containing all necessary settings
083: * for a new project
084: */
085: public void runHandleXMLFiles() {
086: System.out.println("\nXml files editing starts now...\n");
087: projectName = project.getProjectName();
088: // setting basicpaths
089: pathToTmpl = AppValues.BASICPATH + AppValues.TEMPLFOLDERPATH;
090: pathToConf = AppValues.BASICPATH + projectName
091: + AppValues.CONFFOLDER;
092: pathToPages = AppValues.BASICPATH + projectName
093: + AppValues.PATHTO_PAGES;
094: Document domDoc = null;
095:
096: // loop through all necessary files needed to create a project
097: for (int i = 0; i < AppValues.TEMPLATEARR.length; i++) {
098: String tmpDoc = AppValues.TEMPLATEARR[i];
099: String wrtDoc = null;
100: domDoc = readDom(pathToTmpl + tmpDoc);
101:
102: // desciding what to do with the current document
103: // the config,prop.in
104: if (tmpDoc.equals(AppValues.CONFIG_TMPL)) {
105:
106: // loop if there are more servlets defined
107: for (int j = 0; j < project.getServletList().size(); j++) {
108: String defServletName = ((ServletObject) (project
109: .getServletList().get(j))).getServletName();
110: wrtDoc = defServletName + AppValues.CFGFILESUFF;
111: ConfigXmlDom configDom = new ConfigXmlDom(project,
112: domDoc, j + 1);
113: domDoc = configDom.getDom();
114: writeXmlFile(domDoc, wrtDoc);
115: }
116:
117: // the depend.xml
118: } else if (tmpDoc.equals(AppValues.DEPEND_TMPL)) {
119: wrtDoc = AppValues.DEPENDXML;
120: DependXmlDom dependDom = new DependXmlDom(project,
121: domDoc);
122: domDoc = dependDom.getDom();
123:
124: // changing the project.xml.in
125: } else if (tmpDoc.equals(AppValues.PROJECT_TMPL)) {
126: wrtDoc = AppValues.PROJECTXMLIN;
127: ProjectXmlDom projectDom = new ProjectXmlDom(project,
128: domDoc);
129: domDoc = projectDom.getDom();
130:
131: // changing the frame.xml
132: } else if (tmpDoc.equals(AppValues.FRAME_TMPL)) {
133: wrtDoc = AppValues.FRAMEXML;
134: FrameXmlDom frameDom = new FrameXmlDom(project, domDoc);
135: domDoc = frameDom.getDom();
136:
137: // the skin.xml also
138: } else if (tmpDoc.equals(AppValues.SKIN_TMPL)) {
139: wrtDoc = AppValues.SKINXSL;
140:
141: // and the metatags.xsl will also appear
142: } else if (tmpDoc.equals(AppValues.METATAGS_TMPL)) {
143: wrtDoc = AppValues.METATAGSXSL;
144: }
145:
146: // the page will appear in myproject/txt/pages
147: else if (tmpDoc.equals(AppValues.PAGE_TMPL)) {
148: for (int j = 0; j < project.getServletList().size(); j++) {
149: int giveAway = j + 1;
150: wrtDoc = AppValues.PAGEDEFPREFIX
151: + AppValues.PAGEDEFAULT + giveAway
152: + AppValues.PAGEDEFSUFFIX;
153: PageXmlDom pageDom = new PageXmlDom(project,
154: domDoc, giveAway);
155: domDoc = pageDom.getDom();
156: writeXmlFile(domDoc, wrtDoc);
157: }
158: }
159:
160: // writing the dom to a file
161: writeXmlFile(domDoc, wrtDoc);
162: }
163: }
164:
165: /**
166: * Creating a domtree for the current file in order
167: * to edit the content
168: * @return A dom
169: */
170: private Document readDom(String currentDoc) {
171: System.out
172: .println("Try to read the dom of the given Document: "
173: + currentDoc);
174:
175: try {
176: DocumentBuilder myBuilder = DocumentBuilderFactory
177: .newInstance().newDocumentBuilder();
178: Document doc = myBuilder.parse(currentDoc);
179: System.out
180: .println("Transforming into dom has been successfull");
181: return doc;
182: } catch (ParserConfigurationException e) {
183: LOG.debug(e.getMessage(), e);
184: } catch (IOException e) {
185: LOG.debug(e.getMessage(), e);
186: } catch (SAXException e) {
187: LOG.debug(e.getMessage(), e);
188: }
189: return null;
190: }
191:
192: /**
193: * Method for writing the dom into an xml file
194: * @param domDoc The current dom to write
195: * @param fileName The fileName to write
196: */
197: private void writeXmlFile(Document domDoc, String fileName) {
198: System.out.println("Writing the file: " + fileName);
199:
200: StringBuffer buffy = new StringBuffer();
201: File file = null;
202:
203: try {
204: // Prepare the DOM document for writing
205: Source source = new DOMSource(domDoc);
206:
207: // preparing the output file for frame.xml
208: if (fileName.equals(AppValues.FRAMEXML)) {
209: buffy.append(AppValues.BASICPATH);
210: buffy.append(projectName);
211: buffy.append(AppValues.XMLCONSTANT);
212: file = new File(buffy.toString() + fileName);
213: buffy.setLength(0);
214:
215: // preparing the output for skin.xsl
216: } else if (fileName.equals(AppValues.SKINXSL)) {
217: buffy.append(AppValues.BASICPATH);
218: buffy.append(projectName);
219: buffy.append(AppValues.XSLFOLDER);
220: file = new File(buffy.toString() + fileName);
221: buffy.setLength(0);
222:
223: // and in the case that is a normal config file
224: // e.g. The config.prop.in
225: } else if (fileName.endsWith(AppValues.CFGFILESUFF)) {
226: file = new File(pathToConf + fileName);
227:
228: // or the depend.xml.in
229: } else if (fileName.equals(AppValues.DEPENDXML)) {
230: file = new File(pathToConf + fileName);
231:
232: } else if (fileName.startsWith(AppValues.PAGEDEFPREFIX)) {
233: file = new File(pathToPages + fileName);
234: // or maybe also the project.xml.in
235: } else if (fileName.equals(AppValues.PROJECTXMLIN)) {
236: file = new File(pathToConf + fileName);
237:
238: // and last but not least the metatags.xsl
239: } else if (fileName.equals(AppValues.METATAGSXSL)) {
240: buffy.append(AppValues.BASICPATH);
241: buffy.append(projectName);
242: buffy.append(AppValues.XSLFOLDER);
243: file = new File(buffy.toString() + fileName);
244: buffy.setLength(0);
245: }
246:
247: // the output will happen anyway ;o)
248: Result result = new StreamResult(file);
249:
250: // Write the DOM document to the file
251: Transformer xformer = TransformerFactory.newInstance()
252: .newTransformer();
253: xformer.transform(source, result);
254: System.out.println("Writing file has been successfull\n");
255: } catch (TransformerConfigurationException e) {
256: LOG.debug(e.getMessage(), e);
257: } catch (TransformerException e) {
258: LOG.debug(e.getMessage(), e);
259: }
260: }
261: }
|