📄 rsschannelsrc.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.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsProperty;
import org.opencms.file.CmsResource;
import org.opencms.file.types.I_CmsResourceType;
import org.opencms.main.CmsException;
import org.opencms.main.OpenCms;
import org.opencms.xml.CmsXmlException;
import org.opencms.xml.CmsXmlUtils;
import org.opencms.xml.I_CmsXmlDocument;
import org.opencms.xml.content.CmsXmlContent;
import org.opencms.xml.content.CmsXmlContentFactory;
import org.opencms.xml.page.CmsXmlPage;
import org.opencms.xml.page.CmsXmlPageFactory;
import org.opencms.xml.types.I_CmsXmlContentValue;
/**
* A simple bean class for wrapping a Channel Source
*/
public class RssChannelSrc {
/** Date Format String */
private static final DateFormat DATE_PARSER = new SimpleDateFormat("yyyy-MM-dd");
/** CmsObject */
CmsObject m_cms;
/** un-marshalled document */
I_CmsXmlDocument m_iDoc;
/** XML path to this nested content */
String m_basePath;
/** data fields */
private String m_sourceContentType;
private String m_sourceLocation;
private String m_sourceTitleField;
private String m_sourceDescField;
private String m_sourceAuthorField;
private String m_feedLimit;
public final static String META_META = "meta.";
public final static String META_PROPERTY = "property.";
public final static String META_FIELD = "field.";
public static final int DEFAULT_FEED_LIMIT = -1;
public RssChannelSrc(CmsObject cms, I_CmsXmlDocument Doc, String Path) {
m_cms = cms;
m_iDoc = Doc;
m_basePath = Path;
m_sourceContentType = getField("SourceContentType");
m_sourceLocation = getField("SourceLocation");
m_sourceTitleField = getField("SourceTitleField");
m_sourceDescField = getField("SourceDescField");
m_sourceAuthorField = getField("SourceAuthorField");
m_feedLimit = getField("FeedLimit");
}
/**
* Returns the SourceContentType field value
* @return value of the SourceContentType field
*/
public String getSourceContentType() {
return m_sourceContentType;
}
/**
* Returns the SourceLocation field value
* @return value of the SourceLocation field
*/
public String getSourceLocation() {
return m_sourceLocation;
}
public void setSourceLocation(String Location) {
m_sourceLocation = Location;
}
public String getSourceTitleField() {
return m_sourceTitleField;
}
public void setSourceTitleField(String SourceTitleField) {
m_sourceTitleField = SourceTitleField;
}
public String getSourceDescField() {
return m_sourceDescField;
}
public void setSourceDescField(String SourceDescField) {
m_sourceDescField = SourceDescField;
}
public String getSourceAuthorField() {
return m_sourceAuthorField;
}
public void setSourceAuthorField(String SourceAuthorField) {
m_sourceAuthorField = SourceAuthorField;
}
/**
* Returns the Title of the RSS entry using the configured mapping entries.
* @return String containing the Title field for the RSS entry
*/
public String getSourceTitleFieldValue(CmsResource res) {
return getMappedFieldValue(res, getSourceTitleField());
}
/**
* Returns the Description of the RSS entry using the configured mapping entries.
* @return String containing the Description field for the RSS entry
*/
public String getSourceDescFieldValue(CmsResource res) {
return getMappedFieldValue(res, getSourceDescField());
}
/**
* Returns the Author of the RSS entry using the configured mapping entries.
* @return String containing the Author field for the RSS entry
*/
public String getAuthorFieldValue(CmsResource res) {
return getMappedFieldValue(res, getSourceAuthorField());
}
public String getFeedLimit() {
return m_feedLimit;
}
public void setFeedLimit(String Limit) {
m_feedLimit = Limit;
}
/**
* Returns the max number of items for this channel source
*
* @return integer with the max number of items for this source or -1 if no limit specified
*/
public int getSourceItemLimit() {
String strLimit = getFeedLimit();
if (false == StringUtils.isNull(strLimit)) {
try {
return Integer.parseInt(strLimit);
} catch (NumberFormatException nfe) {}
}
// default feed limit (no limit)
return DEFAULT_FEED_LIMIT;
}
/**
* Reads the specified field name from the resource entry
*
* @param res CmsResource to read the field value of
* @param FieldName Name of the field to retrieve
* @return String with the value of the field
*/
protected String getMappedFieldValue(CmsResource res, String FieldName) {
String strField;
try {
if (0 == FieldName.indexOf(META_META)) {
strField = FieldName.substring(META_META.length());
return getMetaField(res, strField);
} else if (0 == FieldName.indexOf(META_PROPERTY)) {
// mapped to property. Property values are read as specified
strField = FieldName.substring(META_PROPERTY.length());
CmsProperty prop = getCmsObject().readPropertyObject(res, strField, false);
return prop.getValue();
} else if (0 == FieldName.indexOf(META_FIELD)) {
// mapped to a field in the resource. field values are read as specified
strField = FieldName.substring(META_FIELD.length());
return getContentField(res, strField);
}
} catch (CmsException e) {
return e.getMessage();
}
return StringUtils.NULLSTR;
}
/**
* Returns the value of a Meta Field value from the passed resource. A Meta Field value is specified
* in the passed FieldSpec argument. The FieldSpec argument must be in the format:<p>
* <code>meta.metafieldname</code><p>
* Valid meta field names are: <p>
* <ul>
* <li>UserCreated - returns the name of the user that created the resource
* <li>UserLastModified - returns the name of the user that last modified the resource
* <li>DateContent - returns the date that the content field was last updated
* <li>DateCreated - returns the creation date of the content
* <li>DateExpired - returns the expiration date of the content
* <li>DateLastModified - returns the last modified date of the content
* <li>DateReleased - returns the released date of the content
* </ul><p>
* @param res CmsResource to read the Meta field value of
* @param FieldSpec The meta field name/specification
* @return String with the value of the field
* @throws CmsException if an error occurs
*/
protected String getMetaField(CmsResource res, String FieldSpec) throws CmsException {
// mapped to a meta-value field:
// supported meta values are datecontent, datecreated, dateexpired,
// datelastmodified, datereleased, usercreate, userlastmodifled
if (FieldSpec.equalsIgnoreCase("datecontent")) {
long lDate = res.getDateContent();
return DATE_PARSER.format(new Date(lDate));
} else if (FieldSpec.equalsIgnoreCase("datecreated")) {
long lDate = res.getDateCreated();
return DATE_PARSER.format(new Date(lDate));
} else if (FieldSpec.equalsIgnoreCase("dateexpired")) {
long lDate = res.getDateExpired();
return DATE_PARSER.format(new Date(lDate));
} else if (FieldSpec.equalsIgnoreCase("datelastmodifled")) {
long lDate = res.getDateLastModified();
return DATE_PARSER.format(new Date(lDate));
} else if (FieldSpec.equalsIgnoreCase("datereleased")) {
long lDate = res.getDateReleased();
return DATE_PARSER.format(new Date(lDate));
} else if (FieldSpec.equalsIgnoreCase("usercreated")) {
return getCmsObject().readUser(res.getUserCreated()).getName();
} else if (FieldSpec.equalsIgnoreCase("userlastmodified")) {
return getCmsObject().readUser(res.getUserLastModified()).getName();
}
return "Unknown Field-" + FieldSpec;
}
/**
* Returns the Value of a content field from the passed resource. The resource must be a structured
* content type containing a field that matches that specified in the FieldSpec. The FieldSpec must be
* in the following format:<p>
* <code>field.fieldname[maxsize+-]</code><p>
* <b>fieldname</b> - contains the field name to retrieve<p>
* <b>maxsize</b> - this is an optional numeric that may be used to specify a size to truncate the value to<p>
* if may be followed with an optional specifier to truncate down or up to the next period.<p>
* <b>Examples:</b><p>
* <b>field.content[200+]</b> - this will retrieve the field named 'content' and truncate it upwards to the
* next period that occurs after 200 characters<p>
* <b>field.body[100]</b> - this will retrieve the field named 'body' and truncate it exactly at 100 characters<p>
* <b>field.body</b> - this will retrieve the field named 'body' with no truncation<p>
* <b>field.content[150-]</b> - this will retrieve the field named 'content' and truncate it downward to the
* next period that occurs before 150 characters<p>
*
* @throws CmsException
* @throws CmsXmlException
*/
protected String getContentField(CmsResource res, String FieldSpec) throws CmsXmlException, CmsException {
String strFieldname = FieldSpec;
// first parse the field specification
// see if there is a size limit specified
int nMaxSize = -1;
int nTrimDirection = 0;
int nDelimStart = strFieldname.indexOf("[");
if (nDelimStart != -1) {
// there is a size specified so we need to truncate the value
int nDelimEnd = strFieldname.indexOf("]");
if (nDelimEnd == -1) {
// bad or missing terminator, strip off the beginning and ignore it
strFieldname = strFieldname.substring(0, nDelimStart);
} else {
// parse out the size specifier
String strSizeSpec = strFieldname.substring(nDelimStart+1, nDelimEnd);
if (strSizeSpec.endsWith("-")) {
// adjust down to last period
strSizeSpec = strSizeSpec.substring(0, strSizeSpec.length() -1);
nTrimDirection = -1;
} else if (strSizeSpec.endsWith("+")) {
// adjust up to next period
strSizeSpec = strSizeSpec.substring(0, strSizeSpec.length() -1);
nTrimDirection = 1;
} else {
// no trimming, just truncate it
}
try {
nMaxSize = Integer.parseInt(strSizeSpec);
} catch (NumberFormatException nfe) {
// if it fails, then don't do any trimming
nMaxSize = -1;
}
// parse out the fieldname
strFieldname = strFieldname.substring(0, nDelimStart);
}
}
// read the resource and retrieve the field value
I_CmsXmlContentValue iVal;
I_CmsResourceType iTyp = OpenCms.getResourceManager().getResourceType(res.getTypeId());
// xmlpage needs to handled differently from xmlcontent derived types
if (iTyp.getTypeName().equals("xmlpage")) {
// read the value
CmsXmlPage page = CmsXmlPageFactory.unmarshal(getCmsObject(), getCmsObject().readFile(res));
String strField = page.getStringValue(getCmsObject(), strFieldname, getCmsObject().getRequestContext().getLocale());
return trimString(strField, nMaxSize, nTrimDirection);
} else {
CmsXmlContent content = CmsXmlContentFactory.unmarshal(getCmsObject(), getCmsObject().readFile(res));
// read the value
iVal = content.getValue(strFieldname, getCmsObject().getRequestContext().getLocale());
}
if (null != iVal) {
// trim the string and return it
return trimString(iVal.getStringValue(getCmsObject()), nMaxSize, nTrimDirection);
}
return "Unknown Field-" + strFieldname;
}
/**
* Utility class to trim the given string
*
* @param Value String value to trim
* @param Max Max size of the string
* @param TrimDirection Direction to trim (0 = no trim, -1 = trim to previous period, 1 = trim to next period)
* @return Trimmed string value
*/
private String trimString(String Value, int Max, int TrimDirection) {
if (null != Value) {
if (Max < 0) {
return Value;
}
switch (TrimDirection) {
default:
case 0: // no trimming, just truncate
return Value.substring(0, Max);
case -1: // trim down
if (Max > Value.length()) {
return Value;
}
return StringUtils.trimDown(Value, Max);
case 1: // trim up
if (Max > Value.length()) {
return Value;
}
return StringUtils.trimUp(Value, Max);
}
}
return StringUtils.NULLSTR;
}
/**
* Helper method to return the locale
* @return Locale
*/
private Locale getLocale() {
return getCmsObject().getRequestContext().getLocale();
}
private CmsObject getCmsObject() {
return m_cms;
}
private String getField(String FieldName) {
try {
return m_iDoc.getStringValue(m_cms, CmsXmlUtils.concatXpath(m_basePath, FieldName), getLocale());
} catch (CmsXmlException e) {
return StringUtils.NULLSTR;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -