📄 cmsrelationsystemvalidator.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src/org/opencms/relations/CmsRelationSystemValidator.java,v $
* Date : $Date: 2007-09-10 10:11:52 $
* Version: $Revision: 1.4 $
*
* 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.relations;
import org.opencms.db.CmsDbContext;
import org.opencms.db.CmsDriverManager;
import org.opencms.db.CmsPublishList;
import org.opencms.file.CmsProject;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsResourceFilter;
import org.opencms.file.CmsVfsResourceNotFoundException;
import org.opencms.file.types.I_CmsResourceType;
import org.opencms.main.CmsException;
import org.opencms.main.CmsIllegalStateException;
import org.opencms.main.CmsLog;
import org.opencms.main.OpenCms;
import org.opencms.report.I_CmsReport;
import org.opencms.util.CmsStringUtil;
import org.opencms.workplace.commons.CmsProgressThread;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
/**
* Validates relations of resources in the OpenCms VFS.<p>
*
* Relations are, for instance, href attribs in anchor tags and src attribs in
* image tags, as well as OpenCmsVfsFile values in Xml Content.<p>
*
* External links to targets outside the OpenCms VFS don't get validated.<p>
*
* Objects using this class are responsible to handle detected broken links.<p>
*
* @author Thomas Weckert
* @author Michael Moossen
*
* @version $Revision: 1.4 $
*
* @since 6.3.0
*/
public class CmsRelationSystemValidator {
/** The log object for this class. */
private static final Log LOG = CmsLog.getLog(CmsRelationSystemValidator.class);
/** The driver manager. */
protected CmsDriverManager m_driverManager;
/**
* Default constructor.<p>
*
* @param driverManager The Cms driver manager
*/
public CmsRelationSystemValidator(CmsDriverManager driverManager) {
m_driverManager = driverManager;
}
/**
* Validates the relations against the online project.<p>
*
* The result is printed to the given report.<p>
*
* Validating references means to answer the question, whether
* we would have broken links in the online project if the given
* publish list would get published.<p>
*
* @param dbc the database context
* @param publishList the publish list to validate
* @param report a report to print messages
*
* @return a map with lists of invalid links
* (<code>{@link org.opencms.relations.CmsRelation}}</code> objects)
* keyed by root paths
*/
public Map validateResources(CmsDbContext dbc, CmsPublishList publishList, I_CmsReport report) {
// check if progress should be set in the thread
CmsProgressThread thread = null;
if (Thread.currentThread() instanceof CmsProgressThread) {
thread = (CmsProgressThread)Thread.currentThread();
}
Map invalidResources = new HashMap();
boolean interProject = (publishList != null);
if (report != null) {
report.println(
Messages.get().container(Messages.RPT_HTMLLINK_VALIDATOR_BEGIN_0),
I_CmsReport.FORMAT_HEADLINE);
}
List resources = new ArrayList();
if (publishList == null) {
CmsResourceFilter filter = CmsResourceFilter.IGNORE_EXPIRATION;
List resTypes = OpenCms.getResourceManager().getResourceTypes();
Iterator itTypes = resTypes.iterator();
int count = 0;
while (itTypes.hasNext()) {
// set progress in thread (first 10 percent)
count++;
if (thread != null) {
if (thread.isInterrupted()) {
throw new CmsIllegalStateException(org.opencms.workplace.commons.Messages.get().container(
org.opencms.workplace.commons.Messages.ERR_PROGRESS_INTERRUPTED_0));
}
thread.setProgress(count * 10 / resTypes.size());
}
I_CmsResourceType type = (I_CmsResourceType)itTypes.next();
if (type instanceof I_CmsLinkParseable) {
filter = filter.addRequireType(type.getTypeId());
try {
resources.addAll(m_driverManager.readResources(dbc, m_driverManager.readResource(
dbc,
"/",
filter), filter, true));
} catch (CmsException e) {
LOG.error(
Messages.get().getBundle().key(Messages.LOG_RETRIEVAL_RESOURCES_1, type.getTypeName()),
e);
}
}
}
} else {
resources.addAll(publishList.getAllResources());
}
// populate a lookup map with the project resources that
// actually get published keyed by their resource names.
// second, resources that don't get validated are ignored.
Map offlineFilesLookup = new HashMap();
List validatableResources = new ArrayList();
Iterator itResources = resources.iterator();
int count = 0;
while (itResources.hasNext()) {
// set progress in thread (next 10 percent)
count++;
if (thread != null) {
if (thread.isInterrupted()) {
throw new CmsIllegalStateException(org.opencms.workplace.commons.Messages.get().container(
org.opencms.workplace.commons.Messages.ERR_PROGRESS_INTERRUPTED_0));
}
thread.setProgress((count * 10 / resources.size()) + 10);
}
CmsResource resource = (CmsResource)itResources.next();
offlineFilesLookup.put(resource.getRootPath(), resource);
try {
I_CmsResourceType resourceType = OpenCms.getResourceManager().getResourceType(resource.getTypeId());
if (resourceType instanceof I_CmsLinkParseable) {
// don't validate links on deleted resources
validatableResources.add(resource);
}
} catch (CmsException e) {
LOG.error(
Messages.get().getBundle().key(Messages.LOG_RETRIEVAL_RESOURCETYPE_1, resource.getRootPath()),
e);
}
}
CmsProject project = dbc.currentProject();
if (interProject) {
try {
project = m_driverManager.readProject(dbc, CmsProject.ONLINE_PROJECT_ID);
} catch (CmsException e) {
// should never happen
LOG.error(e.getLocalizedMessage(), e);
}
}
boolean foundBrokenLinks = false;
for (int index = 0, size = validatableResources.size(); index < size; index++) {
// set progress in thread (next 20 percent; leave rest for creating the list and the html)
if (thread != null) {
if (thread.isInterrupted()) {
throw new CmsIllegalStateException(org.opencms.workplace.commons.Messages.get().container(
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -