1 31 32 package org.opencms.workplace.comparison; 33 34 import org.opencms.file.CmsFile; 35 import org.opencms.file.CmsObject; 36 import org.opencms.file.CmsResourceFilter; 37 import org.opencms.file.types.CmsResourceTypeImage; 38 import org.opencms.file.types.CmsResourceTypeJsp; 39 import org.opencms.file.types.CmsResourceTypePlain; 40 import org.opencms.file.types.CmsResourceTypePointer; 41 import org.opencms.file.types.CmsResourceTypeXmlContent; 42 import org.opencms.file.types.CmsResourceTypeXmlPage; 43 import org.opencms.file.types.I_CmsResourceType; 44 import org.opencms.jsp.CmsJspActionElement; 45 import org.opencms.main.CmsException; 46 import org.opencms.main.CmsLog; 47 import org.opencms.main.OpenCms; 48 import org.opencms.search.extractors.CmsExtractorMsExcel; 49 import org.opencms.search.extractors.CmsExtractorMsPowerPoint; 50 import org.opencms.search.extractors.CmsExtractorMsWord; 51 import org.opencms.search.extractors.CmsExtractorPdf; 52 import org.opencms.search.extractors.CmsExtractorRtf; 53 import org.opencms.search.extractors.I_CmsTextExtractor; 54 import org.opencms.util.CmsStringUtil; 55 import org.opencms.widgets.I_CmsWidget; 56 import org.opencms.widgets.I_CmsWidgetParameter; 57 import org.opencms.workplace.CmsDialog; 58 import org.opencms.workplace.CmsWorkplaceSettings; 59 import org.opencms.workplace.commons.CmsHistoryList; 60 import org.opencms.workplace.list.CmsMultiListDialog; 61 import org.opencms.xml.CmsXmlException; 62 import org.opencms.xml.I_CmsXmlDocument; 63 import org.opencms.xml.content.CmsXmlContent; 64 import org.opencms.xml.content.CmsXmlContentFactory; 65 import org.opencms.xml.content.I_CmsXmlContentValueVisitor; 66 import org.opencms.xml.page.CmsXmlPage; 67 import org.opencms.xml.page.CmsXmlPageFactory; 68 import org.opencms.xml.types.I_CmsXmlContentValue; 69 70 import java.io.UnsupportedEncodingException ; 71 import java.util.ArrayList ; 72 import java.util.Iterator ; 73 import java.util.List ; 74 import java.util.Locale ; 75 76 import javax.servlet.http.HttpServletRequest ; 77 import javax.servlet.http.HttpServletResponse ; 78 import javax.servlet.jsp.PageContext ; 79 80 import org.apache.commons.logging.Log; 81 82 91 public class CmsResourceComparisonDialog extends CmsDialog { 92 93 96 class CmsXmlContentTextExtractor implements I_CmsXmlContentValueVisitor { 97 98 99 private StringBuffer m_buffer; 100 101 102 private List m_locales; 103 104 109 CmsXmlContentTextExtractor(StringBuffer stringBuffer) { 110 111 m_buffer = stringBuffer; 112 m_locales = new ArrayList (); 113 } 114 115 119 public void visit(I_CmsXmlContentValue value) { 120 121 if (value.isSimpleType()) { 123 String locale = value.getLocale().toString(); 124 if (!m_locales.contains(locale)) { 125 m_buffer.append("\n\n[").append(locale).append(']'); 126 m_locales.add(locale); 127 } 128 m_buffer.append("\n\n[").append(value.getPath()).append("]\n\n"); 129 try { 130 I_CmsWidget widget = value.getDocument().getContentDefinition().getContentHandler().getWidget(value); 131 m_buffer.append(widget.getWidgetStringValue( 132 getCms(), 133 new CmsFileInfoDialog(getJsp()), 134 (I_CmsWidgetParameter)value)); 135 } catch (CmsXmlException e) { 136 LOG.error(e.getMessage(), e); 137 } 138 } 139 } 140 } 141 142 143 public static final String COMPARE_ALL_ELEMENTS = "allelements"; 144 145 146 public static final String COMPARE_ATTRIBUTES = "attributes"; 147 148 149 public static final String COMPARE_PROPERTIES = "properties"; 150 151 152 static final Log LOG = CmsLog.getLog(CmsResourceComparisonDialog.class); 153 154 155 private CmsDifferenceDialog m_differenceDialog; 156 157 158 private String m_paramCompare; 159 160 161 private String m_paramElement; 162 163 164 private String m_paramLocale; 165 166 167 private String m_paramPath1; 168 169 170 private String m_paramPath2; 171 172 173 private String m_paramTagId1; 174 175 176 private String m_paramTagId2; 177 178 179 private String m_paramTextmode; 180 181 182 private String m_paramVersion1; 183 184 185 private String m_paramVersion2; 186 187 192 public CmsResourceComparisonDialog(CmsJspActionElement jsp) { 193 194 super(jsp); 195 } 196 197 204 public CmsResourceComparisonDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) { 205 206 this(new CmsJspActionElement(context, req, res)); 207 } 208 209 221 protected static CmsFile readFile(CmsObject cms, String path, String version, int tagId) throws CmsException { 222 223 try { 224 cms.getRequestContext().saveSiteRoot(); 225 cms.getRequestContext().setSiteRoot("/"); 226 if (CmsHistoryList.OFFLINE_PROJECT.equals(version)) { 227 return cms.readFile(cms.getRequestContext().removeSiteRoot(path), CmsResourceFilter.ALL); 228 } else { 229 return cms.readBackupFile(cms.getRequestContext().removeSiteRoot(path), tagId); 230 } 231 } finally { 232 cms.getRequestContext().restoreSiteRoot(); 233 } 234 } 235 236 241 public void displayDialog() throws Exception { 242 243 CmsFileInfoDialog fileInfo = new CmsFileInfoDialog(getJsp()) { 244 245 protected String defaultActionHtmlEnd() { 246 247 return ""; 248 } 249 }; 250 fileInfo.displayDialog(true); 251 if (fileInfo.isForwarded()) { 252 return; 253 } 254 255 CmsPropertyComparisonList propertyDiff = new CmsPropertyComparisonList(getJsp()); 256 CmsAttributeComparisonList attributeDiff = new CmsAttributeComparisonList(getJsp()); 257 List lists = new ArrayList (); 258 lists.add(attributeDiff); 259 I_CmsResourceType resourceType = OpenCms.getResourceManager().getResourceType(propertyDiff.getResourceType()); 260 261 if (resourceType instanceof CmsResourceTypeXmlContent || resourceType instanceof CmsResourceTypeXmlPage) { 262 263 CmsElementComparisonList contentDiff = new CmsElementComparisonList(getJsp()); 265 lists.add(contentDiff); 266 lists.add(propertyDiff); 267 CmsMultiListDialog threeLists = new CmsMultiListDialog(lists); 268 threeLists.displayDialog(true); 270 if (threeLists.isForwarded()) { 272 return; 273 } 274 fileInfo.writeDialog(); 275 threeLists.writeDialog(); 276 } else if (resourceType instanceof CmsResourceTypeImage) { 277 278 lists.add(propertyDiff); 280 CmsMultiListDialog twoLists = new CmsMultiListDialog(lists) { 281 282 public String defaultActionHtmlEnd() { 283 284 return ""; 285 } 286 }; 287 twoLists.displayDialog(true); 288 if (twoLists.isForwarded()) { 289 return; 290 } 291 CmsImageComparisonDialog images = new CmsImageComparisonDialog(getJsp()); 292 fileInfo.writeDialog(); 293 twoLists.writeDialog(); 294 images.displayDialog(); 298 } else if (resourceType instanceof CmsResourceTypePointer) { 299 300 lists.add(propertyDiff); 301 CmsMultiListDialog twoLists = new CmsMultiListDialog(lists) { 302 303 public String defaultActionHtmlEnd() { 304 305 return ""; 306 } 307 }; 308 twoLists.displayDialog(true); 309 if (twoLists.isForwarded()) { 310 return; 311 } 312 CmsPointerComparisonDialog pointers = new CmsPointerComparisonDialog(getJsp()); 313 fileInfo.writeDialog(); 314 twoLists.writeDialog(); 315 pointers.displayDialog(); 317 } else { 318 319 lists.add(propertyDiff); 321 CmsMultiListDialog twoLists = new CmsMultiListDialog(lists); 322 twoLists.displayDialog(true); 323 if (twoLists.isForwarded()) { 324 return; 325 } 326 327 String path1 = propertyDiff.getParamPath1(); 328 String path2 = propertyDiff.getParamPath2(); 329 330 byte[] content1 = propertyDiff.getFile1().getContents(); 331 byte[] content2 = propertyDiff.getFile2().getContents(); 332 333 String originalSource = null; 334 String copySource = null; 335 336 I_CmsTextExtractor textExtractor = null; 337 if (path1.endsWith(".pdf") && path2.endsWith(".pdf")) { 338 textExtractor = CmsExtractorPdf.getExtractor(); 339 } else if (path1.endsWith(".doc") && path2.endsWith(".doc")) { 340 textExtractor = CmsExtractorMsWord.getExtractor(); 341 } else if (path1.endsWith(".xls") && path2.endsWith(".xls")) { 342 textExtractor = CmsExtractorMsExcel.getExtractor(); 343 } else if (path1.endsWith(".rtf") && path2.endsWith(".rtf")) { 344 textExtractor = CmsExtractorRtf.getExtractor(); 345 } else if (path1.endsWith(".ppt") && path2.endsWith(".ppt")) { 346 textExtractor = CmsExtractorMsPowerPoint.getExtractor(); 347 } 348 if (textExtractor != null) { 349 originalSource = textExtractor.extractText(content1).getContent(); 351 copySource = textExtractor.extractText(content2).getContent(); 352 } else if (resourceType instanceof CmsResourceTypePlain || resourceType instanceof CmsResourceTypeJsp) { 353 originalSource = new String (content1); 354 copySource = new String (content2); 355 } 356 fileInfo.writeDialog(); 357 twoLists.writeDialog(); 358 if (CmsStringUtil.isNotEmpty(originalSource) && CmsStringUtil.isNotEmpty(copySource)) { 359 m_differenceDialog.setCopySource(copySource); 360 m_differenceDialog.setOriginalSource(originalSource); 361 m_differenceDialog.displayDialog(); 363 } 364 } 365 } 366 367 371 public void displayDifferenceDialog() throws Exception { 372 373 m_differenceDialog.displayDialog(); 374 } 375 376 382 public String [] getAttributesAsString(List attributes) { 383 384 Iterator i = attributes.iterator(); 385 StringBuffer res1 = new StringBuffer (512); 386 StringBuffer res2 = new StringBuffer (512); 387 while (i.hasNext()) { 388 CmsAttributeComparison compare = (CmsAttributeComparison)i.next(); 389 res1.append(key(compare.getName())).append(": ").append(compare.getVersion1()).append("\n"); 390 res2.append(key(compare.getName())).append(": ").append(compare.getVersion2()).append("\n"); 391 } 392 return new String [] {res1.toString(), res2.toString()}; 393 } 394 395 400 public String getParamCompare() { 401 402 return m_paramCompare; 403 } 404 405 410 public String getParamElement() { 411 412 return m_paramElement; 413 } 414 415 420 public String getParamLocale() { 421 422 return m_paramLocale; 423 } 424 425 430 public String getParamPath1() { 431 432 return m_paramPath1; 433 } 434 435 440 public String getParamPath2() { 441 442 return m_paramPath2; 443 } 444 445 450 public String getParamTagId1() { 451 452 return m_paramTagId1; 453 } 454 455 460 public String getParamTagId2() { 461 462 return m_paramTagId2; 463 } 464 465 470 public String getParamTextmode() { 471 472 return m_paramTextmode; 473 } 474 475 480 public String getParamVersion1() { 481 482 return m_paramVersion1; 483 } 484 485 490 public String getParamVersion2() { 491 492 return m_paramVersion2; 493 } 494 495 501 public String [] getPropertiesAsString(List properties) { 502 503 Iterator i = properties.iterator(); 504 StringBuffer res1 = new StringBuffer (512); 505 StringBuffer res2 = new StringBuffer (512); 506 while (i.hasNext()) { 507 CmsAttributeComparison compare = (CmsAttributeComparison)i.next(); 508 res1.append(compare.getName()).append(": ").append(compare.getVersion1()).append("\n"); 509 res2.append(compare.getName()).append(": ").append(compare.getVersion2()).append("\n"); 510 } 511 return new String [] {res1.toString(), res2.toString()}; 512 } 513 514 519 public void setParamCompare(String paramCompare) { 520 521 m_paramCompare = paramCompare; 522 } 523 524 529 public void setParamElement(String paramElement) { 530 531 m_paramElement = paramElement; 532 } 533 534 539 public void setParamLocale(String paramLocale) { 540 541 m_paramLocale = paramLocale; 542 } 543 544 549 public void setParamPath1(String paramPath1) { 550 551 m_paramPath1 = paramPath1; 552 } 553 554 559 public void setParamPath2(String paramPath2) { 560 561 m_paramPath2 = paramPath2; 562 } 563 564 569 public void setParamTagId1(String paramTagId1) { 570 571 m_paramTagId1 = paramTagId1; 572 } 573 574 579 public void setParamTagId2(String paramTagId2) { 580 581 m_paramTagId2 = paramTagId2; 582 } 583 584 589 public void setParamTextmode(String paramTextmode) { 590 591 m_paramTextmode = paramTextmode; 592 } 593 594 599 public void setParamVersion1(String paramVersion1) { 600 601 m_paramVersion1 = paramVersion1; 602 } 603 604 609 public void setParamVersion2(String paramVersion2) { 610 611 m_paramVersion2 = paramVersion2; 612 } 613 614 617 protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) { 618 619 super.initWorkplaceRequestValues(settings, request); 620 try { 621 CmsFile file1 = CmsResourceComparisonDialog.readFile( 622 getCms(), 623 getParamPath1(), 624 getParamVersion1(), 625 Integer.parseInt(getParamTagId1())); 626 CmsFile file2 = CmsResourceComparisonDialog.readFile( 627 getCms(), 628 getParamPath2(), 629 getParamVersion2(), 630 Integer.parseInt(getParamTagId2())); 631 if (CmsStringUtil.isNotEmpty(getParamElement())) { 633 m_differenceDialog = new CmsHtmlDifferenceDialog(getJsp()); 634 } else { 635 m_differenceDialog = new CmsDifferenceDialog(getJsp()); 636 } 637 if (CmsResourceComparisonDialog.COMPARE_ATTRIBUTES.equals(getParamCompare())) { 638 List comparedAttributes = CmsResourceComparison.compareAttributes(getCms(), file1, file2); 639 String [] attributeStrings = getAttributesAsString(comparedAttributes); 640 m_differenceDialog.setOriginalSource(attributeStrings[0]); 641 m_differenceDialog.setCopySource(attributeStrings[1]); 642 } else if (CmsResourceComparisonDialog.COMPARE_PROPERTIES.equals(getParamCompare())) { 643 List comparedProperties = CmsResourceComparison.compareProperties(getCms(), file1, file2); 644 String [] propertyStrings = getPropertiesAsString(comparedProperties); 645 m_differenceDialog.setOriginalSource(propertyStrings[0]); 646 m_differenceDialog.setCopySource(propertyStrings[1]); 647 } else { 648 649 setContentAsSource(file1, file2); 650 } 651 652 } catch (CmsException e) { 653 654 LOG.error(e.getMessage(), e); 655 } catch (UnsupportedEncodingException e) { 656 657 LOG.error(e.getMessage(), e); 658 } 659 } 660 661 667 private String extractElements(I_CmsXmlDocument xmlDoc) { 668 669 StringBuffer result = new StringBuffer (); 670 if (xmlDoc instanceof CmsXmlPage) { 671 List locales = xmlDoc.getLocales(); 672 Iterator i = locales.iterator(); 673 boolean firstIter = true; 674 while (i.hasNext()) { 675 if (!firstIter) { 676 result.append("\n\n-----"); 677 } 678 Locale locale = (Locale )i.next(); 679 result.append("\n\n[").append(locale.toString()).append(']'); 680 List elements = xmlDoc.getValues(locale); 681 Iterator j = elements.iterator(); 682 while (j.hasNext()) { 683 I_CmsXmlContentValue value = (I_CmsXmlContentValue)j.next(); 684 result.append("\n\n["); 685 result.append(value.getElement().attribute(0).getValue()); 687 result.append("]\n\n"); 688 try { 689 I_CmsWidget widget = value.getDocument().getContentDefinition().getContentHandler().getWidget( 690 value); 691 result.append(widget.getWidgetStringValue( 692 getCms(), 693 new CmsFileInfoDialog(getJsp()), 694 (I_CmsWidgetParameter)value)); 695 } catch (CmsXmlException e) { 696 LOG.error(e.getMessage(), e); 697 } 698 } 699 firstIter = false; 700 } 701 } else if (xmlDoc instanceof CmsXmlContent) { 702 CmsXmlContentTextExtractor visitor = new CmsXmlContentTextExtractor(result); 703 ((CmsXmlContent)xmlDoc).visitAllValuesWith(visitor); 704 705 } 706 return result.toString(); 707 } 708 709 718 private void setContentAsSource(CmsFile file1, CmsFile file2) throws CmsException, UnsupportedEncodingException { 719 720 CmsObject cms = getCms(); 721 if (CmsStringUtil.isNotEmpty(getParamElement())) { 722 723 I_CmsXmlDocument resource1; 724 I_CmsXmlDocument resource2; 725 if (file1.getTypeId() == CmsResourceTypeXmlPage.getStaticTypeId()) { 726 resource1 = CmsXmlPageFactory.unmarshal(cms, file1); 727 } else { 728 resource1 = CmsXmlContentFactory.unmarshal(cms, file1); 729 } 730 if (file2.getTypeId() == CmsResourceTypeXmlPage.getStaticTypeId()) { 731 resource2 = CmsXmlPageFactory.unmarshal(cms, file2); 732 } else { 733 resource2 = CmsXmlContentFactory.unmarshal(cms, file2); 734 } 735 I_CmsXmlContentValue value1 = resource1.getValue(getParamElement(), new Locale (getParamLocale())); 736 I_CmsXmlContentValue value2 = resource2.getValue(getParamElement(), new Locale (getParamLocale())); 737 if (value1 == null) { 738 m_differenceDialog.setOriginalSource(""); 739 } else { 740 m_differenceDialog.setOriginalSource(value1.getStringValue(cms)); 741 } 742 if (value2 == null) { 743 m_differenceDialog.setCopySource(""); 744 } else { 745 m_differenceDialog.setCopySource(value2.getStringValue(cms)); 746 } 747 } else if (CmsResourceComparisonDialog.COMPARE_ALL_ELEMENTS.equals(getParamCompare())) { 748 749 I_CmsXmlDocument resource1; 750 I_CmsXmlDocument resource2; 751 if (file1.getTypeId() == CmsResourceTypeXmlPage.getStaticTypeId()) { 752 resource1 = CmsXmlPageFactory.unmarshal(cms, file1); 753 } else { 754 resource1 = CmsXmlContentFactory.unmarshal(cms, file1); 755 } 756 if (file2.getTypeId() == CmsResourceTypeXmlPage.getStaticTypeId()) { 757 resource2 = CmsXmlPageFactory.unmarshal(cms, file2); 758 } else { 759 resource2 = CmsXmlContentFactory.unmarshal(cms, file2); 760 } 761 m_differenceDialog.setOriginalSource(extractElements(resource1)); 762 m_differenceDialog.setCopySource(extractElements(resource2)); 763 764 } else { 765 m_differenceDialog.setOriginalSource(new String (file1.getContents(), cms.getRequestContext().getEncoding())); 767 m_differenceDialog.setCopySource(new String (file2.getContents(), cms.getRequestContext().getEncoding())); 768 } 769 } 770 } 771 | Popular Tags |