KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opencms > template > CmsXmlTemplateLinkConverter


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/template/CmsXmlTemplateLinkConverter.java,v $
3  * Date : $Date: 2005/06/22 10:38:29 $
4  * Version: $Revision: 1.3 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (C) 2002 Alkacon Software (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, 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 com.opencms.template;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.main.CmsException;
36 import org.opencms.main.OpenCms;
37
38 import com.opencms.htmlconverter.CmsHtmlConverter;
39 import com.opencms.legacy.CmsLegacyException;
40 import com.opencms.legacy.CmsXmlTemplateLoader;
41
42 import java.net.URL JavaDoc;
43
44 import javax.servlet.http.HttpServletRequest JavaDoc;
45
46 /**
47  * Provides utility methods for handling content of deprecated XmlTemplate methods.<p>
48  *
49  * This class is mostly required for database imports of OpenCms versions &lt; 5.0.<p>
50  *
51  * @author Alexander Kandzior
52  * @version $Revision: 1.3 $
53  * @since 5.3.2
54  *
55  * @deprecated Will not be supported past the OpenCms 6 release.
56  */

57 public final class CmsXmlTemplateLinkConverter {
58
59     /** The JTidy configuration for the HTML conversion */
60     private static final String JavaDoc m_converterConfiguration =
61         "<?xml version=\"1.0\"?>"
62             + "<converterconfig>"
63             + " <defaults>"
64             + " <xhtmloutput value=\"false\"/>"
65             + " <globalprefix value=\"\"/>"
66             + " <globalsuffix value=\"\"/>"
67             + " <globaladdeveryline value=\"false\"/>"
68             + " <usebrackets value=\"true\" openbracket=\"#(\" closebracket=\")#\"/>"
69             + " <encodequotationmarks value=\"false\"/>"
70             + " </defaults>"
71             + " <replacecontent>"
72             + " <string content=\"&amp;mdash;\" replace=\"$mdash$\"/>"
73             + " <string content=\"&amp;ndash;\" replace=\"$ndash$\"/>"
74             + " <string content=\"&amp;bull;\" replace=\"$bull$\"/>"
75             + " <string content=\"&#\" replace=\"$Sonder$\"/>"
76             + " </replacecontent>"
77             + " <replacestrings usedefaults=\"true\">"
78             + " <string content=\"$Sonder$\" replace=\"&#\"/>"
79             + " <string content=\"$mdash$\" replace=\"&amp;mdash;\"/>"
80             + " <string content=\"$ndash$\" replace=\"&amp;ndash;\"/>"
81             + " <string content=\"$bull$\" replace=\"&amp;bull;\"/>"
82             + " </replacestrings>"
83             + " <inlinetags>"
84             + " <tag name=\"img\"/>"
85             + " <tag name=\"br\"/>"
86             + " <tag name=\"hr\"/>"
87             + " <tag name=\"input\"/>"
88             + " <tag name=\"frame\"/>"
89             + " <tag name=\"meta\"/>"
90             + " </inlinetags>"
91             + " <replacetags usedefaults=\"true\">"
92             + " <tag name=\"img\" attrib=\"src\" replacestarttag=\"]]&gt;&lt;LINK&gt;&lt;![CDATA[$parameter$]]&gt;&lt;/LINK&gt;&lt;![CDATA[\" parameter=\"src\" replaceparamattr=\"true\"/>"
93             + " <tag name=\"a\" attrib=\"href\" replacestarttag=\"]]&gt;&lt;LINK&gt;&lt;![CDATA[$parameter$]]&gt;&lt;/LINK&gt;&lt;![CDATA[\" replaceendtag=\"&lt;/a&gt;\" parameter=\"href\" replaceparamattr=\"true\"/>"
94             + " </replacetags>"
95             + "</converterconfig>";
96
97     /**
98      * Hide constructor since this class contains only static methods.<p>
99      */

100     private CmsXmlTemplateLinkConverter() {
101         // empty
102
}
103
104     /**
105      * Parses the html content of an imported XmlTemplate. <p>
106      *
107      * It replaces the links in &lt;a HREF="" and in &lt;image SRC="".
108      * They will be replaced with ]]&gt;&lt;LINK&gt;
109      * {path in OpenCms} &lt;LINK&gt;&lt;![CDATA[. <p>
110      *
111      * This method is used for database imports of OpenCms versions &lt; 5.0 <p>
112      *
113      * @param body the html content fragment
114      * @param webappUrl the old web app URL, e.g. http://localhost:8080/opencms/opencms/
115      * @param fileName the path and name of current file
116      * @return String the converted content
117      * @throws CmsException if something goes wrong
118      */

119     public static String JavaDoc convertFromImport(String JavaDoc body, String JavaDoc webappUrl, String JavaDoc fileName) throws CmsException {
120         // prepare the content for the JTidy
121
body = "<html><head></head><body>" + body + "</body></html>";
122         CmsHtmlConverter converter = new CmsHtmlConverter();
123         try {
124             // check errors...
125
if (converter.hasErrors(body)) {
126                 String JavaDoc errors = converter.showErrors(body);
127                 throw new CmsLegacyException(errors);
128             }
129             // configure the converter
130
converter.setConverterConfString(m_converterConfiguration);
131             URL JavaDoc url = new URL JavaDoc(webappUrl + fileName);
132             converter.setServletPrefix(OpenCms.getSystemInfo().getOpenCmsContext(), null);
133             converter.setOriginalUrl(url);
134             // convert html code
135
body = converter.convertHTML(body);
136         } catch (Exception JavaDoc e) {
137             e.printStackTrace(System.err);
138             throw new CmsLegacyException("[LinkSubstitution] can't convert the editor content:" + e.toString());
139         }
140         // remove the preparetags
141
int startIndex = body.indexOf("<body");
142         startIndex = body.indexOf(">", startIndex + 1) + 1;
143         int endIndex = body.lastIndexOf("</body>");
144         if (startIndex > 0) {
145             body = body.substring(startIndex, endIndex);
146         }
147         return body;
148     }
149
150     /**
151      * Parses the html content of an edited XmlTemplate. <p>
152      *
153      * It replaces the links in &lt;a HREF="" and in &lt;image SRC="".
154      * They will be replaced with ]]&gt;&lt;LINK&gt;
155      * {path in OpenCms} &lt;LINK&gt;&lt;![CDATA[. <p>
156      *
157      * This method is used for by the old XmlTemplate page editor.<p>
158      *
159      * @param cms the cms context
160      * @param content the content of the editor
161      * @param path the path of the file
162      * @param relativeRoot the relative root
163      * @return the parsed content
164      * @throws CmsException if something goes wrong
165      */

166     public static String JavaDoc convertFromEditor(CmsObject cms, String JavaDoc content, String JavaDoc path, String JavaDoc relativeRoot) throws CmsException {
167         CmsHtmlConverter converter = new CmsHtmlConverter();
168         String JavaDoc retValue = null;
169         if (path == null || "".equals(path)) {
170             path = "/";
171         }
172         try {
173             if (converter.hasErrors(content)) {
174                 String JavaDoc errors = converter.showErrors(content);
175                 throw new CmsLegacyException(errors);
176             }
177             converter.setConverterConfString(m_converterConfiguration);
178             // get parameter to create the url object of the edited file
179
String JavaDoc servletPrefix = CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getServletUrl();
180             String JavaDoc prot = ((HttpServletRequest JavaDoc)CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getOriginalRequest()).getScheme();
181             String JavaDoc host = ((HttpServletRequest JavaDoc)CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getOriginalRequest()).getServerName();
182             int port = ((HttpServletRequest JavaDoc)CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getOriginalRequest()).getServerPort();
183             URL JavaDoc urltool = new URL JavaDoc(prot, host, port, servletPrefix + path);
184             converter.setServletPrefix(servletPrefix, relativeRoot);
185             converter.setOriginalUrl(urltool);
186             retValue = converter.convertHTML(content);
187         } catch (Exception JavaDoc e) {
188             throw new CmsLegacyException("[LinkSubstitution] can't convert the editor content:" + e.toString());
189         }
190         return retValue;
191     }
192 }
Popular Tags