cmsresourcewrapperxmlpage.java
来自「找了很久才找到到源代码」· Java 代码 · 共 1,069 行 · 第 1/3 页
JAVA
1,069 行
/*
* File : $Source: /usr/local/cvs/opencms/src/org/opencms/file/wrapper/CmsResourceWrapperXmlPage.java,v $
* Date : $Date: 2007-09-05 11:19:35 $
* Version: $Revision: 1.9 $
*
* 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.file.wrapper;
import org.opencms.file.CmsFile;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsProperty;
import org.opencms.file.CmsPropertyDefinition;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsResourceFilter;
import org.opencms.file.CmsVfsResourceNotFoundException;
import org.opencms.file.CmsResource.CmsResourceCopyMode;
import org.opencms.file.CmsResource.CmsResourceDeleteMode;
import org.opencms.file.types.CmsResourceTypeFolder;
import org.opencms.file.types.CmsResourceTypePlain;
import org.opencms.file.types.CmsResourceTypeXmlPage;
import org.opencms.file.types.I_CmsResourceType;
import org.opencms.i18n.CmsEncoder;
import org.opencms.i18n.CmsLocaleManager;
import org.opencms.loader.CmsResourceManager;
import org.opencms.lock.CmsLock;
import org.opencms.main.CmsException;
import org.opencms.main.CmsIllegalArgumentException;
import org.opencms.main.OpenCms;
import org.opencms.util.CmsStringUtil;
import org.opencms.xml.page.CmsXmlPage;
import org.opencms.xml.page.CmsXmlPageFactory;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
/**
* A resource type wrapper for xml page files, which explodes the xml pages to folders.<p>
*
* Every resource of type "xmlpage" becomes a folder with the same name. That folder
* contains the locales of the xml page as folders too. In the locale folder there are
* the elements for that locale as files. The files have the names of the elements with the
* extension "html". Additionaly there is a file in the root folder of that xml page that
* contains the controlcode of the xml page. This file has the name "controlcode.xml".<p>
*
* @author Peter Bonrad
*
* @version $Revision: 1.9 $
*
* @since 6.5.6
*/
public class CmsResourceWrapperXmlPage extends A_CmsResourceWrapper {
/** The extension to use for elements. */
private static final String EXTENSION_ELEMENT = "html";
/** The name of the element to use for the controlcode. */
private static final String NAME_ELEMENT_CONTROLCODE = "controlcode.xml";
/** Table with the states of the virtual files. */
private static final Hashtable TMP_FILE_TABLE = new Hashtable();
/**
* @see org.opencms.file.wrapper.A_CmsResourceWrapper#addResourcesToFolder(CmsObject, String, CmsResourceFilter)
*/
public List addResourcesToFolder(CmsObject cms, String resourcename, CmsResourceFilter filter) throws CmsException {
CmsResource xmlPage = findXmlPage(cms, resourcename);
if (xmlPage != null) {
String path = getSubPath(cms, xmlPage, resourcename);
String rootPath = cms.getRequestContext().removeSiteRoot(xmlPage.getRootPath());
ArrayList ret = new ArrayList();
CmsFile file = cms.readFile(xmlPage);
CmsXmlPage xml = CmsXmlPageFactory.unmarshal(cms, file);
if (CmsStringUtil.isEmptyOrWhitespaceOnly(path)) {
// sub path is empty -> return all existing locales for the resource
if (file.getLength() == 0) {
return ret;
}
List locales = xml.getLocales();
Iterator iter = locales.iterator();
while (iter.hasNext()) {
Locale locale = (Locale)iter.next();
ret.add(getResourceForLocale(xmlPage, locale));
}
// check temp file table to add virtual file
iter = getVirtualFiles().iterator();
while (iter.hasNext()) {
String virtualFileName = (String)iter.next();
String virtualFilePath = rootPath + "/" + virtualFileName;
if ((!TMP_FILE_TABLE.containsKey(virtualFilePath))
|| (!TMP_FILE_TABLE.get(virtualFilePath).equals(new Integer(0)))) {
// read the control code resource
if (virtualFileName.equals(NAME_ELEMENT_CONTROLCODE)) {
CmsWrappedResource wrap = new CmsWrappedResource(xmlPage);
wrap.setRootPath(xmlPage.getRootPath() + "/" + NAME_ELEMENT_CONTROLCODE);
wrap.setTypeId(CmsResourceTypePlain.getStaticTypeId());
CmsFile tmpFile = wrap.getFile();
tmpFile.setContents(file.getContents());
ret.add(tmpFile);
}
}
}
} else {
// sub path is a locale -> return all elements for this locale
Locale locale = new Locale(path);
List names = xml.getNames(locale);
Iterator iter = names.iterator();
while (iter.hasNext()) {
String name = (String)iter.next();
String content = xml.getStringValue(cms, name, locale);
String fullPath = xmlPage.getRootPath() + "/" + path + "/" + name + "." + EXTENSION_ELEMENT;
content = prepareContent(content, cms, xmlPage, fullPath);
int length = content.length();
try {
length = content.getBytes(CmsLocaleManager.getResourceEncoding(cms, xmlPage)).length;
} catch (UnsupportedEncodingException e) {
// this will never happen since UTF-8 is always supported
}
ret.add(getResourceForElement(xmlPage, fullPath, length));
}
}
return ret;
}
return null;
}
/**
* @see org.opencms.file.wrapper.A_CmsResourceWrapper#copyResource(org.opencms.file.CmsObject, java.lang.String, java.lang.String, org.opencms.file.CmsResource.CmsResourceCopyMode)
*/
public boolean copyResource(CmsObject cms, String source, String destination, CmsResourceCopyMode siblingMode)
throws CmsException, CmsIllegalArgumentException {
// only allow copying of xml pages at whole or locales and elements inside the same xml page
CmsResource srcXmlPage = findXmlPage(cms, source);
if (srcXmlPage != null) {
String srcPath = getSubPath(cms, srcXmlPage, source);
// if the source is the xml page itself just copy the resource
if (CmsStringUtil.isEmptyOrWhitespaceOnly(srcPath)) {
cms.copyResource(source, destination, siblingMode);
return true;
} else {
// only a locale or an element should be copied
CmsResource destXmlPage = findXmlPage(cms, destination);
if (srcXmlPage.equals(destXmlPage)) {
// copying inside the same xml page resource
String destPath = getSubPath(cms, destXmlPage, destination);
String[] srcTokens = srcPath.split("/");
String[] destTokens = destPath.split("/");
if (srcTokens.length == destTokens.length) {
CmsFile srcFile = cms.readFile(srcXmlPage);
CmsXmlPage srcXml = CmsXmlPageFactory.unmarshal(cms, srcFile);
if (srcTokens.length == 1) {
if (srcTokens[0].equals(NAME_ELEMENT_CONTROLCODE)) {
// do nothing
} else {
// copy locale
srcXml.copyLocale(new Locale(srcTokens[0]), new Locale(destTokens[0]));
}
} else if (srcTokens.length == 2) {
// TODO: copy element
}
// write files
srcFile.setContents(srcXml.marshal());
cms.writeFile(srcFile);
return true;
} else {
// TODO: error: destination path is invalid
}
} else {
// TODO: error: copying only allowed inside the same xml page
}
}
}
return false;
}
/**
* @see org.opencms.file.wrapper.A_CmsResourceWrapper#createResource(org.opencms.file.CmsObject, java.lang.String, int, byte[], java.util.List)
*/
public CmsResource createResource(CmsObject cms, String resourcename, int type, byte[] content, List properties)
throws CmsException, CmsIllegalArgumentException {
// cut off trailing slash
if (resourcename.endsWith("/")) {
resourcename = resourcename.substring(0, resourcename.length() - 1);
}
// creating new xml pages if type is a folder and the name ends with .html
if ((type == CmsResourceTypeFolder.getStaticTypeId()) && resourcename.endsWith(".html")) {
// mark in temp file table that the visual files does not exist yet
Iterator iter = getVirtualFiles().iterator();
while (iter.hasNext()) {
TMP_FILE_TABLE.put(resourcename + "/" + (String)iter.next(), new Integer(0));
}
return cms.createResource(resourcename, CmsResourceTypeXmlPage.getStaticTypeId());
}
// find the xml page this is for
CmsResource xmlPage = findXmlPage(cms, resourcename);
if (xmlPage != null) {
// get the path below the xml page
String path = getSubPath(cms, xmlPage, resourcename);
// and the path without the site root
String rootPath = cms.getRequestContext().removeSiteRoot(xmlPage.getRootPath());
CmsFile file = cms.readFile(xmlPage);
CmsXmlPage xml = CmsXmlPageFactory.unmarshal(cms, file);
// mark virtual files as created in temp file table
if (getVirtualFiles().contains(path)) {
TMP_FILE_TABLE.remove(resourcename);
// at least lock file, because creating resources usually locks the resource
cms.lockResource(rootPath);
return file;
}
String[] tokens = path.split("/");
if (tokens.length == 1) {
Locale locale = new Locale(tokens[0]);
// workaround: empty xmlpages always have the default locale "en" set
if (file.getLength() == 0) {
Iterator iter = xml.getLocales().iterator();
while (iter.hasNext()) {
xml.removeLocale((Locale)iter.next());
}
}
// create new locale
xml.addLocale(cms, locale);
// save the xml page
file.setContents(xml.marshal());
// lock the resource
cms.lockResource(rootPath);
// write file
cms.writeFile(file);
} else if (tokens.length == 2) {
String name = tokens[1];
if (name.endsWith(EXTENSION_ELEMENT)) {
name = name.substring(0, name.length() - EXTENSION_ELEMENT.length() - 1);
}
// create new element
xml.addValue(name, new Locale(tokens[0]));
// set the content
xml.setStringValue(cms, name, new Locale(tokens[0]), getStringValue(cms, file, content));
// save the xml page
file.setContents(xml.marshal());
// lock the resource
cms.lockResource(rootPath);
// write file
cms.writeFile(file);
}
return file;
}
return null;
}
/**
* @see org.opencms.file.wrapper.A_CmsResourceWrapper#deleteResource(CmsObject, String, org.opencms.file.CmsResource.CmsResourceDeleteMode)
*/
public boolean deleteResource(CmsObject cms, String resourcename, CmsResourceDeleteMode siblingMode)
throws CmsException {
// find the xml page this is for
CmsResource xmlPage = findXmlPage(cms, resourcename);
if (xmlPage != null) {
// cut off trailing slash
if (resourcename.endsWith("/")) {
resourcename = resourcename.substring(0, resourcename.length() - 1);
}
// get the path below the xml page
String path = getSubPath(cms, xmlPage, resourcename);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?