1 31 32 package org.opencms.workplace.tools.database; 33 34 import org.opencms.db.CmsDbIoException; 35 import org.opencms.file.CmsFolder; 36 import org.opencms.file.CmsObject; 37 import org.opencms.file.CmsProperty; 38 import org.opencms.file.CmsPropertyDefinition; 39 import org.opencms.file.CmsResource; 40 import org.opencms.file.CmsResourceFilter; 41 import org.opencms.file.types.CmsResourceTypeFolder; 42 import org.opencms.file.types.CmsResourceTypeImage; 43 import org.opencms.file.types.CmsResourceTypePlain; 44 import org.opencms.file.types.CmsResourceTypePointer; 45 import org.opencms.file.types.CmsResourceTypeXmlPage; 46 import org.opencms.i18n.CmsEncoder; 47 import org.opencms.i18n.CmsLocaleManager; 48 import org.opencms.i18n.CmsMessageContainer; 49 import org.opencms.importexport.CmsImportExportException; 50 import org.opencms.jsp.CmsJspActionElement; 51 import org.opencms.loader.CmsResourceManager; 52 import org.opencms.lock.CmsLock; 53 import org.opencms.main.CmsException; 54 import org.opencms.main.CmsIllegalArgumentException; 55 import org.opencms.main.CmsLog; 56 import org.opencms.main.OpenCms; 57 import org.opencms.report.I_CmsReport; 58 import org.opencms.staticexport.CmsLink; 59 import org.opencms.staticexport.CmsLinkTable; 60 import org.opencms.util.CmsStringUtil; 61 import org.opencms.xml.page.CmsXmlPage; 62 63 import java.io.File ; 64 import java.io.FileInputStream ; 65 import java.io.IOException ; 66 import java.net.MalformedURLException ; 67 import java.net.URL ; 68 import java.util.ArrayList ; 69 import java.util.Enumeration ; 70 import java.util.HashMap ; 71 import java.util.HashSet ; 72 import java.util.Hashtable ; 73 import java.util.Iterator ; 74 import java.util.List ; 75 import java.util.Locale ; 76 import java.util.Map ; 77 import java.util.StringTokenizer ; 78 79 import javax.servlet.http.HttpServletRequest ; 80 81 import org.apache.commons.collections.ExtendedProperties; 82 import org.apache.commons.logging.Log; 83 84 95 public class CmsHtmlImport { 96 97 98 public static final String META_PROPERTIES = "meta.properties"; 99 100 101 private static final Log LOG = CmsLog.getLog(CmsHtmlImport.class); 102 103 104 private URL m_baseUrl; 105 106 private CmsJspActionElement m_cms; 107 108 109 private CmsObject m_cmsObject; 110 111 112 private String m_destinationDir; 113 114 115 private String m_downloadGallery; 116 117 private String m_element; 118 119 120 private String m_endPattern; 121 122 123 private Map m_extensions; 124 125 130 private HashSet m_externalLinks; 131 132 135 private HashMap m_fileIndex; 136 137 138 private CmsHtmlImportConverter m_htmlConverter; 139 140 141 private CmsHtmlImportThread m_htmlImportThread; 142 143 144 private String m_imageGallery; 145 146 149 private HashMap m_imageInfo; 150 151 152 private String m_inputDir; 153 154 155 private String m_inputEncoding; 156 157 158 private String m_keepBrokenLinks; 159 160 161 private boolean m_keepBrokenLinksMode; 162 163 164 private String m_leaveDownloads; 165 166 167 private boolean m_leaveDownloadsMode; 168 169 170 private String m_leaveExternalLinks; 171 172 173 private boolean m_leaveExternalLinksMode; 174 175 176 private String m_leaveImages; 177 178 179 private boolean m_leaveImagesMode; 180 181 182 private String m_linkGallery; 183 184 185 private Locale m_locale; 186 187 188 private String m_overwrite; 189 190 191 private boolean m_overwriteMode; 192 193 194 private HashMap m_parents; 195 196 197 private I_CmsReport m_report; 198 199 200 private String m_startPattern; 201 202 203 private String m_template; 204 205 211 public CmsHtmlImport(CmsJspActionElement cms, HttpServletRequest request) { 212 213 this( 214 cms, 215 request.getParameter("inputDir"), 216 request.getParameter("destinationDir"), 217 request.getParameter("imageGallery"), 218 request.getParameter("linkGallery"), 219 request.getParameter("downloadGallery"), 220 request.getParameter("template"), 221 request.getParameter("element"), 222 request.getParameter("locale"), 223 request.getParameter("encoding"), 224 request.getParameter("startPattern"), 225 request.getParameter("endPattern"), 226 request.getParameter("overwrite"), 227 request.getParameter("leaveimages"), 228 request.getParameter("leavedownloads"), 229 request.getParameter("keepbrokenlinks"), 230 request.getParameter("leaveexternallinks"), 231 request); 232 } 233 234 255 public CmsHtmlImport( 256 CmsJspActionElement cms, 257 String inputDir, 258 String destinationDir, 259 String imageGallery, 260 String linkGallery, 261 String downloadGallery, 262 String template, 263 String element, 264 String locale, 265 String encoding, 266 String startPattern, 267 String endPattern, 268 String overwrite, 269 String leaveImages, 270 String leaveDownloads, 271 String keepBrokenLinks, 272 String leaveExternalLinks, 273 HttpServletRequest request) { 274 275 if (inputDir == null) { 276 inputDir = ""; 277 } 278 if (destinationDir == null) { 279 destinationDir = ""; 280 } 281 if (imageGallery == null) { 282 imageGallery = ""; 283 } 284 if (linkGallery == null) { 285 linkGallery = ""; 286 } 287 if (downloadGallery == null) { 288 downloadGallery = ""; 289 } 290 if (template == null) { 291 template = ""; 292 } 293 if (element == null) { 294 element = "body"; 295 } 296 if (encoding == null) { 297 encoding = ""; 298 } 299 if (startPattern == null) { 300 startPattern = ""; 301 } 302 if (endPattern == null) { 303 endPattern = ""; 304 } 305 if (overwrite == null) { 306 overwrite = ""; 307 } 308 String action = request.getParameter("action"); 309 if (action == null) { 310 overwrite = "checked"; 311 } 312 if (leaveImages == null) { 313 leaveImages = ""; 314 } 315 if (leaveDownloads == null) { 316 leaveDownloads = ""; 317 } 318 319 if (keepBrokenLinks == null) { 320 keepBrokenLinks = ""; 321 } 322 323 if (leaveExternalLinks == null) { 324 leaveExternalLinks = ""; 325 } 326 327 m_cms = cms; 329 m_cmsObject = m_cms.getCmsObject(); 330 m_locale = CmsLocaleManager.getLocale(locale); 331 if (m_locale == null) { 332 m_locale = m_cms.getRequestContext().getLocale(); 333 } 334 m_inputDir = inputDir.trim(); 336 337 if (m_inputDir.endsWith("/") || m_inputDir.endsWith("\\")) { 339 m_inputDir = m_inputDir.substring(0, m_inputDir.length() - 1); 340 } 341 342 m_destinationDir = destinationDir.trim(); 343 if ((!m_destinationDir.equals("")) && (!m_destinationDir.endsWith("/"))) { 344 m_destinationDir += "/"; 345 } 346 347 m_imageGallery = imageGallery.trim(); 348 if ((!m_imageGallery.equals("")) && (!m_imageGallery.endsWith("/"))) { 349 m_imageGallery += "/"; 350 } 351 352 m_linkGallery = linkGallery.trim(); 353 if ((!m_linkGallery.equals("")) && (!m_linkGallery.endsWith("/"))) { 354 m_linkGallery += "/"; 355 } 356 357 m_downloadGallery = downloadGallery.trim(); 358 if ((!m_downloadGallery.equals("")) && (!m_downloadGallery.endsWith("/"))) { 359 m_downloadGallery += "/"; 360 } 361 362 m_template = template; 363 m_element = element; 364 m_inputEncoding = encoding; 365 366 if (CmsStringUtil.isEmpty(m_inputEncoding)) { 367 m_inputEncoding = CmsEncoder.ENCODING_ISO_8859_1; 368 } 369 m_startPattern = startPattern; 370 m_endPattern = endPattern; 371 372 m_overwrite = overwrite.trim(); 373 if (m_overwrite.equals("checked")) { 374 m_overwriteMode = true; 375 } else { 376 m_overwriteMode = false; 377 } 378 379 m_leaveImages = leaveImages.trim(); 380 if (m_leaveImages.equals("checked")) { 381 m_leaveImagesMode = true; 382 } else { 383 m_leaveImagesMode = false; 384 } 385 386 m_leaveDownloads = leaveDownloads.trim(); 387 if (m_leaveDownloads.equals("checked")) { 388 m_leaveDownloadsMode = true; 389 } else { 390 m_leaveDownloadsMode = false; 391 } 392 393 m_keepBrokenLinks = keepBrokenLinks.trim(); 394 if (m_keepBrokenLinks.equals("checked")) { 395 m_keepBrokenLinksMode = true; 396 } else { 397 m_keepBrokenLinksMode = false; 398 } 399 400 m_leaveExternalLinks = leaveExternalLinks.trim(); 401 if (m_leaveExternalLinks.equals("checked")) { 402 m_leaveExternalLinksMode = true; 403 } else { 404 m_leaveExternalLinksMode = false; 405 } 406 407 m_fileIndex = new HashMap (); 409 m_parents = new HashMap (); 410 m_externalLinks = new HashSet (); 411 m_imageInfo = new HashMap (); 412 m_extensions = OpenCms.getResourceManager().getExtensionMapping(); 413 m_htmlConverter = new CmsHtmlImportConverter(this, false); 414 m_baseUrl = null; 415 try { 416 m_baseUrl = new URL ("file://"); 417 } catch (MalformedURLException e) { 418 } 420 } 421 422 428 public void checkParameters() throws CmsIllegalArgumentException { 429 430 File inputDir = new File (m_inputDir); 432 if (!inputDir.exists() || inputDir.isFile()) { 433 throw new CmsIllegalArgumentException(Messages.get().container( 435 Messages.GUI_HTMLIMPORT_INPUTDIR_1, 436 m_inputDir)); 437 } 438 439 try { 441 m_cmsObject.readFolder(m_destinationDir); 442 } catch (CmsException e) { 443 throw new CmsIllegalArgumentException(Messages.get().container( 445 Messages.GUI_HTMLIMPORT_DESTDIR_1, 446 m_destinationDir), e); 447 } 448 449 if (!m_leaveImagesMode) { 452 try { 453 CmsFolder folder = m_cmsObject.readFolder(m_imageGallery); 454 String name = OpenCms.getResourceManager().getResourceType(folder.getTypeId()).getTypeName(); 456 if (!name.equals("imagegallery")) { 457 throw new CmsIllegalArgumentException(Messages.get().container( 458 Messages.GUI_HTMLIMPORT_IMGGALLERY_INVALID_1, 459 m_imageGallery)); 460 } 461 } catch (CmsException e) { 462 throw new CmsIllegalArgumentException(Messages.get().container( 464 Messages.GUI_HTMLIMPORT_IMGGALLERY_1, 465 m_imageGallery), e); 466 } 467 } 468 469 if (!m_leaveExternalLinksMode) { 472 try { 473 CmsFolder folder = m_cmsObject.readFolder(m_linkGallery); 474 String name = OpenCms.getResourceManager().getResourceType(folder.getTypeId()).getTypeName(); 476 if (!name.equals("linkgallery")) { 477 throw new CmsIllegalArgumentException(Messages.get().container( 478 Messages.GUI_HTMLIMPORT_LINKGALLERY_INVALID_1, 479 m_linkGallery)); 480 } 481 } catch (CmsException e) { 482 throw new CmsIllegalArgumentException(Messages.get().container( 484 Messages.GUI_HTMLIMPORT_LINKGALLERY_1, 485 m_linkGallery), e); 486 } 487 } 488 489 if ((!isExternal(m_downloadGallery)) && (!m_leaveDownloadsMode)) { 491 try { 492 CmsFolder folder = m_cmsObject.readFolder(m_downloadGallery); 493 String name = OpenCms.getResourceManager().getResourceType(folder.getTypeId()).getTypeName(); 495 if (!name.equals("downloadgallery")) { 496 throw new CmsIllegalArgumentException(Messages.get().container( 497 Messages.GUI_HTMLIMPORT_DOWNGALLERY_INVALID_1, 498 m_downloadGallery)); 499 } 500 } catch (CmsException e) { 501 throw new CmsIllegalArgumentException(Messages.get().container( 503 Messages.GUI_HTMLIMPORT_DOWNGALLERY_1, 504 m_downloadGallery), e); 505 } 506 } 507 508 try { 510 m_cmsObject.readResource(m_template, CmsResourceFilter.ALL); 511 } catch (CmsException e) { 512 if (!isValidElement()) { 514 throw new CmsIllegalArgumentException(Messages.get().container( 515 Messages.GUI_HTMLIMPORT_TEMPLATE_1, 516 m_template), e); 517 } 518 } 519 520 if (!isValidElement()) { 522 throw new CmsIllegalArgumentException(Messages.get().container( 523 Messages.GUI_HTMLIMPORT_INVALID_ELEM_2, 524 m_element, 525 m_template)); 526 } 527 528 if (m_cmsObject.getRequestContext().currentProject().isOnlineProject()) { 530 throw new CmsIllegalArgumentException( 531 Messages.get().container(Messages.GUI_HTMLIMPORT_CONSTRAINT_OFFLINE_0)); 532 } 533 } 534 535 545 public String getAbsoluteUri(String relativeUri, String baseUri) { 546 547 if ((relativeUri == null) || (relativeUri.charAt(0) == '/') || (relativeUri.startsWith("#"))) { 548 549 return relativeUri; 550 } 551 552 String windowsAddition = ""; 554 if (File.separator.equals("\\")) { 555 windowsAddition = ":"; 556 } 557 558 try { 559 URL url = new URL (new URL (m_baseUrl, "file://" + baseUri), relativeUri); 560 if (url.getQuery() == null) { 561 if (url.getRef() == null) { 562 return url.getHost() + windowsAddition + url.getPath(); 563 } else { 564 return url.getHost() + windowsAddition + url.getPath() + "#" + url.getRef(); 565 } 566 } else { 567 return url.getHost() + windowsAddition + url.getPath() + "?" + url.getQuery(); 568 } 569 } catch (MalformedURLException e) { 570 return relativeUri; 571 } 572 } 573 574 579 public String getDestinationDir() { 580 581 return m_destinationDir; 582 } 584 585 590 public String getDownloadGallery() { 591 592 return m_downloadGallery; 593 } 594 595 600 public String getElement() { 601 602 return m_element; 603 } 604 605 610 public String getEndPattern() { 611 612 return m_endPattern; 613 } 614 615 620 public String getImageGallery() { 621 622 return m_imageGallery; 623 } 624 625 630 public String getInputDir() { 631 632 return m_inputDir; 633 } 634 635 640 public String getInputEncoding() { 641 642 return m_inputEncoding; 643 } 644 645 650 public String getKeepBrokenLinks() { 651 652 return m_keepBrokenLinks; 653 } 654 655 660 public String getLeaveDownloads() { 661 662 return m_leaveDownloads; 663 } 664 665 670 public String getLeaveExternalLinks() { 671 672 return m_leaveExternalLinks; 673 } 674 675 680 public String getLeaveImages() { 681 682 return m_leaveImages; 683 } 684 685 690 public String getLinkGallery() { 691 692 return m_linkGallery; 693 } 694 695 700 public Locale getLocale() { 701 702 return m_locale; 703 } 704 705 710 public String getOverwrite() { 711 712 return m_overwrite; 713 } 714 715 720 public String getStartPattern() { 721 722 return m_startPattern; 723 } 724 725 730 public String getTemplate() { 731 732 return m_template; 733 } 734 735 740 public String getThreadOutput() { 741 742 String output = ""; 743 if (m_htmlImportThread != null) { 745 if (m_htmlImportThread.isAlive()) { 747 output = m_htmlImportThread.getReportUpdate(); 748 } 749 } 750 751 return output; 752 } 753 754 759 public void setKeepBrokenLinks(String keepBrokenLinks) { 760 761 m_keepBrokenLinks = keepBrokenLinks; 762 } 763 764 769 public void setLeaveExternalLinks(String leaveExternalLinks) { 770 771 m_leaveExternalLinks = leaveExternalLinks; 772 } 773 774 781 public void startImport(I_CmsReport report) throws Exception { 782 783 try { 784 m_report = report; 785 m_report.println(Messages.get().container(Messages.RPT_HTML_IMPORT_BEGIN_0), I_CmsReport.FORMAT_HEADLINE); 786 787 buildIndex(m_inputDir); 789 buildParentPath(); 791 copyHtmlFiles(m_inputDir); 794 copyOtherFiles(m_inputDir); 796 createExternalLinks(); 798 m_report.println(Messages.get().container(Messages.RPT_HTML_IMPORT_END_0), I_CmsReport.FORMAT_HEADLINE); 799 } catch (Exception e) { 800 e.printStackTrace(); 801 } finally { 802 clear(); 804 } 805 806 } 807 808 815 public String storeExternalLink(String externalLink) { 816 817 if (!m_leaveExternalLinksMode) { 818 m_externalLinks.add(externalLink); 819 return getExternalLinkFile(externalLink); 820 } 821 822 return null; 823 } 824 825 832 public void storeImageInfo(String image, String altText) { 833 834 m_imageInfo.put(image, altText); 835 } 836 837 842 public boolean threadAlive() { 843 844 boolean alive = false; 845 if (m_htmlImportThread != null) { 846 alive = m_htmlImportThread.isAlive(); 847 } 848 849 return alive; 850 } 851 852 859 public String translateLink(String link) { 860 861 String translatedLink = null; 862 translatedLink = (String )m_fileIndex.get(link.replace('\\', '/')); 863 864 if (translatedLink == null) { 865 if (link.startsWith("#")) { 867 translatedLink = link; 868 } 869 870 else if (link.startsWith("/")) { 872 873 if (link.startsWith(OpenCms.getSystemInfo().getOpenCmsContext())) { 875 link = link.substring(OpenCms.getSystemInfo().getOpenCmsContext().length()); 876 } 877 878 if ((m_keepBrokenLinksMode) || (m_cmsObject.existsResource(link))) { 880 translatedLink = link; 881 } 882 } 883 884 897 else { 898 899 String fileBase = getBasePath(m_inputDir, link); 900 String cmsBase = (String )m_parents.get(fileBase); 901 if (cmsBase != null) { 902 String outLink = cmsBase + link.substring(fileBase.length()).replace('\\', '/'); 903 if ((m_keepBrokenLinksMode) || (m_cmsObject.existsResource(outLink))) { 904 translatedLink = outLink; 905 } 906 } 907 } 908 } 909 910 if ((translatedLink != null) && translatedLink.endsWith("/")) { 912 translatedLink += "index.html"; 913 } 914 915 if (translatedLink == null) { 919 translatedLink = "#"; 920 } 921 922 return translatedLink; 923 } 924 925 932 private void buildIndex(String startfolder) throws Exception { 933 934 File folder = new File (startfolder); 935 937 File [] subresources = folder.listFiles(); 938 for (int i = 0; i < subresources.length; i++) { 940 try { 941 942 String relativeFSName = subresources[i].getAbsolutePath().substring(m_inputDir.length() + 1); 943 String absoluteVFSName = getVfsName(relativeFSName, subresources[i].getName(), subresources[i].isFile()); 944 m_report.print(Messages.get().container(Messages.RPT_CREATE_INDEX_0), I_CmsReport.FORMAT_NOTE); 945 m_report.print(org.opencms.report.Messages.get().container( 946 org.opencms.report.Messages.RPT_ARGUMENT_1, 947 relativeFSName.replace('\\', '/'))); 948 m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0)); 949 m_report.print(Messages.get().container(Messages.RPT_ARROW_RIGHT_0), I_CmsReport.FORMAT_NOTE); 950 m_report.print(org.opencms.report.Messages.get().container( 951 org.opencms.report.Messages.RPT_ARGUMENT_1, 952 absoluteVFSName)); 953 m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0)); 954 m_fileIndex.put(subresources[i].getAbsolutePath().replace('\\', '/'), absoluteVFSName); 955 if (subresources[i].isDirectory()) { 957 buildIndex(subresources[i].getAbsolutePath()); 958 } 959 m_report.println( 960 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0), 961 I_CmsReport.FORMAT_OK); 962 } catch (Exception e) { 963 LOG.error(e); 964 m_report.println(e); 965 } 966 } 967 } 968 969 974 private void buildParentPath() { 975 976 String destFolder = m_destinationDir; 977 String inputDir = m_inputDir.replace('\\', '/'); 978 if (!inputDir.endsWith("/")) { 979 inputDir += "/"; 980 } 981 int pos = inputDir.lastIndexOf("/"); 982 while ((pos > 0) && (destFolder != null)) { 983 inputDir = inputDir.substring(0, pos); 984 m_parents.put(inputDir + "/", destFolder); 985 986 pos = inputDir.lastIndexOf("/", pos-1); 987 destFolder = CmsResource.getParentFolder(destFolder); 988 } 989 } 990 991 996 private void clear() { 997 998 m_fileIndex = null; 999 m_externalLinks = null; 1000 m_imageInfo = null; 1001 } 1002 1003 1010 private void copyHtmlFiles(String startfolder) throws Exception { 1011 1012 try { 1013 File folder = new File (startfolder); 1014 File [] subresources = folder.listFiles(); 1016 for (int i = 0; i < subresources.length; i++) { 1018 if (subresources[i].isDirectory()) { 1020 Hashtable properties = new Hashtable (); 1022 createFolder(subresources[i].getAbsolutePath(), i, properties); 1023 copyHtmlFiles(subresources[i].getAbsolutePath()); 1025 } else { 1026 String vfsFileName = (String )m_fileIndex.get(subresources[i].getAbsolutePath().replace('\\', '/')); 1028 int type = getFileType(vfsFileName); 1030 if (CmsResourceTypePlain.getStaticTypeId() == type) { 1031 Hashtable properties = new Hashtable (); 1032 String content = new String (); 1034 try { 1035 content = parseHtmlFile(subresources[i], properties); 1036 } catch (CmsException e) { 1037 m_report.println(e); 1038 } 1039 properties.put("template", m_template); 1040 1041 createFile(subresources[i].getAbsolutePath(), i, content, properties); 1043 } 1044 } 1045 } 1046 } catch (Exception e) { 1047 LOG.error(e); 1048 } 1049 } 1050 1051 1056 private void copyOtherFiles(String startfolder) { 1057 1058 try { 1059 File folder = new File (startfolder); 1060 File [] subresources = folder.listFiles(); 1062 for (int i = 0; i < subresources.length; i++) { 1064 if (subresources[i].isDirectory()) { 1066 copyOtherFiles(subresources[i].getAbsolutePath()); 1067 } else { 1068 if (!subresources[i].getName().equals(META_PROPERTIES)) { 1070 String vfsFileName = (String )m_fileIndex.get(subresources[i].getAbsolutePath().replace( 1072 '\\', 1073 '/')); 1074 int type = getFileType(vfsFileName); 1076 if (CmsResourceTypePlain.getStaticTypeId() != type) { 1077 1078 if (isExternal(vfsFileName)) { 1079 1080 m_report.print( 1081 Messages.get().container(Messages.RPT_SKIP_EXTERNAL_0), 1082 I_CmsReport.FORMAT_NOTE); 1083 m_report.print(org.opencms.report.Messages.get().container( 1084 org.opencms.report.Messages.RPT_ARGUMENT_1, 1085 subresources[i])); 1086 m_report.print(org.opencms.report.Messages.get().container( 1087 org.opencms.report.Messages.RPT_DOTS_0)); 1088 m_report.print( 1089 Messages.get().container(Messages.RPT_ARROW_RIGHT_0), 1090 I_CmsReport.FORMAT_NOTE); 1091 m_report.println(org.opencms.report.Messages.get().container( 1092 org.opencms.report.Messages.RPT_ARGUMENT_1, 1093 vfsFileName)); 1094 } else { 1095 1096 m_report.print(Messages.get().container(Messages.RPT_IMPORT_0), I_CmsReport.FORMAT_NOTE); 1097 m_report.print(org.opencms.report.Messages.get().container( 1098 org.opencms.report.Messages.RPT_ARGUMENT_1, 1099 vfsFileName)); 1100 m_report.print(org.opencms.report.Messages.get().container( 1101 org.opencms.report.Messages.RPT_DOTS_0)); 1102 1103 byte[] content = getFileBytes(subresources[i]); 1105 1107 List properties = new ArrayList (); 1109 String altText = (String )m_imageInfo.get(subresources[i].getAbsolutePath().replace( 1110 '\\', 1111 '/')); 1112 CmsProperty property1 = new CmsProperty( 1113 CmsPropertyDefinition.PROPERTY_DESCRIPTION, 1114 altText, 1115 altText); 1116 CmsProperty property2 = new CmsProperty( 1117 CmsPropertyDefinition.PROPERTY_TITLE, 1118 altText, 1119 altText); 1120 if (altText != null) { 1122 properties.add(property1); 1123 properties.add(property2); 1124 } 1125 if (!m_overwriteMode) { 1127 m_cmsObject.createResource(vfsFileName, type, content, properties); 1128 } else { 1129 try { 1130 CmsLock lock = m_cmsObject.getLock(vfsFileName); 1131 if (lock.getType() != CmsLock.TYPE_EXCLUSIVE) { 1132 m_cmsObject.lockResource(vfsFileName); 1133 } 1134 m_cmsObject.deleteResource(vfsFileName, CmsResource.DELETE_PRESERVE_SIBLINGS); 1135 } catch (CmsException e) { 1136 } finally { 1138 m_cmsObject.createResource(vfsFileName, type, content, properties); 1139 } 1140 1141 m_report.print( 1142 Messages.get().container(Messages.RPT_OVERWRITE_0), 1143 I_CmsReport.FORMAT_NOTE); 1144 m_report.print(org.opencms.report.Messages.get().container( 1145 org.opencms.report.Messages.RPT_DOTS_0)); 1146 } 1147 m_report.println(org.opencms.report.Messages.get().container( 1148 org.opencms.report.Messages.RPT_OK_0), I_CmsReport.FORMAT_OK); 1149 } 1150 } 1151 } 1152 } 1153 } 1154 } catch (Exception e) { 1155 LOG.error(e); 1156 m_report.println(e); 1157 } 1158 } 1159 1160 1164 private void createExternalLinks() { 1165 1166 Iterator i = m_externalLinks.iterator(); 1168 while (i.hasNext()) { 1169 String linkUrl = (String )i.next(); 1170 String filename = getExternalLinkFile(linkUrl); 1171 1172 m_report.print(Messages.get().container(Messages.RPT_CREATE_EXTERNAL_LINK_0), I_CmsReport.FORMAT_NOTE); 1173 m_report.print(org.opencms.report.Messages.get().container( 1174 org.opencms.report.Messages.RPT_ARGUMENT_1, 1175 filename)); 1176 m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0)); 1177 1178 List properties = new ArrayList (); 1179 CmsProperty property1 = new CmsProperty( 1180 CmsPropertyDefinition.PROPERTY_TITLE, 1181 "Link to " + linkUrl, 1182 "Link to " + linkUrl); 1183 properties.add(property1); 1184 try { 1185 m_cmsObject.createResource( 1186 m_linkGallery + filename, 1187 CmsResourceTypePointer.getStaticTypeId(), 1188 linkUrl.getBytes(), 1189 properties); 1190 } catch (CmsException e) { 1191 } 1193 m_report.println( 1194 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0), 1195 I_CmsReport.FORMAT_OK); 1196 } 1197 } 1198 1199 1207 private void createFile(String filename, int position, String content, Hashtable properties) { 1208 1209 String vfsFileName = (String )m_fileIndex.get(filename.replace('\\', '/')); 1210 1211 if (vfsFileName != null) { 1212 try { 1213 1214 m_report.print(Messages.get().container(Messages.RPT_CREATE_FILE_0), I_CmsReport.FORMAT_NOTE); 1215 m_report.print(org.opencms.report.Messages.get().container( 1216 org.opencms.report.Messages.RPT_ARGUMENT_1, 1217 vfsFileName)); 1218 m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0)); 1219 1220 if ((properties.get(CmsPropertyDefinition.PROPERTY_NAVPOS) == null) 1222 && (properties.get(CmsPropertyDefinition.PROPERTY_NAVTEXT) != null)) { 1223 properties.put(CmsPropertyDefinition.PROPERTY_NAVPOS, (position + 1) + ""); 1226 } 1227 1228 CmsXmlPage page = new CmsXmlPage(m_locale, OpenCms.getSystemInfo().getDefaultEncoding()); 1230 page.addValue(m_element, m_locale); 1231 page.setStringValue(m_cmsObject, m_element, m_locale, content); 1232 1233 CmsLinkTable linkTable = page.getLinkTable(m_element, m_locale); 1235 Iterator i = linkTable.iterator(); 1236 while (i.hasNext()) { 1237 CmsLink link = (CmsLink)i.next(); 1238 String target = link.getTarget(); 1239 if (target.indexOf("://") == 0) { 1241 target = m_cmsObject.getRequestContext().getFileTranslator().translateResource(target); 1243 link.updateLink(target, link.getAnchor(), link.getQuery()); 1245 } 1246 } 1247 byte[] contentByteArray = page.marshal(); 1249 List oldProperties = new ArrayList (); 1250 1251 if (!m_overwriteMode) { 1252 m_cmsObject.createResource( 1253 vfsFileName, 1254 CmsResourceTypeXmlPage.getStaticTypeId(), 1255 contentByteArray, 1256 new ArrayList ()); 1257 } else { 1258 try { 1259 oldProperties = m_cmsObject.readPropertyObjects(vfsFileName, false); 1261 CmsLock lock = m_cmsObject.getLock(vfsFileName); 1262 if (lock.getType() != CmsLock.TYPE_EXCLUSIVE) { 1263 m_cmsObject.lockResource(vfsFileName); 1264 } 1265 m_cmsObject.deleteResource(vfsFileName, CmsResource.DELETE_PRESERVE_SIBLINGS); 1266 } catch (CmsException e) { 1267 } finally { 1269 m_report.print(Messages.get().container(Messages.RPT_OVERWRITE_0), I_CmsReport.FORMAT_NOTE); 1271 m_report.print(org.opencms.report.Messages.get().container( 1272 org.opencms.report.Messages.RPT_DOTS_0)); 1273 m_cmsObject.createResource( 1274 vfsFileName, 1275 CmsResourceTypeXmlPage.getStaticTypeId(), 1276 contentByteArray, 1277 new ArrayList ()); 1278 } 1279 } 1280 Enumeration en = properties.keys(); 1282 List propertyList = new ArrayList (); 1283 while (en.hasMoreElements()) { 1284 String propertyKey = (String )en.nextElement(); 1286 String propertyVal = (String )properties.get(propertyKey); 1287 CmsProperty property = new CmsProperty(propertyKey, propertyVal, propertyVal); 1289 property.setAutoCreatePropertyDefinition(true); 1291 propertyList.add(property); 1293 } 1294 try { 1296 m_cmsObject.writePropertyObjects(vfsFileName, propertyList); 1297 m_cmsObject.writePropertyObjects(vfsFileName, oldProperties); 1299 } catch (CmsException e1) { 1300 e1.printStackTrace(); 1301 } 1302 m_report.println( 1303 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0), 1304 I_CmsReport.FORMAT_OK); 1305 } catch (CmsException e) { 1306 m_report.println(e); 1307 LOG.error(e); 1308 } 1309 } 1310 } 1311 1312 1319 private void createFolder(String foldername, int position, Hashtable properties) { 1320 1321 String vfsFolderName = (String )m_fileIndex.get(foldername.replace('\\', '/')); 1322 1323 m_report.print(Messages.get().container(Messages.RPT_CREATE_FOLDER_0), I_CmsReport.FORMAT_NOTE); 1324 m_report.print(org.opencms.report.Messages.get().container( 1325 org.opencms.report.Messages.RPT_ARGUMENT_1, 1326 vfsFolderName)); 1327 m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0)); 1328 1329 if (vfsFolderName != null) { 1330 String path = vfsFolderName.substring( 1331 0, 1332 vfsFolderName.substring(0, vfsFolderName.length() - 1).lastIndexOf("/")); 1333 String folder = vfsFolderName.substring(path.length(), vfsFolderName.length()); 1334 try { 1335 String propertyFileName = foldername + File.separator + META_PROPERTIES; 1337 1338 boolean metaPropertiesFound = false; 1339 ExtendedProperties propertyFile = new ExtendedProperties(); 1340 try { 1341 propertyFile.load(new FileInputStream (new File (propertyFileName))); 1342 metaPropertiesFound = true; 1343 } catch (Exception e1) { 1344 } 1347 if (metaPropertiesFound) { 1351 Enumeration enu = propertyFile.keys(); 1352 String property = ""; 1353 while (enu.hasMoreElements()) { 1354 try { 1356 property = (String )enu.nextElement(); 1357 String propertyvalue = (String )propertyFile.get(property); 1358 properties.put(property, propertyvalue); 1360 } catch (Exception e2) { 1361 e2.printStackTrace(); 1363 } 1364 } 1365 1366 if (properties.get(CmsPropertyDefinition.PROPERTY_NAVPOS) == null) { 1368 properties.put(CmsPropertyDefinition.PROPERTY_NAVPOS, (position + 1) + ""); 1371 } 1372 if (properties.get(CmsPropertyDefinition.PROPERTY_NAVTEXT) == null) { 1374 String navtext = folder.substring(1, 2).toUpperCase() 1376 + folder.substring(2, folder.length() - 1); 1377 properties.put(CmsPropertyDefinition.PROPERTY_NAVTEXT, navtext); 1378 } 1379 } else { 1380 properties = new Hashtable (); 1383 } 1384 try { 1386 m_cmsObject.readFolder(path + folder); 1387 m_cmsObject.lockResource(path + folder); 1388 } catch (CmsException e1) { 1389 m_cmsObject.createResource(path + folder, CmsResourceTypeFolder.getStaticTypeId()); 1391 } 1392 Enumeration enu = properties.keys(); 1394 List propertyList = new ArrayList (); 1395 while (enu.hasMoreElements()) { 1396 String propertyKey = (String )enu.nextElement(); 1398 String propertyVal = (String )properties.get(propertyKey); 1399 CmsProperty property = new CmsProperty(propertyKey, propertyVal, propertyVal); 1400 property.setAutoCreatePropertyDefinition(true); 1402 propertyList.add(property); 1404 } 1405 try { 1407 m_cmsObject.writePropertyObjects(path + folder, propertyList); 1408 } catch (CmsException e1) { 1409 e1.printStackTrace(); 1410 } 1411 m_report.println( 1412 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0), 1413 I_CmsReport.FORMAT_OK); 1414 } catch (CmsException e) { 1415 m_report.println(e); 1416 LOG.error(e); 1417 } 1418 } 1419 } 1420 1421 1428 private String getBasePath(String path1, String path2) { 1429 1430 StringBuffer base = new StringBuffer (); 1431 path1 = path1.replace('\\', '/'); 1432 path2 = path2.replace('\\', '/'); 1433 1434 String [] parts1 = path1.split("/"); 1435 String [] parts2 = path2.split("/"); 1436 1437 for (int i = 0; i < parts1.length; i++) { 1438 if (i >= parts2.length) { 1439 break; 1440 } 1441 if (parts1[i].equals(parts2[i])) { 1442 base.append(parts1[i] + "/"); 1443 } 1444 } 1445 1446 return base.toString(); 1447 } 1448 1449 1455 private String getExternalLinkFile(String link) { 1456 1457 String filename = link.substring(link.indexOf("://") + 3, link.length()); 1458 filename = m_cmsObject.getRequestContext().getFileTranslator().translateResource(filename.replace('/', '-')); 1459 return filename; 1460 } 1461 1462 1469 private byte[] getFileBytes(File file) throws CmsException { 1470 1471 byte[] buffer = null; 1472 1473 FileInputStream fileStream = null; 1474 int charsRead; 1475 int size; 1476 try { 1477 fileStream = new FileInputStream (file); 1478 charsRead = 0; 1479 size = new Long (file.length()).intValue(); 1480 buffer = new byte[size]; 1481 while (charsRead < size) { 1482 charsRead += fileStream.read(buffer, charsRead, size - charsRead); 1483 } 1484 return buffer; 1485 } catch (IOException e) { 1486 throw new CmsDbIoException( 1487 Messages.get().container(Messages.ERR_GET_FILE_BYTES_1, file.getAbsolutePath()), 1488 e); 1489 } finally { 1490 try { 1491 if (fileStream != null) { 1492 fileStream.close(); 1493 } 1494 } catch (IOException e) { 1495 e.printStackTrace(); 1496 } 1497 } 1498 } 1499 1500 1511 private int getFileType(String filename) throws Exception { 1512 1513 String extension = ""; 1514 if (filename.indexOf(".") > -1) { 1515 extension = filename.substring((filename.lastIndexOf("."))); 1516 } 1517 1518 String typename = (String )m_extensions.get(extension.toLowerCase()); 1519 if (typename == null) { 1520 typename = "binary"; 1521 } 1522 CmsResourceManager resourceManager = OpenCms.getResourceManager(); 1523 1524 return resourceManager.getResourceType(typename).getTypeId(); 1525 } 1526 1527 1539 private String getVfsName(String relativeName, String name, boolean isFile) throws Exception { 1540 1541 String vfsName = relativeName.replace('\\', '/'); 1543 if (isFile) { 1545 int filetype = getFileType(name); 1548 1549 if (name.indexOf(".") == 0) { 1551 name = "unknown" + name; 1552 int dot = relativeName.lastIndexOf("."); 1553 1554 relativeName = relativeName.substring(0, dot) + name; 1555 } 1556 1557 if ((CmsResourceTypeImage.getStaticTypeId() == filetype) && (!m_leaveImagesMode)) { 1564 1565 vfsName = m_imageGallery + name; 1569 } else if ((CmsResourceTypePlain.getStaticTypeId() == filetype) 1570 || (m_leaveImagesMode) 1571 || (m_leaveDownloadsMode)) { 1572 1575 String folderName = relativeName; 1578 if (folderName.indexOf(".") > 0) { 1579 folderName = folderName.substring(0, folderName.indexOf(".")); 1580 } 1581 folderName = m_inputDir + "\\" + folderName; 1582 File folder = new File (folderName); 1583 1584 if ((folder != null) && (folder.isDirectory())) { 1585 vfsName = m_destinationDir + relativeName.substring(0, relativeName.indexOf(".")) + "/index.html"; 1586 } else { 1588 vfsName = m_destinationDir + relativeName; 1590 } 1591 1592 } else { 1593 vfsName = m_downloadGallery + name; 1597 } 1598 return validateFilename(vfsName); 1601 } else { 1602 vfsName = m_destinationDir + vfsName + "/"; 1604 return vfsName; 1605 } 1606 } 1607 1608 1615 private boolean isExternal(String filename) { 1616 1617 boolean external = false; 1618 if (filename.indexOf("://") > 0) { 1619 external = true; 1620 } 1621 return external; 1622 } 1623 1624 1629 private boolean isValidElement() { 1630 1631 boolean validElement = false; 1632 List elementList = new ArrayList (); 1633 try { 1634 String elements = m_cms.property(CmsPropertyDefinition.PROPERTY_TEMPLATE_ELEMENTS, m_template); 1636 if (elements != null) { 1639 StringTokenizer T = new StringTokenizer (elements, ","); 1640 while (T.hasMoreTokens()) { 1641 String currentElement = T.nextToken(); 1643 int sepIndex = currentElement.indexOf("|"); 1644 if (sepIndex != -1) { 1645 currentElement = currentElement.substring(0, sepIndex); 1647 } 1648 if (currentElement.endsWith("*")) { 1649 currentElement = currentElement.substring(0, currentElement.length() - 1); 1651 } 1652 elementList.add(currentElement); 1653 } 1654 } 1655 if (elementList.contains(m_element)) { 1656 validElement = true; 1657 } 1658 } catch (Exception e) { 1659 e.printStackTrace(); 1660 } 1661 1662 return validElement; 1663 } 1664 1665 1674 private String parseHtmlFile(File file, Hashtable properties) throws CmsException { 1675 1676 String parsedHtml = ""; 1677 try { 1678 1679 byte[] content = getFileBytes(file); 1680 1681 String contentString = new String (content, m_inputEncoding); 1683 contentString = CmsEncoder.escapeNonAscii(contentString); 1685 contentString = CmsStringUtil.substitute(contentString, "&#", "{subst}"); 1687 parsedHtml = m_htmlConverter.convertHTML( 1689 file.getAbsolutePath(), 1690 contentString, 1691 m_startPattern, 1692 m_endPattern, 1693 properties); 1694 parsedHtml = CmsStringUtil.substitute(parsedHtml, "{subst}", "&#"); 1696 } catch (Exception e) { 1697 CmsMessageContainer message = Messages.get().container( 1698 Messages.ERR_HTMLIMPORT_PARSE_1, 1699 file.getAbsolutePath()); 1700 LOG.error(message.key(), e); 1701 throw new CmsImportExportException(message, e); 1702 } 1703 return parsedHtml; 1704 } 1705 1706 1715 private String validateFilename(String filename) { 1716 1717 if (isExternal(filename)) { 1719 return filename; 1720 } 1721 1722 1725 int postfix = 1; 1726 boolean found = true; 1727 String validFilename = filename; 1728 1729 if (!m_overwriteMode) { 1732 1733 while (found) { 1734 try { 1735 validFilename = m_cmsObject.getRequestContext().getFileTranslator().translateResource(validFilename); 1737 1738 found = true; 1740 if (!m_fileIndex.containsValue(validFilename.replace('\\', '/'))) { 1742 found = false; 1743 } 1744 if (!found) { 1745 found = true; 1746 m_cmsObject.readResource(validFilename, CmsResourceFilter.ALL); 1748 } 1749 String path = filename.substring(0, filename.lastIndexOf("/") + 1); 1751 String name = filename.substring(filename.lastIndexOf("/") + 1, filename.length()); 1752 validFilename = path; 1753 if (name.lastIndexOf(".") > 0) { 1754 validFilename += name.substring(0, name.lastIndexOf(".")); 1755 } else { 1756 validFilename += name; 1757 } 1758 validFilename += "_" + postfix; 1759 if (name.lastIndexOf(".") > 0) { 1760 validFilename += name.substring(name.lastIndexOf("."), name.length()); 1761 } 1762 postfix++; 1763 } catch (CmsException e) { 1764 found = false; 1766 } 1767 } 1768 1769 } else { 1770 validFilename = validFilename.replace('\\', '/'); 1771 } 1772 1773 return OpenCms.getResourceManager().getFileTranslator().translateResource(validFilename); 1774 } 1775 1776} 1777 | Popular Tags |