Package com.xpn.xwiki.plugin.feed
Class SyndEntryDocumentSource
- java.lang.Object
-
- com.xpn.xwiki.plugin.feed.SyndEntryDocumentSource
-
- All Implemented Interfaces:
SyndEntrySource
public class SyndEntryDocumentSource extends Object implements SyndEntrySource
Concrete strategy for computing the field values of a feed entry from anyXWikiDocument
instance.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
SyndEntryDocumentSource.PropertySelector
Utility class for selecting a property from a XWiki object.
-
Field Summary
Fields Modifier and Type Field Description static String
CONTENT_LENGTH
static String
CONTENT_TYPE
static Map<String,Object>
DEFAULT_PARAMS
static String
FIELD_AUTHOR
static String
FIELD_CATEGORIES
static String
FIELD_CONTRIBUTORS
static String
FIELD_DESCRIPTION
static String
FIELD_LINK
static String
FIELD_PUBLISHED_DATE
static String
FIELD_TITLE
static String
FIELD_UPDATED_DATE
static String
FIELD_URI
protected static org.slf4j.Logger
LOGGER
static Properties
TIDY_FEED_CONFIG
static Properties
TIDY_HTML_CONFIG
protected static TidyMessageLogger
TIDY_LOGGER
static Properties
TIDY_XML_CONFIG
-
Constructor Summary
Constructors Constructor Description SyndEntryDocumentSource()
SyndEntryDocumentSource(Map<String,Object> params)
Creates a new instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected Document
castDocument(Object obj, XWikiContext context)
Casts the given object to aDocument
instance.protected String
getAuthor(Document doc, Map<String,Object> params, XWikiContext context)
protected List<com.sun.syndication.feed.synd.SyndCategory>
getCategories(Document doc, Map<String,Object> params, XWikiContext context)
protected List<String>
getContributors(Document doc, Map<String,Object> params, XWikiContext context)
protected Date
getDateValue(String mapping, Document doc, XWikiContext context)
Retrieves the value of a date property.protected String
getDefaultAuthor(Document doc, Map<String,Object> params, XWikiContext context)
protected List<com.sun.syndication.feed.synd.SyndCategory>
getDefaultCategories(Document doc, Map<String,Object> params, XWikiContext context)
protected List<String>
getDefaultContributors(Document doc, Map<String,Object> params, XWikiContext context)
protected String
getDefaultDescription(Document doc, Map<String,Object> params, XWikiContext context)
protected String
getDefaultLink(Document doc, Map<String,Object> params, XWikiContext context)
protected Map<String,Object>
getDefaultParams()
Strategy class parametersprotected Date
getDefaultPublishedDate(Document doc, Map<String,Object> params, XWikiContext context)
protected String
getDefaultTitle(Document doc, Map<String,Object> params, XWikiContext context)
protected Date
getDefaultUpdateDate(Document doc, Map<String,Object> params, XWikiContext context)
protected String
getDefaultURI(Document doc, Map<String,Object> params, XWikiContext context)
protected com.sun.syndication.feed.synd.SyndContent
getDescription(Document doc, Map<String,Object> params, XWikiContext context)
static String
getHTMLPreview(String htmlFragment, int previewLength)
Extracts the first characters of the given HTML fragment, up to the given length limit, adding only characters in HTML text nodes.protected String
getLink(Document doc, Map<String,Object> params, XWikiContext context)
protected List<Object>
getListValue(String mapping, Document doc, XWikiContext context)
Retrieves the value of a list property.Map<String,Object>
getParams()
static String
getPlainPreview(String plainText, int previewLength)
Extracts the first characters of the given text, up to the last space within the given length limit.protected Date
getPublishedDate(Document doc, Map<String,Object> params, XWikiContext context)
protected String
getStringValue(String mapping, Document doc, XWikiContext context)
Retrieves the value of a string property.protected com.sun.syndication.feed.synd.SyndContent
getSyndContent(String type, String value)
Creates a newSyndContent
instance for the given content type and value.protected String
getTitle(Document doc, Map<String,Object> params, XWikiContext context)
protected Date
getUpdateDate(Document doc, Map<String,Object> params, XWikiContext context)
protected String
getURI(Document doc, Map<String,Object> params, XWikiContext context)
static String
getXMLPreview(String xmlFragment, int previewLength)
Extracts the first characters of the given XML fragment, up to the given length limit, adding only characters in XML text nodes.static int
innerTextLength(Node node)
Computes the sum of lengths of all the text nodes within the given DOM sub-treeprotected boolean
isVelocityCode(String mapping)
Distinguishes between mapping to a property and mapping to a velocity code.protected Map<String,Object>
joinParams(Map<String,Object> base, Map<String,Object> extra)
protected Date
parseDate(String mapping, Document doc, XWikiContext context)
Converts the given velocity string to aDate
instance.protected List<Object>
parseList(String mapping, Document doc, XWikiContext context)
Converts the given velocity code to aList
instance.protected String
parseString(String mapping, Document doc, XWikiContext context)
void
setParams(Map<String,Object> params)
Sets instance parameters.void
source(com.sun.syndication.feed.synd.SyndEntry entry, Object obj, Map<String,Object> params, XWikiContext context)
Overwrites the current values of the given feed entry with new ones computed from the specified source object.void
sourceDocument(com.sun.syndication.feed.synd.SyndEntry entry, Document doc, Map<String,Object> params, XWikiContext context)
Overwrites the current values of the given feed entry with new ones computed from the specified source document.static Document
tidy(String xmlFragment, Properties config)
Cleans up the given XML fragment using the specified configuration.
-
-
-
Field Detail
-
LOGGER
protected static final org.slf4j.Logger LOGGER
-
TIDY_LOGGER
protected static final TidyMessageLogger TIDY_LOGGER
-
CONTENT_TYPE
public static final String CONTENT_TYPE
- See Also:
- Constant Field Values
-
CONTENT_LENGTH
public static final String CONTENT_LENGTH
- See Also:
- Constant Field Values
-
TIDY_FEED_CONFIG
public static final Properties TIDY_FEED_CONFIG
-
TIDY_XML_CONFIG
public static final Properties TIDY_XML_CONFIG
-
TIDY_HTML_CONFIG
public static final Properties TIDY_HTML_CONFIG
-
FIELD_URI
public static final String FIELD_URI
- See Also:
- Constant Field Values
-
FIELD_LINK
public static final String FIELD_LINK
- See Also:
- Constant Field Values
-
FIELD_TITLE
public static final String FIELD_TITLE
- See Also:
- Constant Field Values
-
FIELD_DESCRIPTION
public static final String FIELD_DESCRIPTION
- See Also:
- Constant Field Values
-
FIELD_CATEGORIES
public static final String FIELD_CATEGORIES
- See Also:
- Constant Field Values
-
FIELD_PUBLISHED_DATE
public static final String FIELD_PUBLISHED_DATE
- See Also:
- Constant Field Values
-
FIELD_UPDATED_DATE
public static final String FIELD_UPDATED_DATE
- See Also:
- Constant Field Values
-
FIELD_AUTHOR
public static final String FIELD_AUTHOR
- See Also:
- Constant Field Values
-
FIELD_CONTRIBUTORS
public static final String FIELD_CONTRIBUTORS
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
SyndEntryDocumentSource
public SyndEntryDocumentSource()
-
SyndEntryDocumentSource
public SyndEntryDocumentSource(Map<String,Object> params)
Creates a new instance. The given parameters overwriteDEFAULT_PARAMS
.- Parameters:
params
- parameters only for this instance
-
-
Method Detail
-
setParams
public void setParams(Map<String,Object> params)
Sets instance parameters. Instance parameters overwriteDEFAULT_PARAMS
- Parameters:
params
- instance parameters
-
source
public void source(com.sun.syndication.feed.synd.SyndEntry entry, Object obj, Map<String,Object> params, XWikiContext context) throws XWikiException
Description copied from interface:SyndEntrySource
Overwrites the current values of the given feed entry with new ones computed from the specified source object.- Specified by:
source
in interfaceSyndEntrySource
- Parameters:
entry
- the feed entry whose fields are going to be overwrittenobj
- the source for the new values to be set on the fields of the feed entryparams
- parameters to adjust the computation. Each concrete strategy may define its own (key, value) pairscontext
- the XWiki context- Throws:
XWikiException
-
sourceDocument
public void sourceDocument(com.sun.syndication.feed.synd.SyndEntry entry, Document doc, Map<String,Object> params, XWikiContext context) throws XWikiException
Overwrites the current values of the given feed entry with new ones computed from the specified source document.- Parameters:
entry
- the feed entry whose fields are going to be overwrittendoc
- the source for the new values to be set on the fields of the feed entryparams
- parameters to adjust the computation. Each concrete strategy may define its own (key, value) pairscontext
- the XWiki context- Throws:
XWikiException
-
getDefaultURI
protected String getDefaultURI(Document doc, Map<String,Object> params, XWikiContext context) throws XWikiException
- Throws:
XWikiException
-
getURI
protected String getURI(Document doc, Map<String,Object> params, XWikiContext context) throws XWikiException
- Throws:
XWikiException
-
getDefaultLink
protected String getDefaultLink(Document doc, Map<String,Object> params, XWikiContext context) throws XWikiException
- Throws:
XWikiException
-
getLink
protected String getLink(Document doc, Map<String,Object> params, XWikiContext context) throws XWikiException
- Throws:
XWikiException
-
getDefaultTitle
protected String getDefaultTitle(Document doc, Map<String,Object> params, XWikiContext context)
-
getTitle
protected String getTitle(Document doc, Map<String,Object> params, XWikiContext context) throws XWikiException
- Throws:
XWikiException
-
getDefaultDescription
protected String getDefaultDescription(Document doc, Map<String,Object> params, XWikiContext context)
-
getDescription
protected com.sun.syndication.feed.synd.SyndContent getDescription(Document doc, Map<String,Object> params, XWikiContext context) throws XWikiException
- Throws:
XWikiException
-
getDefaultCategories
protected List<com.sun.syndication.feed.synd.SyndCategory> getDefaultCategories(Document doc, Map<String,Object> params, XWikiContext context)
-
getCategories
protected List<com.sun.syndication.feed.synd.SyndCategory> getCategories(Document doc, Map<String,Object> params, XWikiContext context) throws XWikiException
- Throws:
XWikiException
-
getDefaultPublishedDate
protected Date getDefaultPublishedDate(Document doc, Map<String,Object> params, XWikiContext context)
-
getPublishedDate
protected Date getPublishedDate(Document doc, Map<String,Object> params, XWikiContext context) throws XWikiException
- Throws:
XWikiException
-
getDefaultUpdateDate
protected Date getDefaultUpdateDate(Document doc, Map<String,Object> params, XWikiContext context)
-
getUpdateDate
protected Date getUpdateDate(Document doc, Map<String,Object> params, XWikiContext context) throws XWikiException
- Throws:
XWikiException
-
getDefaultAuthor
protected String getDefaultAuthor(Document doc, Map<String,Object> params, XWikiContext context)
-
getAuthor
protected String getAuthor(Document doc, Map<String,Object> params, XWikiContext context) throws XWikiException
- Throws:
XWikiException
-
getDefaultContributors
protected List<String> getDefaultContributors(Document doc, Map<String,Object> params, XWikiContext context)
-
getContributors
protected List<String> getContributors(Document doc, Map<String,Object> params, XWikiContext context) throws XWikiException
- Throws:
XWikiException
-
isVelocityCode
protected boolean isVelocityCode(String mapping)
Distinguishes between mapping to a property and mapping to a velocity code.- Parameters:
mapping
-- Returns:
- true if the given string is a mapping to a velocity code
-
parseString
protected String parseString(String mapping, Document doc, XWikiContext context) throws XWikiException
- Throws:
XWikiException
-
parseDate
protected Date parseDate(String mapping, Document doc, XWikiContext context) throws NumberFormatException, XWikiException
Converts the given velocity string to aDate
instance. The velocity code must be evaluated to a long representing a time stamp.- Throws:
NumberFormatException
XWikiException
-
parseList
protected List<Object> parseList(String mapping, Document doc, XWikiContext context) throws XWikiException
Converts the given velocity code to aList
instance. The velocity code must be evaluated to a string with the following format: "[item1,item2,...,itemN]".- Throws:
XWikiException
-
getStringValue
protected String getStringValue(String mapping, Document doc, XWikiContext context) throws XWikiException
Retrieves the value of a string property.- Throws:
XWikiException
-
getDateValue
protected Date getDateValue(String mapping, Document doc, XWikiContext context) throws XWikiException
Retrieves the value of a date property.- Throws:
XWikiException
-
getListValue
protected List<Object> getListValue(String mapping, Document doc, XWikiContext context) throws XWikiException
Retrieves the value of a list property.- Throws:
XWikiException
-
joinParams
protected Map<String,Object> joinParams(Map<String,Object> base, Map<String,Object> extra)
- Returns:
- base + (extra - base)
-
tidy
public static Document tidy(String xmlFragment, Properties config)
Cleans up the given XML fragment using the specified configuration.- Parameters:
xmlFragment
- the XML fragment to be cleaned upconfig
- the configuration properties to use- Returns:
- the DOM tree associated with the cleaned up XML fragment
-
innerTextLength
public static int innerTextLength(Node node)
Computes the sum of lengths of all the text nodes within the given DOM sub-tree- Parameters:
node
- the root of the DOM sub-tree containing the text- Returns:
- the sum of lengths of text nodes within the given DOM sub-tree
-
getXMLPreview
public static String getXMLPreview(String xmlFragment, int previewLength)
Extracts the first characters of the given XML fragment, up to the given length limit, adding only characters in XML text nodes. The XML fragment is cleaned up before extracting the prefix to be sure that the result is well-formed.- Parameters:
xmlFragment
- the full XML textpreviewLength
- the maximum number of characters allowed in the preview, considering only the XML text nodes- Returns:
- a prefix of the given XML fragment summing at most
previewLength
characters in its text nodes
-
getHTMLPreview
public static String getHTMLPreview(String htmlFragment, int previewLength)
Extracts the first characters of the given HTML fragment, up to the given length limit, adding only characters in HTML text nodes. The HTML fragment is cleaned up before extracting the prefix to be sure the result is well-formed.- Parameters:
htmlFragment
- the full HTML textpreviewLength
- the maximum number of characters allowed in the preview, considering only the HTML text nodes- Returns:
- a prefix of the given HTML fragment summing at most
previewLength
characters in its text nodes
-
getPlainPreview
public static String getPlainPreview(String plainText, int previewLength)
Extracts the first characters of the given text, up to the last space within the given length limit.- Parameters:
plainText
- the full textpreviewLength
- the maximum number of characters allowed in the preview- Returns:
- a prefix of the
plainText
having at mostpreviewLength
characters
-
getSyndContent
protected com.sun.syndication.feed.synd.SyndContent getSyndContent(String type, String value)
Creates a newSyndContent
instance for the given content type and value.- Parameters:
type
- content typevalue
- the content- Returns:
- a new
SyndContent
instance
-
castDocument
protected Document castDocument(Object obj, XWikiContext context) throws XWikiException
Casts the given object to aDocument
instance. The given object must be either aDocument
instance already, aXWikiDocument
instance or the full name of the document.- Parameters:
obj
- object to be castedcontext
- the XWiki context- Returns:
- the document associated with the given object
- Throws:
XWikiException
- if the given object is neither aDocument
instance, aXWikiDocument
instance nor the full name of the document
-
-