001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://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: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://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.CmsXmlException;
034import org.opencms.xml.CmsXmlUtils;
035import org.opencms.xml.page.CmsXmlPage;
036
037import java.util.Iterator;
038import java.util.Locale;
039
040import org.dom4j.Document;
041import org.dom4j.Element;
042import org.dom4j.Node;
043
044/**
045 * Converts legacy pages (OpenCms 5 and earlier) to XML pages (OpenCms 6).<p>
046 *
047 * @since 6.0.0
048 *
049 * @deprecated no longer in use
050 */
051@Deprecated
052public final class CmsXmlPageConverter {
053
054    /**
055     * Constructor made private to avoid class instanciation.<p>
056     */
057    private CmsXmlPageConverter() {
058
059        // noop
060    }
061
062    /**
063     * Converts the contents of a page into an xml page.<p>
064     *
065     * @param cms the cms object
066     * @param content the content used with xml templates
067     * @param locale the locale of the body element(s)
068     * @param encoding the encoding to the xml page
069     * @return the xml page content or null if conversion failed
070     * @throws CmsImportExportException if the body content or the XMLTEMPLATE element were not found
071     * @throws CmsXmlException if there is an error reading xml contents from the byte array into a document
072     */
073    @SuppressWarnings("unchecked")
074    public static CmsXmlPage convertToXmlPage(CmsObject cms, byte[] content, Locale locale, String encoding)
075    throws CmsImportExportException, CmsXmlException {
076
077        CmsXmlPage xmlPage = null;
078
079        Document page = CmsXmlUtils.unmarshalHelper(content, null);
080
081        Element xmltemplate = page.getRootElement();
082        if ((xmltemplate == null) || !"XMLTEMPLATE".equals(xmltemplate.getName())) {
083            throw new CmsImportExportException(Messages.get().container(Messages.ERR_NOT_FOUND_ELEM_XMLTEMPLATE_0));
084        }
085
086        // get all edittemplate nodes
087        Iterator<Element> i = xmltemplate.elementIterator("edittemplate");
088        boolean useEditTemplates = true;
089        if (!i.hasNext()) {
090            // no edittemplate nodes found, get the template nodes
091            i = xmltemplate.elementIterator("TEMPLATE");
092            useEditTemplates = false;
093        }
094
095        // now create the XML page
096        xmlPage = new CmsXmlPage(locale, encoding);
097
098        while (i.hasNext()) {
099            Element currentTemplate = i.next();
100            String bodyName = currentTemplate.attributeValue("name");
101            if (CmsStringUtil.isEmpty(bodyName)) {
102                // no template name found, use the parameter body name
103                bodyName = "body";
104            }
105            String bodyContent = null;
106
107            if (useEditTemplates) {
108                // no content manipulation needed for edittemplates
109                bodyContent = currentTemplate.getText();
110            } else {
111                // parse content for TEMPLATEs
112                StringBuffer contentBuffer = new StringBuffer();
113                for (Iterator<Node> k = currentTemplate.nodeIterator(); k.hasNext();) {
114                    Node n = k.next();
115                    if (n.getNodeType() == Node.CDATA_SECTION_NODE) {
116                        contentBuffer.append(n.getText());
117                        continue;
118                    } else if (n.getNodeType() == Node.ELEMENT_NODE) {
119                        if ("LINK".equals(n.getName())) {
120                            contentBuffer.append(OpenCms.getSystemInfo().getOpenCmsContext());
121                            contentBuffer.append(n.getText());
122                            continue;
123                        }
124                    }
125                }
126                bodyContent = contentBuffer.toString();
127            }
128
129            if (bodyContent == null) {
130                throw new CmsImportExportException(Messages.get().container(Messages.ERR_BODY_CONTENT_NOT_FOUND_0));
131            }
132
133            bodyContent = CmsStringUtil.substitute(
134                bodyContent,
135                CmsStringUtil.MACRO_OPENCMS_CONTEXT,
136                OpenCms.getSystemInfo().getOpenCmsContext());
137
138            if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(bodyContent)) {
139                xmlPage.addValue(bodyName, locale);
140                xmlPage.setStringValue(cms, bodyName, locale, bodyContent);
141            }
142        }
143
144        return xmlPage;
145
146    }
147}