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