1 31 32 package org.opencms.importexport; 33 34 import org.opencms.db.CmsDbUtil; 35 import org.opencms.file.CmsFile; 36 import org.opencms.file.CmsFolder; 37 import org.opencms.file.CmsObject; 38 import org.opencms.file.CmsProperty; 39 import org.opencms.file.CmsPropertyDefinition; 40 import org.opencms.file.CmsResource; 41 import org.opencms.file.CmsResourceFilter; 42 import org.opencms.file.types.CmsResourceTypeFolder; 43 import org.opencms.file.types.CmsResourceTypePlain; 44 import org.opencms.file.types.CmsResourceTypeXmlPage; 45 import org.opencms.file.types.I_CmsResourceType; 46 import org.opencms.i18n.CmsMessageContainer; 47 import org.opencms.lock.CmsLockException; 48 import org.opencms.main.CmsException; 49 import org.opencms.main.CmsLog; 50 import org.opencms.main.OpenCms; 51 import org.opencms.report.I_CmsReport; 52 import org.opencms.security.CmsRole; 53 import org.opencms.security.I_CmsPasswordHandler; 54 import org.opencms.util.CmsStringUtil; 55 import org.opencms.util.CmsUUID; 56 import org.opencms.xml.CmsXmlException; 57 import org.opencms.xml.CmsXmlUtils; 58 import org.opencms.xml.page.CmsXmlPage; 59 60 import java.io.File ; 61 import java.util.ArrayList ; 62 import java.util.Collections ; 63 import java.util.HashMap ; 64 import java.util.Iterator ; 65 import java.util.List ; 66 import java.util.Map ; 67 import java.util.StringTokenizer ; 68 import java.util.zip.ZipFile ; 69 70 import org.apache.commons.logging.Log; 71 72 import org.dom4j.Document; 73 import org.dom4j.Element; 74 import org.dom4j.Node; 75 76 91 public class CmsImportVersion2 extends A_CmsImport { 92 93 94 private static final String COMPATIBILITY_WEBAPPNAMES = "compatibility.support.webAppNames"; 95 96 97 private static final int IMPORT_VERSION = 2; 98 99 100 private static final Log LOG = CmsLog.getLog(CmsImportVersion2.class); 101 102 103 protected List m_webAppNames; 104 105 106 protected String m_webappUrl; 107 108 109 private List m_folderStorage; 110 111 112 private List m_pageStorage; 113 114 121 public static String setDirectories(String content, String [] rules) { 122 123 for (int i = 0; i < rules.length; i++) { 125 String actRule = rules[i]; 126 actRule = CmsStringUtil.substitute(actRule, "/default/vfs", ""); 128 StringTokenizer ruleT = new StringTokenizer (actRule, "#"); 130 ruleT.nextToken(); 131 String search = ruleT.nextToken(); 132 int pos = search.lastIndexOf("(.*)"); 133 if (pos >= 0) { 134 search = search.substring(0, pos); 135 } 136 String replace = ruleT.nextToken(); 137 if (pos >= 0) { 138 replace = replace.substring(0, replace.lastIndexOf("$1")); 139 } 140 if (content.indexOf(replace) == -1 && content.indexOf(search) != -1) { 142 search = "([}>\"'\\[]\\s*)" + search; 144 replace = "$1" + replace; 145 content = CmsStringUtil.substitutePerl(content, search, replace, "g"); 146 } 147 } 148 return content; 149 } 150 151 155 public int getVersion() { 156 157 return CmsImportVersion2.IMPORT_VERSION; 158 } 159 160 163 public void importResources( 164 CmsObject cms, 165 String importPath, 166 I_CmsReport report, 167 File importResource, 168 ZipFile importZip, 169 Document docXml) throws CmsImportExportException { 170 171 initialize(); 173 m_cms = cms; 174 m_importPath = importPath; 175 m_report = report; 176 m_importResource = importResource; 177 m_importZip = importZip; 178 m_docXml = docXml; 179 m_importingChannelData = false; 180 181 m_folderStorage = new ArrayList (); 182 m_pageStorage = new ArrayList (); 183 m_linkStorage = new HashMap (); 184 m_linkPropertyStorage = new HashMap (); 185 186 if (OpenCms.getRunLevel() >= OpenCms.RUNLEVEL_3_SHELL_ACCESS) { 187 if ((OpenCms.getMemoryMonitor() != null) && OpenCms.getMemoryMonitor().enabled()) { 188 OpenCms.getMemoryMonitor().register(this.getClass().getName() + ".m_folderStorage", m_folderStorage); 189 OpenCms.getMemoryMonitor().register(this.getClass().getName() + ".m_pageStorage", m_pageStorage); 190 OpenCms.getMemoryMonitor().register(this.getClass().getName() + ".m_linkStorage", m_linkStorage); 191 OpenCms.getMemoryMonitor().register( 192 this.getClass().getName() + ".m_linkPropertyStorage", 193 m_linkPropertyStorage); 194 } 195 } 196 197 try { 198 if (cms.hasRole(CmsRole.ACCOUNT_MANAGER)) { 200 importGroups(); 201 importUsers(); 202 } 203 importAllResources(); 205 convertPointerToSiblings(); 206 } finally { 207 cleanUp(); 208 } 209 } 210 211 217 protected void cleanUp() { 218 219 m_pageStorage = null; 220 m_folderStorage = null; 221 m_webAppNames = null; 222 m_webappUrl = null; 223 super.cleanUp(); 224 } 225 226 237 protected byte[] convertContent(String source, String destination, byte[] content, String resType) { 238 239 if (getVersion() < 3) { 241 if ("page".equals(resType)) { 242 if (DEBUG > 0) { 243 System.err.println("#########################"); 244 System.err.println("[" 245 + this.getClass().getName() 246 + ".convertContent()]: storing resource " 247 + source 248 + '.'); 249 } 250 m_pageStorage.add(destination); 253 } else if ("folder".equals(resType)) { 254 if (destination.startsWith(CmsCompatibleCheck.VFS_PATH_BODIES.substring(1))) { 256 m_folderStorage.add(destination); 258 } 259 } 260 } 261 262 return content; 263 } 264 265 271 protected String getEncoding(String content) { 272 273 String encoding = content; 274 int index = encoding.toLowerCase().indexOf("encoding=\""); 275 if (index != -1) { 277 encoding = encoding.substring(index + 10); 278 index = encoding.indexOf("\""); 279 if (index != -1) { 280 encoding = encoding.substring(0, index); 281 return encoding.toUpperCase(); 282 } 283 } 284 return ""; 286 } 287 288 304 protected void importUser( 305 String name, 306 String description, 307 String flags, 308 String password, 309 String firstname, 310 String lastname, 311 String email, 312 String address, 313 String type, 314 Map userInfo, 315 List userGroups) throws CmsImportExportException { 316 317 boolean convert = false; 318 319 Map config = OpenCms.getPasswordHandler().getConfiguration(); 320 if (config != null && config.containsKey(I_CmsPasswordHandler.CONVERT_DIGEST_ENCODING)) { 321 convert = Boolean.valueOf((String )config.get(I_CmsPasswordHandler.CONVERT_DIGEST_ENCODING)).booleanValue(); 322 } 323 324 if (convert) { 325 password = convertDigestEncoding(password); 326 } 327 328 super.importUser( 329 name, 330 description, 331 flags, 332 password, 333 firstname, 334 lastname, 335 email, 336 address, 337 type, 338 userInfo, 339 userGroups); 340 } 341 342 348 protected void initialize() { 349 350 m_convertToXmlPage = true; 351 m_webAppNames = new ArrayList (); 352 super.initialize(); 353 } 354 355 362 protected String setEncoding(String content, String encoding) { 363 364 if (content.toLowerCase().indexOf("<?xml") == -1) { 365 return content; 366 } else { 367 String xmlTag = content.substring(0, content.indexOf(">") + 1); 370 if (xmlTag.toLowerCase().indexOf("encoding") == -1) { 371 content = content.substring(content.indexOf(">") + 1); 372 content = "<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>" + content; 373 } 374 } 375 return content; 376 } 377 378 383 private List getCompatibilityWebAppNames() { 384 385 List webAppNamesOri = new ArrayList (); 386 387 String configuredWebAppNames = (String )OpenCms.getRuntimeProperty(COMPATIBILITY_WEBAPPNAMES); 388 if (configuredWebAppNames != null && configuredWebAppNames.length() != 0) { 389 StringTokenizer tokenizer = new StringTokenizer (configuredWebAppNames, ",;"); 391 while (tokenizer.hasMoreTokens()) { 392 webAppNamesOri.add(tokenizer.nextToken()); 393 } 394 } 395 396 List webAppNames = new ArrayList (); 397 for (int i = 0; i < webAppNamesOri.size(); i++) { 398 String name = ((String )webAppNamesOri.get(i)).trim(); 400 if (CmsStringUtil.isNotEmpty(name)) { 401 webAppNames.add(name); 402 if (LOG.isInfoEnabled()) { 403 LOG.info(Messages.get().getBundle().key( 404 Messages.INIT_IMPORTEXPORT_OLD_CONTEXT_PATH_2, 405 Integer.toString((i + 1)), 406 name)); 407 } 408 } 409 } 410 411 String key = (webAppNames.size() > 0) ? Messages.INIT_IMPORTEXPORT_OLD_CONTEXT_SUPPORT_ENABLED_0 412 : Messages.INIT_IMPORTEXPORT_OLD_CONTEXT_SUPPORT_DISABLED_0; 413 if (LOG.isInfoEnabled()) { 414 LOG.info(Messages.get().getBundle().key(key)); 415 } 416 417 if (webAppNames == null) { 419 webAppNames = new ArrayList (); 420 } 421 422 if (!webAppNames.contains(OpenCms.getSystemInfo().getOpenCmsContext())) { 424 webAppNames.add(OpenCms.getSystemInfo().getOpenCmsContext()); 425 } 426 427 return webAppNames; 428 } 429 430 435 private void importAllResources() throws CmsImportExportException { 436 437 List fileNodes = null, acentryNodes = null; 438 Element currentElement = null, currentEntry = null; 439 String source = null, destination = null, resourceTypeName = null, timestamp = null, uuid = null, uuidresource = null; 440 long lastmodified = 0; 441 int resourceTypeId = CmsDbUtil.UNKNOWN_ID; 442 List properties = null; 443 boolean old_overwriteCollidingResources = false; 444 445 if (m_importingChannelData) { 446 m_cms.getRequestContext().saveSiteRoot(); 447 m_cms.getRequestContext().setSiteRoot(CmsResource.VFS_FOLDER_CHANNELS); 448 } 449 try { 450 m_webAppNames = getCompatibilityWebAppNames(); 451 } catch (Exception e) { 452 if (LOG.isDebugEnabled()) { 453 LOG.debug(Messages.get().getBundle().key( 454 Messages.LOG_IMPORTEXPORT_ERROR_GETTING_WEBAPP_COMPATIBILITY_NAMES_0), e); 455 } 456 m_report.println(e); 457 } 458 if (m_webAppNames == null) { 459 m_webAppNames = Collections.EMPTY_LIST; 460 } 461 462 m_webappUrl = OpenCms.getImportExportManager().getOldWebAppUrl(); 464 if (m_webappUrl == null) { 465 m_webappUrl = "http://localhost:8080/opencms/opencms"; 467 } 468 if (m_webappUrl.endsWith("/")) { 470 m_webappUrl = m_webappUrl.substring(0, m_webappUrl.lastIndexOf("/")); 471 } 472 473 List deleteProperties = OpenCms.getImportExportManager().getIgnoredProperties(); 475 476 List immutableResources = OpenCms.getImportExportManager().getImmutableResources(); 478 if (immutableResources == null) { 479 immutableResources = Collections.EMPTY_LIST; 480 } 481 if (LOG.isDebugEnabled()) { 482 LOG.debug(Messages.get().getBundle().key( 483 Messages.LOG_IMPORTEXPORT_IMMUTABLE_RESOURCES_SIZE_1, 484 Integer.toString(immutableResources.size()))); 485 } 486 487 old_overwriteCollidingResources = OpenCms.getImportExportManager().overwriteCollidingResources(); 489 490 OpenCms.getImportExportManager().setOverwriteCollidingResources(true); 493 494 try { 495 fileNodes = m_docXml.selectNodes("//" + CmsImportExportManager.N_FILE); 497 int importSize = fileNodes.size(); 498 499 for (int i = 0; i < importSize; i++) { 501 502 m_report.print(org.opencms.report.Messages.get().container( 503 org.opencms.report.Messages.RPT_SUCCESSION_2, 504 String.valueOf(i + 1), 505 String.valueOf(importSize)), I_CmsReport.FORMAT_NOTE); 506 currentElement = (Element)fileNodes.get(i); 507 508 source = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_SOURCE); 510 destination = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_DESTINATION); 511 512 resourceTypeName = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_TYPE); 513 if (RESOURCE_TYPE_NEWPAGE_NAME.equals(resourceTypeName)) { 514 resourceTypeId = RESOURCE_TYPE_NEWPAGE_ID; 515 } else if (RESOURCE_TYPE_LEGACY_PAGE_NAME.equals(resourceTypeName)) { 516 resourceTypeId = CmsResourceTypePlain.getStaticTypeId(); 519 } else if (RESOURCE_TYPE_LINK_NAME.equals(resourceTypeName)) { 520 resourceTypeId = RESOURCE_TYPE_LINK_ID; 522 } else { 523 I_CmsResourceType type = OpenCms.getResourceManager().getResourceType(resourceTypeName); 524 resourceTypeId = type.getTypeId(); 525 } 526 527 uuid = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_UUIDSTRUCTURE); 528 uuidresource = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_UUIDRESOURCE); 529 530 timestamp = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_LASTMODIFIED); 531 if (timestamp != null) { 532 lastmodified = Long.parseLong(timestamp); 533 } else { 534 lastmodified = System.currentTimeMillis(); 535 } 536 537 if ("script".equals(resourceTypeName)) { 539 resourceTypeName = CmsResourceTypePlain.getStaticTypeName(); 540 } 541 542 if (LOG.isDebugEnabled()) { 543 LOG.debug(Messages.get().getBundle().key( 544 Messages.LOG_IMPORTEXPORT_ORIGINAL_RESOURCE_NAME_1, 545 destination)); 546 } 547 548 String translatedName = m_cms.getRequestContext().addSiteRoot(m_importPath + destination); 549 if (CmsResourceTypeFolder.RESOURCE_TYPE_NAME.equals(resourceTypeName)) { 550 if (!CmsResource.isFolder(translatedName)) { 552 translatedName += "/"; 553 } 554 } 555 556 if (LOG.isDebugEnabled()) { 557 LOG.debug(Messages.get().getBundle().key( 558 Messages.LOG_IMPORTEXPORT_TRANSLATED_RESOURCE_NAME_1, 559 translatedName)); 560 } 561 562 boolean resourceNotImmutable = checkImmutable(translatedName, immutableResources); 563 translatedName = m_cms.getRequestContext().removeSiteRoot(translatedName); 564 if (resourceNotImmutable) { 565 566 m_report.print(Messages.get().container(Messages.RPT_IMPORTING_0), I_CmsReport.FORMAT_NOTE); 568 m_report.print(org.opencms.report.Messages.get().container( 569 org.opencms.report.Messages.RPT_ARGUMENT_1, 570 translatedName)); 571 m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0)); 572 573 properties = readPropertiesFromManifest(currentElement, deleteProperties); 575 576 CmsResource res = importResource( 578 source, 579 destination, 580 uuid, 581 uuidresource, 582 resourceTypeId, 583 resourceTypeName, 584 lastmodified, 585 properties); 586 587 if (res != null) { 588 589 List aceList = new ArrayList (); 590 acentryNodes = currentElement.selectNodes("*/" + CmsImportExportManager.N_ACCESSCONTROL_ENTRY); 592 for (int j = 0; j < acentryNodes.size(); j++) { 594 currentEntry = (Element)acentryNodes.get(j); 595 String id = CmsImport.getChildElementTextValue(currentEntry, CmsImportExportManager.N_ID); 597 String acflags = CmsImport.getChildElementTextValue( 598 currentEntry, 599 CmsImportExportManager.N_FLAGS); 600 String allowed = CmsImport.getChildElementTextValue( 601 currentEntry, 602 CmsImportExportManager.N_ACCESSCONTROL_ALLOWEDPERMISSIONS); 603 String denied = CmsImport.getChildElementTextValue( 604 currentEntry, 605 CmsImportExportManager.N_ACCESSCONTROL_DENIEDPERMISSIONS); 606 607 aceList.add(getImportAccessControlEntry(res, id, allowed, denied, acflags)); 609 } 610 importAccessControlEntries(res, aceList); 611 if (LOG.isInfoEnabled()) { 612 LOG.info(Messages.get().getBundle().key( 613 Messages.LOG_IMPORTING_4, 614 new Object [] { 615 String.valueOf(i + 1), 616 String.valueOf(importSize), 617 translatedName, 618 destination})); 619 } 620 621 } else { 622 m_report.print(Messages.get().container(Messages.RPT_SKIPPING_0), I_CmsReport.FORMAT_OK); 624 m_report.println(org.opencms.report.Messages.get().container( 625 org.opencms.report.Messages.RPT_ARGUMENT_1, 626 translatedName)); 627 if (LOG.isInfoEnabled()) { 628 LOG.info(Messages.get().getBundle().key( 629 Messages.LOG_SKIPPING_3, 630 String.valueOf(i + 1), 631 String.valueOf(importSize), 632 translatedName)); 633 } 634 } 635 } else { 636 m_report.print(Messages.get().container(Messages.RPT_SKIPPING_0), I_CmsReport.FORMAT_NOTE); 638 m_report.println(org.opencms.report.Messages.get().container( 639 org.opencms.report.Messages.RPT_ARGUMENT_1, 640 translatedName)); 641 642 if (LOG.isInfoEnabled()) { 643 LOG.info(Messages.get().getBundle().key( 644 Messages.LOG_SKIPPING_3, 645 String.valueOf(i + 1), 646 String.valueOf(importSize), 647 translatedName)); 648 } 649 } 650 } 651 652 if (getVersion() < 3 && m_convertToXmlPage) { 655 mergePageFiles(); 656 removeFolders(); 657 } 658 } catch (Exception e) { 659 660 m_report.println(e); 661 662 CmsMessageContainer message = Messages.get().container( 663 Messages.ERR_IMPORTEXPORT_ERROR_IMPORTING_RESOURCES_0); 664 if (LOG.isDebugEnabled()) { 665 LOG.debug(message.key(), e); 666 } 667 668 throw new CmsImportExportException(message, e); 669 } finally { 670 if (m_importingChannelData) { 671 m_cms.getRequestContext().restoreSiteRoot(); 672 } 673 674 OpenCms.getImportExportManager().setOverwriteCollidingResources(old_overwriteCollidingResources); 676 } 677 } 678 679 693 private CmsResource importResource( 694 String source, 695 String destination, 696 String uuid, 697 String uuidresource, 698 int resourceTypeId, 699 String resourceTypeName, 700 long lastmodified, 701 List properties) { 702 703 byte[] content = null; 704 CmsResource res = null; 705 String targetName = null; 706 707 try { 708 709 if (m_importingChannelData) { 710 String channelId = null; 712 try { 713 if ((resourceTypeName.equalsIgnoreCase(CmsResourceTypeFolder.RESOURCE_TYPE_NAME)) 714 && (!destination.endsWith("/"))) { 715 destination += "/"; 716 } 717 CmsResource channel = m_cms.readResource("/" + destination); 718 719 channelId = m_cms.readPropertyObject( 720 m_cms.getSitePath(channel), 721 CmsPropertyDefinition.PROPERTY_CHANNELID, 722 false).getValue(); 723 724 } catch (Exception e) { 725 } 727 728 if (channelId != null) { 729 properties.add(new CmsProperty(CmsPropertyDefinition.PROPERTY_CHANNELID, channelId, null)); 730 } 731 } 732 733 if (source != null) { 735 content = getFileBytes(source); 736 } 737 738 content = convertContent(source, destination, content, resourceTypeName); 739 740 int size = 0; 742 if (content != null) { 743 size = content.length; 744 } 745 CmsUUID curUser = m_cms.getRequestContext().currentUser().getId(); 747 CmsUUID newUuidstructure = new CmsUUID(); 748 CmsUUID newUuidresource = new CmsUUID(); 749 if (uuid != null) { 750 newUuidstructure = new CmsUUID(uuid); 751 } 752 if (uuidresource != null) { 753 newUuidresource = new CmsUUID(uuidresource); 754 } 755 756 targetName = destination; 758 if (targetName.endsWith("/")) { 759 targetName = targetName.substring(0, targetName.length() - 1); 760 } 761 762 boolean isFolder = false; 763 try { 764 isFolder = CmsFolder.isFolderType(resourceTypeId); 765 } catch (Throwable t) { 766 isFolder = ((content.length == 0) && CmsResourceTypeFolder.RESOURCE_TYPE_NAME.equalsIgnoreCase(resourceTypeName)); 770 } 771 772 CmsResource resource = new CmsResource( 774 newUuidstructure, 775 newUuidresource, 776 targetName, 777 resourceTypeId, 778 isFolder, 779 0, 780 m_cms.getRequestContext().currentProject().getId(), 781 CmsResource.STATE_NEW, 782 lastmodified, 783 curUser, 784 lastmodified, 785 curUser, 786 CmsResource.DATE_RELEASED_DEFAULT, 787 CmsResource.DATE_EXPIRED_DEFAULT, 788 1, 789 size); 790 791 if (RESOURCE_TYPE_LINK_ID == resourceTypeId) { 792 m_report.print(Messages.get().container(Messages.RPT_STORING_LINK_0), I_CmsReport.FORMAT_NOTE); 794 m_linkStorage.put(m_importPath + destination, new String (content)); 795 m_linkPropertyStorage.put(m_importPath + destination, properties); 796 res = resource; 797 } else { 798 String resName = m_importPath + destination; 800 res = m_cms.importResource(resName, resource, content, properties); 801 try { 802 m_cms.unlockResource(resName); 803 } catch (CmsLockException e) { 804 if (LOG.isDebugEnabled()) { 805 LOG.debug(Messages.get().getBundle().key( 806 Messages.LOG_IMPORTEXPORT_UNABLE_TO_UNLOCK_RESOURCE_1, 807 resName), e); 808 } 809 } 810 } 811 812 m_report.println(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0)); 813 814 } catch (CmsException exc) { 815 CmsMessageContainer message = Messages.get().container( 816 Messages.ERR_IMPORTEXPORT_ERROR_IMPORTING_RESOURCE_1, 817 targetName); 818 if (LOG.isDebugEnabled()) { 819 LOG.debug(message.key(), exc); 820 } 821 822 m_report.println(exc); 824 try { 825 Thread.sleep(1000); 827 } catch (Exception e) { 828 } 830 } 831 return res; 832 } 833 834 841 private void mergePageFile(String resourcename) throws CmsXmlException, CmsImportExportException { 842 843 try { 844 845 if (LOG.isDebugEnabled()) { 846 LOG.debug(Messages.get().getBundle().key(Messages.LOG_IMPORTEXPORT_START_MERGING_1, resourcename)); 847 } 848 849 852 CmsFile pagefile = m_cms.readFile(resourcename, CmsResourceFilter.ALL); 854 Document contentXml = CmsXmlUtils.unmarshalHelper(pagefile.getContents(), null); 855 856 String masterTemplateNodeName = "//masterTemplate"; 858 Node masterTemplateNode = contentXml.selectSingleNode(masterTemplateNodeName); 859 if (masterTemplateNode == null) { 860 masterTemplateNode = contentXml.selectSingleNode(masterTemplateNodeName.toLowerCase()); 861 } 862 if (masterTemplateNode == null) { 863 masterTemplateNode = contentXml.selectSingleNode(masterTemplateNodeName.toUpperCase()); 864 } 865 866 String mastertemplate = null; 868 if (masterTemplateNode != null) { 869 mastertemplate = masterTemplateNode.getText().trim(); 871 } 872 873 String elementDefNodeName = "//ELEMENTDEF"; 876 Node bodyNode = contentXml.selectSingleNode(elementDefNodeName); 877 if (bodyNode == null) { 878 bodyNode = contentXml.selectSingleNode(elementDefNodeName.toLowerCase()); 879 } 880 881 if (bodyNode != null) { 883 884 String bodyclass = null; 885 String bodyname = null; 886 Map bodyparams = null; 887 888 List nodes = ((Element)bodyNode).elements(); 889 for (int i = 0, n = nodes.size(); i < n; i++) { 890 891 Node node = (Node)nodes.get(i); 892 893 if ("CLASS".equalsIgnoreCase(node.getName())) { 894 bodyclass = node.getText().trim(); 895 } else if ("TEMPLATE".equalsIgnoreCase(node.getName())) { 896 bodyname = node.getText().trim(); 897 if (!bodyname.startsWith("/")) { 898 bodyname = CmsResource.getFolderPath(resourcename) + bodyname; 899 } 900 } else if ("PARAMETER".equalsIgnoreCase(node.getName())) { 901 Element paramElement = (Element)node; 902 if (bodyparams == null) { 903 bodyparams = new HashMap (); 904 } 905 bodyparams.put((paramElement.attribute("name")).getText(), paramElement.getTextTrim()); 906 } 907 } 908 909 if (mastertemplate == null || bodyname == null) { 910 911 CmsMessageContainer message = Messages.get().container( 912 Messages.ERR_IMPORTEXPORT_ERROR_CANNOT_MERGE_PAGE_FILE_3, 913 resourcename, 914 mastertemplate, 915 bodyname); 916 if (LOG.isDebugEnabled()) { 917 LOG.debug(message.key()); 918 } 919 920 throw new CmsImportExportException(message); 921 } 922 923 m_cms.lockResource(resourcename); 925 926 List properties = m_cms.readPropertyObjects(resourcename, false); 928 929 CmsFile bodyfile = m_cms.readFile(bodyname, CmsResourceFilter.IGNORE_EXPIRATION); 931 932 String encoding = CmsProperty.get(CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING, properties).getValue(); 934 if (encoding == null) { 935 encoding = OpenCms.getSystemInfo().getDefaultEncoding(); 936 } 937 938 if (m_convertToXmlPage) { 939 if (LOG.isDebugEnabled()) { 940 LOG.debug(Messages.get().getBundle().key(Messages.LOG_IMPORTEXPORT_START_CONVERTING_TO_XML_0)); 941 } 942 943 CmsXmlPage xmlPage = CmsXmlPageConverter.convertToXmlPage(m_cms, bodyfile.getContents(), getLocale( 944 resourcename, 945 properties), encoding); 946 947 if (LOG.isDebugEnabled()) { 948 LOG.debug(Messages.get().getBundle().key(Messages.LOG_IMPORTEXPORT_END_CONVERTING_TO_XML_0)); 949 } 950 951 if (xmlPage != null) { 952 pagefile.setContents(xmlPage.marshal()); 953 954 pagefile.setType(CmsResourceTypeXmlPage.getStaticTypeId()); 956 } 957 } 958 959 CmsProperty newProperty = new CmsProperty(CmsPropertyDefinition.PROPERTY_TEMPLATE, mastertemplate, null); 961 properties.remove(newProperty); 963 properties.add(newProperty); 964 965 if (CmsStringUtil.isNotEmpty(bodyclass)) { 967 newProperty = new CmsProperty(CmsPropertyDefinition.PROPERTY_TEMPLATE, mastertemplate, null); 968 newProperty.setAutoCreatePropertyDefinition(true); 969 properties.remove(newProperty); 970 properties.add(newProperty); 971 } 972 if (bodyparams != null) { 974 for (Iterator p = bodyparams.keySet().iterator(); p.hasNext();) { 975 String key = (String )p.next(); 976 newProperty = new CmsProperty(key, (String )bodyparams.get(key), null); 977 newProperty.setAutoCreatePropertyDefinition(true); 978 properties.remove(newProperty); 979 properties.add(newProperty); 980 } 981 } 982 983 if (LOG.isDebugEnabled()) { 984 LOG.debug(Messages.get().getBundle().key(Messages.LOG_IMPORTEXPORT_START_IMPORTING_XML_PAGE_0)); 985 } 986 987 m_cms.importResource(resourcename, pagefile, pagefile.getContents(), properties); 989 990 m_cms.lockResource(bodyname); 992 m_cms.deleteResource(bodyname, CmsResource.DELETE_PRESERVE_SIBLINGS); 993 994 if (LOG.isDebugEnabled()) { 995 LOG.debug(Messages.get().getBundle().key(Messages.LOG_IMPORTEXPORT_END_IMPORTING_XML_PAGE_0)); 996 } 997 998 m_report.println( 999 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0), 1000 I_CmsReport.FORMAT_OK); 1001 1002 } else { 1003 1004 m_cms.lockResource(resourcename); 1008 pagefile.setType(CmsResourceTypePlain.getStaticTypeId()); 1010 m_cms.writeFile(pagefile); 1012 m_cms.unlockResource(resourcename); 1014 1015 if (LOG.isDebugEnabled()) { 1016 LOG.debug(Messages.get().getBundle().key( 1017 Messages.LOG_IMPORTEXPORT_CANNOT_CONVERT_XML_STRUCTURE_1, 1018 resourcename)); 1019 } 1020 1021 m_report.println(Messages.get().container(Messages.RPT_NOT_CONVERTED_0), I_CmsReport.FORMAT_OK); 1022 1023 } 1024 1025 if (LOG.isDebugEnabled()) { 1026 LOG.debug(Messages.get().getBundle().key(Messages.LOG_IMPORTEXPORT_END_MERGING_1, resourcename)); 1027 } 1028 } catch (CmsXmlException e) { 1029 1030 throw e; 1031 } catch (CmsException e) { 1032 1033 m_report.println(e); 1034 1035 CmsMessageContainer message = Messages.get().container( 1036 Messages.ERR_IMPORTEXPORT_ERROR_MERGING_PAGE_FILE_1, 1037 resourcename); 1038 if (LOG.isDebugEnabled()) { 1039 LOG.debug(message.key(), e); 1040 } 1041 1042 throw new CmsImportExportException(message, e); 1043 } 1044 1045 } 1046 1047 1053 private void mergePageFiles() throws CmsXmlException, CmsImportExportException { 1054 1055 try { 1056 try { 1058 m_cms.readPropertyDefinition(CmsPropertyDefinition.PROPERTY_TEMPLATE); 1059 } catch (CmsException e) { 1060 m_cms.createPropertyDefinition(CmsPropertyDefinition.PROPERTY_TEMPLATE); 1062 } 1063 1064 List definitions = m_cms.readAllPropertyDefinitions(); 1066 1067 Iterator j = definitions.iterator(); 1068 while (j.hasNext()) { 1069 CmsPropertyDefinition definition = (CmsPropertyDefinition)j.next(); 1070 try { 1072 m_cms.readPropertyDefinition(definition.getName()); 1073 } catch (Exception e) { 1074 m_cms.createPropertyDefinition(definition.getName()); 1075 } 1076 } 1077 } catch (CmsException e) { 1078 1079 CmsMessageContainer message = Messages.get().container( 1080 Messages.ERR_IMPORTEXPORT_ERROR_COPYING_PROPERTY_DEFINITIONS_0); 1081 if (LOG.isDebugEnabled()) { 1082 LOG.debug(message.key(), e); 1083 } 1084 1085 throw new CmsImportExportException(message); 1086 } 1087 1088 int size = m_pageStorage.size(); 1090 m_report.println(Messages.get().container(Messages.RPT_MERGE_START_0), I_CmsReport.FORMAT_HEADLINE); 1091 Iterator i = m_pageStorage.iterator(); 1092 int counter = 1; 1093 while (i.hasNext()) { 1094 String resname = (String )i.next(); 1095 if (!resname.startsWith("/")) { 1097 resname = "/" + resname; 1098 } 1099 1100 m_report.print(org.opencms.report.Messages.get().container( 1101 org.opencms.report.Messages.RPT_SUCCESSION_2, 1102 String.valueOf(counter), 1103 String.valueOf(size)), I_CmsReport.FORMAT_NOTE); 1104 m_report.print(Messages.get().container(Messages.RPT_MERGE_0), I_CmsReport.FORMAT_NOTE); 1105 m_report.print(org.opencms.report.Messages.get().container( 1106 org.opencms.report.Messages.RPT_ARGUMENT_1, 1107 resname)); 1108 1109 mergePageFile(resname); 1110 if (LOG.isInfoEnabled()) { 1111 LOG.info(Messages.get().getBundle().key( 1112 Messages.LOG_MERGING_3, 1113 String.valueOf(counter), 1114 String.valueOf(size), 1115 resname)); 1116 } 1117 1118 counter++; 1119 1120 } 1121 m_pageStorage.clear(); 1123 1124 } 1125 1126 1131 private void removeFolders() throws CmsImportExportException { 1132 1133 try { 1134 1135 int size = m_folderStorage.size(); 1136 1137 m_report.println(Messages.get().container(Messages.RPT_DELFOLDER_START_0), I_CmsReport.FORMAT_HEADLINE); 1138 int counter = 1; 1141 for (int j = (size - 1); j >= 0; j--) { 1142 String resname = (String )m_folderStorage.get(j); 1143 resname = (resname.startsWith("/") ? "" : "/") + resname + (resname.endsWith("/") ? "" : "/"); 1144 List files = m_cms.getFilesInFolder(resname, CmsResourceFilter.IGNORE_EXPIRATION); 1146 1147 if (files.size() == 0) { 1148 List folders = m_cms.getSubFolders(resname, CmsResourceFilter.IGNORE_EXPIRATION); 1149 if (folders.size() == 0) { 1150 m_report.print(org.opencms.report.Messages.get().container( 1151 org.opencms.report.Messages.RPT_SUCCESSION_2, 1152 String.valueOf(counter), 1153 String.valueOf(size)), I_CmsReport.FORMAT_NOTE); 1154 m_report.print(Messages.get().container(Messages.RPT_DELFOLDER_0), I_CmsReport.FORMAT_NOTE); 1155 m_report.print(org.opencms.report.Messages.get().container( 1156 org.opencms.report.Messages.RPT_ARGUMENT_1, 1157 resname), I_CmsReport.FORMAT_DEFAULT); 1158 m_cms.lockResource(resname); 1159 m_cms.deleteResource(resname, CmsResource.DELETE_PRESERVE_SIBLINGS); 1160 m_report.println(org.opencms.report.Messages.get().container( 1161 org.opencms.report.Messages.RPT_OK_0), I_CmsReport.FORMAT_OK); 1162 counter++; 1163 } 1164 } 1165 } 1166 } catch (CmsException e) { 1167 1168 CmsMessageContainer message = Messages.get().container( 1169 Messages.ERR_IMPORTEXPORT_ERROR_REMOVING_FOLDERS_OF_IMPORTED_BODY_FILES_0); 1170 if (LOG.isDebugEnabled()) { 1171 LOG.debug(message.key(), e); 1172 } 1173 1174 throw new CmsImportExportException(message, e); 1175 } 1176 } 1177} | Popular Tags |