📄 cmsxmltemplateloader.java
字号:
}
if ((cmsUri == null) || !elementCacheEnabled) {
// Entry point to page file analysis.
// For performance reasons this should only be done if the element
// cache is not activated or if it's activated but no URI object could be found.
// Parse the page file
try {
if (xmlTemplateContent == null) {
doc = new CmsXmlControlFile(cms, file);
} else {
doc = new CmsXmlControlFile(cms, uri, xmlTemplateContent);
}
} catch (Exception e) {
// there was an error while parsing the document.
// No chance to go on here.
handleException(cms, e, "There was an error while parsing XML page file " + cms.getSitePath(file));
return "".getBytes();
}
if (! elementCacheEnabled && (replaceDef != null)) {
// Required to enable element replacement if element cache is disabled
doc.setElementClass(replaceDef.getName(), replaceDef.getClassName());
doc.setElementTemplate(replaceDef.getName(), replaceDef.getTemplateName());
}
// Get the names of the master template and the template class from
// the parsed page file. Fall back to default value, if template class
// is not defined
templateClass = doc.getTemplateClass();
if (templateClass == null || "".equals(templateClass)) {
templateClass = this.getClass().getName();
}
if (templateClass == null || "".equals(templateClass)) {
templateClass = org.opencms.importexport.CmsCompatibleCheck.XML_CONTROL_DEFAULT_CLASS;
}
templateName = doc.getMasterTemplate();
if (templateName != null && !"".equals(templateName)) {
templateName = CmsLinkManager.getAbsoluteUri(templateName, cms.getSitePath(file));
}
// Previously, the template class was loaded here.
// We avoid doing this so early, since in element cache mode the template
// class is not needed here.
// Now look for parameters in the page file...
// ... first the params of the master template...
Enumeration masterTemplateParams = doc.getParameterNames();
while (masterTemplateParams.hasMoreElements()) {
String paramName = (String)masterTemplateParams.nextElement();
String paramValue = doc.getParameter(paramName);
newParameters.put(com.opencms.core.I_CmsConstants.C_ROOT_TEMPLATE_NAME + "." + paramName, paramValue);
}
// ... and now the params of all subtemplates
Enumeration elementDefinitions = doc.getElementDefinitions();
while (elementDefinitions.hasMoreElements()) {
String elementName = (String)elementDefinitions.nextElement();
if (doc.isElementClassDefined(elementName)) {
newParameters.put(elementName + "._CLASS_", doc.getElementClass(elementName));
}
if (doc.isElementTemplateDefined(elementName)) {
// need to check for the body template here so that non-XMLTemplate templates
// like JSPs know where to find the body defined in the XMLTemplate
String template = doc.getElementTemplate(elementName);
if (xmlTemplateContent == null) {
template = doc.validateBodyPath(cms, template, file);
}
if (CmsDefaultPageEditor.XML_BODY_ELEMENT.equalsIgnoreCase(elementName)) {
// found body element
if (template != null) {
cms.getRequestContext().setAttribute(CmsDefaultPageEditor.XML_BODY_ELEMENT, template);
}
}
newParameters.put(elementName + "._TEMPLATE_", template);
}
if (doc.isElementTemplSelectorDefined(elementName)) {
newParameters.put(elementName + "._TEMPLATESELECTOR_", doc.getElementTemplSelector(elementName));
}
Enumeration parameters = doc.getElementParameterNames(elementName);
while (parameters.hasMoreElements()) {
String paramName = (String)parameters.nextElement();
String paramValue = doc.getElementParameter(elementName, paramName);
if (paramValue != null) {
newParameters.put(elementName + "." + paramName, paramValue);
} else {
if (CmsLog.getLog(this).isInfoEnabled()) {
CmsLog.getLog(this).info("Empty parameter \"" + paramName + "\" found.");
}
}
}
}
}
// URL parameters ary really dynamic.
// We cannot store them in an element cache.
// Therefore these parameters must be collected in ANY case!
String datafor = req.getParameter("datafor");
if (datafor == null) {
datafor = "";
} else {
if (!"".equals(datafor)) {
datafor = datafor + ".";
}
}
Enumeration urlParameterNames = req.getParameterNames();
while (urlParameterNames.hasMoreElements()) {
String pname = (String)urlParameterNames.nextElement();
String paramValue = req.getParameter(pname);
if (paramValue != null) {
if ((!"datafor".equals(pname)) && (!"_clearcache".equals(pname))) {
newParameters.put(datafor + pname, paramValue);
}
} else {
if (CmsLog.getLog(this).isInfoEnabled()) {
CmsLog.getLog(this).info("Empty URL parameter \"" + pname + "\" found.");
}
}
}
if (elementCacheEnabled && (cmsUri == null)) {
// ---- element cache stuff --------
// No URI could be found in cache.
// So create a new URI object with a start element and store it using the UriLocator
CmsElementDescriptor elemDesc = new CmsElementDescriptor(templateClass, templateName);
CmsElementDefinitionCollection eldefs = doc.getElementDefinitionCollection();
if (elementreplace) {
// we cant cach this
eldefs.add(replaceDef);
cmsUri = new CmsUri(elemDesc, eldefs);
} else {
cmsUri = new CmsUri(elemDesc, eldefs);
elementCache.getUriLocator().put(uriDesc, cmsUri);
}
}
if (elementCacheEnabled) {
// TODO: Make cache more efficient
clearLoaderCache(true, true);
// now lets get the output
if (elementreplace) {
output = cmsUri.callCanonicalRoot(elementCache, cms, newParameters);
} else {
output = elementCache.callCanonicalRoot(cms, newParameters, uri);
}
} else {
// ----- traditional stuff ------
// Element cache is deactivated. So let's go on as usual.
try {
CmsFile masterTemplate = loadMasterTemplateFile(cms, templateName, doc);
I_CmsTemplate tmpl = getTemplateClass(templateClass);
if (!(tmpl instanceof I_CmsXmlTemplate)) {
String errorMessage = "Error in " + cms.getSitePath(file) + ": " + templateClass + " is not a XML template class.";
if (CmsLog.getLog(this).isErrorEnabled()) {
CmsLog.getLog(this).error(errorMessage);
}
throw new CmsLegacyException(errorMessage, CmsLegacyException.C_XML_WRONG_TEMPLATE_CLASS);
}
// TODO: Make cache more efficient
clearLoaderCache(true, true);
output = callCanonicalRoot(cms, tmpl, masterTemplate, newParameters);
} catch (CmsException e) {
if (CmsLog.getLog(this).isWarnEnabled()) {
CmsLog.getLog(this);
}
doc.removeFromFileCache();
throw e;
}
}
return output;
}
/**
* @see org.opencms.loader.I_CmsResourceLoader#getLoaderId()
*/
public int getLoaderId() {
return C_RESOURCE_LOADER_ID;
}
/**
* Return a String describing the ResourceLoader,
* which is <code>"The OpenCms default resource loader for XMLTemplates"</code>.<p>
*
* @return a describing String for the ResourceLoader
*/
public String getResourceLoaderInfo() {
return "The OpenCms default resource loader for XMLTemplates";
}
/**
* Calls the CmsClassManager to get an instance of the given template class.<p>
*
* The returned object is checked to be an implementing class of the interface
* I_CmsTemplate.
* If the template cache of the template class is not yet set up,
* this will be done, too.<p>
*
* @param classname name of the requested template class
* @return instance of the template class
* @throws CmsException if something goes wrong
*/
private I_CmsTemplate getTemplateClass(String classname) throws CmsException {
if (CmsLog.getLog(this).isDebugEnabled()) {
CmsLog.getLog(this).debug("Getting start template class: " + classname);
}
Object o = CmsTemplateClassManager.getClassInstance(classname);
// Check, if the loaded class really is a OpenCms template class.
// This is done be checking the implemented interface.
if (!(o instanceof I_CmsTemplate)) {
String errorMessage = "Class " + classname + " is not an OpenCms template class.";
if (CmsLog.getLog(this).isErrorEnabled()) {
CmsLog.getLog(this).error(errorMessage);
}
throw new CmsLegacyException(errorMessage, CmsLegacyException.C_XML_NO_TEMPLATE_CLASS);
}
I_CmsTemplate cmsTemplate = (I_CmsTemplate)o;
if (!cmsTemplate.isTemplateCacheSet()) {
cmsTemplate.setTemplateCache(m_templateCache);
}
return cmsTemplate;
}
/**
* Utility method to handle any occurence of an execption.<p>
*
* If the Exception is NO CmsException (i.e. it was not detected previously)
* it will be written to the logfile.<p>
*
* If the current user is the anonymous user, no further exception will
* be thrown, but a server error will be sent
* (we want to prevent the user from seeing any exeptions).
* Otherwise a new Exception will be thrown.
* This will trigger the OpenCms error message box.<p>
*
* @param cms the cms context object
* @param e Exception that should be handled
* @param errorText error message that should be shown
* @throws CmsException if
*/
private void handleException(CmsObject cms, Exception e, String errorText) throws CmsException {
// log the error if it is no CmsException
if (! (e instanceof CmsException)) {
if (CmsLog.getLog(this).isErrorEnabled()) {
CmsLog.getLog(this).error(errorText, e);
}
}
// if the user is a guest (and it's not a login exception) we send an servlet error,
// otherwise we try to throw an exception.
CmsRequestContext reqContext = cms.getRequestContext();
if ((DEBUG == 0) && reqContext.currentUser().isGuestUser()
&& (!(e instanceof CmsLegacyException && ((e instanceof CmsLegacyException) && (((CmsLegacyException)e).getType() == CmsLegacyException.C_NO_USER))))) {
throw new CmsLegacyException(errorText, CmsLegacyException.C_SERVICE_UNAVAILABLE, e);
} else {
if (e instanceof CmsException) {
throw (CmsException)e;
} else {
throw new CmsLegacyException(errorText, CmsLegacyException.C_LOADER_GENERIC_ERROR, e);
}
}
}
/**
* @see org.opencms.configuration.I_CmsConfigurationParameterHandler#initConfiguration()
*/
public void initConfiguration() {
ExtendedProperties config = new ExtendedProperties();
config.putAll(m_configuration);
// check if the element cache is enabled
boolean elementCacheEnabled = config.getBoolean("elementcache.enabled", false);
if (CmsLog.INIT.isInfoEnabled()) {
CmsLog.INIT.info(". Loader init : XMLTemplate element cache " + (elementCacheEnabled ? "enabled" : "disabled"));
}
if (elementCacheEnabled) {
try {
m_elementCache = new CmsElementCache(
config.getInteger("elementcache.uri", 10000),
config.getInteger("elementcache.elements", 50000),
config.getInteger("elementcache.variants", 100));
} catch (Exception e) {
if (CmsLog.INIT.isWarnEnabled()) {
CmsLog.INIT.warn(". Loader init : XMLTemplate element cache non-critical error " + e.toString());
}
}
m_variantDeps = new Hashtable();
m_elementCache.getElementLocator().setExternDependencies(m_variantDeps);
} else {
m_elementCache = null;
}
if (CmsLog.INIT.isInfoEnabled()) {
CmsLog.INIT.info(". Loader init : " + this.getClass().getName() + " initialized");
}
}
/**
* @see org.opencms.loader.I_CmsResourceLoader#load(org.opencms.file.CmsObject, org.opencms.file.CmsResource, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public void load(CmsObject cms, CmsResource resource, HttpServletRequest req, HttpServletResponse res)
throws CmsException {
initLegacyRequest(cms, req, res);
processXmlTemplate(cms, CmsFile.upgrade(resource, cms));
}
/**
* Initializes the current request with the legacy Cms request and response wrappers.<p>
*
* @param cms the current cms context
* @param req the request to wrap
* @param res the response to wrap
* @throws CmsException if something goes wrong
*/
public static void initLegacyRequest(CmsObject cms, HttpServletRequest req, HttpServletResponse res) throws CmsException {
if (cms.getRequestContext().getAttribute(I_CmsRequest.C_CMS_REQUEST) != null) {
return;
}
try {
CmsRequestHttpServlet cmsReq = new CmsRequestHttpServlet(req, cms.getRequestContext().getFileTranslator());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -