📄 linksubstitution.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src/com/opencms/util/LinkSubstitution.java,v $
* Date : $Date: 2003/04/01 15:20:17 $
* Version: $Revision: 1.27 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (C) 2001 The OpenCms Group
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* For further information about OpenCms, please see the
* OpenCms Website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.opencms.util;
import com.opencms.boot.I_CmsLogChannels;
import com.opencms.core.A_OpenCms;
import com.opencms.core.CmsException;
import com.opencms.core.CmsStaticExportProperties;
import com.opencms.core.OpenCms;
import com.opencms.file.CmsObject;
import com.opencms.file.CmsStaticExport;
import com.opencms.htmlconverter.CmsHtmlConverter;
import java.net.URL;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import org.apache.oro.text.perl.MalformedPerl5PatternException;
import org.apache.oro.text.perl.Perl5Util;
/**
*
*
* @author Hanjo Riege
* @version 1.0
*/
public class LinkSubstitution {
/**
* Reference to the CmsElementCache object containing locators for all
* URIs and elements in cache.
*/
private static Perl5Util c_perlUtil = null;
/**
* String to configure the CmsHtmlConverter
*/
private static String m_converterConfiguration = "<?xml version=\"1.0\"?>"+
"<converterconfig>"+
" <defaults>"+
" <xhtmloutput value=\"false\"/>"+
" <globalprefix value=\"\"/>"+
" <globalsuffix value=\"\"/>"+
" <globaladdeveryline value=\"false\"/>"+
" <usebrackets value=\"true\" openbracket=\"#(\" closebracket=\")#\"/>"+
" <encodequotationmarks value=\"false\"/>"+
" </defaults>"+
" <replacecontent>"+
" <string content=\"&mdash;\" replace=\"$mdash$\"/>"+
" <string content=\"&ndash;\" replace=\"$ndash$\"/>"+
" <string content=\"&bull;\" replace=\"$bull$\"/>"+
" <string content=\"&#\" replace=\"$Sonder$\"/>"+
" </replacecontent>"+
" <replacestrings usedefaults=\"true\">"+
" <string content=\"$Sonder$\" replace=\"&#\"/>"+
" <string content=\"$mdash$\" replace=\"&mdash;\"/>"+
" <string content=\"$ndash$\" replace=\"&ndash;\"/>"+
" <string content=\"$bull$\" replace=\"&bull;\"/>"+
" </replacestrings>"+
" <inlinetags>"+
" <tag name=\"img\"/>"+
" <tag name=\"br\"/>"+
" <tag name=\"hr\"/>"+
" <tag name=\"input\"/>"+
" <tag name=\"frame\"/>"+
" <tag name=\"meta\"/>"+
" </inlinetags>"+
" <replacetags usedefaults=\"true\">"+
" <tag name=\"img\" attrib=\"src\" replacestarttag=\"]]><LINK><![CDATA[$parameter$]]></LINK><![CDATA[\" parameter=\"src\" replaceparamattr=\"true\"/>"+
" <tag name=\"a\" attrib=\"href\" replacestarttag=\"]]><LINK><![CDATA[$parameter$]]></LINK><![CDATA[\" replaceendtag=\"</a>\" parameter=\"href\" replaceparamattr=\"true\"/>"+
" </replacetags>"+
"</converterconfig>";
public LinkSubstitution() {
c_perlUtil = new Perl5Util();
}
/**
* Parses the content of the body tag of a html page. It is the same as
* substituteEditorContent except that it expects only the part of the
* html page between the body tags.
*/
public String substituteEditorContentBody(CmsObject cms, String body) throws CmsException{
// we have to prepare the content for the tidy
body = "<html><head></head><body>" + body + "</body></html>";
// start the tidy
String result = substituteEditorContent(cms, body);
// remove the preparetags
int startIndex = result.indexOf("<body");
startIndex = result.indexOf(">", startIndex + 1) + 1;
int endIndex = result.lastIndexOf("</body>");
if(startIndex > 0) {
result = result.substring(startIndex, endIndex);
}
return result;
}
/**
* Parses the html content for the editor. It replaces the links in <a href=""
* and in <image src="". They will be replaced with ]]><LINK> path in opencms <LINK><![CDATA[.<p>
*
* This method is used for database imports of OpenCms versions < 5.0<p>
*
* @param cms the CmsObject
* @param content the html content fragment
* @param webappUrl the old web app URL, e.g. http://localhost:8080/opencms/opencms/
* @param fileName the path and name of current file
* @return String the converted content
*/
public String substituteEditorContent(CmsObject cms, String content)throws CmsException{
return substituteEditorContent(cms, content, null, null);
}
public String substituteContentBody(String body, String webappUrl, String fileName) throws CmsException {
// prepare the content for the JTidy
body = "<html><head></head><body>" + body + "</body></html>";
CmsHtmlConverter converter = new CmsHtmlConverter();
try {
// check errors...
if (converter.hasErrors(body)) {
String errors = converter.showErrors(body);
throw new CmsException(errors);
}
// configure the converter
converter.setConverterConfString(m_converterConfiguration);
URL url = new URL(webappUrl + fileName);
String servletPrefix = A_OpenCms.getOpenCmsContext();
converter.setServletPrefix(servletPrefix.substring(0, servletPrefix.lastIndexOf("/")), null);
converter.setOriginalUrl(url);
// convert html code
body = converter.convertHTML(body);
} catch (Exception e) {
e.printStackTrace(System.err);
throw new CmsException(
"[" + this.getClass().getName() + "] can't convert the editor content:" + e.toString());
}
// remove the preparetags
int startIndex = body.indexOf("<body");
startIndex = body.indexOf(">", startIndex + 1) + 1;
int endIndex = body.lastIndexOf("</body>");
if (startIndex > 0) {
body = body.substring(startIndex, endIndex);
}
return body;
}
/**
* parses the html content from the editor. It replaces the links in <a href=""
* and in <image src="". They will be replaced with ]]><LINK> path in opencms <LINK><![CDATA[
*/
public String substituteEditorContent(CmsObject cms, String content, String path, String relativeRoot)throws CmsException{
CmsHtmlConverter converter = new CmsHtmlConverter();
String retValue = null;
if(path == null || "".equals(path)){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -