📄 cmsxmltemplatefile.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src/com/opencms/template/CmsXmlTemplateFile.java,v $
* Date : $Date: 2003/04/09 14:04:04 $
* Version: $Revision: 1.69 $
*
* 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 com.opencms.boot.I_CmsLogChannels;
import com.opencms.core.A_OpenCms;
import com.opencms.core.CmsException;
import com.opencms.core.I_CmsRequest;
import com.opencms.core.OpenCms;
import com.opencms.file.CmsFile;
import com.opencms.file.CmsObject;
import com.opencms.flex.util.CmsStringSubstitution;
import com.opencms.template.cache.CmsElementLink;
import com.opencms.template.cache.CmsElementVariant;
import com.opencms.template.cache.CmsMethodLink;
import com.opencms.util.LinkSubstitution;
import com.opencms.workplace.I_CmsWpConstants;
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.69 $ $Date: 2003/04/09 14:04:04 $
*/
public class CmsXmlTemplateFile extends A_CmsXmlContent implements I_CmsWpConstants {
/** 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(OpenCms.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(!cms.getRequestContext().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(!cms.getRequestContext().isElementCacheEnabled()) {
registerMyTags();
}
init(cms, filename);
}
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 = ((Element)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 = ((Element)getXmlDocument().getDocumentElement()).getChildNodes();
int numElements = list.getLength();
for(int i=0; i < numElements; i++){
Node n = (Node)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 = (Node)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(I_CmsLogChannels.C_LOGGING && A_OpenCms.isLogging(I_CmsLogChannels.C_OPENCMS_DEBUG) ) {
A_OpenCms.log(C_OPENCMS_DEBUG, getClassName() + "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) {
if(n.getNodeType() == Node.CDATA_SECTION_NODE) {
cdatas.addElement(n.getNodeValue());
n.setNodeValue("");
}
n = treeWalker(rootElem, n);
}
}
StringWriter out = new StringWriter();
getXmlParser().getXmlText(tempDoc, out);
String xmlString = out.toString();
int endOpeningXmlTag = xmlString.indexOf(">");
int endOpeningDocTag = xmlString.indexOf(">", endOpeningXmlTag + 1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -