/*
* Copyright (c) 1999-2002 ChurchillObjects.com All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met: Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer. Redistributions in
* binary form must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution. Neither the name of the copyright
* holder nor the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT, INCLUDING NEGLIGENCE OR OTHERWISE, ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
*/
package churchillobjects.rss4j.generator;
import java.util.Enumeration;
import org.w3c.dom.Element;
import churchillobjects.rss4j.RssChannel;
import churchillobjects.rss4j.RssChannelImage;
import churchillobjects.rss4j.RssChannelItem;
import churchillobjects.rss4j.RssChannelTextInput;
import churchillobjects.rss4j.RssDocument;
/**
* A RssGenerator implementation for creating RSS 0.90 compliant documents.
*/
class RssGeneratorImpl090 extends RssGenerator{
/**
* Constructor.
* @throws RssGenerationException
*/
RssGeneratorImpl090() throws RssGenerationException{
super();
}
/**
* Sets the maximum field lengths for elements in RSS 0.90.
*/
protected void setMaxLengths(){
channelTitleMax = 40;
channelDescriptionMax = 500;
channelLinkMax = 500;
imageTitleMax = 40;
imageUrlMax = 500;
imageLinkMax = 500;
itemTitleMax = 100;
itemLinkMax = 500;
textInputTitleMax = 40;
textInputDescriptionMax = 100;
textInputNameMax = 500;
textInputLinkMax = 500;
}
/**
* This method is used for any post-processing or clean-up activities. There
* is nothing to do here for RSS 0.90, but superclasses do.
* @throws RssGenerationException
*/
protected void finishDocument() throws RssGenerationException{
}
/**
* Starts the document with RSS 0.90 fields.
* @param data
*/
protected void createRssDocument(RssDocument data){
doc = domImpl.createDocument("http://my.netscape.com/rdf/simple/0.9/", "rdf:RDF", null);
rootElement = doc.getDocumentElement();
rootElement.setAttribute("xmlns:rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
rootElement.setAttribute("xmlns", "http://my.netscape.com/rdf/simple/0.9/");
}
protected void handleChannel(RssChannel channel) throws RssGenerationException{
Element channelElement = doc.createElement("channel");
rootElement.appendChild(channelElement);
handleChannelTitle(channel, channelElement);
handleChannelDescription(channel, channelElement);
handleChannelLink(channel, channelElement);
handleTextInput(channel, channelElement);
handleImage(channel, channelElement);
handleItems(channel, channelElement);
}
private void handleImage(RssChannel channel, Element channelElement) throws RssGenerationException{
RssChannelImage image = (RssChannelImage)channel.getChannelImage();
if(image!=null){
Element imageElement = doc.createElement("image");
handleImageTitle(image, imageElement);
handleImageUrl(image, imageElement);
handleImageLink(image, imageElement);
channelElement.appendChild(imageElement);
}
}
private void handleItems(RssChannel channel, Element channelElement) throws RssGenerationException{
if(channel.getItemCount()>15){
throw new RssGenerationException("Channel in 0.91 RSS cannot have more than 15 items");
}
Enumeration enumeration = channel.items();
while(enumeration.hasMoreElements()){
RssChannelItem item = (RssChannelItem)enumeration.nextElement();
if(item!=null){
Element itemElement = doc.createElement("item");
handleItemTitle(item, itemElement);
handleItemLink(item, itemElement);
channelElement.appendChild(itemElement);
}
}
}
private void handleTextInput(RssChannel channel, Element channelElement) throws RssGenerationException{
RssChannelTextInput textInput = channel.getChannelTextInput();
if(textInput!=null){
Element textInputElement = doc.createElement("textInput");
handleTextInputTitle(textInput, textInputElement);
handleTextInputDescription(textInput, textInputElement);
handleTextInputName(textInput, textInputElement);
handleTextInputLink(textInput, textInputElement);
channelElement.appendChild(textInputElement);
}
}
//
// CHANNEL SUBELEMENTS
//
protected void handleChannelTitle(RssChannel channel, Element channelElement) throws RssGenerationException{
String title = channel.getChannelTitle();
title = truncate(title, channelTitleMax);
validateValueRequired(title, "Channel title", channelTitleMax);
add(channelElement, "title", title);
}
protected void handleChannelDescription(RssChannel channel, Element channelElement) throws RssGenerationException{
String description = channel.getChannelDescription();
description = truncate(description, channelDescriptionMax);
validateValueRequired(description, "Channel description", channelDescriptionMax);
add(channelElement, "description", description);
}
protected void handleChannelLink(RssChannel channel, Element channelElement) throws RssGenerationException{
String link = channel.getChannelLink();
validateUri(link);
validateValueRequired(link, "Channel link", channelLinkMax);
add(channelElement, "link", link);
}
//
// IMAGE SUBELEMENTS
//
protected void handleImageTitle(RssChannelImage image, Element imageElement) throws RssGenerationException{
String title = image.getImageTitle();
title = truncate(title, imageTitleMax);
validateValueRequired(title, "Image title", imageTitleMax);
add(imageElement, "title", title);
}
protected void handleImageUrl(RssChannelImage image, Element imageElement) throws RssGenerationException{
String url = image.getImageUrl();
validateUri(url);
validateValueRequired(url, "Image URL", imageUrlMax);
add(imageElement, "url", url);
}
protected void handleImageLink(RssChannelImage image, Element imageElement) throws RssGenerationException{
String link = image.getImageLink();
validateUri(link);
validateValueRequired(link, "Image link", imageLinkMax);
add(imageElement, "link", link);
}
//
// ITEM SUBELEMENTS
//
protected void handleItemTitle(RssChannelItem item, Element itemElement) throws RssGenerationException{
String title = item.getItemTitle();
title = truncate(title, itemTitleMax);
validateValueRequired(title, "Item title", itemTitleMax);
add(itemElement, "title", title);
}
protected void handleItemLink(RssChannelItem item, Element itemElement) throws RssGenerationException{
String link = item.getItemLink();
validateUri(link);
validateValueRequired(link, "Item link", itemLinkMax);
add(itemElement, "link", link);
}
//
// TEXT INPUT SUBELEMENTS
//
protected void handleTextInputTitle(RssChannelTextInput textInput, Element textInputElement) throws RssGenerationException{
String title = textInput.getInputTitle();
title = truncate(title, textInputTitleMax);
validateValueRequired(title, "Text input title", textInputTitleMax);
add(textInputElement, "title", title);
}
protected void handleTextInputDescription(RssChannelTextInput textInput, Element textInputElement) throws RssGenerationException{
String description = textInput.getInputDescription();
description = truncate(description, textInputDescriptionMax);
validateValueRequired(description, "Text input description", textInputDescriptionMax);
add(textInputElement, "description", description);
}
protected void handleTextInputName(RssChannelTextInput textInput, Element textInputElement) throws RssGenerationException{
String name = textInput.getInputName();
validateValueRequired(name, "Text input name", textInputNameMax);
add(textInputElement, "name", name);
}
protected void handleTextInputLink(RssChannelTextInput textInput, Element textInputElement) throws RssGenerationException{
String link = textInput.getInputLink();
validateUri(link);
validateValueRequired(link, "Text input link", textInputLinkMax);
add(textInputElement, "link", link);
}
}