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

📄 rsschanneldef.java

📁 OpenCMS内容管理入门指南
💻 JAVA
字号:
/*
 * This library is part of the code for the book: OpenCms for Developers
 *
 * Copyright (c) 2007, 2008 Dan Liliedahl
 *
 * 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.
 *
 * 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.deepthoughts.rss;

import java.util.Date;
import java.util.Locale;

import org.opencms.file.CmsFile;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsResource;
import org.opencms.main.CmsException;
import org.opencms.xml.CmsXmlUtils;
import org.opencms.xml.I_CmsXmlDocument;
import org.opencms.xml.content.CmsXmlContentFactory;
import org.opencms.xml.types.I_CmsXmlContentValue;

import com.deepthoughts.rss.admin.Messages;

/**
 * This class is a Java representation of the the RssChannelDef structured content type. It is meant to
 * be used as a utility class to make accessing the RssChannelDef easier.
 *  
 */
public class RssChannelDef {

	/** The resource type id for the RSSChannelDef type */
	public static final int RSSCHANNELDEF_TYPEID = 3001;

	public static final String FIELD_CHANNELSOURCE = "ChannelSource"; 
	
	/** CmsObject */
	protected CmsObject m_cms;
	
	/** The CmsResource containing the rsschanneldef */
	protected CmsResource m_res;

	/** The un-marshalled rsschanneldef instance */
	protected I_CmsXmlDocument m_iDoc;

	/** contains the Rss Channel Sources */
	private RssChannelSrc[] m_ChannelSources;

	/** data fields */
	private String m_title;
	private String m_Description;
	private String m_Copyright;
	private String m_Author;
	private String m_Image;
	private String m_ImageTitle;
	private long m_PublishedDate;
	private String m_Filename;

	/**
	 * Constructor empty instance with a CmsObject
	 */
	public RssChannelDef(CmsObject cms) {
		m_cms = cms;
		Date lDate = new Date();
		m_PublishedDate = lDate.getTime();
	}
	
	/**
	 * Constructs the RSS Channel Definition from the CmsResource. The passed resource must be a structured
	 * content item of type rsschanneldef.
	 * 
	 * @param cms the CmObject instance
	 * @param res the CmsResource containing the RssChannelDef instance
	 * @throws CmsException if an error occurs
	 */
	public RssChannelDef(CmsObject cms, CmsResource res) throws CmsException {
		initFromResource(cms, res);
	}

	/** 
	 * Constructs the RSS Channel Definition from the resource path. The resource found on the given path must
	 * be a structured content item of type rsschanneldef.
	 * 
	 * @param cms the CmObject instance
	 * @param res the CmsResource containing the RssChannelDef instance
	 * @throws CmsException if an error occurs
	 */
	public RssChannelDef(CmsObject cms, String Path) throws CmsException {
		initFromResource(cms, cms.readResource(Path));
	}

	/**
	 * Initializes the RssChannelDef from the passed CmsResource by reading the values
	 * and populating the object
	 * 
	 * @param cms the CmObject instance
	 * @param res the CmsResource containing the RssChannelDef instance
	 * @throws CmsException if an error occurs
	 */
	private void initFromResource(CmsObject cms, CmsResource res) throws CmsException {
		m_cms = cms;
		m_res = res;
		// sanity check - make sure it is an rss channel definition
		if (m_res.getTypeId() != RSSCHANNELDEF_TYPEID) {
            throw new CmsException(Messages.get().container(Messages.ERR_RESOURCE_NOT_RSS_1, res.getName()));
		}

        // read the definition and parse it
    	CmsFile rssDef = getCmsObject().readFile(m_res);
		m_iDoc = CmsXmlContentFactory.unmarshal(getCmsObject(), rssDef);

		// populate the fields
		m_title = getField("Title");
		m_Description = getField("Description");
		m_Copyright = getField("Copyright");
		m_Author = getField("Author");
		m_Image = getField("Image");
		m_ImageTitle = getField("ImageTitle");
		m_PublishedDate = m_res.getDateReleased();
		m_Filename = res.getName();

		// read the list of sources
        int nSources = m_iDoc.getValues(FIELD_CHANNELSOURCE, getLocale()).size();
		m_ChannelSources = new RssChannelSrc[nSources];
        for (int i = 0; i < nSources; i++) {
	        String basePath = CmsXmlUtils.createXpath(FIELD_CHANNELSOURCE, i+1);
			m_ChannelSources[i] = new RssChannelSrc(getCmsObject(), m_iDoc, basePath); 
        }
	}
	
	/**
	 * Returns the name of the feed
	 * @return
	 */
	public String getName() {
		return m_res.getName();
	}
	
	/**
	 * Returns the title of the Feed
	 * @return
	 */
	public String getTitle() {
		return m_title;
	}
	public void setTitle(String Title) {
		m_title = Title;
	}
	
	public String getDescription() {
		return m_Description;
	}
	public void setDescription(String Desc) {
		m_Description = Desc;
	}
	
	public String getCopyright() {
		return m_Copyright;
	}
	public void setCopyright(String Copyright) {
		m_Copyright = Copyright;
	}
	
	public String getAuthor() {
		return m_Author;
	}
	public void setAuthor(String Author) {
		m_Author = Author;
	}

	public long getPublishedDate() {
		return m_PublishedDate;		
	}
	public void setPublishedDate(long date) {
		m_PublishedDate = date;
	}
	
	public String getImage() {
		return m_Image;
	}
	public void setImage(String Image) {
		m_Image = Image;
	}
	
	public String getImageTitle() {
		return m_ImageTitle;
	}
	public void setImageTitle(String ImageTitle) {
		m_ImageTitle = ImageTitle;
	}
	
	public String getFilename() {
		return m_Filename;
	}
	public void setfilename(String Filename) {
		m_Filename = Filename;
	}
	
	/**
	 * Returns an array of Rss Channel Source definition objects
	 * @return array of channel source definitions
	 */
	public RssChannelSrc[] getChannelSources() {
		return m_ChannelSources;
	}
	public void setChannelSources(RssChannelSrc[] Sources) {
		m_ChannelSources = Sources;
	}

    /**
     * Helper method to return the locale
     * @return Locale
     */
    private Locale getLocale() {
    	return getCmsObject().getRequestContext().getLocale();
    }
	
	/**
	 * Accessor method for the CmsObject instance
	 * @return CmsObject
	 */
	private CmsObject getCmsObject() {
		return m_cms;
	}
	
	/**
	 * Helper to retrieve a field from the XML content
	 * 
	 * @param FieldName field name to retrieve value of
	 * @return String with value of the field
	 */
	private String getField(String FieldName) {
		I_CmsXmlContentValue xml = m_iDoc.getValue(FieldName, getLocale());
		if (xml!= null) {
			return xml.getStringValue(getCmsObject());
		}
		return null;
	}
}

⌨️ 快捷键说明

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