<commit_before>/*
* File   : $Source: /alkacon/cvs/opencms/src/com/opencms/workplace/Attic/CmsNewResourcePage.java,v $
* Date   : $Date: 2002/11/07 19:33:56 $
* Version: $Revision: 1.52 $
*
* 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.linkmanagement.*;
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 <NAME>
 * @version $Revision: 1.52 $ $Date: 2002/11/07 19:33:56 $
 */

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\" encoding=\"" + I_CmsXmlParser.C_XML_ENCODING + "\"?>\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_VFS_PATH_BODIES
                            + 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 (! currentFilelist.endsWith(C_FOLDER_SEPERATOR)) currentFilelist += C_FOLDER_SEPERATOR;
                    if(resourceType != null && "gemadipage".equals(resourceType)){
                        file = ((CmsResourceTypePage)cms.getResourceType(resourceType)).createResourceForTemplate(cms, currentFilelist + newFile, prop, "".getBytes(), templatefile) ;
                    }else{
                        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;
                    boolean layoutFileDefined = false;
                    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();
                        layoutFileDefined = true;
                    }
                    CmsFile bodyFile = cms.readFile(C_VFS_PATH_BODIES + currentFilelist.substring(1,
                                currentFilelist.length()), newFile);
                    bodyFile.setContents(bodyBytes);
                    cms.writeFile(bodyFile);<commit_msg>Bugfix: Template selection did not work<commit_after>/*
* File   : $Source: /alkacon/cvs/opencms/src/com/opencms/workplace/Attic/CmsNewResourcePage.java,v $
* Date   : $Date: 2002/11/13 14:59:43 $
* Version: $Revision: 1.53 $
*
* 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.linkmanagement.*;
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 <NAME>
 * @version $Revision: 1.53 $ $Date: 2002/11/13 14:59:43 $
 */

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\" encoding=\"" + I_CmsXmlParser.C_XML_ENCODING + "\"?>\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_VFS_PATH_BODIES
                            + 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 (! currentFilelist.endsWith(C_FOLDER_SEPERATOR)) currentFilelist += C_FOLDER_SEPERATOR;
                    if(resourceType != null && "gemadipage".equals(resourceType)){
                        file = ((CmsResourceTypePage)cms.getResourceType(resourceType)).createResourceForTemplate(cms, currentFilelist + newFile, prop, "".getBytes(), templatefile) ;
                    }else{
                        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 = C_DEFAULTBODY.getBytes();
                    } else {
                        // do not catch exceptions, a specified layout should exist
                        CmsFile layoutFile = cms.readFile(layoutFilePath);
                        bodyBytes = layoutFile.getContents();
                        layoutFileDefined = true;
                    }
                    CmsFile bodyFile = cms.readFile(C_VFS_PATH_BODIES + currentFilelist.substring(1,
                                currentFilelist.length()), newFile);
                    bodyFile.setContents(bodyBytes);
                    cms.writeFile(bodyFile);
{'fraction_non_alphanumeric': 0.09462890138881976, 'fraction_numerical': 0.006172532231569516, 'mean_word_length': 2.3584085590103645, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 2, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 20}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '972542', 'n_tokens_mistral': 5429, 'n_tokens_neox': 5199, 'n_words': 1446}