📄 cmsxmltemplatefile.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/template/CmsXmlTemplateFile.java,v $
* Date : $Date: 2005/05/31 15:51:19 $
* Version: $Revision: 1.3 $
*
* 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.template;
import org.opencms.file.CmsFile;
import org.opencms.file.CmsObject;
import org.opencms.main.CmsException;
import org.opencms.main.CmsLog;
import org.opencms.main.OpenCms;
import org.opencms.util.CmsStringUtil;
import com.opencms.core.I_CmsRequest;
import com.opencms.legacy.CmsLegacyException;
import com.opencms.legacy.CmsXmlTemplateLoader;
import com.opencms.template.cache.CmsElementLink;
import com.opencms.template.cache.CmsElementVariant;
import com.opencms.template.cache.CmsMethodLink;
import java.io.OutputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Hashtable;
import java.util.Vector;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* Content definition for XML template files.
*
* @author Alexander Lucas
* @version $Revision: 1.3 $ $Date: 2005/05/31 15:51:19 $
*
* @deprecated Will not be supported past the OpenCms 6 release.
*/
public class CmsXmlTemplateFile extends A_CmsXmlContent {
/** Name of the tag for the editable templates */
public static final String C_EDIT_TEMPLATE = "edittemplate";
/** Name of the tag for the templates */
public static final String C_TEMPLATE = "template";
/**
* Default constructor.
*/
public CmsXmlTemplateFile() throws CmsException {
if(CmsXmlTemplateLoader.getOnlineElementCache() == null){
registerTag("ELEMENT", CmsXmlTemplateFile.class, "handleElementTag", C_REGISTER_MAIN_RUN);
}
}
/**
* Constructor for creating a new object containing the content
* of the given filename.
*
* @param cms CmsObject object for accessing system resources.
* @param filename Name of the body file that shoul be read.
*/
public CmsXmlTemplateFile(CmsObject cms, CmsFile file) throws CmsException {
super();
if(!CmsXmlTemplateLoader.isElementCacheEnabled()) {
registerMyTags();
}
init(cms, file);
}
/**
* Constructor for creating a new object containing the content
* of the given filename.
*
* @param cms CmsObject object for accessing system resources.
* @param filename Name of the body file that shoul be read.
*/
public CmsXmlTemplateFile(CmsObject cms, String filename) throws CmsException {
super();
if(!CmsXmlTemplateLoader.isElementCacheEnabled()) {
registerMyTags();
}
init(cms, filename);
}
public CmsXmlTemplateFile(CmsObject cms, String filename, String content) throws CmsException {
super();
if(!CmsXmlTemplateLoader.isElementCacheEnabled()) {
registerMyTags();
}
init(cms, filename, content);
}
public int createNewSection(String sectionName) {
int loop = 2;
String tempName = sectionName + loop;
while(hasData("template." + tempName)) {
tempName = sectionName + (++loop);
}
Element newData = getXmlDocument().createElement("template");
newData.setAttribute("name", tempName);
setData("template." + tempName, newData);
// and now create the section for the editor
Element newEditData = getXmlDocument().createElement(C_EDIT_TEMPLATE);
newEditData.setAttribute("name", tempName);
setData(C_EDIT_TEMPLATE + "."+ tempName, newEditData);
return loop;
}
public Vector getAllSections() throws CmsException {
NodeList nl = getXmlDocument().getDocumentElement().getChildNodes();
return getNamesFromNodeList(nl, "TEMPLATE", true);
}
/**
* This method is used by the linkmanagement. It returns a Vector with all
* link tag values in all TEMPLATE sections of the document.
*/
public Vector getAllLinkTagValues()throws CmsException{
Vector retValue = new Vector();
NodeList list = getXmlDocument().getDocumentElement().getChildNodes();
int numElements = list.getLength();
for(int i=0; i < numElements; i++){
Node n = list.item(i);
// we only search in the template tags
if(n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().toLowerCase().equals(C_TEMPLATE)){
NodeList subList = n.getChildNodes();
for(int j=0; j<subList.getLength(); j++){
Node subNode = subList.item(j);
if(subNode.getNodeType()==Node.ELEMENT_NODE && subNode.getNodeName().equalsIgnoreCase("link")){
// TODO: check firstChild null?
String value = subNode.getFirstChild().getNodeValue();
if(!retValue.contains(value)){
retValue.add(value);
}
}
}
}
}
return retValue;
}
/**
* Gets an enumeration of all used subelements in all sections of
* of this template file.
* @return Vector of all subtemplate names.
* @throws CmsException
*/
public Vector getAllSubElements() throws CmsException {
NodeList nl = getXmlDocument().getDocumentElement().getElementsByTagName("*");
return getNamesFromNodeList(nl, "ELEMENT", false);
}
/**
* Gets an enumeration of all subelements defined in all sections of
* of this template file.
* @return Vector of all subtemplate names.
* @throws CmsException
*/
public Vector getAllSubElementDefinitions() throws CmsException {
NodeList nl = getXmlDocument().getDocumentElement().getElementsByTagName("*");
return getNamesFromNodeList(nl, "ELEMENTDEF", false);
}
/**
* Gets an enumeration of all used subelements in the given section
* of a template file.
* @param selector Section to be scanned for subelements
* @return Vector of all subtemplate names.
* @throws CmsException
*/
public Vector getAllSubElements(String selector) throws CmsException {
String templateDatablockName = getTemplateDatablockName(selector);
Element templateElement = getData(templateDatablockName);
NodeList nl = templateElement.getChildNodes();
return getNamesFromNodeList(nl, "ELEMENT", false);
}
public Element getBodyTag() throws CmsException {
Element result = null;
if(hasData("bodyTag")) {
result = getData("bodytag");
}
else {
if(CmsLog.getLog(this).isDebugEnabled() ) {
CmsLog.getLog(this).debug("Cannot find \"bodytag\" tag in XML template file " + getFilename());
}
}
return result;
}
/**
* Gets a description of this content type.
* @return Content type description.
*/
public String getContentDescription() {
return "OpenCms XML template file";
}
/**
* Gets a complete datablock from the datablock hashtable.
*
* @param tag Key for the datablocks hashtable.
* @return Complete DOM element of the datablock for the given key
* or null if no datablock is found for this key.
*/
public Element getData(String tag) throws CmsException {
return super.getData(tag);
}
/**
* Gets the text and CDATA content of a datablock from the
* datablock hashtable.
*
* @param tag Key for the datablocks hashtable.
* @return Datablock content for the given key or null if no datablock
* is found for this key.
*/
public String getDataValue(String tag) throws CmsException {
return super.getDataValue(tag);
}
public String getEditableTemplateContent(Object callingObject, Hashtable parameters, String templateSelector, boolean html, String style) throws CmsException {
Vector cdatas = new Vector();
String editDatablockName = this.getEditTemplateDatablockName(templateSelector);
String datablockName = null;
String testValue = getDataValue(editDatablockName);
// if the editDatablock is empty (or not there) this seems to be an old template,
// so we use the original file
if(testValue == null || "".equals(testValue)){
datablockName = this.getTemplateDatablockName(templateSelector);
}else{
datablockName = editDatablockName;
}
Element data = getData(datablockName);
StringBuffer result = new StringBuffer();
if(style == null) {
style = "";
}
Document tempDoc = (Document)getXmlDocument().cloneNode(true);
Element rootElem = tempDoc.getDocumentElement();
while(rootElem.hasChildNodes()) {
rootElem.removeChild(rootElem.getFirstChild());
}
data = (Element)getXmlParser().importNode(tempDoc, data);
rootElem.appendChild(data);
if(html) {
// Scan for cdatas
Node n = data;
while(n != null) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -