📄 cmsresourcecomparisondialog.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/comparison/CmsResourceComparisonDialog.java,v $
* Date : $Date: 2006/03/30 09:30:30 $
* Version: $Revision: 1.4 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
*
* 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 Alkacon Software GmbH, please see the
* company website: http://www.alkacon.com
*
* For further information about OpenCms, please see the
* project 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 org.opencms.workplace.comparison;
import org.opencms.file.CmsFile;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsResourceFilter;
import org.opencms.file.types.CmsResourceTypeImage;
import org.opencms.file.types.CmsResourceTypeJsp;
import org.opencms.file.types.CmsResourceTypePlain;
import org.opencms.file.types.CmsResourceTypePointer;
import org.opencms.file.types.CmsResourceTypeXmlContent;
import org.opencms.file.types.CmsResourceTypeXmlPage;
import org.opencms.file.types.I_CmsResourceType;
import org.opencms.jsp.CmsJspActionElement;
import org.opencms.main.CmsException;
import org.opencms.main.CmsLog;
import org.opencms.main.OpenCms;
import org.opencms.search.extractors.CmsExtractorMsExcel;
import org.opencms.search.extractors.CmsExtractorMsPowerPoint;
import org.opencms.search.extractors.CmsExtractorMsWord;
import org.opencms.search.extractors.CmsExtractorPdf;
import org.opencms.search.extractors.CmsExtractorRtf;
import org.opencms.search.extractors.I_CmsTextExtractor;
import org.opencms.util.CmsStringUtil;
import org.opencms.widgets.I_CmsWidget;
import org.opencms.widgets.I_CmsWidgetParameter;
import org.opencms.workplace.CmsDialog;
import org.opencms.workplace.CmsWorkplaceSettings;
import org.opencms.workplace.commons.CmsHistoryList;
import org.opencms.workplace.list.CmsMultiListDialog;
import org.opencms.xml.CmsXmlException;
import org.opencms.xml.I_CmsXmlDocument;
import org.opencms.xml.content.CmsXmlContent;
import org.opencms.xml.content.CmsXmlContentFactory;
import org.opencms.xml.content.I_CmsXmlContentValueVisitor;
import org.opencms.xml.page.CmsXmlPage;
import org.opencms.xml.page.CmsXmlPageFactory;
import org.opencms.xml.types.I_CmsXmlContentValue;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;
import org.apache.commons.logging.Log;
/**
* Helper class for managing three lists on the same dialog.<p>
*
* @author Jan Baudisch
*
* @version $Revision: 1.4 $
*
* @since 6.0.0
*/
public class CmsResourceComparisonDialog extends CmsDialog {
/**
* Visitor that collects the xpath expressions of xml contents.<p>
*/
class CmsXmlContentTextExtractor implements I_CmsXmlContentValueVisitor {
/** The StringBuffer to write the extracted text to. */
private StringBuffer m_buffer;
/** The locales of the xml content. */
private List m_locales;
/**
* Creates a new CmsXmlContentTextExtractor.<p>
*
* @param stringBuffer the StringBuffer to append the element text to
*/
CmsXmlContentTextExtractor(StringBuffer stringBuffer) {
m_buffer = stringBuffer;
m_locales = new ArrayList();
}
/**
*
* @see org.opencms.xml.content.I_CmsXmlContentValueVisitor#visit(org.opencms.xml.types.I_CmsXmlContentValue)
*/
public void visit(I_CmsXmlContentValue value) {
// only add simple types
if (value.isSimpleType()) {
String locale = value.getLocale().toString();
if (!m_locales.contains(locale)) {
m_buffer.append("\n\n[").append(locale).append(']');
m_locales.add(locale);
}
m_buffer.append("\n\n[").append(value.getPath()).append("]\n\n");
try {
I_CmsWidget widget = value.getDocument().getContentDefinition().getContentHandler().getWidget(value);
m_buffer.append(widget.getWidgetStringValue(
getCms(),
new CmsFileInfoDialog(getJsp()),
(I_CmsWidgetParameter)value));
} catch (CmsXmlException e) {
LOG.error(e.getMessage(), e);
}
}
}
}
/** Constant indicating that all elements are compared.<p> */
public static final String COMPARE_ALL_ELEMENTS = "allelements";
/** Constant indicating that the attributes are compared.<p> */
public static final String COMPARE_ATTRIBUTES = "attributes";
/** Constant indicating that the properties are compared.<p> */
public static final String COMPARE_PROPERTIES = "properties";
/** The log object for this class. */
static final Log LOG = CmsLog.getLog(CmsResourceComparisonDialog.class);
/** The difference dialog. */
private CmsDifferenceDialog m_differenceDialog;
/** Parameter value indicating wether to compare properties, attributes or elements. */
private String m_paramCompare;
/** Parameter value for the element name. */
private String m_paramElement;
/** Parameter value for the locale. */
private String m_paramLocale;
/** Parameter value for the path of the first file. */
private String m_paramPath1;
/** Parameter value for the path of the second file. */
private String m_paramPath2;
/** Parameter value for the tag id of the first file. */
private String m_paramTagId1;
/** Parameter value for the tag id of the second file. */
private String m_paramTagId2;
/** Parameter value for the text mode. */
private String m_paramTextmode;
/** Parameter value for the version of the first file. */
private String m_paramVersion1;
/** Parameter value for the version of the second file. */
private String m_paramVersion2;
/**
* Public constructor with JSP action element.<p>
*
* @param jsp an initialized JSP action element
*/
public CmsResourceComparisonDialog(CmsJspActionElement jsp) {
super(jsp);
}
/**
* Public constructor with JSP variables.<p>
*
* @param context the JSP page context
* @param req the JSP request
* @param res the JSP response
*/
public CmsResourceComparisonDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) {
this(new CmsJspActionElement(context, req, res));
}
/**
* Returns either the backup file or the offline file, depending on the version number.<p>
*
* @param cms the CmsObject to use
* @param path the path of the file
* @param version the backup version
* @param tagId the tag id of the file
*
* @return either the backup file or the offline file, depending on the version number
*
* @throws CmsException if something goes wrong
*/
protected static CmsFile readFile(CmsObject cms, String path, String version, int tagId) throws CmsException {
try {
cms.getRequestContext().saveSiteRoot();
cms.getRequestContext().setSiteRoot("/");
if (CmsHistoryList.OFFLINE_PROJECT.equals(version)) {
return cms.readFile(cms.getRequestContext().removeSiteRoot(path), CmsResourceFilter.ALL);
} else {
return cms.readBackupFile(cms.getRequestContext().removeSiteRoot(path), tagId);
}
} finally {
cms.getRequestContext().restoreSiteRoot();
}
}
/**
* Display method for two list dialogs.<p>
*
* @throws Exception if something goes wrong
*/
public void displayDialog() throws Exception {
CmsFileInfoDialog fileInfo = new CmsFileInfoDialog(getJsp()) {
protected String defaultActionHtmlEnd() {
return "";
}
};
fileInfo.displayDialog(true);
if (fileInfo.isForwarded()) {
return;
}
CmsPropertyComparisonList propertyDiff = new CmsPropertyComparisonList(getJsp());
CmsAttributeComparisonList attributeDiff = new CmsAttributeComparisonList(getJsp());
List lists = new ArrayList();
lists.add(attributeDiff);
I_CmsResourceType resourceType = OpenCms.getResourceManager().getResourceType(propertyDiff.getResourceType());
if (resourceType instanceof CmsResourceTypeXmlContent || resourceType instanceof CmsResourceTypeXmlPage) {
// display attributes, properties and compared elements
CmsElementComparisonList contentDiff = new CmsElementComparisonList(getJsp());
lists.add(contentDiff);
lists.add(propertyDiff);
CmsMultiListDialog threeLists = new CmsMultiListDialog(lists);
// perform the active list actions
threeLists.displayDialog(true);
// write the dialog just if no list has been forwarded
if (threeLists.isForwarded()) {
return;
}
fileInfo.writeDialog();
threeLists.writeDialog();
} else if (resourceType instanceof CmsResourceTypeImage) {
// display attributes, properties and images
lists.add(propertyDiff);
CmsMultiListDialog twoLists = new CmsMultiListDialog(lists) {
public String defaultActionHtmlEnd() {
return "";
}
};
twoLists.displayDialog(true);
if (twoLists.isForwarded()) {
return;
}
CmsImageComparisonDialog images = new CmsImageComparisonDialog(getJsp());
fileInfo.writeDialog();
twoLists.writeDialog();
// this is very dangerous
// it is possible that here a forward is tried, what should not be sence we already wrote to the output stream.
// CmsImageComparisonDialog should implement isForwarded, writeDialog and displayDialog(boolean) methods
images.displayDialog();
} else if (resourceType instanceof CmsResourceTypePointer) {
lists.add(propertyDiff);
CmsMultiListDialog twoLists = new CmsMultiListDialog(lists) {
public String defaultActionHtmlEnd() {
return "";
}
};
twoLists.displayDialog(true);
if (twoLists.isForwarded()) {
return;
}
CmsPointerComparisonDialog pointers = new CmsPointerComparisonDialog(getJsp());
fileInfo.writeDialog();
twoLists.writeDialog();
// same as for CmsImageComparisonDialog
pointers.displayDialog();
} else {
// display attributes and properties
lists.add(propertyDiff);
CmsMultiListDialog twoLists = new CmsMultiListDialog(lists);
twoLists.displayDialog(true);
if (twoLists.isForwarded()) {
return;
}
String path1 = propertyDiff.getParamPath1();
String path2 = propertyDiff.getParamPath2();
byte[] content1 = propertyDiff.getFile1().getContents();
byte[] content2 = propertyDiff.getFile2().getContents();
String originalSource = null;
String copySource = null;
I_CmsTextExtractor textExtractor = null;
if (path1.endsWith(".pdf") && path2.endsWith(".pdf")) {
textExtractor = CmsExtractorPdf.getExtractor();
} else if (path1.endsWith(".doc") && path2.endsWith(".doc")) {
textExtractor = CmsExtractorMsWord.getExtractor();
} else if (path1.endsWith(".xls") && path2.endsWith(".xls")) {
textExtractor = CmsExtractorMsExcel.getExtractor();
} else if (path1.endsWith(".rtf") && path2.endsWith(".rtf")) {
textExtractor = CmsExtractorRtf.getExtractor();
} else if (path1.endsWith(".ppt") && path2.endsWith(".ppt")) {
textExtractor = CmsExtractorMsPowerPoint.getExtractor();
}
if (textExtractor != null) {
// extract the content
originalSource = textExtractor.extractText(content1).getContent();
copySource = textExtractor.extractText(content2).getContent();
} else if (resourceType instanceof CmsResourceTypePlain || resourceType instanceof CmsResourceTypeJsp) {
originalSource = new String(content1);
copySource = new String(content2);
}
fileInfo.writeDialog();
twoLists.writeDialog();
if (CmsStringUtil.isNotEmpty(originalSource) && CmsStringUtil.isNotEmpty(copySource)) {
m_differenceDialog.setCopySource(copySource);
m_differenceDialog.setOriginalSource(originalSource);
// same as for CmsImageComparisonDialog
m_differenceDialog.displayDialog();
}
}
}
/**
* Displays the difference dialog.<p>
* @throws Exception if something goes wrong
*/
public void displayDifferenceDialog() throws Exception {
m_differenceDialog.displayDialog();
}
/**
* Converts an attribute list to a string.<p>
*
* @param attributes a list of compared attributes to be converted to a string
* @return a string respresentation of the attribute list
*/
public String[] getAttributesAsString(List attributes) {
Iterator i = attributes.iterator();
StringBuffer res1 = new StringBuffer(512);
StringBuffer res2 = new StringBuffer(512);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -