KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > clipbuilder > html > XML > ClipperDocument


1 package org.jahia.clipbuilder.html.XML;
2
3 import org.jdom.*;
4 import org.jdom.output.*;
5 import java.io.*;
6 import java.util.*;
7 import org.jahia.clipbuilder.html.bean.*;
8 // Java imports
9
import java.io.File JavaDoc;
10 import java.io.FileInputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12
13 // JDOM imports
14
import org.jdom.Document;
15 import org.jdom.JDOMException;
16 import org.jdom.input.SAXBuilder;
17 import org.jdom.output.XMLOutputter;
18
19 /**
20  * Description of the Class
21  *
22  *@author Tlili Khaled
23  */

24 public class ClipperDocument {
25     private Document doc = new Document();
26
27     //private Element clipperEle;
28
private ClipperParser parser;
29
30     //Ressource + Logger
31
private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(ClipperDocument.class);
32
33
34     /**
35      * Constructor for the ClipperDocument object
36      *
37      *@param name Description of Parameter
38      *@param description Description of Parameter
39      */

40     public ClipperDocument(String JavaDoc name, String JavaDoc description) {
41         Element clipperEle = ElementBuilder.getInstance().buildClipper(name, description);
42         doc.setRootElement(clipperEle);
43         init();
44
45     }
46
47
48     /**
49      * Constructor for the ClipperDocument object
50      *
51      *@param doc Description of Parameter
52      */

53     public ClipperDocument(Document doc) {
54         setDoc(doc);
55         init();
56     }
57
58
59     /**
60      * Constructor for the ClipperDocument object
61      *
62      *@param documentPath Description of Parameter
63      */

64     public ClipperDocument(String JavaDoc documentPath) {
65         buildFromPath(documentPath);
66         init();
67     }
68
69
70     /**
71      * Sets the Doc attribute of the ClipperDocument object
72      *
73      *@param doc The new Doc value
74      */

75     public void setDoc(Document doc) {
76         this.doc = doc;
77     }
78
79
80     /**
81      * Sets the Name attribute of the ClipperDocument object
82      *
83      *@param name The new Name value
84      */

85     public void setName(String JavaDoc name) {
86         parser.getClipper().setAttribute(Label.NAME, name);
87     }
88
89
90     /**
91      * Sets the Filter attribute of the ClipperDocument object
92      *
93      *@param filter The new Filter value
94      */

95     public void setFilter(FilterBean filter) {
96         Map keysMap = filter.getKeyMap();
97         String JavaDoc name = filter.getName();
98         parser.getClipper().addContent(ElementBuilder.getInstance().buildFilter(name, keysMap));
99     }
100
101
102     /**
103      * Sets the Configuration attribute of the ClipperDocument object
104      *
105      *@param cBean The new Configuration value
106      */

107     public void setConfiguration(ConfigureBean cBean) {
108         // add the configuration
109
String JavaDoc proxy = cBean.getProxy();
110         String JavaDoc enableSSL = cBean.getEnableCSS();
111         String JavaDoc enableJavascript = cBean.getEnableJavascript();
112         String JavaDoc client = cBean.getClient();
113         String JavaDoc htmlDocument = cBean.getHtmlDocument();
114         String JavaDoc enableCSS = cBean.getEnableCSS();
115         String JavaDoc browserJavascriptEvent = cBean.getBrowserJavascriptEvent();
116         String JavaDoc browserJavascriptCode = cBean.getBrowserJavascriptCode();
117         String JavaDoc portletEnableSSL = cBean.getPortletEnableSSL();
118         String JavaDoc portletContinualClipping = cBean.getPortletContinualClipping();
119                 String JavaDoc portletCacheExpiration = cBean.getPortletCacheExpiration();
120                 String JavaDoc portletCacheContext = cBean.getPortletCacheContext();
121         parser.getClipper().addContent(ElementBuilder.getInstance().buildConfiguration(proxy, browserJavascriptEvent, browserJavascriptCode, enableSSL, enableJavascript, client, htmlDocument, enableCSS, portletEnableSSL, portletContinualClipping,portletCacheExpiration,portletCacheContext));
122     }
123
124
125     /**
126      * Gets the Doc attribute of the ClipperDocument object
127      *
128      *@return The Doc value
129      */

130     public Document getDoc() {
131         return doc;
132     }
133
134
135     /**
136      * Gets the RootElement attribute of the ClipperDocument object
137      *
138      *@return The RootElement value
139      */

140     public Element getRootElement() {
141         return doc.getRootElement();
142     }
143
144
145     /**
146      * Gets the Parser attribute of the ClipperDocument object
147      *
148      *@return The Parser value
149      */

150     public ClipperParser getParser() {
151         return parser;
152     }
153
154
155     /**
156      * Adds a feature to the Url attribute of the ClipperDocument object
157      *
158      *@param uBean The feature to be added to the Url attribute
159      */

160     public void addUrlWhithoutParam(UrlBean uBean) {
161         // Build the new url element
162

163         String JavaDoc base = uBean.getBaseUrlValue();
164         String JavaDoc relative = uBean.getRelativeUrlValue();
165         String JavaDoc absolute = uBean.getAbsoluteUrlValue();
166         String JavaDoc redirect = uBean.getRedirectUrl().toExternalForm();
167
168         String JavaDoc from = uBean.getFrom();
169         String JavaDoc hash = uBean.getHash();
170         Element urlElement = ElementBuilder.getInstance().buildURL(base, relative, redirect, from, hash);
171
172         // add to the manager
173
parser.addUrl(absolute, urlElement);
174
175         // add to the document
176
parser.getSequenceUrl().addContent(new Comment("Next url to visit"));
177         parser.getSequenceUrl().addContent(urlElement);
178         logger.debug("[Url " + relative + " added]");
179     }
180
181
182     /**
183      * Adds a feature to the UrlParam attribute of the ClipperDocument object
184      *
185      *@param uBean The feature to be added to the UrlParam attribute
186      */

187     public void addUrlWhithParam(UrlBean uBean) {
188         // add the url
189
addUrlWhithoutParam(uBean);
190
191         //add all the form params
192
List fBeanList = uBean.getFormParamBeanList();
193         for (int i = 0; i < fBeanList.size(); i++) {
194             addUserParam(uBean, (FormParamBean) fBeanList.get(i));
195         }
196
197         //add all the query params
198
List qBeanList = uBean.getQueryParamBeanList();
199         for (int i = 0; i < qBeanList.size(); i++) {
200             addQueryParam(uBean, (QueryParamBean) qBeanList.get(i));
201         }
202
203     }
204
205
206
207     /**
208      * Adds a feature to the Param attribute of the ClipperDocument object
209      *
210      *@param uBean The feature to be added to the Param attribute
211      *@param fBean The feature to be added to the FormParam attribute
212      */

213     public void addUserParam(UrlBean uBean, FormParamBean fBean) {
214         //Get the url element
215
String JavaDoc url = uBean.getAbsoluteUrlValue();
216
217         // build a parameter element whit its attributes
218
Element formParamEle =
219                 ElementBuilder.getInstance().buildUserParam(
220                 fBean.getFormParentName(), fBean.getFormParentId(), fBean.getFormParentPosition(),
221                 fBean.getName(), fBean.getMapping(), fBean.getType(),
222                 fBean.getListPossibleStringValue(), fBean.getVisibility(), fBean.getUpdate(), fBean.getPosition()
223                 );
224
225         // add to ListParamElement
226
Element listParamEle = parser.getListFormParam(url);
227         listParamEle.addContent(formParamEle);
228
229         logger.debug("[Form Param " + fBean.getName() + " of " + url + " added]");
230     }
231
232
233     /**
234      * Adds a feature to the QueryParam attribute of the ClipperDocument object
235      *
236      *@param uBean The feature to be added to the QueryParam attribute
237      *@param qBean The feature to be added to the QueryParam attribute
238      */

239     public void addQueryParam(UrlBean uBean, QueryParamBean qBean) {
240         //Get the url element
241
String JavaDoc url = uBean.getAbsoluteUrlValue();
242
243         // build a parameter element whit its attributes
244
Element paramEle = ElementBuilder.getInstance().buildQueryParam(qBean.getName(), qBean.getDefaultValue(), qBean.getPosition(),qBean.getUseAsDefaultValue());
245
246         // add to ListParamElement
247
Element listParamEle = parser.getListQueryParam(url);
248         listParamEle.addContent(paramEle);
249
250         logger.debug("[Form Param " + qBean.getName() + " of " + url + " added]");
251     }
252
253
254     /**
255      * save the document in a file
256      *
257      *@param file patn and name of the file
258      *@return Description of the Returned Value
259      */

260     public boolean saveInFile(String JavaDoc file) {
261         boolean succeded = false;
262         try {
263             //Print in a file
264
XMLOutputter serializer = new XMLOutputter();
265             serializer.setFormat(Format.getPrettyFormat());
266             File JavaDoc f = new java.io.File JavaDoc(refactorFileName(file));
267             logger.debug("[Try to write in file: " + f.getAbsolutePath() + " ]");
268             // create the file if it doesn't exist
269
if (!f.exists()) {
270                 succeded = f.createNewFile();
271             }
272             serializer.output(doc, new FileOutputStream(f));
273             succeded = true;
274             logger.debug("[Clipper document saved in file: " + f.getAbsolutePath() +
275                     " ]");
276         }
277         catch (Exception JavaDoc ex) {
278             logger.error("[Error occured while writting in the file] ");
279             succeded = false;
280             ex.printStackTrace();
281         }
282         finally {
283             return succeded;
284         }
285
286     }
287
288
289     /**
290      * Description of the Method
291      *
292      *@param path Description of Parameter
293      */

294     private void buildFromPath(String JavaDoc path) {
295         try {
296             SAXBuilder builder = new SAXBuilder();
297             Document doc = builder.build(new FileInputStream JavaDoc(new File JavaDoc(path)));
298             setDoc(doc);
299         }
300         catch (IOException JavaDoc ex) {
301             logger.error("[ IOException " + ex.getMessage() + "]");
302             logger.error("[ Path is: " + path + " ]");
303             ex.printStackTrace();
304         }
305         catch (JDOMException ex) {
306             logger.error("[ JDOMException " + ex.getMessage() + "]");
307             logger.error("[ Path is: " + path + " ]");
308             ex.printStackTrace();
309         }
310
311     }
312
313
314     /**
315      * Description of the Method
316      */

317     private void init() {
318         parser = new ClipperParser(this);
319         logger.debug("[Build instance of ClipperDocument]");
320     }
321
322
323     /**
324      * add extension
325      *
326      *@param file Description of Parameter
327      *@return Description of the Returned Value
328      */

329     private String JavaDoc refactorFileName(String JavaDoc file) {
330         if (!file.endsWith("xml")) {
331             file = file + ".xml";
332         }
333
334         return file;
335     }
336
337 }
338
Popular Tags