1 package org.jahia.clipbuilder.html.bean; 2 3 import java.util.*; 4 5 import org.jahia.clipbuilder.html.XML.*; 6 import org.jahia.clipbuilder.html.struts.*; 7 import org.jahia.clipbuilder.html.util.*; 8 9 14 public class ClipperBean extends Bean { 15 16 private String description; 18 private String name; 19 private String targetUrl; 20 21 private ConfigureBean configurationBean; 23 24 private PortletConfigBean portletConfigBean; 26 27 private FilterBean filterBean; 29 30 private List urlListBean = new ArrayList(); 32 private boolean hasBeanInit = false; 33 34 private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(ClipperBean.class); 35 36 37 40 public ClipperBean() { 41 42 } 43 44 45 50 public ClipperBean(String xmlPath) { 51 loadWhithoutUrlBeanListFromXML(xmlPath); 52 setHasBeanInit(false); 53 54 } 55 56 57 62 public ClipperBean(DescriptionClipperForm form) { 63 buildFromDescriptionForm(form); 64 setHasBeanInit(true); 65 } 66 67 68 69 72 79 84 public void setDescription(String description) { 85 this.description = description; 86 } 87 88 89 94 public void setName(String name) { 95 this.name = name; 96 } 97 98 99 104 public void setTargetUrl(String targetUrl) { 105 this.targetUrl = targetUrl; 107 } 108 109 110 111 116 public void setUrlListBean(List urlListBean) { 117 this.urlListBean = urlListBean; 118 } 119 120 121 126 public void setHasBeanInit(boolean hasBeanInit) { 127 this.hasBeanInit = hasBeanInit; 128 } 129 130 131 136 public void setConfigurationBean(ConfigureBean configurationBean) { 137 this.configurationBean = configurationBean; 138 } 139 140 141 146 public void setFilterBean(FilterBean filterBean) { 147 this.filterBean = filterBean; 148 } 149 150 151 156 public void setPortletConfigBean(PortletConfigBean portletConfigBean) { 157 this.portletConfigBean = portletConfigBean; 158 } 159 160 161 166 public void setTargetUrlResources(UrlBean uBean) { 167 setTargetUrl(uBean.getAbsoluteUrlValue()); 168 setUrlListBean(new ArrayList()); 169 addUrlBean(uBean); 170 } 171 172 173 179 public UrlBean getUrlBean(int pos) { 180 UrlBean uBean = null; 181 try { 182 uBean = (UrlBean) getUrlListBean().get(pos); 183 if (uBean == null) { 184 logger.error("[ UrlBean at position " + pos +" not found ]"); 185 } 186 else { 187 logger.debug("[ UrlBean at position " + pos +" found ]"); 188 } 189 } 190 catch (Exception ex) { 191 logger.error("Error: "+ex.toString()); 192 ex.printStackTrace(); 193 } 194 return uBean; 195 } 196 197 198 203 public String getLastRecordedUrlValue() { 204 UrlBean uBean = getLastRecordedUrlBean(); 205 return uBean.getAbsoluteUrlValue(); 206 } 207 208 209 214 public UrlBean getLastRecordedUrlBean() { 215 if (urlListIsEmpty()) { 216 logger.debug("[ Url list bean is empty ]"); 217 return null; 218 } 219 Object o = getUrlListBean().get(getPositionLastRecordeUrl()); 220 return (UrlBean) o; 221 } 222 223 224 230 public UrlBean getNextRecordedUrlBean(int posUrl) { 231 UrlBean uBean = this.getUrlBean(posUrl); 232 return this.getNextRecordedUrlBean(uBean); 233 } 234 235 236 242 public UrlBean getNextRecordedUrlBean(UrlBean uBean) { 243 int currentIndex = getUrlListBean().indexOf(uBean); 244 int indexNextUrl = currentIndex + 1; 245 if (indexNextUrl > (getUrlListBean().size() - 1)) { 247 logger.debug("[url " + uBean.getAbsoluteUrlValue() + 248 " is the last recorded bean]"); 249 return null; 250 } 251 252 UrlBean nextUrlBean = (UrlBean) getUrlListBean().get(indexNextUrl); 253 logger.debug("[nextUrl of " + uBean.getAbsoluteUrlValue() + " is: " + 254 nextUrlBean.getAbsoluteUrlValue() + " ]"); 255 261 return nextUrlBean; 262 } 263 264 265 272 public String getFormParamDefaultValue(UrlBean uBean, String paramName) { 273 UrlBean nextBean = getNextRecordedUrlBean(uBean); 275 276 if (nextBean == null) { 277 return "no value used"; 278 } 279 return nextBean.getQueryParamDefaultValue(paramName); 280 } 281 282 283 290 public String getFormParamDefaultValue(int posUrl, String paramName) { 291 UrlBean uBean = getUrlBean(posUrl); 293 return getFormParamDefaultValue(uBean, paramName); 294 } 295 296 297 302 public String getDescription() { 303 return description; 304 } 305 306 307 312 public String getName() { 313 return name; 314 } 315 316 317 322 public String getTargetUrl() { 323 return targetUrl; 324 } 325 326 327 328 333 public List getUrlListBean() { 334 return urlListBean; 335 } 336 337 338 343 public ConfigureBean getConfigurationBean() { 344 351 return configurationBean; 352 } 353 354 355 360 public FilterBean getFilterBean() { 361 return filterBean; 362 } 363 364 365 370 public PortletConfigBean getPortletConfigBean() { 371 return portletConfigBean; 372 } 373 374 375 380 public boolean isEmpty() { 381 return this.getUrlListBean().isEmpty(); 382 } 383 384 385 391 public List getAllFormParam(int posUrl) { 392 UrlBean bean = getUrlBean(posUrl); 393 logger.debug("[ url at position " + posUrl + " whith value " + bean.getAbsoluteUrlValue() + " in process]"); 394 return bean.getFormParamBeanList(); 395 } 396 397 398 404 public List getAllQueryParam(int posUrl) { 405 UrlBean bean = getUrlBean(posUrl); 406 return bean.getQueryParamBeanList(); 407 } 408 409 410 421 public String getParameterMapping(int posUrl, String formParentName, String formParentId, int formParentPos, String paramName, int paramPosition) { 422 UrlBean bean = this.getUrlBean(posUrl); 423 return bean.getFormParameterMapping(formParentName, formParentId, formParentPos, paramName, paramPosition); 424 } 425 426 427 434 public String getParameterDefaultValue(int posUrl, String param) { 435 UrlBean bean = this.getUrlBean(posUrl); 436 return bean.getQueryParamDefaultValue(param); 437 } 438 439 440 446 public String getFirstParamName(int posUrl) { 447 UrlBean bean = this.getUrlBean(posUrl); 448 return bean.getFirtParamName(); 449 } 450 451 452 463 public String getParamType(int posUrl, String formParentName, String formParentId, int formParentPos, String paramName, int paramPosition) { 464 UrlBean bean = this.getUrlBean(posUrl); 465 return bean.getFormParamType(formParentName, formParentId, formParentPos, paramName, paramPosition); 466 } 467 468 469 480 public ArrayList getParamAllowedValuesBeanList(int posUrl, String formParentName, String formParentId, int formParentPos, String paramName, int paramPosition) { 481 UrlBean uBean = getUrlBean(posUrl); 482 return uBean.getParamFormAllowedValueBeanList(formParentName, formParentId, formParentPos, paramName, paramPosition); 483 } 484 485 486 497 public ArrayList getParamAllowedValuesStringList(int posUrl, String formParentName, String formParentId, int formParentPos, String paramName, int paramPosition) { 498 UrlBean uBean = this.getUrlBean(posUrl); 499 return uBean.getParamFormAllowedValueStringList(formParentName, formParentId, formParentPos, paramName, paramPosition); 500 } 501 502 503 508 public int getPositionLastRecordeUrl() { 509 return getUrlListBean().size() - 1; 510 } 511 512 513 518 public boolean urlListIsEmpty() { 519 return getUrlListBean().isEmpty(); 520 } 521 522 523 528 529 public void loadFromXml(String xmlPath) { 530 ClipperDocument document = new ClipperDocument(xmlPath); 532 ClipperParser parser = document.getParser(); 533 setDescription(parser.getDescription()); 534 setName(parser.getName()); 535 setTargetUrl(parser.getTargetUrl()); 536 537 int urlListSize = parser.getUrlListSize(); 539 for (int i = 0; i < urlListSize; i++) { 540 String baseURL = parser.getBaseUrlValue(i); 541 String relativeURL = parser.getRelativeUrlValue(i); 542 String finalValue = parser.getRedirectUrlValue(i); 543 String from = parser.getUrlFrom(i); 544 String hash = parser.getUrlHash(i); 545 Map queryMap = parser.getUrlQueryMap(i); 546 547 UrlBean uBean = new UrlBean(this, baseURL, relativeURL, queryMap, from, hash); 549 uBean.setRedirectUrl(URLUtilities.getURL(finalValue)); 550 551 int formSize = parser.getUrlListFormParamSize(i); 553 for (int j = 0; j < formSize; j++) { 554 String name = parser.getFromName(i, j); 556 String type = parser.getFormType(i, j); 557 String visibility = parser.getFormParamVisibility(i, j); 558 String update = parser.getFormParamUpdate(i, j); 559 int paramPosition = Integer.parseInt(parser.getFormParamPosition(i, j)); 560 List possibleValueList = parser.getFromPossibleValue(i, j); 561 String formParentName = parser.getFormFormParentName(i, j); 562 String formParentId = parser.getFormFormParentId(i, j); 563 int formParentPosition = Integer.parseInt(parser.getFormFormParentPosition(i, j)); 564 565 FormParamBean fBean = null; 566 for (int k = 0; k < possibleValueList.size(); k++) { 567 String possibleValue = parser.getPossibleValue(possibleValueList.get(k)); 568 fBean = uBean.addFormParameter(formParentName, formParentId, formParentPosition, name, possibleValue, type, visibility, paramPosition); 570 } 571 String mapping = parser.getFormParamMapping(i, j); 573 fBean.setUpdate(update); 574 uBean.setMappingFormParam(formParentName, formParentId, formParentPosition, name, mapping, paramPosition); 575 576 } 577 578 int querySize = parser.getUrlListQueryParamSize(i); 580 logger.debug("Query param size " + querySize + " ]"); 581 for (int j = 0; j < querySize; j++) { 582 String name = parser.getQueryName(i, j); 584 String value = parser.getQueryDefault(i, j); 585 logger.debug("[ Query param whith name " + name + " and value " + value + " found ]"); 586 int qPosition = Integer.parseInt(parser.getQueryPosition(i, j)); 587 QueryParamBean qBean = uBean.addQueryParameter(name, value, qPosition); 588 if (value == null) { 589 qBean.setUseAsDefaultValue("false"); 590 } 591 else { 592 qBean.setUseAsDefaultValue("true"); 593 } 594 if (i > 0) { 596 UrlBean previousUrlBean = getUrlBean(i - 1); 597 FormParamBean fBean = previousUrlBean.getFormParamByNameAndFormParentHash(name, qPosition, hash); 598 if (fBean != null) { 599 fBean.setQueryParamBean(qBean); 600 } 601 } 602 603 } 604 605 addUrlBean(uBean); 607 } 608 609 String typeFilter = parser.getFilterType(); 611 if (typeFilter == null) { 612 logger.error("[Filter type is null ]"); 613 } 614 else { 615 logger.debug("[Filter type is " + typeFilter + " ]"); 616 FilterBean filterBean = new FilterBean(); 617 filterBean.setName(typeFilter); 618 if (filterBean == null) { 619 logger.error("Unexpected type of filter"); 620 logger.warn("Filter not initialised"); 621 setHasBeanInit(false); 622 } 623 else { 624 Map keysMap = parser.getKeysMap(); 626 Iterator it = keysMap.keySet().iterator(); 627 while (it.hasNext()) { 628 String name = (String ) it.next(); 629 String value = (String ) keysMap.get(name); 630 filterBean.addKeyPart(name, value); 631 } 632 633 setFilterBean(filterBean); 635 } 636 } 637 638 ConfigureBean config = new ConfigureBean(); 640 String proxy = parser.getConfigProxy(); 641 String enableSSL = parser.getConfigEnableSSL(); 642 String enableJavascript = parser.getConfigEnableJavascript(); 643 String client = parser.getConfigClient(); 644 String htmlDocument = parser.getConfigHtmlDocument(); 645 String enableCSS = parser.getConfigEnableCSS(); 646 String browserJavascriptEvent = parser.getConfigBrowserJavascriptEvent(); 647 String browserJavascriptCode = parser.getConfigBrowserJavascriptCode(); 648 String portletEnableSSL = parser.getPortletEnableSSL(); 649 String portletContinualClipping = parser.getPortletContinualClipping(); 650 String portletCacheExpiration = parser.getPortletCacheExpiration(); 651 String portletCacheContext = parser.getPortletCacheContext(); 652 653 config.setProxy(proxy); 654 config.setEnableCSS(enableCSS); 655 config.setEnableJavascript(enableJavascript); 656 config.setClient(client); 657 config.setHtmlDocument(htmlDocument); 658 config.setEnableSSL(enableSSL); 659 config.setBrowserJavascriptCode(browserJavascriptCode); 660 config.setBrowserJavascriptEvent(browserJavascriptEvent); 661 config.setPortletEnableSSL(portletEnableSSL); 662 config.setPortletContinualClipping(portletContinualClipping); 663 config.setPortletCacheExpiration(portletCacheExpiration); 664 config.setPortletCacheContext(portletCacheContext); 665 setConfigurationBean(config); 666 667 setHasBeanInit(true); 669 670 } 671 672 673 674 675 680 public boolean hasBeanInit() { 681 return hasBeanInit; 682 } 683 684 685 688 public void removeLastRecordedUrl() { 689 getUrlListBean().remove(getUrlListBean().size() - 1); 690 } 691 692 693 696 public void clearUrlParam() { 697 for (int i = 0; i < this.getUrlListBean().size(); i++) { 698 UrlBean uBean = (UrlBean) this.getUrlListBean().get(i); 699 uBean.clearParam(); 700 } 701 702 } 703 704 705 724 public void addFormParam(int posUrl, String formParentName, String formParentId, int formParentPosition, String param, String possibleValue, String type, String visibility, int paramPosition) { 725 UrlBean bean = this.getUrlBean(posUrl); 726 logger.debug("[Param " + param + " whith possible value " + possibleValue + " is being added to url " + bean.getAbsoluteUrlValue() + " ]"); 727 bean.addFormParameter(formParentName, formParentId, formParentPosition, param, possibleValue, type, visibility, paramPosition); 728 } 729 730 731 752 public void addFormParamToCurrentUrl(String formParentName, String formParentId, int formParentPosition, String param, String possibleValue, String type, String visibility, int paramPosition) { 753 int posUrl = getPositionLastRecordeUrl(); 754 addFormParam(posUrl, formParentName, formParentId, formParentPosition, param, possibleValue, type, visibility, paramPosition); 755 } 756 757 758 763 public void saveAsXML(String path) { 764 ClipperDocument doc = buildXmlDocument(); 765 766 if (path == null) { 768 logger.error("[ Path is null. ]"); 769 return; 770 } 771 String file = path + java.io.File.separator + getName(); 772 doc.saveInFile(file); 773 logger.debug("[ Saved in " + file + " ]"); 774 } 775 776 777 780 public void saveAsPortlet() { 781 } 783 784 785 790 public void addUrlBean(UrlBean u) { 791 int pos = getUrlListBean().size(); 792 logger.debug("[ Url " + u.getAbsoluteUrlValue() + " added at position " + pos + " ]"); 793 u.setPosition(pos); 794 if (!getUrlListBean().add(u)) { 795 logger.error("[Error occured when adding Url " + u.getAbsoluteUrlValue() + " added at position " + pos + " ]"); 796 } 797 } 798 799 800 805 public void buildFromDescriptionForm(DescriptionClipperForm form) { 806 setDescription(form.getWebClippingDescription()); 807 setName(form.getWebClippingName()); 808 setTargetUrl(form.getWebClippingTargetUrl()); 809 clearRecordedUrl(); 810 } 811 812 813 816 public void clearRecordedUrl() { 817 setUrlListBean(new ArrayList()); 818 } 819 820 821 826 public ClipperDocument buildXmlDocument() { 827 ClipperDocument doc = new ClipperDocument(getName(), getDescription()); 829 830 doc.setConfiguration(getConfigurationBean()); 832 833 List urlBeanList = this.getUrlListBean(); 835 for (int i = 0; i < urlBeanList.size(); i++) { 836 UrlBean uBean = (UrlBean) urlBeanList.get(i); 838 doc.addUrlWhithParam(uBean); 839 } 840 841 doc.setFilter(getFilterBean()); 843 return doc; 844 } 845 846 847 852 private void loadWhithoutUrlBeanListFromXML(String name) { 853 ClipperDocument document = new ClipperDocument(name); 855 ClipperParser parser = document.getParser(); 856 setDescription(parser.getDescription()); 857 setName(parser.getName()); 858 setTargetUrl(parser.getTargetUrl()); 859 } 860 861 } 862 | Popular Tags |