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 java.util.ArrayList;
023:
024: import org.w3c.dom.Document;
025: import org.w3c.dom.Element;
026: import org.w3c.dom.Node;
027: import org.w3c.dom.Text;
028:
029: import de.schlund.pfixcore.util.basicapp.helper.AppValues;
030: import de.schlund.pfixcore.util.basicapp.helper.XmlUtils;
031: import de.schlund.pfixcore.util.basicapp.objects.Project;
032: import de.schlund.pfixcore.util.basicapp.objects.ServletObject;
033:
034: /**
035: * Represents the dom of depend.xml.in
036: *
037: * @author <a href="mailto:rapude@schlund.de">Ralf Rapude</a>
038: * @version $Id: DependXmlDom.java 3367 2008-02-21 13:53:07Z smarsching $
039: */
040: public final class DependXmlDom {
041:
042: /** The current dom */
043: private Document domDoc = null;
044: /** A String Buffer to get e.g. correctPaths */
045: private StringBuffer buffy = new StringBuffer();
046: /** The current Project */
047: Project project = null;
048: private String projectName = null;
049: private String projectLang = null;
050: private int servletAmount = 0;
051:
052: /**
053: * Constructor initializes the Project Object
054: * and the dom for the current document
055: * @param project The current project
056: * @param domDoc the current Dom given by HandleXmlFiles
057: */
058: public DependXmlDom(Project project, Document domDoc) {
059: this .domDoc = domDoc;
060: this .project = project;
061: projectName = project.getProjectName();
062: projectLang = project.getLanguage();
063: servletAmount = project.getServletList().size();
064:
065: prepareDependXml();
066: }
067:
068: /**
069: * Preparing the depend.xml.in whicht means, that the content of some
070: * of the doms attributes and tags will be changed
071: * @param buffy A StringBuffer for the new values
072: * @param domDoc The dom
073: * @return The new dom
074: */
075: private void prepareDependXml() {
076: // change attribute project
077: domDoc = XmlUtils.changeAttributes(domDoc,
078: AppValues.DEPENDTAG_MAKE, AppValues.DEPENDATT_PROJECT,
079: projectName, false);
080:
081: // change attribute language
082: domDoc = XmlUtils.changeAttributes(domDoc,
083: AppValues.DEPENDTAG_MAKE, AppValues.DEPENDATT_LANG,
084: projectLang, false);
085:
086: // change attribute handler in the page tag for the default servlet
087: String defServletHandler = ((ServletObject) (project
088: .getServletList().get(0))).getServletName();
089: buffy.append(AppValues.XMLCONSTANT);
090: buffy.append(defServletHandler);
091:
092: domDoc = XmlUtils.changeAttributes(domDoc,
093: AppValues.DEPENDTAG_PAGE, AppValues.DEPENDATT_HANDLER,
094: buffy.toString(), false);
095:
096: buffy.setLength(0);
097:
098: // change attribute stylesheet in the include tag for standardmaster
099: buffy.append(projectName);
100: buffy.append(AppValues.XSLFOLDER);
101: buffy.append(AppValues.SKINXSL);
102:
103: domDoc = XmlUtils
104: .changeAttributes(domDoc, AppValues.DEPENDTAG_INCLUDE,
105: AppValues.DEPENDATT_STYLESHEET, buffy
106: .toString(), false);
107:
108: buffy.setLength(0);
109:
110: // change attribute stylesheet in the include tag for standardmetatags
111: buffy.append(projectName);
112: buffy.append(AppValues.XSLFOLDER);
113: buffy.append(AppValues.METATAGSXSL);
114:
115: domDoc = XmlUtils.changeAttributes(domDoc,
116: AppValues.DEPENDTAG_INCLUDE,
117: AppValues.DEPENDATT_STYLESHEET, buffy.toString(), true);
118:
119: buffy.setLength(0);
120:
121: // change the attribute xml in the standardpage tag
122: buffy.append(projectName);
123: buffy.append(AppValues.XMLCONSTANT);
124: buffy.append(AppValues.FRAMEXML);
125:
126: domDoc = XmlUtils.changeAttributes(domDoc,
127: AppValues.DEPENDTAG_STDPAGE, AppValues.DEPENDATT_XML,
128: buffy.toString(), false);
129: buffy.setLength(0);
130:
131: // setting the amount of servlets in depend.xml
132: if (servletAmount > 0) {
133: domDoc = insertNavigationTags(domDoc, servletAmount);
134: domDoc = insertStandardpageTags(domDoc, servletAmount);
135: }
136: }
137:
138: /**
139: * If the user has decided to use more than one
140: * servlet, a page for every servlet must be added
141: * to the current dom.
142: * It will happen here.
143: * @param domDoc The current document
144: * @param myAmount The amount of servlets, the user
145: * wants to add
146: */
147: private Document insertNavigationTags(Document domDoc, int myAmount) {
148: // the node we need is called navigation
149: Node navigationNode = domDoc.getElementsByTagName(
150: AppValues.DEPENDTAG_NAVIGATION).item(0);
151:
152: // an ArrayList with the servlet spec
153: ArrayList<ServletObject> tmpList = project.getServletList();
154: Element newElement = null;
155: String tmpName = null;
156: String myHandler = null;
157: Text lastIndent = domDoc.createTextNode(" ");
158:
159: for (int i = 0; i < myAmount; i++) {
160: Text txtIndent = domDoc.createTextNode(" ");
161: Text txtReturn = domDoc.createTextNode("\n");
162: tmpName = ((ServletObject) tmpList.get(i)).getServletName();
163: myHandler = AppValues.XMLCONSTANT + tmpName;
164: newElement = domDoc.createElement(AppValues.DEPENDTAG_PAGE);
165:
166: newElement.setAttribute(AppValues.DEPENDATT_NAME,
167: AppValues.DEPENDATT_HOME + (i + 1));
168: newElement.setAttribute(AppValues.DEPENDATT_HANDLER,
169: myHandler);
170:
171: navigationNode.appendChild(newElement);
172: navigationNode.appendChild(txtReturn);
173: navigationNode.insertBefore(txtIndent, newElement);
174: }
175:
176: navigationNode.appendChild(lastIndent);
177: return domDoc;
178: }
179:
180: /**
181: * Generating the standardpage tag for each site defined
182: * in the navigation tag.
183: * @return The changed document
184: */
185: private Document insertStandardpageTags(Document domDoc,
186: int servletAmount) {
187: Element rootNode = domDoc.getDocumentElement();
188: Text myReturn = domDoc.createTextNode("\n");
189: Text firstIndent = domDoc.createTextNode(" ");
190:
191: rootNode.appendChild(firstIndent);
192:
193: for (int i = 0; i < servletAmount; i++) {
194: Text txtIndent = domDoc.createTextNode(" ");
195: Text txtReturn = domDoc.createTextNode("\n");
196:
197: Element newStdPage = domDoc
198: .createElement(AppValues.DEPENDTAG_STDPAGE);
199: newStdPage.setAttribute(AppValues.DEPENDATT_NAME,
200: AppValues.DEPENDATT_HOME + (i + 1));
201: newStdPage.setAttribute(AppValues.DEPENDATT_XML,
202: projectName + AppValues.XMLCONSTANT
203: + AppValues.FRAMEXML);
204:
205: rootNode.appendChild(newStdPage);
206: rootNode.appendChild(txtReturn);
207: rootNode.appendChild(txtIndent);
208: }
209:
210: rootNode.appendChild(myReturn);
211: return domDoc;
212: }
213:
214: /**
215: * Gives the current dom back to
216: * HandleXmlFiles
217: * @return Document The current dom
218: */
219: public Document getDom() {
220: return domDoc;
221: }
222: }
|