📄 a_cmsxmlcontent.java
字号:
*/
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.
* @throws 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);
cms.createResource(folder, filename, documentType, null, "".getBytes());
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();
}
/**
* 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.
* @throws 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.
* @throws 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.
* @throws 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.
* @throws 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.
* @throws 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.
* @throws 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.
* @throws 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
* the generated result directly to the response output stream while processing.
* <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.
* @throws CmsException
*/
protected String getProcessedDataValue(String tag, Object callingObject, Object userObj, OutputStream stream) throws CmsException {
// we cant cache the methods here, so we use the other way
registerTag("METHOD", A_CmsXmlContent.class, "handleMethodTagForSure", C_REGISTER_MAIN_RUN);
Element data = getProcessedData(tag, callingObject, userObj, stream);
registerTag("METHOD", A_CmsXmlContent.class, "handleMethodTag", C_REGISTER_MAIN_RUN);
return getTagValue(data);
}
/**
* Reads all text or CDATA values from the given XML element,
* e.g. <code><ELEMENT>foo blah <![CDATA[<H1>Hello</H1>]]></ELEMENT></code>.
*
* @param n Element that should be read out.
* @return Concatenated string of all text and CDATA nodes or <code>null</code>
* if no nodes were found.
*/
protected String getTagValue(Element n) {
StringBuffer result = new StringBuffer();
if (n != null) {
NodeList childNodes = n.getChildNodes();
Node child = null;
if (childNodes != null) {
int numchilds = childNodes.getLength();
for (int i = 0; i < numchilds; i++) {
child = childNodes.item(i);
String nodeValue = child.getNodeValue();
if (nodeValue != null) {
//if(child.getNodeType() == n.TEXT_NODE || child.getNodeType() == n.CDATA_SECTION_NODE) {
if (child.getNodeType() == Element.CDATA_SECTION_NODE) {
//result.append(child.getNodeValue());
result.append(nodeValue);
}
else {
if (child.getNodeType() == Element.TEXT_NODE) {
//String s = child.getNodeValue().trim();
nodeValue = nodeValue.trim();
//if(!"".equals(s)) {
if (!"".equals(nodeValue)) {
//result.append(child.getNodeValue());
result.append(nodeValue);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -