KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > webEditor > core > presentation


1 /* $Id: presentation.java,v 1.8 2001/04/30 20:50:08 agarcia3 Exp $
2     webEditor. The new way in content management
3     Copyright (C) 2001 Alfredo Garcia
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12     GNU General Public License for more details.
13 */

14
15 package webEditor.core;
16
17 import java.io.*;
18
19 import org.w3c.dom.*;
20 import org.apache.xerces.parsers.DOMParser;
21 import org.apache.regexp.RE;
22
23 import webEditor.util.*;
24
25
26 /**
27  * Presentation management
28  * @author <a HREF="mailto:agarcia@mundofree.com">Alfredo Garcia</a>
29  */

30 public class presentation
31 {
32    /**
33     * Editor root
34     */

35    private String JavaDoc wEd_root;
36
37    /**
38     * DOM tree with the configuration values
39     */

40    private Document configDoc = null;
41
42    /**
43     * Path for the data configuration files
44     */

45    private String JavaDoc dataDir;
46    
47    /**
48     * Path for the XSL template files
49     */

50    private String JavaDoc tplDir;
51
52    /**
53     * Path for the edition XSL template files
54     */

55    private String JavaDoc editTplDir;
56
57    /**
58     * Path for the presentation XSL template files
59     */

60    private String JavaDoc showTplDir;
61
62    /**
63     * Path for the configuration XSL template files
64     */

65    private String JavaDoc configTplDir;
66    
67    /**
68     * Distinctive name of the web server.
69     */

70    private String JavaDoc serverName;
71    
72    /**
73     * Document root of the web server
74     */

75    private String JavaDoc docRoot;
76    
77    public presentation (configuration initParam)
78    {
79     xml_parser confParser = new xml_parser ();
80     this.configDoc = confParser.writeDOM ("general", initParam.returnData());
81
82     String JavaDoc categoryName = "directories";
83
84     this.wEd_root = initParam.readValue ("webEditor_Root","wEd_root");
85     this.dataDir = initParam.readValue (categoryName,"dataDir");
86     this.dataDir = this.wEd_root + "/" + this.dataDir;
87
88     this.tplDir = initParam.readValue (categoryName,"tplDir");
89     this.tplDir = this.dataDir + "/" + this.tplDir;
90     this.editTplDir = this.tplDir + "/editor";
91     this.showTplDir = this.tplDir + "/presentation";
92     this.configTplDir = this.tplDir + "/config";
93    }
94    
95    /**
96     * Compose the news data input pages
97     * @param newsDoc DOM tree of the document to edit
98     * @param mode Edition mode (rich or simple)
99     * @param browserType User-agent headers
100     * @return String HTML output
101     */

102    public String JavaDoc showTextEditor(Document newsDoc, String JavaDoc mode, String JavaDoc browserType)
103    {
104     String JavaDoc templateFile = "/editor.xsl";
105 try {
106     RE r = new RE("MSIE");
107     if ( mode.equals ("rich") && (r.match (browserType)) ) {
108         // You must use IE and enable the rich content edition
109
// if you want to use this feature :-)
110
templateFile = "/editor-ie.xsl";
111
112         // We also will try to escape special characters in the doc
113
//escapeChars myChar = new escapeChars ();
114
//newsDoc = myChar.escapeDoc (newsDoc);
115
}
116     // We must add the configuration tree to the document tree
117
newsDoc = this.addConfiguration (newsDoc, this.configDoc);
118 }
119 catch (Exception JavaDoc e) {
120     e.printStackTrace();
121 }
122
123     String JavaDoc outputString = null;
124     // The template name must be URL compilant
125
String JavaDoc templateName = new File
126         (this.editTplDir + templateFile).getAbsolutePath();
127     templateName = "file:" + templateName;
128 try {
129     xsl_transform transform = new xsl_transform ();
130     outputString = transform.processDOM (newsDoc, templateName);
131 }
132 catch (Exception JavaDoc e) {
133     e.printStackTrace();
134 }
135
136     return (outputString);
137    }
138    
139    /**
140     * Compose the images input pages
141     * @param newsDoc DOM tree of the document to edit
142     * @param newsSection Section of the document
143     * @return String HTML output
144     */

145    public String JavaDoc showImgEditor(Document newsDoc, String JavaDoc newsSection)
146    {
147     String JavaDoc outputString = null;
148     // The template name must be URL compilant
149
String JavaDoc templateName = new File
150         (this.editTplDir + "/image.xsl").getAbsolutePath();
151     templateName = "file:" + templateName;
152     // We must add the configuration tree to the document tree
153
newsDoc = this.addConfiguration (newsDoc, this.configDoc);
154 try {
155     xsl_transform transform = new xsl_transform ();
156     outputString = transform.processDOM (newsDoc, templateName);
157 }
158 catch (Exception JavaDoc e) {
159     e.printStackTrace();
160 }
161
162     return (outputString);
163    }
164
165   /**
166     * Compose the composition index page
167     * @param newsDoc DOM tree of the index
168     * @param mode Mode of presentation (basicaly "edit" or "presentation")
169     * @return String HTML output
170     */

171    public String JavaDoc showIndex (Document indexDoc, String JavaDoc mode)
172    {
173     String JavaDoc outputString = null;
174     String JavaDoc templatePath = null;
175
176     // The template name must be URL compilant
177
if ( mode == "edit" ) {
178         templatePath = this.editTplDir;
179     }
180     else {
181         templatePath = this.showTplDir;
182     }
183
184     String JavaDoc templateName = new File
185         (templatePath + "/index.xsl").getAbsolutePath();
186     templateName = "file:" + templateName;
187
188     // We must add the configuration tree to the document tree
189
indexDoc = this.addConfiguration (indexDoc, this.configDoc);
190 try {
191     xsl_transform transform = new xsl_transform ();
192     outputString = transform.processDOM (indexDoc, templateName);
193 }
194 catch (Exception JavaDoc e) {
195     e.printStackTrace();
196 }
197
198     return (outputString);
199    }
200
201   /**
202     * Compose the presentation of a news document
203     * @param newsDoc DOM tree of the document
204     * @param mode Mode of presentation (basicaly "show" or "publish")
205     * @return String HTML output
206     */

207
208    public String JavaDoc showDoc (Document newsDoc, String JavaDoc mode)
209    {
210     String JavaDoc outputString = null;
211     String JavaDoc templateFile = null;
212
213     // The template name must be URL compilant
214
if ( mode == "show" ) {
215         templateFile = "/doc.xsl";
216     }
217     else {
218         templateFile = "/htmldoc.xsl";
219     }
220
221     String JavaDoc templateName = new File
222         (this.showTplDir+ templateFile).getAbsolutePath();
223     templateName = "file:" + templateName;
224     // We must add the configuration tree to the document tree
225
newsDoc = this.addConfiguration (newsDoc, this.configDoc);
226 try {
227     xsl_transform transform = new xsl_transform ();
228     outputString = transform.processDOM (newsDoc, templateName);
229 }
230 catch (Exception JavaDoc e) {
231     e.printStackTrace();
232 }
233
234     return (outputString);
235    }
236
237    /**
238     * Compose the configuration screen
239     * @param configDoc DOM tree of the configuration parameters
240     * @return String HTML output
241     */

242    public String JavaDoc showConfig (Document configDoc)
243    {
244     String JavaDoc outputString = null;
245     // The template name must be URL compilant
246
String JavaDoc templateName = new File
247         (this.configTplDir + "/sample.xsl").getAbsolutePath();
248     templateName = "file:" + templateName;
249     // We must add the configuration tree to the document tree
250
configDoc = this.addConfiguration (configDoc, this.configDoc);
251 try {
252     xsl_transform transform = new xsl_transform ();
253     outputString = transform.processDOM (configDoc, templateName);
254 }
255 catch (Exception JavaDoc e) {
256     e.printStackTrace();
257 }
258
259     return (outputString);
260    }
261
262
263    /**
264     * Compose the configuration editor screen
265     * @param configDoc DOM tree of the configuration parameters
266     * @return String HTML output
267     */

268    public String JavaDoc showConfigEditor (Document configDoc)
269    {
270     String JavaDoc outputString = null;
271     // The template name must be URL compilant
272
String JavaDoc templateName = new File
273         (this.configTplDir + "/edit-config.xsl").getAbsolutePath();
274     templateName = "file:" + templateName;
275     // We must add the configuration tree to the document tree
276
configDoc = this.addConfiguration (configDoc, this.configDoc);
277 try {
278     xsl_transform transform = new xsl_transform ();
279     outputString = transform.processDOM (configDoc, templateName);
280 }
281 catch (Exception JavaDoc e) {
282     e.printStackTrace();
283 }
284
285     return (outputString);
286    }
287
288
289    /**
290     * Adds the current configuration to the DOM tree;
291     * In this way, the templates can know the behavior of the page
292     * @param doc Original DOM tree
293     * @param config Configuration values
294     * @return String HTML output
295     */

296    public Document addConfiguration (Document doc, Document config)
297    {
298     // We get the document root
299
Element root = doc.getDocumentElement();
300
301     Element item = doc.createElement ("configuration");
302     item.appendChild (doc.importNode (config.getDocumentElement(), true));
303
304     root.appendChild (item);
305
306     return (doc);
307    }
308
309 }
310
Popular Tags