📄 a_cmsxmlcontent.java
字号:
* @param data2 Second datablock hashtable.
* @return Concatenated data.
*/
private Hashtable concatData(Hashtable data1, Hashtable data2) {
Hashtable retValue = (Hashtable)data1.clone();
Enumeration keys = data2.keys();
Object key;
while(keys.hasMoreElements()) {
key = keys.nextElement();
retValue.put(key, data2.get(key));
}
return retValue;
}
/**
* Create a new CmsFile object containing an empty XML file of the
* current content type.
* The String returned by <code>getXmlDocumentTagName()</code>
* will be used to build the XML document element.
* @param cms Current cms object used for accessing system resources.
* @param filename Name of the file to be created.
* @param documentType Document type of the new file.
* @exception CmsException if no absolute filename is given or write access failed.
*/
public void createNewFile(CmsObject cms, String filename, String documentType) throws CmsException {
if(!filename.startsWith("/")) {
// this is no absolute filename.
this.throwException("Cannot create new file. Bad name.", CmsException.C_BAD_NAME);
}
int slashIndex = filename.lastIndexOf("/") + 1;
String folder = filename.substring(0, slashIndex);
String file = filename.substring(slashIndex);
cms.createFile(folder, file, "".getBytes(), documentType);
cms.lockResource(filename);
m_cms = cms;
m_filename = filename;
try {
m_content = parser.createEmptyDocument(getXmlDocumentTagName());
}
catch(Exception e) {
throwException("Cannot create empty XML document for file " + m_filename + ". ", CmsException.C_XML_PARSING_ERROR);
}
write();
}
/**
* Internal method for debugging purposes.
* Dumps the content of the datablock hashtable to the logfile.
*/
private void dumpDatablocks() {
if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging() ) {
Enumeration hashKeys = m_blocks.keys();
String key = null;
Element node = null;
A_OpenCms.log(C_OPENCMS_DEBUG, "******** DUMP OF DATABLOCK HASHTABLE *********");
while(hashKeys.hasMoreElements()) {
key = (String)hashKeys.nextElement();
node = (Element)m_blocks.get(key);
A_OpenCms.log(C_OPENCMS_DEBUG, "* " + key + " --> " + node.getNodeName());
}
A_OpenCms.log(C_OPENCMS_DEBUG, "**********************************************");
}
}
/**
* Internal method for debugging purposes.
* Dumpes the content of a given NodeList to the logfile.
*
* @param l NodeList to dump.
*/
private void dumpNodeList(NodeList l) {
if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging() ) {
if(l == null) {
A_OpenCms.log(C_OPENCMS_DEBUG, "******* NODE LIST IS NULL ********");
}
else {
int len = l.getLength();
A_OpenCms.log(C_OPENCMS_DEBUG, "******** DUMP OF NODELIST ********");
A_OpenCms.log(C_OPENCMS_DEBUG, "* LEN: " + len);
for(int i = 0;i < len;i++) {
A_OpenCms.log(C_OPENCMS_DEBUG, "*" + l.item(i));
}
A_OpenCms.log(C_OPENCMS_DEBUG, "**********************************");
}
}
}
/**
* Fast method to replace a datablock.
* <P>
* <b>USE WITH CARE!</b>
* <P>
* Using this method only if
* <ul>
* <li>The tag name is given in lowercase</li>
* <li>The datablock already exists (it may be empty)</li>
* <li>Neither tag nor data are <code>null</code></li>
* <li>You are sure, there will occure no errors</li>
* </ul>
*
* @param tag Key for this datablock.
* @param data String to be put in the datablock.
*/
protected void fastSetData(String tag, String data) {
// fastSetData could have been called with an upper case argument
tag = tag.toLowerCase();
Element originalBlock = (Element)(m_blocks.get(tag));
while(originalBlock.hasChildNodes()) {
originalBlock.removeChild(originalBlock.getFirstChild());
}
originalBlock.appendChild(m_content.createTextNode(data));
}
/**
* Gets the absolute filename of the XML file represented by this content class
* @return Absolute filename
*/
public String getAbsoluteFilename() {
return m_filename;
}
/**
* Gets all datablocks (the datablock hashtable).
* @return Hashtable with all datablocks.
*/
protected Hashtable getAllData() {
return m_blocks;
}
/**
* Help method to print nice classnames in error messages
* @return class name in [ClassName] format
*/
protected String getClassName() {
String name = getClass().getName();
return "[" + name.substring(name.lastIndexOf(".") + 1) + "] ";
}
/**
* This method should be implemented by every extending class.
* It returns a short description of the content definition type
* (e.g. "OpenCms news article").
* @return content description.
*/
abstract public String getContentDescription();
/**
* 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.
*/
protected Element getData(String tag) throws CmsException {
Object result = m_blocks.get(tag.toLowerCase());
if(result == null) {
String errorMessage = "Unknown Datablock " + tag + " requested.";
throwException(errorMessage, CmsException.C_XML_UNKNOWN_DATA);
}
else {
if(!(result instanceof Element)) {
String errorMessage = "Unexpected object returned as datablock. Requested Tagname: " + tag + ". Returned object: " + result.getClass().getName() + ".";
throwException(errorMessage, CmsException.C_XML_CORRUPT_INTERNAL_STRUCTURE);
}
}
return (Element)m_blocks.get(tag.toLowerCase());
}
/**
* 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.
*/
protected String getDataValue(String tag) throws CmsException {
Element dataElement = getData(tag);
return getTagValue(dataElement);
}
/**
* Gets a short filename (without path) of the XML file represented by this content class
* of the template file.
* @return filename
*/
public String getFilename() {
return m_filename.substring(m_filename.lastIndexOf("/") + 1);
}
/**
* Gets a processed datablock from the datablock hashtable.
*
* @param tag Key for the datablocks hashtable.
* @return Processed datablock for the given key.
* @exception CmsException
*/
protected Element getProcessedData(String tag) throws CmsException {
return getProcessedData(tag, null, null);
}
/**
* Gets a processed datablock from the datablock hashtable.
*
* @param tag Key for the datablocks hashtable.
* @param callingObject Object that should be used to look up user methods.
* @return Processed datablock for the given key.
* @exception CmsException
*/
protected Element getProcessedData(String tag, Object callingObject) throws CmsException {
return getProcessedData(tag, callingObject, null);
}
/**
* Gets a processed datablock from the datablock hashtable.
* <P>
* The userObj Object is passed to all called user methods.
* By using this, the initiating class can pass customized data to its methods.
*
* @param tag Key for the datablocks hashtable.
* @param callingObject Object that should be used to look up user methods.
* @param userObj any object that should be passed to user methods
* @return Processed datablock for the given key.
* @exception CmsException
*/
protected Element getProcessedData(String tag, Object callingObject, Object userObj) throws CmsException {
Element dBlock = (Element)getData(tag).cloneNode(true);
processNode(dBlock, m_mainProcessTags, null, callingObject, userObj);
return dBlock;
}
/**
* Gets a processed datablock from the datablock hashtable.
* <P>
* The userObj Object is passed to all called user methods.
* By using this, the initiating class can pass customized data to its methods.
*
* @param tag Key for the datablocks hashtable.
* @param callingObject Object that should be used to look up user methods.
* @param userObj any object that should be passed to user methods
* @param stream OutputStream that may be used for directly streaming the results or null.
* @return Processed datablock for the given key.
* @exception CmsException
*/
protected Element getProcessedData(String tag, Object callingObject, Object userObj, OutputStream stream) throws CmsException {
Element dBlock = (Element)getData(tag).cloneNode(true);
processNode(dBlock, m_mainProcessTags, null, callingObject, userObj, stream);
return dBlock;
}
/**
* Gets the text and CDATA content of a processed datablock from the
* datablock hashtable.
*
* @param tag Key for the datablocks hashtable.
* @return Processed datablock for the given key.
* @exception CmsException
*/
protected String getProcessedDataValue(String tag) throws CmsException {
return getProcessedDataValue(tag, null, null, null);
}
/**
* Gets the text and CDATA content of a processed datablock from the
* datablock hashtable.
*
* @param tag Key for the datablocks hashtable.
* @param callingObject Object that should be used to look up user methods.
* @return Processed datablock for the given key.
* @exception CmsException
*/
protected String getProcessedDataValue(String tag, Object callingObject) throws CmsException {
return getProcessedDataValue(tag, callingObject, null, null);
}
/**
* Gets the text and CDATA content of a processed datablock from the
* datablock hashtable.
* <P>
* The userObj Object is passed to all called user methods.
* By using this, the initiating class can pass customized data to its methods.
*
* @param tag Key for the datablocks hashtable.
* @param callingObject Object that should be used to look up user methods.
* @param userObj any object that should be passed to user methods
* @return Processed datablock for the given key.
* @exception CmsException
*/
protected String getProcessedDataValue(String tag, Object callingObject, Object userObj) throws CmsException {
return getProcessedDataValue(tag, callingObject, userObj, null);
}
/**
* Gets the text and CDATA content of a processed datablock from the
* datablock hashtable. An eventually given output stream is user for streaming
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -