KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > clipbuilder > html > web > html > Impl > JDom > JDomHTMLDocument


1 package org.jahia.clipbuilder.html.web.html.Impl.JDom;
2
3 import java.util.*;
4
5 import org.jahia.clipbuilder.html.bean.*;
6 import org.jahia.clipbuilder.html.web.*;
7 import org.jahia.clipbuilder.html.web.html.*;
8 import org.jdom.*;
9 import org.jdom.output.*;
10 import org.org.apache.commons.httpclient.*;
11
12 /**
13  * Document representing the HTML page extended with the relative attribute
14  * of the url of the page
15  *
16  *@author Tlili Khaled
17  */

18 public class JDomHTMLDocument implements HTMLDocument {
19
20     //Url
21
private UrlBean urlBean;
22     private List framesList;
23
24     //Transformer
25
private JDomHTMLTransformer transformer;
26     private String JavaDoc charEncoding;
27     //List of head children
28
private ArrayList headChildrenList = new ArrayList();
29
30     //Documents
31
private org.jdom.Document originalDocument;
32     private org.jdom.Document transformedDocument;
33     private org.jdom.Document documentWhithParamsLabel;
34     private org.jdom.Document chewDocument;
35     private org.jdom.Document userDocument;
36
37     //Hastable
38
private Hashtable selectedPartHash = new Hashtable();
39     private Hashtable paramsHashtable = new Hashtable();
40     private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(JDomHTMLDocument.class);
41
42
43     /**
44      * Constructor for the JDomHTMLDocument object
45      *
46      *@param uBean Description of Parameter
47      *@param doc Description of Parameter
48      *@exception URIException Description of Exception
49      */

50     public JDomHTMLDocument(UrlBean uBean, org.jdom.Document doc) throws URIException {
51         this(doc);
52         setUrlBean(uBean);
53     }
54
55
56     /**
57      * Constructor for the JDomHTMLDocument object
58      *
59      *@param uBean Description of Parameter
60      *@param w3cDoc Description of Parameter
61      *@exception URIException Description of Exception
62      */

63     public JDomHTMLDocument(UrlBean uBean, org.w3c.dom.Document JavaDoc w3cDoc) throws URIException {
64         org.jdom.input.DOMBuilder builder = new org.jdom.input.DOMBuilder();
65         org.jdom.Document jdomDoc = builder.build(w3cDoc);
66         getDocumentAsString(jdomDoc);
67         setOriginalDocument(jdomDoc);
68         setUrlBean(uBean);
69     }
70
71
72     /**
73      * Constructor for the JDomHTMLDocument object
74      *
75      *@param doc Description of Parameter
76      *@exception URIException Description of Exception
77      */

78     public JDomHTMLDocument(org.jdom.Document doc) throws URIException {
79         setOriginalDocument(doc);
80     }
81
82
83
84     /**
85      * Sets the UrlBean attribute of the JDomHTMLDocument object
86      *
87      *@param urlBean The new UrlBean value
88      */

89     public void setUrlBean(UrlBean urlBean) {
90         this.urlBean = urlBean;
91     }
92
93
94     /**
95      * Sets the Transformer attribute of the JDomHTMLDocument object
96      *
97      *@param transformer The new Transformer value
98      */

99     public void setTransformer(JDomHTMLTransformer transformer) {
100         this.transformer = transformer;
101     }
102
103
104     /**
105      * Sets the OriginalDocument attribute of the JDomHTMLDocument object
106      *
107      *@param originalDocument The new OriginalDocument value
108      */

109     public void setOriginalDocument(org.jdom.Document originalDocument) {
110         this.originalDocument = originalDocument;
111     }
112
113
114     /**
115      * Sets the CharEncoding attribute of the JDomHTMLDocument object
116      *
117      *@param charEncoding The new CharEncoding value
118      */

119     public void setCharEncoding(String JavaDoc charEncoding) {
120         this.charEncoding = charEncoding;
121     }
122
123
124     /**
125      * Sets the FramesList attribute of the JDomHTMLDocument object
126      *
127      *@param framesList The new FramesList value
128      */

129     public void setFramesList(List framesList) {
130         this.framesList = framesList;
131     }
132
133
134
135
136     /**
137      * Gets the ParsingErrors attribute of the JDomHTMLDocument object
138      *
139      *@return The ParsingErrors value
140      */

141     public java.util.List JavaDoc getTransformationErrors() {
142         return getTransformer().getLastParsingErrors();
143     }
144
145
146     /**
147      * Gets the SelectPartFromHash attribute of the JDomHTMLDocument object
148      *
149      *@param hash Description of Parameter
150      *@return The SelectPartFromHash value
151      */

152     public String JavaDoc getSelectPartFromHash(String JavaDoc hash) {
153         Element ele = getElementFromHash(hash);
154         ele.detach();
155         org.jdom.Document doc = new org.jdom.Document(ele);
156         return this.getDocumentAsString(doc);
157     }
158
159
160     /**
161      * Gets the DocumentAsString attribute of the JDomHTMLDocument object
162      *
163      *@return The DocumentAsString value
164      */

165     public String JavaDoc getOriginalDocumentAsString() {
166         return getDocumentAsString(this.getOriginalDocument());
167     }
168
169
170     /**
171      * Gets the TransformedDocumentAsString attribute of the JDomHTMLTransformer
172      * object as a String
173      *
174      *@return The TransformedDocumentAsString value
175      */

176     public String JavaDoc getTransformedDocumentAsString() {
177         logger.debug("[ Get transformed document as sring ]");
178         return getDocumentAsString(getTransformedDocument());
179     }
180
181
182
183     /**
184      * Gets the ChewDocumentAsString attribute of the JDomHTMLDocument object
185      *
186      *@param tagName Description of Parameter
187      *@return The ChewDocumentAsString value
188      */

189     public String JavaDoc getChewDocumentAsString(String JavaDoc tagName) {
190         return getDocumentAsString(getChewDocument(tagName));
191     }
192
193
194     /**
195      * Gets the UserDocumentAsString attribute of the JDomHTMLDocument object
196      *
197      *@return The UserDocumentAsString value
198      */

199     public String JavaDoc getUserDocumentAsString() {
200         return getDocumentAsString(getUserDocument());
201     }
202
203
204
205     /**
206      * Gets the DocumentWhitParamLabelAsString attribute of the JDomHTMLDocument
207      * object
208      *
209      *@return The DocumentWhitParamLabelAsString value
210      */

211     public String JavaDoc getDocumentWhithParamLabelAsString() {
212         return getDocumentAsString(getDocumentWhithParamsLabel());
213     }
214
215
216     /**
217      * Gets the TransformedDocument attribute of the JDomHTMLDocument object
218      *
219      *@return The TransformedDocument value
220      */

221     public org.jdom.Document getTransformedDocument() {
222         if (getTransformer() == null) {
223             getTransformer().buildTransformedDocument();
224         }
225
226         return transformedDocument;
227     }
228
229
230     /**
231      * Gets the UrlBean attribute of the JDomHTMLDocument object
232      *
233      *@return The UrlBean value
234      */

235     public UrlBean getUrlBean() {
236         return urlBean;
237     }
238
239
240     /**
241      * Gets the OriginalDocument attribute of the JDomHTMLDocument object
242      *
243      *@return The OriginalDocument value
244      */

245     public org.jdom.Document getOriginalDocument() {
246         return originalDocument;
247     }
248
249
250     /**
251      * Gets the Transformer attribute of the JDomHTMLDocument object
252      *
253      *@return The Transformer value
254      */

255     public HTMLTransformer getTransformer() {
256         return transformer;
257     }
258
259
260     /**
261      * Gets the CharEncoding attribute of the JDomHTMLDocument object
262      *
263      *@return The CharEncoding value
264      */

265     public String JavaDoc getCharEncoding() {
266         return charEncoding;
267     }
268
269
270     /**
271      * Gets the FramesList attribute of the JDomHTMLDocument object
272      *
273      *@return The FramesList value
274      */

275     public List getFramesList() {
276         return framesList;
277     }
278
279
280
281     /**
282      * Sets the TransformedDocument attribute of the JDomHTMLDocument object
283      *
284      *@param transformedDocument The new TransformedDocument value
285      */

286     protected void setTransformedDocument(org.jdom.Document transformedDocument) {
287         this.transformedDocument = transformedDocument;
288     }
289
290
291     /**
292      * Sets the ParamsHashtable attribute of the JDomHTMLDocument object
293      *
294      *@param paramsHashtable The new ParamsHashtable value
295      */

296     protected void setParamsHashtable(Hashtable paramsHashtable) {
297         this.paramsHashtable = paramsHashtable;
298     }
299
300
301     /**
302      * Sets the DocumentWhithParamsLabel attribute of the JDomHTMLDocument
303      * object
304      *
305      *@param documentWhithParamsLabel The new DocumentWhithParamsLabel value
306      */

307     protected void setDocumentWhithParamsLabel(org.jdom.Document
308             documentWhithParamsLabel) {
309         this.documentWhithParamsLabel = documentWhithParamsLabel;
310     }
311
312
313     /**
314      * Sets the ChewDocument attribute of the JDomHTMLDocument object
315      *
316      *@param chewDocument The new ChewDocument value
317      */

318     protected void setChewDocument(org.jdom.Document chewDocument) {
319         this.chewDocument = chewDocument;
320     }
321
322
323     /**
324      * Sets the UserDocument attribute of the JDomHTMLDocument object
325      *
326      *@param userDocument The new UserDocument value
327      */

328     protected void setUserDocument(org.jdom.Document userDocument) {
329         this.userDocument = userDocument;
330     }
331
332
333     /**
334      * Gets the AllScriptElement attribute of the JDomHTMLDocument object
335      *
336      *@return The AllScriptElement value
337      */

338     protected ArrayList getAllHeadChildrenList() {
339         return headChildrenList;
340     }
341
342
343     /**
344      * Gets the ParamsHashtable attribute of the JDomHTMLDocument object
345      *
346      *@return The ParamsHashtable value
347      */

348     protected Hashtable getParamsHashtable() {
349         return paramsHashtable;
350     }
351
352
353     /**
354      * Gets the DocumentWhithParamsLabel attribute of the JDomHTMLDocument
355      * object
356      *
357      *@return The DocumentWhithParamsLabel value
358      */

359     protected org.jdom.Document getDocumentWhithParamsLabel() {
360         if (this.documentWhithParamsLabel == null) {
361             try {
362                 getTransformer().buildDocumentWhithLabel();
363             }
364             catch (Exception JavaDoc ex) {
365                 logger.error("[C an't build the wanted document] ");
366                 ex.printStackTrace();
367             }
368         }
369
370         return documentWhithParamsLabel;
371     }
372
373
374     /**
375      * Gets the ChewDocument attribute of the JDomHTMLDocument object
376      *
377      *@param tagName Description of Parameter
378      *@return The ChewDocument value
379      */

380     protected org.jdom.Document getChewDocument(String JavaDoc tagName) {
381         if (this.chewDocument == null) {
382             getTransformer().buildChewDocument(tagName);
383         }
384         return chewDocument;
385     }
386
387
388     /**
389      * Gets the UserDocument attribute of the JDomHTMLDocument object
390      *
391      *@return The UserDocument value
392      */

393     protected org.jdom.Document getUserDocument() {
394         if (this.userDocument == null) {
395             getTransformer().buildUserDocument();
396         }
397         return userDocument;
398     }
399
400
401     /**
402      * Gets the ParamDefaultValue attribute of the JDomHTMLDocument object
403      *
404      *@param name Description of Parameter
405      *@return The ParamDefaultValue value
406      */

407     protected String JavaDoc getParamDefaultValue(String JavaDoc name) {
408         return (String JavaDoc) getParamsHashtable().get(name);
409     }
410
411
412     /**
413      * Gets the AllParamsName attribute of the JDomHTMLDocument object
414      *
415      *@return The AllParamsName value
416      */

417     protected Enumeration getAllParamsName() {
418         return getParamsHashtable().keys();
419     }
420
421
422     /**
423      * Adds a feature to the ScriptElement attribute of the JDomHTMLDocument
424      * object
425      *
426      *@param ele The feature to be added to the HeadChildrenList attribute
427      */

428     protected void addHeadChildrenList(Element ele) {
429         this.headChildrenList.add(ele);
430     }
431
432
433     /**
434      * Adds a feature to the Hash attribute of the JDomHTMLDocument object
435      *
436      *@param hash The feature to be added to the Hash attribute
437      *@param ele The feature to be added to the Hash attribute
438      */

439     protected void addHash(String JavaDoc hash, Element ele) {
440         this.selectedPartHash.put(hash, ele);
441     }
442
443
444     /**
445      * Adds a feature to the Param attribute of the JDomHTMLDocument object
446      *
447      *@param name The feature to be added to the Param attribute
448      *@param defaultValue The feature to be added to the Param attribute
449      */

450     protected void addParam(String JavaDoc name, String JavaDoc defaultValue) {
451         getParamsHashtable().put(name, defaultValue);
452     }
453
454
455     /**
456      * Gets the ElementFromHash attribute of the JDomHTMLDocument object
457      *
458      *@param hash Description of Parameter
459      *@return The ElementFromHash value
460      */

461     private Element getElementFromHash(String JavaDoc hash) {
462         return (Element) selectedPartHash.get(hash);
463     }
464
465
466     /**
467      * Gets the DocumentAsString attribute of the JDomHTMLDocument object
468      *
469      *@param doc Description of Parameter
470      *@return The DocumentAsString value
471      */

472     private String JavaDoc getDocumentAsString(org.jdom.Document doc) {
473         String JavaDoc res = "<p> Error !!! </p>";
474         try {
475             org.w3c.dom.Document JavaDoc domDoc = new org.jdom.output.DOMOutputter().output(doc);
476             // get a serializer
477
XMLOutputter serializer = new XMLOutputter();
478             //set the ouptput format
479
Format format = org.jdom.output.Format.getPrettyFormat();
480             String JavaDoc encoding = "ISO-8859-1";
481             format.setEncoding(encoding);
482             serializer.setFormat(format);
483
484             res = serializer.outputString(doc);
485             res = org.jahia.clipbuilder.html.util.DomUtilities.getDocumentAsString(domDoc);
486             logger.debug("[ document from neko and jdom ] ");
487
488         }
489         catch (Exception JavaDoc ex) {
490             logger.error("[document from neko and jdom: can't get the transformed document] ");
491             ex.printStackTrace();
492         }
493
494         return res;
495     }
496
497 }
498
Popular Tags