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:
020: package de.schlund.pfixcore.util.basicapp.projectdom;
021:
022: import org.w3c.dom.Document;
023: import org.w3c.dom.Element;
024: import org.w3c.dom.Node;
025: import org.w3c.dom.Text;
026:
027: import de.schlund.pfixcore.util.basicapp.helper.AppValues;
028: import de.schlund.pfixcore.util.basicapp.helper.XmlUtils;
029: import de.schlund.pfixcore.util.basicapp.objects.Project;
030: import de.schlund.pfixcore.util.basicapp.objects.ServletObject;
031:
032: /**
033: * Represents the dom of project.xml.in
034: *
035: * @author <a href="mailto:rapude@schlund.de">Ralf Rapude</a>
036: * @version $Id: ProjectXmlDom.java 3302 2007-11-30 16:56:16Z jenstl $
037: */
038: public final class ProjectXmlDom {
039: /** The current dom */
040: private Document domDoc = null;
041: /** A String Buffer to get e.g. correctPaths */
042: private StringBuffer buffy = new StringBuffer();
043: /** The current Project */
044: Project project = null;
045: private String projectName = null;
046:
047: /**
048: * Constructor initializes the Project Object
049: * and the dom for the current document
050: * @param project The current project
051: * @param domDoc the current Dom given by HandleXmlFiles
052: */
053: public ProjectXmlDom(Project project, Document domDoc) {
054: this .domDoc = domDoc;
055: projectName = project.getProjectName();
056: this .project = project;
057: prepareProjectXml();
058: }
059:
060: /**
061: * Preparing the project.xml whicht means, that the content of some
062: * of the doms attributes and tags will be changed
063: * @param buffy A StringBuffer for the new values
064: * @param domDoc The dom
065: * @return The new dom
066: */
067: private void prepareProjectXml() {
068: // change the tag depend
069: buffy.append(projectName);
070: buffy.append(AppValues.CONFFOLDER);
071: buffy.append(AppValues.DEPENDXML);
072: domDoc = XmlUtils.changeTextValues(domDoc,
073: AppValues.PROJECTTAG_DEPEND, buffy.toString(), /*true if firstChild ->*/
074: false);
075: buffy.setLength(0);
076:
077: // change attribute name for the project tag
078: domDoc = XmlUtils.changeAttributes(domDoc,
079: AppValues.PROJECTTAG_PROJECT,
080: AppValues.PROJECTATT_NAME, projectName, false);
081:
082: // change the tag comment
083: buffy.append(project.getComment());
084: domDoc = XmlUtils.changeTextValues(domDoc,
085: AppValues.PROJECTTAG_COMMENT, buffy.toString(), true);
086: buffy.setLength(0);
087:
088: // change the text content of servername and serveralias
089: buffy.append(projectName);
090: buffy.append(".");
091: domDoc = XmlUtils.changeTextValues(domDoc,
092: AppValues.PROJECTTAG_SERVERNAME, buffy.toString(), /*true if firstChild ->*/
093: true);
094: domDoc = XmlUtils.changeTextValues(domDoc,
095: AppValues.PROJECTTAG_SERVERALIAS, buffy.toString(), /*true if firstChild ->*/
096: true);
097: buffy.setLength(0);
098:
099: // change the content of the defpath tag
100: String defServletHandler = ((ServletObject) (project
101: .getServletList().get(0))).getServletName();
102: buffy.append(AppValues.XMLCONSTANT);
103: buffy.append(defServletHandler);
104: domDoc = XmlUtils.changeTextValues(domDoc,
105: AppValues.PROJECTTAG_DEFPATH, buffy.toString(), true);
106: buffy.setLength(0);
107:
108: // change the passthrough tag
109: buffy.append(projectName);
110: buffy.append(AppValues.IMGFOLDER);
111: domDoc = XmlUtils.changeTextValues(domDoc,
112: AppValues.PROJECTTAG_PASSTHROUGH, buffy.toString(),
113: true);
114: buffy.setLength(0);
115:
116: // changing the text node for docroot
117: buffy.append(projectName);
118: buffy.append(AppValues.HTDOCSFOLDER);
119: domDoc = XmlUtils.changeTextValues(domDoc,
120: AppValues.PROJECTTAG_DOCUMENTROOT, buffy.toString(),
121: false);
122: buffy.setLength(0);
123:
124: // creating the servlets
125: for (int i = 0; i < project.getServletList().size(); i++) {
126: domDoc = insertServlets(domDoc, i);
127: }
128: }
129:
130: /**
131: * inserting the servlets created by the user
132: * @return the dom with all servlets
133: */
134: private Document insertServlets(Document domDoc, int currentServlet) {
135: // the current servlets name
136: String curServletName = ((ServletObject) (project
137: .getServletList().get(currentServlet)))
138: .getServletName();
139:
140: // my new servlet node
141: Element myNewServlet = domDoc
142: .createElement(AppValues.PROJECTTAG_SERVLET);
143: Element newServletActive = domDoc
144: .createElement(AppValues.PROJECTTAG_ACTIVE);
145: Text servletActiveChild = domDoc
146: .createTextNode(AppValues.PROJECTVALUE_TRUE);
147: Element servletClass = domDoc
148: .createElement(AppValues.PROJECTTAG_CLASS);
149: Text classValue = domDoc
150: .createTextNode(AppValues.PROJECTVALUE_CLASS);
151: Element newServletPropfile = domDoc
152: .createElement(AppValues.PROJECTTAG_PROPFILE);
153: // Element newDocRootNode = domDoc.createElement(AppValues.PROJECTTAG_DOCROOT);
154: Element rootNode = domDoc.getDocumentElement();
155: Text afterDocRootNode = domDoc.createTextNode(projectName
156: + AppValues.CONFFOLDER + curServletName
157: + AppValues.CFGFILESUFF);
158:
159: // setting the attribute of the new servlet tag
160: myNewServlet.setAttribute(AppValues.PROJECTATT_NAME,
161: curServletName);
162: myNewServlet.setAttribute(AppValues.PROJECTATT_EDITOR,
163: AppValues.PROJECTVALUE_TRUE);
164:
165: //setting the text value of active
166: newServletActive.appendChild(servletActiveChild);
167: myNewServlet.appendChild(newServletActive);
168: Text myActiveReturn = domDoc.createTextNode("\n");
169: Text myActiveIndent = domDoc.createTextNode(" ");
170: myNewServlet.insertBefore(myActiveReturn, newServletActive);
171: myNewServlet.insertBefore(myActiveIndent, newServletActive);
172:
173: // setting the class tag
174: servletClass.appendChild(classValue);
175: myNewServlet.appendChild(servletClass);
176: Text myClassReturn = domDoc.createTextNode("\n");
177: Text myClassIndent = domDoc.createTextNode(" ");
178: myNewServlet.insertBefore(myClassReturn, servletClass);
179: myNewServlet.insertBefore(myClassIndent, servletClass);
180:
181: // prepare the propfile tag
182: newServletPropfile.appendChild(afterDocRootNode);
183: // newServletPropfile.insertBefore(newDocRootNode,afterDocRootNode);
184: Text myPropfileReturn = domDoc.createTextNode("\n");
185: Text myPropfileIndent = domDoc.createTextNode(" ");
186: myNewServlet.appendChild(newServletPropfile);
187: myNewServlet.insertBefore(myPropfileReturn, newServletPropfile);
188: myNewServlet.insertBefore(myPropfileIndent, newServletPropfile);
189: Text lastReturn = domDoc.createTextNode("\n");
190: Text lastIndent = domDoc.createTextNode(" ");
191: myNewServlet.appendChild(lastReturn);
192: myNewServlet.appendChild(lastIndent);
193:
194: // go to the first servlet (it's the deref servlet
195: Node servletNode = domDoc.getElementsByTagName(
196: AppValues.PROJECTTAG_SERVLET).item(0);
197: Text mainServletReturn = domDoc.createTextNode("\n");
198: Text mainServletIndent = domDoc.createTextNode(" ");
199: rootNode.insertBefore(myNewServlet, servletNode);
200: rootNode.insertBefore(mainServletReturn, servletNode);
201: rootNode.insertBefore(mainServletIndent, servletNode);
202:
203: return domDoc;
204: }
205:
206: /**
207: * Gives the current dom back to
208: * HandleXmlFiles
209: * @return Document The current dom
210: */
211: public Document getDom() {
212: return domDoc;
213: }
214: }
|