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 import java.io.File ; 10 import java.io.FileInputStream ; 11 import java.io.IOException ; 12 13 import org.jdom.Document; 15 import org.jdom.JDOMException; 16 import org.jdom.input.SAXBuilder; 17 import org.jdom.output.XMLOutputter; 18 19 24 public class ClipperDocument { 25 private Document doc = new Document(); 26 27 private ClipperParser parser; 29 30 private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(ClipperDocument.class); 32 33 34 40 public ClipperDocument(String name, String description) { 41 Element clipperEle = ElementBuilder.getInstance().buildClipper(name, description); 42 doc.setRootElement(clipperEle); 43 init(); 44 45 } 46 47 48 53 public ClipperDocument(Document doc) { 54 setDoc(doc); 55 init(); 56 } 57 58 59 64 public ClipperDocument(String documentPath) { 65 buildFromPath(documentPath); 66 init(); 67 } 68 69 70 75 public void setDoc(Document doc) { 76 this.doc = doc; 77 } 78 79 80 85 public void setName(String name) { 86 parser.getClipper().setAttribute(Label.NAME, name); 87 } 88 89 90 95 public void setFilter(FilterBean filter) { 96 Map keysMap = filter.getKeyMap(); 97 String name = filter.getName(); 98 parser.getClipper().addContent(ElementBuilder.getInstance().buildFilter(name, keysMap)); 99 } 100 101 102 107 public void setConfiguration(ConfigureBean cBean) { 108 String proxy = cBean.getProxy(); 110 String enableSSL = cBean.getEnableCSS(); 111 String enableJavascript = cBean.getEnableJavascript(); 112 String client = cBean.getClient(); 113 String htmlDocument = cBean.getHtmlDocument(); 114 String enableCSS = cBean.getEnableCSS(); 115 String browserJavascriptEvent = cBean.getBrowserJavascriptEvent(); 116 String browserJavascriptCode = cBean.getBrowserJavascriptCode(); 117 String portletEnableSSL = cBean.getPortletEnableSSL(); 118 String portletContinualClipping = cBean.getPortletContinualClipping(); 119 String portletCacheExpiration = cBean.getPortletCacheExpiration(); 120 String 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 130 public Document getDoc() { 131 return doc; 132 } 133 134 135 140 public Element getRootElement() { 141 return doc.getRootElement(); 142 } 143 144 145 150 public ClipperParser getParser() { 151 return parser; 152 } 153 154 155 160 public void addUrlWhithoutParam(UrlBean uBean) { 161 163 String base = uBean.getBaseUrlValue(); 164 String relative = uBean.getRelativeUrlValue(); 165 String absolute = uBean.getAbsoluteUrlValue(); 166 String redirect = uBean.getRedirectUrl().toExternalForm(); 167 168 String from = uBean.getFrom(); 169 String hash = uBean.getHash(); 170 Element urlElement = ElementBuilder.getInstance().buildURL(base, relative, redirect, from, hash); 171 172 parser.addUrl(absolute, urlElement); 174 175 parser.getSequenceUrl().addContent(new Comment("Next url to visit")); 177 parser.getSequenceUrl().addContent(urlElement); 178 logger.debug("[Url " + relative + " added]"); 179 } 180 181 182 187 public void addUrlWhithParam(UrlBean uBean) { 188 addUrlWhithoutParam(uBean); 190 191 List fBeanList = uBean.getFormParamBeanList(); 193 for (int i = 0; i < fBeanList.size(); i++) { 194 addUserParam(uBean, (FormParamBean) fBeanList.get(i)); 195 } 196 197 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 213 public void addUserParam(UrlBean uBean, FormParamBean fBean) { 214 String url = uBean.getAbsoluteUrlValue(); 216 217 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 Element listParamEle = parser.getListFormParam(url); 227 listParamEle.addContent(formParamEle); 228 229 logger.debug("[Form Param " + fBean.getName() + " of " + url + " added]"); 230 } 231 232 233 239 public void addQueryParam(UrlBean uBean, QueryParamBean qBean) { 240 String url = uBean.getAbsoluteUrlValue(); 242 243 Element paramEle = ElementBuilder.getInstance().buildQueryParam(qBean.getName(), qBean.getDefaultValue(), qBean.getPosition(),qBean.getUseAsDefaultValue()); 245 246 Element listParamEle = parser.getListQueryParam(url); 248 listParamEle.addContent(paramEle); 249 250 logger.debug("[Form Param " + qBean.getName() + " of " + url + " added]"); 251 } 252 253 254 260 public boolean saveInFile(String file) { 261 boolean succeded = false; 262 try { 263 XMLOutputter serializer = new XMLOutputter(); 265 serializer.setFormat(Format.getPrettyFormat()); 266 File f = new java.io.File (refactorFileName(file)); 267 logger.debug("[Try to write in file: " + f.getAbsolutePath() + " ]"); 268 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 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 294 private void buildFromPath(String path) { 295 try { 296 SAXBuilder builder = new SAXBuilder(); 297 Document doc = builder.build(new FileInputStream (new File (path))); 298 setDoc(doc); 299 } 300 catch (IOException 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 317 private void init() { 318 parser = new ClipperParser(this); 319 logger.debug("[Build instance of ClipperDocument]"); 320 } 321 322 323 329 private String refactorFileName(String file) { 330 if (!file.endsWith("xml")) { 331 file = file + ".xml"; 332 } 333 334 return file; 335 } 336 337 } 338 | Popular Tags |