cmsresourcecomparisondialog.java
来自「找了很久才找到到源代码」· Java 代码 · 共 784 行 · 第 1/2 页
JAVA
784 行
/*
* File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/comparison/CmsResourceComparisonDialog.java,v $
* Date : $Date: 2007-09-05 11:19:35 $
* Version: $Revision: 1.10 $
*
* This library is part of OpenCms -
* the Open Source Content Management System
*
* Copyright (c) 2002 - 2007 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.CmsProject;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsResourceFilter;
import org.opencms.file.history.CmsHistoryFile;
import org.opencms.file.history.CmsHistoryResourceHandler;
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.util.CmsUUID;
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.CmsResourceInfoDialog;
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.10 $
*
* @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 CmsResourceInfoDialog(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 whether to compare properties, attributes or elements. */
private String m_paramCompare;
/** Parameter value for the element name. */
private String m_paramElement;
/** Parameter value for the structure id of the first file. */
private String m_paramId1;
/** Parameter value for the structure id of the second file. */
private String m_paramId2;
/** Parameter value for the locale. */
private String m_paramLocale;
/** 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 historical file or the offline file, depending on the version number.<p>
*
* @param cms the CmsObject to use
* @param structureId the structure id of the file
* @param version the historical version number
*
* @return either the historical file or the offline file, depending on the version number
*
* @throws CmsException if something goes wrong
*/
protected static CmsFile readFile(CmsObject cms, CmsUUID structureId, String version) throws CmsException {
if (Integer.parseInt(version) == CmsHistoryResourceHandler.PROJECT_OFFLINE_VERSION) {
// offline
CmsResource resource = cms.readResource(structureId);
return cms.readFile(resource);
} else {
int ver = Integer.parseInt(version);
if (ver < 0) {
// online
CmsProject project = cms.getRequestContext().currentProject();
try {
cms.getRequestContext().setCurrentProject(cms.readProject(CmsProject.ONLINE_PROJECT_ID));
CmsResource resource = cms.readResource(structureId);
return cms.readFile(resource);
} finally {
cms.getRequestContext().setCurrentProject(project);
}
}
// backup
return cms.readFile((CmsHistoryFile)cms.readResource(structureId, ver));
}
}
/**
* Returns either the historical resource or the offline resource, depending on the version number.<p>
*
* @param cms the CmsObject to use
* @param id the structure id of the resource
* @param version the historical version number
*
* @return either the historical resource or the offline resource, depending on the version number
*
* @throws CmsException if something goes wrong
*/
protected static CmsResource readResource(CmsObject cms, CmsUUID id, String version) throws CmsException {
if (Integer.parseInt(version) == CmsHistoryResourceHandler.PROJECT_OFFLINE_VERSION) {
return cms.readResource(id, CmsResourceFilter.IGNORE_EXPIRATION);
} else {
int ver = Integer.parseInt(version);
if (ver < 0) {
CmsProject project = cms.getRequestContext().currentProject();
try {
cms.getRequestContext().setCurrentProject(cms.readProject(CmsProject.ONLINE_PROJECT_ID));
return cms.readResource(id, CmsResourceFilter.IGNORE_EXPIRATION);
} finally {
cms.getRequestContext().setCurrentProject(project);
}
}
return (CmsResource)cms.readResource(id, ver);
}
}
/**
* Display method for two list dialogs.<p>
*
* @throws Exception if something goes wrong
*/
public void displayDialog() throws Exception {
CmsResourceInfoDialog fileInfo = new CmsResourceInfoDialog(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) {
/**
* @see org.opencms.workplace.list.CmsMultiListDialog#defaultActionHtmlEnd()
*/
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) {
/**
* @see org.opencms.workplace.list.CmsMultiListDialog#defaultActionHtmlEnd()
*/
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 if (propertyDiff.getResource1().isFile()) {
// display attributes and properties
lists.add(propertyDiff);
CmsMultiListDialog twoLists = new CmsMultiListDialog(lists);
twoLists.displayDialog(true);
if (twoLists.isForwarded()) {
return;
}
String path1 = propertyDiff.getResource1().getRootPath();
String path2 = propertyDiff.getResource2().getRootPath();
byte[] content1 = readFile(getCms(), propertyDiff.getResource1().getStructureId(), getParamVersion1()).getContents();
byte[] content2 = readFile(getCms(), propertyDiff.getResource2().getStructureId(), getParamVersion2()).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
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?