1 package org.jahia.clipbuilder.html.XML; 2 3 import org.jdom.*; 4 import java.util.*; 5 import org.jahia.clipbuilder.html.util.DESUtilities; 6 7 12 public class ClipperParser { 13 private ClipperDocument clipperDoc; 14 private Element sequenceUrlEle; 15 private Element filterEle; 16 private Element configEle; 17 private Hashtable urlHashtable = new Hashtable(); 18 private Hashtable urlFormParamHashtable = new Hashtable(); 19 private Hashtable urlQueryParamHashtable = new Hashtable(); 20 private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(ClipperParser.class); 21 22 23 28 public ClipperParser(ClipperDocument doc) { 29 setClipperDoc(doc); 30 } 31 32 33 38 public void setClipperDoc(ClipperDocument clipperDoc) { 39 this.clipperDoc = clipperDoc; 40 } 41 42 43 48 public ClipperDocument getClipperDoc() { 49 return clipperDoc; 50 } 51 52 53 58 public Element getClipper() { 59 return clipperDoc.getRootElement(); 60 } 61 62 63 68 public Element getClipperName() { 69 return getClipper().getChild(Label.NAME); 70 } 71 72 73 78 public String getConfigProxy() { 79 return this.getConfiguration().getChild(Label.PROXY).getTextTrim(); 80 } 81 82 83 88 public String getConfigEnableSSL() { 89 return this.getConfiguration().getChild(Label.ENABLESSL).getTextTrim(); 90 } 91 92 93 98 public String getConfigEnableJavascript() { 99 return this.getConfiguration().getChild(Label.ENABLEJAVASCRIPT).getTextTrim(); 100 } 101 102 103 108 public String getConfigClient() { 109 return this.getConfiguration().getChild(Label.CLIENT).getTextTrim(); 110 } 111 112 113 118 public String getConfigHtmlDocument() { 119 return this.getConfiguration().getChild(Label.HTMLDOCUMENT).getTextTrim(); 120 } 121 122 123 128 public String getConfigEnableCSS() { 129 return this.getConfiguration().getChild(Label.ENABLECSS).getTextTrim(); 130 } 131 132 133 139 public String getConfigBrowserJavascriptEvent() { 140 return this.getConfiguration().getChild(Label.BROWSER_JAVASCRIPT_EVENT).getTextTrim(); 141 } 142 143 144 150 public String getConfigBrowserJavascriptCode() { 151 return this.getConfiguration().getChild(Label.BROWSER_JAVASCRIPT_CODE).getTextTrim(); 152 } 153 154 155 160 public String getPortletEnableSSL() { 161 return this.getConfiguration().getChild(Label.PORTLET_SSL).getTextTrim(); 162 } 163 164 165 170 public String getPortletContinualClipping() { 171 return this.getConfiguration().getChild(Label.PORTLET_CLIPPING).getTextTrim(); 172 } 173 174 175 180 public String getPortletCacheExpiration() { 181 return this.getConfiguration().getChild(Label.PORTLET_CACHE_EXPIRATION).getTextTrim(); 182 } 183 184 185 190 public String getPortletCacheContext() { 191 return this.getConfiguration().getChild(Label.PORTLET_CACHE_CONTEXT).getTextTrim(); 192 } 193 194 195 196 201 public Element getClipperDescription() { 202 return getClipper().getChild(Label.DESCRIPTION); 203 } 204 205 206 211 public String getName() { 212 return getClipperName().getTextTrim(); 213 } 214 215 216 222 public Element getUrl(int pos) { 223 List allUrlEle = getUrlList(); 224 225 if (pos >= allUrlEle.size()) { 226 logger.debug("[ No url at position " + pos + " ]"); 227 return null; 228 } 229 230 return (Element) allUrlEle.get(pos); 231 } 232 233 234 239 public String getFilterType() { 240 Element filter = getFilter(); 241 String type = filter.getAttributeValue(Label.FILTER_TYPE); 242 logger.debug("[ Filter type is :" + type + " ]"); 243 return type; 244 } 245 246 247 248 253 public Map getKeysMap() { 254 Element filter = getFilter(); 255 List keys = filter.getChildren(Label.KEY); 256 Map m = new HashMap(); 257 for (int i = 0; i < keys.size(); i++) { 258 Element key = (Element) keys.get(i); 259 String name = key.getAttributeValue(Label.KEY_NAME); 260 String value = key.getText(); 261 m.put(name, value); 262 } 263 return m; 264 } 265 266 267 268 274 public String getBaseUrlValue(int pos) { 275 Element urlEle = this.getUrl(pos); 276 return this.getBaseURL(urlEle); 277 } 278 279 280 286 public String getRelativeUrlValue(int pos) { 287 Element urlEle = this.getUrl(pos); 288 return this.getRelativeURL(urlEle); 289 } 290 291 292 293 299 public String getRedirectUrlValue(int pos) { 300 Element urlEle = this.getUrl(pos); 301 return this.getRedirectUrlValue(urlEle); 302 } 303 304 305 311 public String getUrlFrom(int pos) { 312 Element urlEle = this.getUrl(pos); 313 return this.getUrlFrom(urlEle); 314 } 315 316 317 323 public String getUrlHash(int pos) { 324 Element urlEle = getUrl(pos); 325 return getUrlHash(urlEle); 326 } 327 328 329 330 336 public Map getUrlQueryMap(int pos) { 337 Map queryMap = new TreeMap(); 338 Element urlEle = this.getUrl(pos); 339 340 Element queryParamsList = getListQueryParam(urlEle); 342 343 return queryMap; 344 } 345 346 347 354 public Element getQueryParam(int posUrl, int posParam) { 355 Element urlEle = this.getUrl(posUrl); 356 Element queryListEle = getListQueryParam(urlEle); 357 return (Element) queryListEle.getChildren().get(posParam); 358 } 359 360 361 362 369 public Element getQueryParam(int posUrl, String name) { 370 Element urlEle = getUrl(posUrl); 371 Element queryListEle = getListQueryParam(urlEle); 373 List queryParamList = queryListEle.getChildren(); 375 Element currentParam = null; 377 for (int i = 0; i < queryParamList.size(); i++) { 378 currentParam = (Element) queryParamList.get(i); 379 String currentName = currentParam.getAttributeValue(Label.PARAMETER_NAME); 380 if (currentName.equalsIgnoreCase(name)) { 381 break; 382 } 383 } 384 385 return currentParam; 386 } 387 388 389 396 public Element getForm(int posUrl, int posParam) { 397 Element urlEle = this.getUrl(posUrl); 398 Element formListEle = getListFormParam(urlEle); 399 return (Element) formListEle.getChildren().get(posParam); 400 } 401 402 403 410 public String getFromName(int posUrl, int posParam) { 411 Element formEle = getForm(posUrl, posParam); 412 return formEle.getAttributeValue(Label.PARAMETER_NAME); 413 } 414 415 416 423 public List getFromPossibleValue(int posUrl, int posParam) { 424 Element formEle = getForm(posUrl, posParam); 425 return formEle.getChildren(Label.PARAMETER_VALUE); 426 } 427 428 429 436 public String getFormParamMapping(int posUrl, int posParam) { 437 Element formEle = getForm(posUrl, posParam); 438 return formEle.getAttributeValue(Label.PARAMETER_MAPPING); 439 } 440 441 442 449 public String getFormFormParentName(int posUrl, int posParam) { 450 Element formEle = getForm(posUrl, posParam); 451 return formEle.getAttributeValue(Label.FORM_PARENT_NAME); 452 } 453 454 455 462 public String getFormFormParentId(int posUrl, int posParam) { 463 Element formEle = getForm(posUrl, posParam); 464 return formEle.getAttributeValue(Label.FORM_PARENT_ID); 465 } 466 467 468 475 public String getFormFormParentPosition(int posUrl, int posParam) { 476 Element formEle = getForm(posUrl, posParam); 477 return formEle.getAttributeValue(Label.FORM_PARENT_POSITION); 478 } 479 480 481 482 489 public String getQueryName(int posUrl, int posParam) { 490 Element queryEle = getQueryParam(posUrl, posParam); 491 return queryEle.getAttributeValue(Label.PARAMETER_NAME); 492 } 493 494 495 502 public String getQueryDefault(int posUrl, int posParam) { 503 Element queryEle = getQueryParam(posUrl, posParam); 504 String encrypted = queryEle.getAttributeValue(Label.PARAMETER_DEFAULT); 505 if (encrypted != null) { 506 String defaultValue = DESUtilities.getInstance().decrypt(Label.KEY_ENCRYPT, encrypted); 507 return defaultValue; 508 } 509 else { 510 return null; 511 } 512 } 513 514 515 522 public String getQueryPosition(int posUrl, int posParam) { 523 Element queryEle = getQueryParam(posUrl, posParam); 524 String pos = queryEle.getAttributeValue(Label.PARAMETER_POSITION); 525 return pos; 526 } 527 528 529 530 537 public String getFormParamVisibility(int posUrl, int posParam) { 538 Element formEle = getForm(posUrl, posParam); 539 return formEle.getAttributeValue(Label.PARAMETER_VISIBILITY); 540 } 541 542 543 550 public String getFormParamUpdate(int posUrl, int posParam) { 551 Element formEle = getForm(posUrl, posParam); 552 return formEle.getAttributeValue(Label.PARAMETER_UPDATE); 553 } 554 555 556 557 564 public String getFormParamPosition(int posUrl, int posParam) { 565 Element formEle = getForm(posUrl, posParam); 566 return formEle.getAttributeValue(Label.PARAMETER_POSITION); 567 } 568 569 570 571 578 public String getFormType(int posUrl, int posParam) { 579 Element formEle = getForm(posUrl, posParam); 580 return formEle.getAttributeValue(Label.PARAMETER_TYPE); 581 } 582 583 584 589 public String getTargetUrl() { 590 Element urlEle = getUrl(0); 591 return getBaseURL(urlEle); 592 } 593 594 595 600 public int getUrlListSize() { 601 return getUrlList().size(); 602 } 603 604 605 611 public int getUrlListFormParamSize(int posUrl) { 612 Element urlEle = this.getUrl(posUrl); 613 Element formListEle = getListFormParam(urlEle); 614 return formListEle.getChildren().size(); 615 } 616 617 618 624 public int getUrlListQueryParamSize(int posUrl) { 625 Element urlEle = this.getUrl(posUrl); 626 Element queryListEle = getListQueryParam(urlEle); 627 return queryListEle.getChildren().size(); 628 } 629 630 631 637 public String getPossibleValue(Object possibleValueEle) { 638 return ((Element) possibleValueEle).getTextTrim(); 639 } 640 641 642 649 public String getFormDefaultValue(int posUrl, String name) { 650 if (posUrl >= (getUrlListSize() - 1)) { 652 logger.debug("[ Last url]"); 653 return ""; 654 } 655 Element queryParamEle = getQueryParam(posUrl + 1, name); 657 if (queryParamEle == null) { 659 return ""; 660 } 661 return queryParamEle.getAttributeValue(Label.PARAMETER_DEFAULT); 662 } 663 664 665 671 public String getBaseURL(Element urlEle) { 672 Element value = urlEle.getChild(Label.BASE); 673 return value.getTextTrim(); 674 } 675 676 677 683 public String getRelativeURL(Element urlEle) { 684 Element value = urlEle.getChild(Label.VALUE); 685 return value.getTextTrim(); 686 } 687 688 689 690 696 public String getRedirectUrlValue(Element urlEle) { 697 Element value = urlEle.getChild(Label.FINAL_VALUE); 698 return value.getTextTrim(); 699 } 700 701 702 708 public String getUrlFrom(Element urlEle) { 709 String value = urlEle.getAttributeValue(Label.URL_FROM); 710 return value; 711 } 712 713 714 720 public String getUrlHash(Element urlEle) { 721 String value = urlEle.getAttributeValue(Label.URL_HASH); 722 return value; 723 } 724 725 726 727 732 public String getDescription() { 733 return getClipperDescription().getTextTrim(); 734 } 735 736 737 742 public Element getSequenceUrl() { 743 Element clipperEle = getClipperDoc().getRootElement(); 744 if (sequenceUrlEle == null) { 745 sequenceUrlEle = clipperEle.getChild(Label.SEQUENCEURL); 746 } 747 return sequenceUrlEle; 748 } 749 750 751 756 public Element getFilter() { 757 Element clipperEle = getClipperDoc().getRootElement(); 758 if (this.filterEle == null) { 759 filterEle = clipperEle.getChild(Label.FILTER); 760 } 761 return filterEle; 762 } 763 764 765 770 public Element getConfiguration() { 771 Element clipperEle = getClipperDoc().getRootElement(); 772 if (this.configEle == null) { 773 configEle = clipperEle.getChild(Label.CONFIGURATION); 774 } 775 return configEle; 776 } 777 778 779 780 786 public Element getListFormParam(String url) { 787 Object o = urlFormParamHashtable.get(url); 789 if (o != null) { 790 return (Element) o; 791 } 792 793 Element urlEle = getUrl(url); 795 Element formParamEle = getListFormParam(urlEle); 796 if (formParamEle == null) { 797 logger.error("[can't retrieve <form-param>]"); 798 return null; 799 } 800 urlFormParamHashtable.put(url, formParamEle); 801 return formParamEle; 802 } 803 804 805 811 public Element getListFormParam(Element urlEle) { 812 return urlEle.getChild(Label.USERPARAM); 813 } 814 815 816 822 public Element getListQueryParam(Element urlEle) { 823 return urlEle.getChild(Label.QUERYPARAM); 824 } 825 826 827 833 public Element getListQueryParam(String url) { 834 Object o = urlQueryParamHashtable.get(url); 836 if (o != null) { 837 return (Element) o; 838 } 839 840 Element urlEle = getUrl(url); 842 Element queryParamEle = getListQueryParam(urlEle); 843 if (queryParamEle == null) { 844 logger.error("[can't retrieve <query-param>]"); 845 return null; 846 } 847 urlQueryParamHashtable.put(url, queryParamEle); 848 return queryParamEle; 849 } 850 851 852 858 public Element getUrl(String url) { 859 Object o = urlHashtable.get(url); 860 if (url != null) { 862 return (Element) o; 863 } 864 else { 866 Element sequenceUrlEle = getSequenceUrl(); 868 List allUrlEle = sequenceUrlEle.getChildren(); 870 Element ele = null; 871 for (int i = 0; i < allUrlEle.size(); i++) { 872 ele = (Element) allUrlEle.get(i); 873 Element valueEle = ele.getChild(Label.VALUE); 875 if (valueEle.getTextTrim().equalsIgnoreCase(url)) { 876 urlHashtable.put(url, ele); 878 break; 879 } 880 } 881 return ele; 882 } 883 } 884 885 886 891 public ArrayList getUrlListBean() { 892 ArrayList urlBeanList = new ArrayList(); 893 return urlBeanList; 894 } 895 896 897 902 public ArrayList getUrlListBeanWithParams() { 903 ArrayList urlBeanList = new ArrayList(); 904 return urlBeanList; 905 } 906 907 908 914 public void addUrl(String url, Element urlEle) { 915 this.urlHashtable.put(url, urlEle); 916 } 917 918 919 924 private List getUrlList() { 925 Element sequenceUrlEle = getSequenceUrl(); 926 List allUrlEle = sequenceUrlEle.getChildren(); 928 return allUrlEle; 929 } 930 931 } 932 | Popular Tags |