001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software GmbH & Co. KG, please see the 018 * company website: https://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: https://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.importexport; 029 030import org.opencms.file.CmsObject; 031import org.opencms.main.OpenCms; 032import org.opencms.util.CmsStringUtil; 033import org.opencms.xml.CmsXmlEntityResolver; 034import org.opencms.xml.CmsXmlException; 035import org.opencms.xml.CmsXmlUtils; 036import org.opencms.xml.page.CmsXmlPage; 037 038import java.util.Iterator; 039import java.util.Locale; 040 041import org.dom4j.Document; 042import org.dom4j.Element; 043import org.dom4j.Node; 044 045/** 046 * Converts legacy pages (OpenCms 5 and earlier) to XML pages (OpenCms 6).<p> 047 * 048 * @since 6.0.0 049 * 050 * @deprecated no longer in use 051 */ 052@Deprecated 053public final class CmsXmlPageConverter { 054 055 /** 056 * Constructor made private to avoid class instanciation.<p> 057 */ 058 private CmsXmlPageConverter() { 059 060 // noop 061 } 062 063 /** 064 * Converts the contents of a page into an xml page.<p> 065 * 066 * @param cms the cms object 067 * @param content the content used with xml templates 068 * @param locale the locale of the body element(s) 069 * @param encoding the encoding to the xml page 070 * @return the xml page content or null if conversion failed 071 * @throws CmsImportExportException if the body content or the XMLTEMPLATE element were not found 072 * @throws CmsXmlException if there is an error reading xml contents from the byte array into a document 073 */ 074 @SuppressWarnings("unchecked") 075 public static CmsXmlPage convertToXmlPage(CmsObject cms, byte[] content, Locale locale, String encoding) 076 throws CmsImportExportException, CmsXmlException { 077 078 CmsXmlPage xmlPage = null; 079 080 Document page = CmsXmlUtils.unmarshalHelper(content, new CmsXmlEntityResolver(null)); 081 082 Element xmltemplate = page.getRootElement(); 083 if ((xmltemplate == null) || !"XMLTEMPLATE".equals(xmltemplate.getName())) { 084 throw new CmsImportExportException(Messages.get().container(Messages.ERR_NOT_FOUND_ELEM_XMLTEMPLATE_0)); 085 } 086 087 // get all edittemplate nodes 088 Iterator<Element> i = xmltemplate.elementIterator("edittemplate"); 089 boolean useEditTemplates = true; 090 if (!i.hasNext()) { 091 // no edittemplate nodes found, get the template nodes 092 i = xmltemplate.elementIterator("TEMPLATE"); 093 useEditTemplates = false; 094 } 095 096 // now create the XML page 097 xmlPage = new CmsXmlPage(locale, encoding); 098 099 while (i.hasNext()) { 100 Element currentTemplate = i.next(); 101 String bodyName = currentTemplate.attributeValue("name"); 102 if (CmsStringUtil.isEmpty(bodyName)) { 103 // no template name found, use the parameter body name 104 bodyName = "body"; 105 } 106 String bodyContent = null; 107 108 if (useEditTemplates) { 109 // no content manipulation needed for edittemplates 110 bodyContent = currentTemplate.getText(); 111 } else { 112 // parse content for TEMPLATEs 113 StringBuffer contentBuffer = new StringBuffer(); 114 for (Iterator<Node> k = currentTemplate.nodeIterator(); k.hasNext();) { 115 Node n = k.next(); 116 if (n.getNodeType() == Node.CDATA_SECTION_NODE) { 117 contentBuffer.append(n.getText()); 118 continue; 119 } else if (n.getNodeType() == Node.ELEMENT_NODE) { 120 if ("LINK".equals(n.getName())) { 121 contentBuffer.append(OpenCms.getSystemInfo().getOpenCmsContext()); 122 contentBuffer.append(n.getText()); 123 continue; 124 } 125 } 126 } 127 bodyContent = contentBuffer.toString(); 128 } 129 130 if (bodyContent == null) { 131 throw new CmsImportExportException(Messages.get().container(Messages.ERR_BODY_CONTENT_NOT_FOUND_0)); 132 } 133 134 bodyContent = CmsStringUtil.substitute( 135 bodyContent, 136 CmsStringUtil.MACRO_OPENCMS_CONTEXT, 137 OpenCms.getSystemInfo().getOpenCmsContext()); 138 139 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(bodyContent)) { 140 xmlPage.addValue(bodyName, locale); 141 xmlPage.setStringValue(cms, bodyName, locale, bodyContent); 142 } 143 } 144 145 return xmlPage; 146 147 } 148}