📄 a_cmsxmlcontent.java
字号:
* 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.
* @exception 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() == n.CDATA_SECTION_NODE) {
//result.append(child.getNodeValue());
result.append(nodeValue);
}
else {
if(child.getNodeType() == n.TEXT_NODE) {
//String s = child.getNodeValue().trim();
nodeValue = nodeValue.trim();
//if(!"".equals(s)) {
if(!"".equals(nodeValue)) {
//result.append(child.getNodeValue());
result.append(nodeValue);
}
}
}
}
}
}
}
return result.toString();
}
/**
* Looks up a user defined method requested by a "METHOD" tag.
* The method is searched in the Object callingObject.
* @param methodName Name of the user method
* @param callingObject Object that requested the processing of the XML document
* @return user method
* @exception NoSuchMethodException
*/
private Method getUserMethod(String methodName, Object callingObject) throws NoSuchMethodException {
if(methodName == null || "".equals(methodName)) {
// no valid user method name
throw (new NoSuchMethodException("method name is null or empty"));
}
return callingObject.getClass().getMethod(methodName, C_PARAMTYPES_USER_METHODS);
}
/**
* Gets the XML parsed content of this template file as a DOM document.
* <P>
* <em>WARNING: The returned value is the original DOM document, not a clone.
* Any changes will take effect to the behaviour of this class.
* Especially datablocks are concerned by this!</em>
*
* @return the content of this template file.
*/
protected Document getXmlDocument() {
return m_content;
}
/**
* This method should be implemented by every extending class.
* It returns the name of the XML document tag to scan for.
* @return name of the XML document tag.
*/
abstract public String getXmlDocumentTagName();
/**
* Gets the currently used XML Parser.
* @return currently used parser.
*/
public static I_CmsXmlParser getXmlParser() {
return parser;
}
/**
* Prints the XML parsed content to a String
* @return String with XML content
*/
public String getXmlText() {
StringWriter writer = new StringWriter();
getXmlText(writer);
return writer.toString();
}
/**
* Prints the XML parsed content of this template file
* to the given Writer.
*
* @param out Writer to print to.
*/
public void getXmlText(Writer out) {
parser.getXmlText(m_content, out);
}
/**
* Prints the XML parsed content of the given Node and
* its subnodes to the given Writer.
*
* @param out Writer to print to.
* @param n Node that should be printed.
*/
public void getXmlText(Writer out, Node n) {
Document tempDoc = (Document)m_content.cloneNode(false);
tempDoc.appendChild(parser.importNode(tempDoc, n));
parser.getXmlText(tempDoc, out);
}
/**
* Prints the XML parsed content of a given node and
* its subnodes to a String
* @param n Node that should be printed.
* @return String with XML content
*/
public String getXmlText(Node n) {
StringWriter writer = new StringWriter();
getXmlText(writer, n);
return writer.toString();
}
/**
* Handling of "DATA" tags and unknown tags.
* A reference to each data tag ist stored in an internal hashtable with
* the name of the datablock as key.
* Nested datablocks are stored with names like outername.innername
*
* @param n XML element containing the <code><DATA></code> tag.
* @param callingObject Reference to the object requesting the node processing.
* @param userObj Customizable user object that will be passed through to handling and user methods.
*/
private void handleDataTag(Element n, Object callingObject, Object userObj) {
String blockname;
String bestFit = null;
String parentname = null;
Node parent = n.getParentNode();
while(parent != null && parent.getNodeType() == Node.ELEMENT_NODE) {
// check if this datablock is part of a datablock
// hierarchy like 'language.de.btn_yes'
// look for the best fitting hierarchy name part, too
if(parent.getNodeName().equals("DATA")) {
blockname = ((Element)parent).getAttribute("name");
}
else {
blockname = parent.getNodeName();
String secondName = ((Element)parent).getAttribute("name");
if(!"".equals(secondName)) {
blockname = blockname + "." + secondName;
}
}
blockname = blockname.toLowerCase();
if(parentname == null) {
parentname = blockname;
}
else {
parentname = blockname + "." + parentname;
}
if(m_blocks.containsKey(parentname)) {
bestFit = parentname;
}
parent = parent.getParentNode();
}
// bestFit now contains the best fitting name part
// next, look for the tag name (the part behind the last ".")
if(n.getNodeName().equals("DATA")) {
blockname = n.getAttribute("name");
}
else {
blockname = n.getNodeName();
String secondName = n.getAttribute("name");
if(!"".equals(secondName)) {
blockname = blockname + "." + secondName;
}
}
blockname = blockname.toLowerCase();
// now we can build the complete datablock name
if(bestFit != null) {
blockname = bestFit + "." + blockname;
}
if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging() && C_DEBUG) {
A_OpenCms.log(C_OPENCMS_DEBUG, "Reading datablock " + blockname);
}
// finally we cat put the new datablock into the hashtable
m_blocks.put(blockname, n);
//return null;
}
/**
* Handling of "INCLUDE" tags.
* @param n XML element containing the <code><INCLUDE></code> tag.
* @param callingObject Reference to the object requesting the node processing.
* @param userObj Customizable user object that will be passed through to handling and user methods.
*/
private Object handleIncludeTag(Element n, Object callingObject, Object userObj) throws CmsException {
A_CmsXmlContent include = null;
String tagcontent = getTagValue(n);
include = readIncludeFile(tagcontent);
return include.getXmlDocument().getDocumentElement().getChildNodes();
}
/**
* Handling of "LINK" tags.
* @param n XML element containing the <code><LINK></code> tag.
* @param callingObject Reference to the object requesting the node processing.
* @param userObj Customizable user object that will be passed through to handling and user methods.
*/
private Object handleLinkTag(Element n, Object callingObject, Object userObj) throws CmsException {
// get the string and call the getLinkSubstitution method
Element dBlock = (Element)n.cloneNode(true);
processNode(dBlock, m_mainProcessTags, null, callingObject, userObj, null);
String link = getTagValue(dBlock);
return m_cms.getLinkSubstitution(link);
}
/**
* Handling of the "METHOD name=..." tags.
* Name attribute and value of the element are read and the user method
* 'name' is invoked with the element value as parameter.
*
* @param n XML element containing the <code><METHOD></code> tag.
* @param callingObject Reference to the object requesting the node processing.
* @param userObj Customizable user object that will be passed through to handling and user methods.
* @return Object returned by the user method
* @exception CmsException
*/
private Object handleMethodTag(Element n, Object callingObject, Object userObj) throws CmsException {
processNode(n, m_mainProcessTags, null, callingObject, userObj);
String tagcontent = getTagValue(n);
String method = n.getAttribute("name");
Object result = null;
try {
result = callUserMethod(method, tagcontent, callingObject, userObj, false);
}
catch(Throwable e1) {
if(e1 instanceof CmsException) {
throw (CmsException)e1;
}
else {
throwException("handleMethodTag() received an exception from callUserMethod() while calling \"" + method + "\" requested by class " + callingObject.getClass().getName() + ": " + e1);
}
}
return result;
}
/**
* Handling of the "METHOD name=..." tags.
* In contrast to the method handleMethodTag this method resolves
* every method even if it has it own CacheDirectives. It is used only for
* getProcessedDataValue.
* Name attribute and value of the element are read and the user method
* 'name' is invoked with the element value as parameter.
*
* @param n XML element containing the <code><METHOD></code> tag.
* @param callingObject Reference to the object requesting the node processing.
* @param userObj Customizable user object that will be passed through to handling and user methods.
* @return Object returned by the user method
* @exception CmsException
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -