📄 cmsxmltemplatefile.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src/com/opencms/template/CmsXmlTemplateFile.java,v $
* Date : $Date: 2002/04/05 12:34:50 $
* Version: $Revision: 1.52 $
*
* 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.file.*;
import com.opencms.core.*;
import com.opencms.template.cache.*;
import com.opencms.util.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import java.util.*;
import java.io.*;
/**
* Content definition for XML template files.
*
* @author Alexander Lucas
* @version $Revision: 1.52 $ $Date: 2002/04/05 12:34:50 $
*/
public class CmsXmlTemplateFile extends A_CmsXmlContent {
/** Name of the tag for the editable templates */
public static final String C_EDIT_TEMPLATE = "edittemplate";
/**
* 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);
}
/**
* Gets an enumeration of all used subelements in all sections of
* of this template file.
* @return Vector of all subtemplate names.
* @exception 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.
* @exception 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.
* @exception 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_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging() ) {
A_OpenCms.log(C_OPENCMS_INFO, 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() == n.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);
int endOpeningBodyTag = xmlString.indexOf(">", endOpeningDocTag + 1) + 1;
int startClosingDocTag = xmlString.lastIndexOf("<");
int startClosingBodyTag = xmlString.lastIndexOf("<", startClosingDocTag - 1);
if(startClosingBodyTag <= endOpeningBodyTag) {
xmlString = "";
}else {
xmlString = xmlString.substring(endOpeningBodyTag, startClosingBodyTag);
xmlString = xmlString.trim();
}
if(html) {
int cdataStart = xmlString.indexOf("<![CDATA[");
int currentPos = 0;
int loop = 0;
result.append("<HTML>\n<HEAD>\n");
result.append("<link rel=stylesheet type=\"text/css\" href=\"" + style + "\">\n");
result.append("</HEAD>\n");
result.append("<BASE href=\"");
I_CmsRequest req = m_cms.getRequestContext().getRequest();
result.append(req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + req.getServletUrl() + (String)parameters.get("file"));
result.append("\"></BASE>");
result.append("<BODY " + getProcessedDataValue("bodytag", callingObject, parameters) + ">\n");
while(cdataStart != -1) {
String tempString = xmlString.substring(currentPos, cdataStart);
tempString = replaceBack(tempString);
//result.append(xmlString.substring(currentPos, cdataStart).replace('<', '[').replace('>', ']'));
result.append(tempString);
result.append((String)cdatas.elementAt(loop++));
cdataStart = xmlString.indexOf("<![CDATA[", cdataStart + 1);
currentPos = xmlString.indexOf("]]>", currentPos + 1) + 3;
}
String tempString = xmlString.substring(currentPos);
tempString = replaceBack(tempString);
//result.append(xmlString.substring(currentPos).replace('<', '[').replace('>', ']'));
result.append(tempString);
result.append("\n</BODY>\n</HTML>");
xmlString = result.toString();
}else {
// We are in text mode.
// Check, if there is any content in this body.
// Otherwise, set empty CDATA blocks.
if(xmlString.trim().equals("")) {
xmlString = "<![CDATA[\n]]>";
}
}
return xmlString;
}
/**
* Internal utility method to extract the values of the "name" attribute
* from defined nodes of a given nodelist.
* @param nl NodeList to extract.
* @param tag Name of the tag whose "name" attribute should be extracted
* @param unnamedAllowed Indicates if unnamed tags are allowed or an exception should
* be thrown.
* @return Enumeration of all "name" attributes.
* @exception CmsException
*/
private Vector getNamesFromNodeList(NodeList nl, String tag, boolean unnamedAllowed) throws CmsException {
int numElements = nl.getLength();
Vector collectNames = new Vector();
for(int i = 0;i < numElements;i++) {
Node n = (Node)nl.item(i);
if(n.getNodeType() == n.ELEMENT_NODE && n.getNodeName().toLowerCase().equals(tag.toLowerCase())) {
String name = ((Element)n).getAttribute("name");
if(name == null || "".equals(name)) {
// unnamed element found.
if(unnamedAllowed) {
name = "(default)";
}
else {
if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging() ) {
A_OpenCms.log(C_OPENCMS_CRITICAL, "[CmsXmlControlFile] unnamed <" + n.getNodeName() + "> found in OpenCms control file " + getAbsoluteFilename() + ".");
}
throw new CmsException("Unnamed \"" + n.getNodeName() + "\" found in OpenCms control file " + getAbsoluteFilename() + ".", CmsException.C_XML_TAG_MISSING);
}
}
collectNames.addElement(name);
}
}
return collectNames;
}
/**
* Gets the value of a single parameter of a given subelement definition.
* @param elementName Name of the subelement.
* @param parameterName Name of the requested parameter.
*/
public String getParameter(String elementName, String parameterName) throws CmsException {
return getDataValue("ELEMENTDEF." + elementName + ".PARAMETER." + parameterName);
}
/**
* Gets an enumeration of all parameter names of a given subelement definition.
* @param elementName Name of the subelement.
* @return Vector of all names.
* @exception CmsException
*/
public Vector getParameterNames(String elementName) throws CmsException {
if(hasData("elementdef." + elementName)) {
Element elementDefinition = getData("elementdef." + elementName);
NodeList parameterTags = elementDefinition.getChildNodes();
return getNamesFromNodeList(parameterTags, "PARAMETER", false);
}
else {
return null;
}
}
/**
* Get a hashtable containing all parameters and thies values of a given subelement definition.
* @param elementName Name of the subelement.
* @return Enumeration of all names.
* @exception CmsException
*/
public Hashtable getParameters(String elementName) throws CmsException {
Hashtable result = new Hashtable();
if(hasData("elementdef." + elementName)) {
Element elementDefinition = getData("elementdef." + elementName);
NodeList parameterTags = elementDefinition.getChildNodes();
int numElements = parameterTags.getLength();
for(int i = 0;i < numElements;i++) {
Node n = (Node)parameterTags.item(i);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -