cmslayoutpagebean.java

来自「找了很久才找到到源代码」· Java 代码 · 共 1,164 行 · 第 1/3 页

JAVA
1,164
字号
/*
 * File   : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/frontend/layoutpage/CmsLayoutPageBean.java,v $
 * Date   : $Date: 2007-08-13 16:30:08 $
 * Version: $Revision: 1.4 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Management System
 *
 * Copyright (c) 2002 - 2007 Alkacon Software GmbH (http://www.alkacon.com)
 *
 * 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 Alkacon Software GmbH, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project 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 org.opencms.frontend.layoutpage;

import org.opencms.file.CmsFile;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsPropertyDefinition;
import org.opencms.i18n.CmsLocaleManager;
import org.opencms.i18n.CmsMessages;
import org.opencms.jsp.CmsJspActionElement;
import org.opencms.loader.CmsImageScaler;
import org.opencms.main.CmsException;
import org.opencms.main.CmsLog;
import org.opencms.main.OpenCms;
import org.opencms.util.CmsStringUtil;
import org.opencms.workplace.CmsWorkplace;
import org.opencms.xml.content.CmsXmlContent;
import org.opencms.xml.content.CmsXmlContentFactory;
import org.opencms.xml.types.I_CmsXmlContentValue;

import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Vector;

import org.apache.commons.collections.ExtendedProperties;
import org.apache.commons.logging.Log;

/**
 * Provides special methods to build the predefined layout page output HTML.<p>
 * 
 * Please read the package information for details about modifying layouts or adding new layouts.<p>
 * 
 * @author Andreas Zahner
 * 
 * @version $Revision: 1.4 $ 
 * 
 * @since 6.1.9 
 */
public class CmsLayoutPageBean {

    /** Columns layout: one column. */
    public static final String COLUMNS_LAYOUT_1 = "1col";

    /** Columns layout: two columns. */
    public static final String COLUMNS_LAYOUT_2 = "2col";

    /** Image Link variant: provide link to larger version. */
    public static final String IMG_LINK_LARGER = "linklarger";

    /** Image Link variant: no link to larger version. */
    public static final String IMG_LINK_NONE = "nolink";

    /** Image width variant name for large images. */
    public static final String IMG_WIDTH_LARGE = "large";

    /** Image width variant name for medium images. */
    public static final String IMG_WIDTH_MEDIUM = "medium";

    /** Image width variant name for small images. */
    public static final String IMG_WIDTH_SMALL = "small";

    /** Macro name for the image description macro. */
    public static final String MACRO_DESCRIPTION = "description";

    /** Macro name for the headline macro. */
    public static final String MACRO_HEADLINE = "headline";

    /** Macro name for the image macro. */
    public static final String MACRO_IMAGE = "image";

    /** Macro name for the image width macro. */
    public static final String MACRO_IMAGE_WIDTH = "imagewidth";

    /** Macro name for the target macro. */
    public static final String MACRO_TARGET = "target";

    /** Macro name for the text macro. */
    public static final String MACRO_TEXT = "text";

    /** Macro name for the title macro. */
    public static final String MACRO_TITLE = "title";

    /** The name of the module. */
    public static final String MODULE_NAME = "org.opencms.frontend.layoutpage";

    /** Paragraph type: paragraph with image on bottom, text above. */
    public static final String PARAGRAPH_TYPE_BOTTOM = "imagebottom";

    /** Paragraph type: paragraph with image on bottom and description, text above. */
    public static final String PARAGRAPH_TYPE_BOTTOM_DESCRIPTION = "imagebottom_desc";

    /** Paragraph type: paragraph with image to the left. */
    public static final String PARAGRAPH_TYPE_IMAGE_LEFT = "imageleft";

    /** Paragraph type: paragraph with image to the left and image description. */
    public static final String PARAGRAPH_TYPE_IMAGE_LEFT_DESCRIPTION = "imageleft_desc";

    /** Paragraph type: paragraph with image left, text to the right. */
    public static final String PARAGRAPH_TYPE_IMAGE_LEFT_TEXT_RIGHT = "imageleft_textright";

    /** Paragraph type: paragraph with image left, text to the right and image description. */
    public static final String PARAGRAPH_TYPE_IMAGE_LEFT_TEXT_RIGHT_DESCRIPTION = "imageleft_textright_desc";

    /** Paragraph type: paragraph with image to the right. */
    public static final String PARAGRAPH_TYPE_IMAGE_RIGHT = "imageright";

    /** Paragraph type: paragraph with image to the right and image description. */
    public static final String PARAGRAPH_TYPE_IMAGE_RIGHT_DESCRIPTION = "imageright_desc";

    /** Paragraph type: paragraph with image right, text to the left. */
    public static final String PARAGRAPH_TYPE_IMAGE_RIGHT_TEXT_LEFT = "imageright_textleft";

    /** Paragraph type: paragraph with image right, text to the left and image description. */
    public static final String PARAGRAPH_TYPE_IMAGE_RIGHT_TEXT_LEFT_DESCRIPTION = "imageright_textleft_desc";

    /** Paragraph type: paragraph without image. */
    public static final String PARAGRAPH_TYPE_TEXT_ONLY = "textonly";

    /** Paragraph type: paragraph with image on top, text below. */
    public static final String PARAGRAPH_TYPE_TOP = "imagetop";

    /** Paragraph type: paragraph with image on top and description, text below. */
    public static final String PARAGRAPH_TYPE_TOP_DESCRIPTION = "imagetop_desc";

    /** Default VFS path to the html snippet files to include to render the layout paragraphs. */
    public static final String VFS_PATH_LAYOUTELEMENTS = CmsWorkplace.VFS_PATH_MODULES + MODULE_NAME + "/layouts/";

    /** Name of the align node. */
    protected static final String NODE_ALIGN = "Align";

    /** Name of the columns layout node. */
    protected static final String NODE_COLUMNS_LAYOUT = "ColumnsLayout";

    /** Name of the description node. */
    protected static final String NODE_DESCRIPTION = "Description";

    /** Name of the headline node. */
    protected static final String NODE_HEADLINE = "Headline";

    /** Name of the Image node. */
    protected static final String NODE_IMAGE = "Image";

    /** Name of the ImageOptions node. */
    protected static final String NODE_IMAGEOPTIONS = "ImageOptions";

    /** Name of the paragraph node. */
    protected static final String NODE_PARAGRAPH = "Paragraph";

    /** Name of the text node. */
    protected static final String NODE_TEXT = "Text";

    /** Name of the file link node. */
    protected static final String NODE_FILELINK = "Filelink";

    /** Name of the file link node. */
    protected static final String KEY_HEADLINE = "headline";

    /** Name of the file link node. */
    protected static final String KEY_TEXTVALUE = "textValue";

    /** Name of the file link node. */
    protected static final String KEY_IMGURI = "imgUri";

    /** The log object for this class. */
    private static final Log LOG = CmsLog.getLog(CmsLayoutPageBean.class);

    /** The width of the content area of the template. */
    private int m_bodyWdith;

    /** The padding of the columns in the content area. */
    private int m_colPadding;

    /** The spacing of the columns in the content area. */
    private int m_colSpacing;

    /** The column layout variant of the parapgraphs. */
    private String m_columnLayout;

    /** The calculated width of a single paragraph column. */
    private int m_columnWidth;

    /** The XML content that configures the layout. */
    private CmsXmlContent m_content;

    /** Indicates if the layout images should be fixed or calculated from the content area width. */
    private boolean m_fixedImageSize;

    /** The image width to use for large images in fixed image size mode. */
    private int m_imgWidthLarge;

    /** The image width to use for medium images in fixed image size mode. */
    private int m_imgWidthMedium;

    /** The image width to use for small images in fixed image size mode. */
    private int m_imgWidthSmall;

    /** The JSP action element to get access to the OpenCms API. */
    private CmsJspActionElement m_jspActionElement;

    /** The possible patterns to show with corresponding image widths to use. */
    private Map m_layoutPatterns;

    /** The VFS path to the html snippet files.  */
    private String m_pathLayoutElements;

    /** The map to store the information of the .properties files for each integrated xml content type.*/
    private Map m_typeMappings;

    /** The layout variant to show, e.g. "common", "print" or "accessibe". */
    private String m_variant;

    /**
     * Empty constructor, required for every JavaBean.<p>
     * 
     * It is required to call either the init() method or set the members manually before you can use the 
     * instance of this bean.
     */
    public CmsLayoutPageBean() {

        super();
    }

    /**
     * Constructor, with parameters.<p>
     * 
     * Use this constructor for the template.<p>
     * 
     * @param jsp the current JSP action element
     * @param content the XML content that configures the layout
     * @param variant the layout variant to show, e.g. "common", "print" or "accessibe"
     * @param bodyWith the width of the content area of the template
     * @param colPadding the padding of the columns in the content area
     * @param colSpacing the spacing of the columns in the content area
     */
    public CmsLayoutPageBean(
        CmsJspActionElement jsp,
        CmsXmlContent content,
        String variant,
        int bodyWith,
        int colPadding,
        int colSpacing) {

        super();
        init(jsp, content, variant, bodyWith, colPadding, colSpacing);
    }

    /**
     * Returns the HTML for the large image to show in a popup window with a desired width of 600px.<p>
     * 
     * @param cms the initialized JSP action element
     * @param imgUri the URI of the image to link to
     * @param imgSize the image size property value containing the original image information
     * @return the HTML for the large image to show in a popup window
     */
    public static String buildLargeImageTag(CmsJspActionElement cms, String imgUri, String imgSize) {

        // create scaler instance of original image
        CmsImageScaler origImage = new CmsImageScaler(imgSize);
        // create scaler with desired image width
        CmsImageScaler scaler = new CmsImageScaler();
        scaler.setWidth(600);
        // return scaler with result image width
        CmsImageScaler resultScaler = origImage.getWidthScaler(scaler);
        return cms.img(imgUri, resultScaler, null);
    }

    /**
     * Adds a paragraph layout pattern with the provided name and image width variant name to the possible patterns to show.<p>
     * 
     * Possible image width variants are:<p>
     * <ul>
     * <li>{@link #IMG_WIDTH_SMALL}: small image</li>
     * <li>{@link #IMG_WIDTH_MEDIUM}: medium image</li>
     * <li>{@link #IMG_WIDTH_LARGE}: large image</li>
     * </ul>
     * 
     * @param patternName the name of the pattern layout
     * @param imgWidthVariant the image width variant name to use
     */
    public void addLayoutPattern(String patternName, String imgWidthVariant) {

        m_layoutPatterns.put(patternName, imgWidthVariant);
    }

    /**
     * Returns the HTML for the paragraphs to display.<p>
     * 
     * @return the HTML for the paragraphs to display
     */
    public String buildHtmlParagraphs() {

        StringBuffer result = new StringBuffer(16384);
        Locale locale = getCmsObject().getRequestContext().getLocale();
        
        m_typeMappings = new HashMap();

        // first calculate the column width
        calculateColumnWidth();

        // get the macros
        I_CmsMacroWrapper macros = null;
        try {
            macros = getMacroWrapper();
        } catch (Exception e) {
            // log error and stop output
            if (!(e instanceof CmsException)) {
                if (LOG.isErrorEnabled()) {
                    LOG.error(e.getMessage());
                }
            }
            return "";
        }

        // determine if two columns should be shown
        boolean showTwoCols = getColumnLayout().indexOf(COLUMNS_LAYOUT_2) != -1;
        // determine if image links to larger image version should be shown
        boolean showImgLinks = IMG_LINK_LARGER.equals(m_content.getStringValue(
            getCmsObject(),
            NODE_IMAGEOPTIONS,
            locale));

        // determine localized image link title
        String imgLinkTitleLocalized = "";
        CmsMessages messages = getCmsJspActionElement().getMessages(
            "org/opencms/frontend/layoutpage/frontendmessages",
            locale);
        if (showImgLinks) {
            imgLinkTitleLocalized = messages.keyDefault("link.image.original", "");
        }

        // get the paragraph nodes from the XML content
        Iterator i = m_content.getValues(NODE_PARAGRAPH, locale).iterator();

        // open column table
        result.append(macros.getResult("content_start"));

        // set variables needed in loop to determine correct column to show
        boolean firstInRow = true;

        while (i.hasNext()) {
            // loop all paragraph nodes
            I_CmsXmlContentValue value = (I_CmsXmlContentValue)i.next();
            String xPath = value.getPath() + "/";

            // check if a file has to be integrated
            boolean hasFileLink = m_content.hasValue(xPath + NODE_FILELINK, locale);
            
            ExtendedProperties xmlElementsProperties = null;
            CmsFile linkToFile = null;
            CmsXmlContent xmlContentFileLink = null;
            if (hasFileLink) {
                try {
                    // read the integrated file, get the xml content of it and recieve the properties for
                    // the type of this resource
                    linkToFile = getCmsObject().readFile(
                        m_content.getStringValue(getCmsObject(), xPath + NODE_FILELINK, locale));

⌨️ 快捷键说明

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