⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmsnewresourcepage.java

📁 java 编写的程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
* File   : $Source: /usr/local/cvs/opencms/src/com/opencms/workplace/CmsNewResourcePage.java,v $
* Date   : $Date: 2001/10/12 07:46:09 $
* Version: $Revision: 1.46 $
*
* 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.file.*;
import com.opencms.core.*;
import com.opencms.util.*;
import com.opencms.template.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.*;

/**
 * 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.46 $ $Date: 2001/10/12 07:46: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";

    private static final String C_DEFAULTBODY = "<?xml version=\"1.0\"?>\n<XMLTEMPLATE>\n<TEMPLATE/>\n</XMLTEMPLATE>";

    /**
     * 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.
     * @exception 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);

        // TODO: check, if this is neede: String type=null;
        byte[] content = new byte[0];
        CmsFile contentFile = null;
        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 = (String)parameters.get(C_PARA_TITLE);
        String keywords = (String)parameters.get(C_PARA_KEYWORDS);
        String description = (String)parameters.get(C_PARA_DESCRIPTION);

        // 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", "");
        }

        // TODO: check, if this is neede: String flags=(String)parameters.get(C_PARA_FLAGS);
        String templatefile = (String)parameters.get(C_PARA_TEMPLATE);
        String navtitle = (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
                    content = createPagefile(C_CLASSNAME, templatefile, C_CONTENTBODYPATH
                            + currentFilelist.substring(1, currentFilelist.length()) + newFile);

                    // check if the nescessary folders for the content files are existing.

                    // if not, create the missing folders.
                    //checkFolders(cms, currentFilelist);

                    // create the page file
                    Hashtable prop = new Hashtable();
                    prop.put(C_PROPERTY_TITLE, title);
                    CmsResource file = null;
                    String resourceType = (String)session.getValue("resourctype_for_new_page");
                    session.removeValue("resourctype_for_new_page");
                    if(resourceType != null && "gemadipage".equals(resourceType)){
                        file = ((CmsResourceTypePage)cms.getResourceType(resourceType)).createResource(cms, currentFilelist, newFile, prop, "".getBytes(), templatefile) ;
                    }else{
                        //CmsResourceTypePage rtpage = new CmsResourceTypePage();
                        //file = rtpage.createResource(cms, currentFilelist, newFile, prop, "".getBytes(), templatefile);
                        file = ((CmsResourceTypePage)cms.getResourceType("page")).createResource(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;
                    if (layoutFilePath == null || layoutFilePath.equals("")) {
                        // layout not specified, use default body
                        bodyBytes = C_DEFAULTBODY.getBytes();
                    } else {
                        // do not catch exceptions, a specified layout should exist
                        CmsFile layoutFile = cms.readFile(layoutFilePath);
                        bodyBytes = layoutFile.getContents();
                    }
                    CmsFile bodyFile = cms.readFile(C_CONTENTBODYPATH + currentFilelist.substring(1,
                                currentFilelist.length()), newFile);
                    bodyFile.setContents(bodyBytes);
                    cms.writeFile(bodyFile);

                    // 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);
                }

                // TODO: ErrorHandling
                // 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);
    }

    /**
     * Gets all required navigation information from the files and subfolders of a folder.
     * A file list of all files and folder is created, for all those resources, the navigation
     * property is read. The list is sorted by their navigation position.
     * @param cms The CmsObject.
     * @return Hashtable including three arrays of strings containing the filenames,
     * nicenames and navigation positions.
     * @exception Throws CmsException if something goes wrong.
     */

    private Hashtable getNavData(CmsObject cms) throws CmsException {
        I_CmsSession session = cms.getRequestContext().getSession(true);
        CmsXmlLanguageFile lang = new CmsXmlLanguageFile(cms);
        String[] filenames;
        String[] nicenames;
        String[] positions;
        Hashtable storage = new Hashtable();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -