📄 a_cmsxmlcontent.java
字号:
}
datablock = (Element) ((Element) m_blocks.get(blockname));
if (datablock == null) {
if (I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
String logUri = "";
try {
logUri = " RequestUri is " + m_cms.getRequestContext().getFolderUri() + m_cms.getRequestContext().getFileUri() + ".";
}
catch (Exception e) {}
A_OpenCms.log(C_OPENCMS_CRITICAL, getClassName() + "Requested datablock \"" + blockname + "\" not found in " + m_filename + "!" + logUri);
}
return C_ERR_NODATABLOCK + blockname;
}
else {
return datablock.getChildNodes();
}
}
/**
* Checks if this Template owns a datablock with the given key.
* @param key Datablock key to be checked.
* @return true if a datablock is found, false otherwise.
*/
protected boolean hasData(String key) {
return m_blocks.containsKey(key.toLowerCase());
}
/**
* Initialize the XML content class.
* Load and parse the content of the given CmsFile object.
* @param cms CmsObject Object for accessing resources.
* @param file CmsFile object of the file to be loaded and parsed.
* @throws CmsException
*/
public void init(CmsObject cms, CmsFile file) throws CmsException {
String filename = file.getAbsolutePath();
String currentProject = cms.getRequestContext().currentProject().getName();
Document parsedContent = null;
m_cms = cms;
m_filename = filename;
parsedContent = loadCachedDocument(filename);
if (parsedContent == null) {
byte[] fileContent = file.getContents();
if (fileContent == null || fileContent.length <= 1) {
// The file content is empty. Possibly the file object is only
// a file header. Re-read the file object and try again
file = cms.readFile(filename);
fileContent = file.getContents();
}
if (fileContent == null || fileContent.length <= 1) {
// The file content is still emtpy.
// Start with an empty XML document.
try {
parsedContent = getXmlParser().createEmptyDocument(getXmlDocumentTagName());
}
catch (Exception e) {
throwException("Could not initialize now XML document " + filename + ". " + e, CmsException.C_XML_PARSING_ERROR);
}
}
else {
parsedContent = parse(fileContent);
}
m_filecache.put(currentProject + ":" + filename, parsedContent.cloneNode(true));
}
init(cms, parsedContent, filename);
}
/**
* Initialize the XML content class.
* Load and parse the content of the given CmsFile object.
* <P>
* If a previously cached parsed content exists, it will be re-used.
* <P>
* If no absolute file name ist given,
* template files will be searched a hierachical order using
* <code>lookupAbsoluteFilename</code>.
*
* @param cms CmsObject Object for accessing resources.
* @param file CmsFile object of the file to be loaded and parsed.
* @throws CmsException
*/
public void init(CmsObject cms, String filename) throws CmsException {
if (!filename.startsWith("/")) {
throw new CmsException("A relative path has entered the A_CmsXmlContent class. filename=" + filename + "");
}
String currentProject = cms.getRequestContext().currentProject().getName();
Document parsedContent = null;
m_cms = cms;
m_filename = filename;
parsedContent = loadCachedDocument(filename);
if (parsedContent == null) {
CmsFile file = cms.readFile(filename);
parsedContent = parse(file.getContents());
m_filecache.put(currentProject + ":" + filename, parsedContent.cloneNode(true));
}
else {
// File was found in cache.
// We have to read the file header to check access rights.
cms.readFileHeader(filename);
}
if (C_PRINTNODES) {
if (filename.indexOf(I_CmsWpConstants.C_VFS_DIR_LOCALES) != -1) {
System.err.println("\n" + filename);
this.printNode(parsedContent, 0, "");
}
}
init(cms, parsedContent, filename);
}
/** Flag to enable / disable printNode() method */
private static final boolean C_PRINTNODES = false;
/**
* Prints all nodes of a XML locale file in depth first order split by "."
* to STDOUT.<p>
*
* This method is useful for backward compatibility: you can copy
* the output of this method (which is written to $TOMCAT_HOME/logs/catalina.
* out) to build Java resource bundles. This method is for internal use
* only, should be deactivated on a production system!<p>
*
* Activate this method by setting the value of C_PRINTNODES to true;
*
* @param node the current node in the XML document that is examined
* @param depth the current depth in the XML tree
* @param path the current path of the XML nodes, eg. node1.node2.node3...
*/
private void printNode(Node node, int depth, String path) {
if (C_PRINTNODES) {
// Char array for the printNode() method
final String badChars = "\n";
// Char array for the printNode() method
final String[] goodChars = { "\\n" };
int nodeType = node.getNodeType();
if (nodeType == Node.ELEMENT_NODE) {
String nodeName = node.getNodeName();
if (!"".equals(nodeName)) {
if (depth > 2) {
path += ".";
}
if (depth > 1) {
path += nodeName;
}
}
}
else if (nodeType == Node.TEXT_NODE) {
String nodeValue = node.getNodeValue();
if (!"".equals(nodeValue)) {
int nodeValueLength = nodeValue.length();
String nodeValueNoBadChars = "";
for (int i = 0; i < nodeValueLength; i++) {
int index = 0;
if ((index = badChars.indexOf(nodeValue.charAt(i))) != -1) {
nodeValueNoBadChars += goodChars[index];
}
else {
nodeValueNoBadChars += nodeValue.charAt(i);
}
}
if (node.getPreviousSibling() == null) {
System.out.print(path + "=");
}
System.out.print(nodeValueNoBadChars);
if (node.getNextSibling() == null) {
System.out.print("\n");
}
}
}
else if (nodeType == Node.CDATA_SECTION_NODE) {
CDATASection cdata = (CDATASection) node;
String nodeValue = cdata.getData();
if (!"".equals(nodeValue)) {
int nodeValueLength = nodeValue.length();
String nodeValueNoBadChars = "";
for (int i = 0; i < nodeValueLength; i++) {
int index = 0;
if ((index = badChars.indexOf(nodeValue.charAt(i))) != -1) {
nodeValueNoBadChars += goodChars[index];
}
else {
nodeValueNoBadChars += nodeValue.charAt(i);
}
}
if (node.getPreviousSibling() == null) {
System.out.print(path + "=");
}
System.out.print(nodeValueNoBadChars);
if (node.getNextSibling() == null) {
System.out.print("\n");
}
}
}
NodeList nodeChildren = node.getChildNodes();
if (nodeChildren != null) {
for (int i = 0; i < nodeChildren.getLength(); i++) {
printNode(nodeChildren.item(i), depth + 1, path);
}
}
}
}
/**
* Initialize the class with the given parsed XML DOM document.
* @param cms CmsObject Object for accessing system resources.
* @param document DOM document object containing the parsed XML file.
* @param filename OpenCms filename of the XML file.
* @throws CmsException
*/
public void init(CmsObject cms, Document content, String filename) throws CmsException {
m_cms = cms;
m_content = content;
m_filename = filename;
// First check the document tag. Is this the right document type?
Element docRootElement = m_content.getDocumentElement();
String docRootElementName = docRootElement.getNodeName().toLowerCase();
if (!docRootElementName.equals(getXmlDocumentTagName().toLowerCase())) {
// Hey! This is a wrong XML document!
// We will throw an execption and the document away :-)
removeFromFileCache();
m_content = null;
String errorMessage = "XML document " + getAbsoluteFilename() + " is not of the expected type. This document is \"" + docRootElementName + "\", but it should be \"" + getXmlDocumentTagName() + "\" (" + getContentDescription() + ").";
throwException(errorMessage, CmsException.C_XML_WRONG_CONTENT_TYPE);
}
// OK. Document tag is fine. Now get the DATA tags and collect them
// in a Hashtable (still in DOM representation!)
try {
processNode(m_content, m_firstRunTags, A_CmsXmlContent.class.getDeclaredMethod("handleDataTag", C_PARAMTYPES_HANDLING_METHODS), null, null);
}
catch (CmsException e) {
if (I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
A_OpenCms.log(C_OPENCMS_INFO, "Error while scanning for DATA and INCLUDE tags in file " + getAbsoluteFilename() + ".");
}
throw e;
}
catch (NoSuchMethodException e2) {
String errorMessage = "XML tag process method \"handleDataTag\" could not be found";
throwException(errorMessage, CmsException.C_XML_NO_PROCESS_METHOD);
}
}
/**
* Internal method for creating a new datablock.
* <P>
* This method is called by setData() if a new, not existing
* datablock must be created.
* <P>
* <B>Functionality:</B> If a non-hierarchical datablock is given,
* it is inserted at the end of the DOM document.
* If a hierarchical datablock is given, all possible parent
* names are checked in a backward oriented order. If a
* datablock with a name that equals a part of the hierarchy is
* found, the new datablock will be created as a (sub)child
* of this datablock.
*
* @param tag Key for this datablock.
* @param data DOM element node for this datablock.
*/
private void insertNewDatablock(String tag, Element data) {
// First check, if this is an extended datablock
// in <NAME1 name="name2>... format, that has to be inserted
// as name1.name2
String nameAttr = data.getAttribute("name");
String workTag = null;
if ((!data.getNodeName().toLowerCase().equals("data")) && nameAttr != null && (!"".equals(nameAttr))) {
// this is an extended datablock
workTag = tag.substring(0, tag.lastIndexOf("."));
}
else {
workTag = tag;
}
// Import the node for later inserting
Element importedNode = (Element) parser.importNode(m_content, data);
// Check, if this is a simple datablock without hierarchy.
if (workTag.indexOf(".") == -1) {
// Fine. We can insert the new Datablock at the of the document
m_content.getDocumentElement().appendChild(importedNode);
m_blocks.put(tag, importedNode);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -