KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > importexport > CmsXmlPageConverter


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/importexport/CmsXmlPageConverter.java,v $
3  * Date : $Date: 2006/03/27 14:52:54 $
4  * Version: $Revision: 1.25 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.importexport;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.main.OpenCms;
36 import org.opencms.util.CmsStringUtil;
37 import org.opencms.xml.CmsXmlException;
38 import org.opencms.xml.CmsXmlUtils;
39 import org.opencms.xml.page.CmsXmlPage;
40
41 import java.util.Iterator JavaDoc;
42 import java.util.Locale JavaDoc;
43
44 import org.dom4j.Document;
45 import org.dom4j.Element;
46 import org.dom4j.Node;
47
48 /**
49  * Converts legacy pages (OpenCms 5 and earlier) to XML pages (OpenCms 6).<p>
50  *
51  * @author Carsten Weinholz
52  *
53  * @version $Revision: 1.25 $
54  *
55  * @since 6.0.0
56  */

57 public final class CmsXmlPageConverter {
58
59     /**
60      * Constructor made private to avoid class instanciation.<p>
61      */

62     private CmsXmlPageConverter() {
63
64         // noop
65
}
66
67     /**
68      * Converts the contents of a page into an xml page.<p>
69      *
70      * @param cms the cms object
71      * @param content the content used with xml templates
72      * @param locale the locale of the body element(s)
73      * @param encoding the encoding to the xml page
74      * @return the xml page content or null if conversion failed
75      * @throws CmsImportExportException if the body content or the XMLTEMPLATE element were not found
76      * @throws CmsXmlException if there is an error reading xml contents from the byte array into a document
77      */

78     public static CmsXmlPage convertToXmlPage(CmsObject cms, byte[] content, Locale JavaDoc locale, String JavaDoc encoding)
79     throws CmsImportExportException, CmsXmlException {
80
81         CmsXmlPage xmlPage = null;
82
83         Document page = CmsXmlUtils.unmarshalHelper(content, null);
84
85         Element xmltemplate = page.getRootElement();
86         if (xmltemplate == null || !"XMLTEMPLATE".equals(xmltemplate.getName())) {
87             throw new CmsImportExportException(Messages.get().container(Messages.ERR_NOT_FOUND_ELEM_XMLTEMPLATE_0));
88         }
89
90         // get all edittemplate nodes
91
Iterator JavaDoc i = xmltemplate.elementIterator("edittemplate");
92         boolean useEditTemplates = true;
93         if (!i.hasNext()) {
94             // no edittemplate nodes found, get the template nodes
95
i = xmltemplate.elementIterator("TEMPLATE");
96             useEditTemplates = false;
97         }
98
99         // now create the XML page
100
xmlPage = new CmsXmlPage(locale, encoding);
101
102         while (i.hasNext()) {
103             Element currentTemplate = (Element)i.next();
104             String JavaDoc bodyName = currentTemplate.attributeValue("name");
105             if (CmsStringUtil.isEmpty(bodyName)) {
106                 // no template name found, use the parameter body name
107
bodyName = "body";
108             }
109             String JavaDoc bodyContent = null;
110
111             if (useEditTemplates) {
112                 // no content manipulation needed for edittemplates
113
bodyContent = currentTemplate.getText();
114             } else {
115                 // parse content for TEMPLATEs
116
if (currentTemplate != null) {
117                     StringBuffer JavaDoc contentBuffer = new StringBuffer JavaDoc();
118                     for (Iterator JavaDoc k = currentTemplate.nodeIterator(); k.hasNext();) {
119                         Node n = (Node)k.next();
120                         if (n.getNodeType() == Node.CDATA_SECTION_NODE) {
121                             contentBuffer.append(n.getText());
122                             continue;
123                         } else if (n.getNodeType() == Node.ELEMENT_NODE) {
124                             if ("LINK".equals(n.getName())) {
125                                 contentBuffer.append(OpenCms.getSystemInfo().getOpenCmsContext());
126                                 contentBuffer.append(n.getText());
127                                 continue;
128                             }
129                         }
130                     }
131                     bodyContent = contentBuffer.toString();
132                 }
133             }
134
135             if (bodyContent == null) {
136                 throw new CmsImportExportException(Messages.get().container(Messages.ERR_BODY_CONTENT_NOT_FOUND_0));
137             }
138
139             bodyContent = CmsStringUtil.substitute(
140                 bodyContent,
141                 CmsStringUtil.MACRO_OPENCMS_CONTEXT,
142                 OpenCms.getSystemInfo().getOpenCmsContext());
143
144             if (!"".equals(bodyContent.trim())) {
145                 xmlPage.addValue(bodyName, locale);
146                 xmlPage.setStringValue(cms, bodyName, locale, bodyContent);
147             }
148         }
149
150         return xmlPage;
151
152     }
153 }
Popular Tags