📄 cmsnewresourcepage.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src/com/opencms/workplace/CmsNewResourcePage.java,v $
* Date : $Date: 2004/01/07 10:57:09 $
* Version: $Revision: 1.64.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.core.A_OpenCms;
import com.opencms.core.CmsException;
import com.opencms.core.I_CmsConstants;
import com.opencms.core.I_CmsSession;
import com.opencms.file.CmsFile;
import com.opencms.file.CmsFolder;
import com.opencms.file.CmsObject;
import com.opencms.file.CmsResource;
import com.opencms.file.CmsResourceTypePage;
import com.opencms.linkmanagement.CmsPageLinks;
import com.opencms.template.A_CmsXmlContent;
import com.opencms.template.I_CmsXmlParser;
import com.opencms.util.Encoder;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
/**
* Template class for displaying the new resource screen for a new page
* of the OpenCms workplace.<P>
* Reads template files of the content type <code>CmsXmlWpTemplateFile</code>.
*
* @author Michael Emmerich
* @version $Revision: 1.64.2.1 $ $Date: 2004/01/07 10:57:09 $
*/
public class CmsNewResourcePage extends CmsWorkplaceDefault implements I_CmsWpConstants,I_CmsConstants {
/** Definition of the class */
private final static String C_CLASSNAME = "com.opencms.template.CmsXmlTemplate";
/**
* Create the pagefile for this new page.
* @classname The name of the class used by this page.
* @template The name of the template (content) used by this page.
* @return Bytearray containgin the XML code for the pagefile.
*/
private byte[] createPagefile(String classname, String template,
String contenttemplate) throws CmsException {
byte[] xmlContent = null;
try {
I_CmsXmlParser parser = A_CmsXmlContent.getXmlParser();
Document docXml = parser.createEmptyDocument("page");
Element firstElement = docXml.getDocumentElement();
// add element CLASS
Element elClass = docXml.createElement("CLASS");
firstElement.appendChild(elClass);
Node noClass = docXml.createTextNode(classname);
elClass.appendChild(noClass);
// add element MASTERTEMPLATE
Element elTempl = docXml.createElement("MASTERTEMPLATE");
firstElement.appendChild(elTempl);
Node noTempl = docXml.createTextNode(template);
elTempl.appendChild(noTempl);
//add element ELEMENTDEF
Element elEldef = docXml.createElement("ELEMENTDEF");
elEldef.setAttribute("name", "body");
firstElement.appendChild(elEldef);
//add element ELEMENTDEF.CLASS
Element elElClass = docXml.createElement("CLASS");
elEldef.appendChild(elElClass);
Node noElClass = docXml.createTextNode(classname);
elElClass.appendChild(noElClass);
//add element ELEMENTDEF.TEMPLATE
Element elElTempl = docXml.createElement("TEMPLATE");
elEldef.appendChild(elElTempl);
Node noElTempl = docXml.createTextNode(contenttemplate);
elElTempl.appendChild(noElTempl);
// generate the output
StringWriter writer = new StringWriter();
parser.getXmlText(docXml, writer);
xmlContent = writer.toString().getBytes();
}
catch(Exception e) {
throw new CmsException(e.getMessage(), CmsException.C_UNKNOWN_EXCEPTION, e);
}
return xmlContent;
}
/**
* Overwrites the getContent method of the CmsWorkplaceDefault.<br>
* Gets the content of the new resource page template and processed the data input.
* @param cms The CmsObject.
* @param templateFile The new page template file
* @param elementName not used
* @param parameters Parameters of the request and the template.
* @param templateSelector Selector of the template tag to be displayed.
* @return Bytearry containing the processed data of the template.
* @throws Throws CmsException if something goes wrong.
*/
public byte[] getContent(CmsObject cms, String templateFile, String elementName,
Hashtable parameters, String templateSelector) throws CmsException {
// the template to be displayed
String template = null;
// get the document to display
CmsXmlWpTemplateFile xmlTemplateDocument = new CmsXmlWpTemplateFile(cms, templateFile);
I_CmsSession session = cms.getRequestContext().getSession(true);
//get the current filelist
String currentFilelist = (String)session.getValue(C_PARA_FILELIST);
if(currentFilelist == null) {
currentFilelist = cms.rootFolder().getAbsolutePath();
}
// get request parameters
String newFile = (String)parameters.get(C_PARA_NEWFILE);
String title = Encoder.redecodeUriComponent((String)parameters.get(C_PARA_TITLE));
String keywords = Encoder.redecodeUriComponent((String)parameters.get(C_PARA_KEYWORDS));
String description = Encoder.redecodeUriComponent((String)parameters.get(C_PARA_DESCRIPTION));
if (newFile != null) {
newFile = newFile.trim();
}
// look if createFolder called us, then we have to preselect index.html as name
String fromFolder = (String)parameters.get("fromFolder");
if((fromFolder != null) && ("true".equals(fromFolder))){
xmlTemplateDocument.setData("name", "index.html");
// deaktivate the linklayer
xmlTemplateDocument.setData("doOnload", "checkInTheBox();");
}else{
xmlTemplateDocument.setData("name", "");
xmlTemplateDocument.setData("doOnload", "");
}
String templatefile = (String)parameters.get(C_PARA_TEMPLATE);
String navtitle = Encoder.redecodeUriComponent((String)parameters.get(C_PARA_NAVTEXT));
String navpos = (String)parameters.get(C_PARA_NAVPOS);
String layoutFilePath = (String)parameters.get(C_PARA_LAYOUT);
// get the current phase of this wizard
String step = cms.getRequestContext().getRequest().getParameter("step");
if(step != null) {
if(step.equals("1")) {
//check if the fielname has a file extension
if(newFile.indexOf(".") == -1) {
newFile += ".html";
}
try {
// create the content for the page file
createPagefile(C_CLASSNAME, templatefile, C_VFS_PATH_BODIES
+ currentFilelist.substring(1, currentFilelist.length()) + newFile);
// create the page file
Hashtable prop = new Hashtable();
prop.put(C_PROPERTY_TITLE, title);
CmsResource file = null;
if (! currentFilelist.endsWith(C_FOLDER_SEPARATOR)) currentFilelist += C_FOLDER_SEPARATOR;
file = ((CmsResourceTypePage)cms.getResourceType("page")).createResourceForTemplate(cms, currentFilelist + newFile, prop, "".getBytes(), templatefile) ;
if( keywords != null && !keywords.equals("") ) {
cms.writeProperty(file.getAbsolutePath(), C_PROPERTY_KEYWORDS, keywords);
}
if( description != null && !description.equals("") ) {
cms.writeProperty(file.getAbsolutePath(), C_PROPERTY_DESCRIPTION, description);
}
byte[] bodyBytes = null;
boolean layoutFileDefined = false;
if (layoutFilePath == null || layoutFilePath.equals("")) {
// layout not specified, use default body
bodyBytes = (CmsResourceTypePage.getDefaultBodyStart() + CmsResourceTypePage.getDefaultBodyEnd()).getBytes();
} else {
// do not catch exceptions, a specified layout should exist
bodyBytes = ensureBodyEncoding(cms, layoutFilePath);
layoutFileDefined = true;
}
CmsFile bodyFile = cms.readFile(C_VFS_PATH_BODIES + currentFilelist.substring(1,
currentFilelist.length()), newFile);
bodyFile.setContents(bodyBytes);
cms.writeFile(bodyFile);
// care about the linkmanagement if a default body was selected
if(layoutFileDefined){
CmsPageLinks linkObject = cms.getPageLinks(currentFilelist+newFile);
cms.createLinkEntrys(linkObject.getResourceId(), linkObject.getLinkTargets());
}
// now check if navigation informations have to be added to the new page.
if(navtitle != null) {
cms.writeProperty(file.getAbsolutePath(), C_PROPERTY_NAVTEXT, navtitle);
// update the navposition.
if(navpos != null) {
updateNavPos(cms, file, navpos);
}
}
}catch(CmsException ex) {
throw new CmsException("Error while creating new Page" + ex.getMessage(), ex.getType(), ex);
}
// now return to filelist
try {
cms.getRequestContext().getResponse().sendCmsRedirect(getConfigFile(cms).getWorkplaceActionPath()
+ C_WP_EXPLORER_FILELIST);
}catch(Exception e) {
throw new CmsException("Redirect fails :" + getConfigFile(cms).getWorkplaceActionPath()
+ C_WP_EXPLORER_FILELIST, CmsException.C_UNKNOWN_EXCEPTION, e);
}
return null;
}
}else {
String putValue = (String)parameters.get("root.pagetype");
if(putValue != null){
session.putValue("resourctype_for_new_page", (String)parameters.get("root.pagetype"));
}else{
session.removeValue("resourctype_for_new_page");
}
session.removeValue(C_PARA_FILE);
}
// process the selected template
return startProcessing(cms, xmlTemplateDocument, "", parameters, template);
}
/**
* Ensures that a "default body file" for a new page uses the systems default encoding.<p>
*
* The encoding of new page bodys should be in the encoding set for the system default.
* However, the default body file in the VFS will be static and so must have
* some setting hardcoded in the header. This method parses the XML and replaces the
* "content-encoding" setting in the header with the system default.<p>
*
* In case you do not want the encoding in the body file to be set to the
* system default, append the C_PROPERTY_CONTENT_ENCODING to it with some value. In this
* case the contents will be copied without modification.
*
* @param cms the current CmsObject
* @param bodyFilename the filename of the default body file
* @return byte[] the contents for the new body file that will be created for the page
* @throws CmsException in case something goes wrong
*/
private byte[] ensureBodyEncoding(CmsObject cms, String bodyFilename) throws CmsException {
CmsFile bodyFile = cms.readFile(bodyFilename);
byte[] result = bodyFile.getContents();
// check if C_PROPERTY_CONTENT_ENCODING is set on body file
// if so, this means that the files encoding is fixed and must not be ensured
String bodyEncoding = cms.readProperty(bodyFilename, C_PROPERTY_CONTENT_ENCODING);
if (bodyEncoding == null) {
// encoding not set, ensure that encoding is default encoding
Document doc = null;
// create a XML Document from the contents of the body file
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -