📄 cmsxmltemplateeditor.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src/com/opencms/workplace/CmsXmlTemplateEditor.java,v $
* Date : $Date: 2004/01/07 10:57:09 $
* Version: $Revision: 1.92.2.1 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (C) 2001 The OpenCms Group
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* For further information about OpenCms, please see the
* OpenCms Website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.opencms.workplace;
import com.opencms.boot.I_CmsLogChannels;
import com.opencms.core.A_OpenCms;
import com.opencms.core.CmsException;
import com.opencms.core.I_CmsConstants;
import com.opencms.core.I_CmsSession;
import com.opencms.core.OpenCms;
import com.opencms.file.CmsFile;
import com.opencms.file.CmsObject;
import com.opencms.file.CmsRequestContext;
import com.opencms.file.CmsResource;
import com.opencms.flex.util.CmsStringSubstitution;
import com.opencms.linkmanagement.CmsPageLinks;
import com.opencms.template.A_CmsXmlContent;
import com.opencms.template.CmsTemplateClassManager;
import com.opencms.template.CmsXmlControlFile;
import com.opencms.template.CmsXmlTemplate;
import com.opencms.template.CmsXmlTemplateFile;
import com.opencms.util.Encoder;
import com.opencms.util.Utils;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import org.w3c.dom.Element;
/**
* Template class for displaying the XML template editor of the OpenCms workplace.<P>
* Reads template files of the content type <code>CmsXmlWpTemplateFile</code>.
*
* @author Alexander Lucas
* @version $Revision: 1.92.2.1 $ $Date: 2004/01/07 10:57:09 $
* @see com.opencms.workplace.CmsXmlWpTemplateFile
*/
public class CmsXmlTemplateEditor extends CmsWorkplaceDefault implements I_CmsConstants, I_CmsWpConstants {
protected void commitTemporaryFile(CmsObject cms, String originalFilename, String temporaryFilename,
int tempProject, int curProject) throws CmsException {
// set current project to tempfileproject
cms.getRequestContext().setCurrentProject(tempProject);
CmsFile tempFile = cms.readFile(temporaryFilename);
Map minfos = cms.readProperties(temporaryFilename);
// set current project
cms.getRequestContext().setCurrentProject(curProject);
CmsFile orgFile = cms.readFile(originalFilename);
orgFile.setContents(tempFile.getContents());
cms.writeFile(orgFile);
Iterator keys = minfos.keySet().iterator();
while(keys.hasNext()) {
String keyName = (String)keys.next();
cms.writeProperty(originalFilename, keyName, (String)minfos.get(keyName));
}
// don't forget to clear the cache.
A_CmsXmlContent.clearFileCache(cms.getRequestContext().currentProject().getName()+":"
+ originalFilename);
}
protected String createTemporaryFile(CmsObject cms, CmsResource file, int tempProject, int curProject) throws CmsException {
String temporaryFilename = file.getPath() + C_TEMP_PREFIX + file.getName();
boolean ok = true;
cms.getRequestContext().setCurrentProject(tempProject);
try {
cms.copyResource(file.getAbsolutePath(), temporaryFilename);
cms.chmod(temporaryFilename, 91);
}
catch(CmsException e) {
if((e.getType() == CmsException.C_FILE_EXISTS) || (e.getType() != CmsException.C_SQL_ERROR)) {
try {
// try to re-use the old temporary file
cms.changeLockedInProject(tempProject, temporaryFilename);
cms.lockResource(temporaryFilename, true);
cms.chmod(temporaryFilename, 91);
}
catch (Exception ex) {
ok = false;
}
} else {
throw e;
}
}
String extendedTempFile = temporaryFilename;
int loop = 0;
while(!ok) {
ok = true;
extendedTempFile = temporaryFilename + loop;
try {
cms.copyResource(file.getAbsolutePath(), extendedTempFile);
cms.chmod(extendedTempFile, 91);
}
catch(CmsException e) {
if((e.getType() != CmsException.C_FILE_EXISTS) && (e.getType() != CmsException.C_SQL_ERROR)) {
cms.getRequestContext().setCurrentProject(curProject);
// This was no file exists exception.
// Vary bad. We should not go on here since we may run
// in an endless loop.
throw e;
}
// temp file could not be created
loop++;
ok = false;
}
}
cms.getRequestContext().setCurrentProject(curProject);
// Oh. we have found a temporary file.
temporaryFilename = extendedTempFile;
return temporaryFilename;
}
/**
* Gets the templates displayed in the template select box.
* @param cms The CmsObject.
* @param lang The langauge definitions.
* @param names The names of the new rescources.
* @param values The links that are connected with each resource.
* @param parameters Hashtable of parameters (not used yet).
* @return The vectors names and values are filled with the information found in the
* workplace.ini.
* @throws Throws CmsException if something goes wrong.
*/
public Integer getAvailableTemplates(CmsObject cms, CmsXmlLanguageFile lang, Vector names,
Vector values, Hashtable parameters) throws CmsException {
Integer retValue = CmsHelperMastertemplates.getTemplates(cms, names, values, (String)parameters.get("template"), -1);
if(retValue.intValue() != -1) {
return retValue;
} else {
// no template found -> use the given one
// first clean the vectors
names.removeAllElements();
values.removeAllElements();
// now add the current template
String name = (String)parameters.get("template");
try { // to read the title of this template
name = cms.readProperty(name, C_PROPERTY_TITLE);
} catch(CmsException exc) {
// ignore this exception - the title for this template was not readable
}
names.add(name);
values.add((String)parameters.get("template"));
return new Integer(0);
}
}
/**
* Gets all views available in the workplace screen.
* <P>
* The given vectors <code>names</code> and <code>values</code> will
* be filled with the appropriate information to be used for building
* a select box.
* <P>
* <code>names</code> will contain language specific view descriptions
* and <code>values</code> will contain the correspondig URL for each
* of these views after returning from this method.
* <P>
*
* @param cms CmsObject Object for accessing system resources.
* @param lang reference to the currently valid language file
* @param names Vector to be filled with the appropriate values in this method.
* @param values Vector to be filled with the appropriate values in this method.
* @param parameters Hashtable containing all user parameters <em>(not used here)</em>.
* @return Index representing the user's current workplace view in the vectors.
* @throws CmsException
*/
public Integer getBodys(CmsObject cms, CmsXmlLanguageFile lang, Vector names, Vector values,
Hashtable parameters) throws CmsException {
I_CmsSession session = cms.getRequestContext().getSession(true);
String currentBodySection = (String)parameters.get("body");
String bodyClassName = (String)parameters.get("bodyclass");
String tempBodyFilename = (String)session.getValue("te_tempbodyfile");
Object tempObj = CmsTemplateClassManager.getClassInstance(cms, bodyClassName);
CmsXmlTemplate bodyElementClassObject = (CmsXmlTemplate)tempObj;
CmsXmlTemplateFile bodyTemplateFile = bodyElementClassObject.getOwnTemplateFile(cms,
tempBodyFilename, C_BODY_ELEMENT, parameters, null);
Vector allBodys = bodyTemplateFile.getAllSections();
int loop = 0;
int currentBodySectionIndex = 0;
int numBodys = allBodys.size();
for(int i = 0;i < numBodys;i++) {
String bodyname = (String)allBodys.elementAt(i);
String encodedBodyname = Encoder.escapeXml(bodyname);
if(bodyname.equals(currentBodySection)) {
currentBodySectionIndex = loop;
}
values.addElement(encodedBodyname);
names.addElement(encodedBodyname);
loop++;
}
return new Integer(currentBodySectionIndex);
}
/**
* Gets the content of a defined section in a given template file and its subtemplates
* with the given parameters.
*
* @see #getContent(CmsObject, String, String, Hashtable, String)
* @param cms CmsObject Object for accessing system resources.
* @param templateFile Filename of the template file.
* @param elementName Element name of this template in our parent template.
* @param parameters Hashtable with all template class parameters.
* @param templateSelector template section that should be processed.
*/
public byte[] getContent(CmsObject cms, String templateFile, String elementName,
Hashtable parameters, String templateSelector) throws CmsException {
CmsRequestContext reqCont = cms.getRequestContext();
HttpServletRequest orgReq = (HttpServletRequest)reqCont.getRequest().getOriginalRequest();
I_CmsSession session = cms.getRequestContext().getSession(true);
// get the temporary file projectid
int tempProject = 0;
try{
tempProject = Integer.parseInt((String)cms.getRegistry().getSystemValue("tempfileproject"));
}catch(Exception e){
throw new CmsException("Can not read projectId of tempfileproject for creating temporary file for editing! "+e.toString());
}
if(tempProject == 0){
throw new CmsException("Can not create temporary file for editing!");
}
int curProject = cms.getRequestContext().currentProject().getId();
// Get the user's browser
String browser = orgReq.getHeader("user-agent");;
String hostName = orgReq.getScheme() + "://" + orgReq.getServerName() + ":" + orgReq.getServerPort();
// Get all URL parameters
String content = (String)parameters.get(C_PARA_CONTENT);
if(content == null){
// try to get the value from the session because we might come from errorpage
content = (String)session.getValue(C_PARA_CONTENT);
session.removeValue(C_PARA_CONTENT);
}
String body = (String)parameters.get("body");
if(body == null){
// try to get the value from the session because we might come from errorpage
body = (String)session.getValue("body");
session.removeValue("body");
}
String file = (String)parameters.get(C_PARA_FILE);
if(file == null){
// try to get the value from the session because we might come from errorpage
file = (String)session.getValue(C_PARA_FILE);
session.removeValue(C_PARA_FILE);
}
if (file != null) {
file = file.trim();
}
String editor = (String)parameters.get("editor");
if((editor == null) || "".equals(editor)){
// try to get the value from the session because we might come from errorpage
editor = (String)session.getValue("editor");
if(editor != null){
parameters.put("editor", editor);
}
session.removeValue("editor");
}
String title = (String)parameters.get(C_PARA_TITLE);
if(title == null){
// try to get the value from the session because we might come from errorpage
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -