package com.statesman.PluckRSSAdmin;
import java.util.*;
/**
*
XSLElement is a concrete class that represents one XSL entry in a
* Transform within a TransformList document. And individual XSLElement is
* composed of
*
*
* - An XSL String, indicating the XSL file containing the transformation
* instructions
* - An integer specifying how often this transformation should occur, in
* minutes
* - A Set of Destinations, where each element in the set represents
* a file name to which the the transformation should be written,
* as well as whether this destination should be published
*
*
* Each XSLElement in a Transform is identified by its source string and its
* interval.
*
* @author Christopher M. Friend (cfriend@statesman.com)
*/
public class XSLElement implements Cloneable {
/**
* the default interval value that will be applied to construct new
* XSLElements when no interval value is present
*/
public static int DEFAULT_INTERVAL = 1;
private String xsl;
private int interval;
private Set destinations;
/**
* Class constructor specifying the XSL String and a Set of
* String destinations. The interval will be set to the value of
* XSLElement.DEFAULT_INTERVAL.
*/
public XSLElement(String xsl, Set destinations) {
this(xsl, DEFAULT_INTERVAL, destinations);
}
/**
* Class constructor specifying the XSL String, the interval, and a Set of
* String destinations.
*/
public XSLElement(String xsl, int interval, Set destinations) {
if (xsl == null || xsl.trim().equals("")) {
throw new IllegalArgumentException("XSL string cannot be null or empty!");
}
if (interval < 1) {
throw new IllegalArgumentException("Interval cannot be less than 1!");
}
if (destinations == null || destinations.size() == 0) {
throw new IllegalArgumentException("Destination set cannot be null or empty!");
}
setXSL(xsl);
setInterval(interval);
setDestinations(destinations);
}
/**
* Class constructor specifying the XSL String and an Array of String
* destinations. The interval will be set to the value of
* XSLElement.DEFAULT_INTERVAL.
*/
public XSLElement(String xsl, Destination [] dests) {
this(xsl, DEFAULT_INTERVAL, dests);
}
/**
* Class constructor specifying the XSL String, the interval, and an Array
* of String destinations.
*/
public XSLElement(String xsl, int interval, Destination [] dests) {
if (xsl == null || xsl.trim().equals("")) {
throw new IllegalArgumentException("XSL string cannot be null or empty!");
}
if (interval < 1) {
throw new IllegalArgumentException("Interval cannot be less than 1!");
}
if (dests == null || dests.length == 0) {
throw new IllegalArgumentException("Destination array cannot be null or empty!");
}
setXSL(xsl);
setInterval(interval);
setDestinations(dests);
}
/**
* Gets the XSL String of this XSLElement
*
* @return a String containing the value of this XSLElement's source
* attribute
*/
public String getXSL() {
return xsl;
}
/**
* Sets the XSL String of this XSLElement
*
* @param xsl a String containing the value of this XSLElement's source
* attribute
*/
public void setXSL(String xsl) {
if (xsl == null || xsl.trim().equals("")) {
throw new IllegalArgumentException("XSL string cannot be null or empty!");
}
this.xsl = xsl;
}
/**
* Gets the interval of this XSLElement
*
* @return an int containing the value of this XSLElements interval
* attribute
*/
public int getInterval() {
return interval;
}
/**
* Sets the interval of this XSLElement
*
* @param interval an int containing the value of this XSLElement's
* interval attribute
*/
public void setInterval(int interval) {
if (interval < 1) {
throw new IllegalArgumentException("Interval cannot be less than 1!");
}
this.interval = interval;
}
/**
* Gets the set of destination Strings of this XSLElement
*
* @return a Set containing Strings representing the destinations
* of this XSL Element
*/
public Set getDestinations() {
return destinations;
}
/**
* Gets the number of destinations for this XSLElement
*
* @return an int containing the number of destinations in this
* XSLElement
*/
public int getNumberOfDestinations() {
return destinations.size();
}
/**
* Sets the destinations for this XSLElement
*
* @param dests a Set of Strings representing the file names to
* which this transformation should be written. Any
* existing destinations will be overwritten.
*/
public void setDestinations(Set dests) {
if (dests == null || dests.size() == 0) {
throw new IllegalArgumentException("Destination set cannot be null or empty!");
}
Destination destArray [] = dests.toArray(new Destination[1]);
setDestinations(destArray);
}
/**
* Sets the destinations for this XSLElement
*
* @param dests an array of Strings representing the file names to
* which this transformation should be written. Any
* existing destinations will be overwritten.
*/
public void setDestinations(Destination [] dests) {
if (dests == null || dests.length == 0) {
throw new IllegalArgumentException("Destination array cannot be null or empty!");
}
this.destinations = new HashSet();
for (int i = 0; i < dests.length; i++) {
addDestination(dests[i]);
}
}
/**
* Adds a destination to this XSLElement
*
* @param dest a String representing a file name to which this
* transformation should be written.
*
* @return a boolean indicating if this XSLElement was modified
*/
public boolean addDestination(Destination dest) {
if (dest == null) {
throw new IllegalArgumentException("Destination cannot be null!");
}
return this.destinations.add(dest);
}
/**
* Adds multiple destinations to this XSLElement
*
* @param dests an array of Strings representing the file names to
* which this transformation should be written.
*
* @return a boolean indicating if this XSLElement was modified
*/
public boolean addDestinations(Destination [] dests) {
if (dests == null || dests.length == 0) {
throw new IllegalArgumentException("Destination array cannot be null or empty!");
}
boolean changed = false;
for (int i = 0; i < dests.length; i++) {
if (addDestination(dests[i])) {
changed = true;
}
}
return changed;
}
/**
* Adds multiple destinations to this XSLElement
*
* @param dests a Set of Strings representing the file names to
* which this transformation should be written.
*
* @return a boolean indicating if this XSLElement was modified
*/
public boolean addDestinations(Set dests) {
if (dests == null || dests.size() == 0) {
throw new IllegalArgumentException("Destination set cannot be null or empty!");
}
return destinations.addAll(dests);
}
/**
* Removes a destination from this XSLElement
*
* @param dest a String representing a destination file name that
* should be removed from this XSLElement.
*
* @return a boolean indicating if this XSLElement was modified
*/
public boolean removeDestination(Destination dest) {
if (dest == null) {
throw new IllegalArgumentException("Destination cannot be null!");
}
return this.destinations.remove(dest);
}
/**
* Removes multiple destinations from this XSLElement
*
* @param dests a String array representing destination file names
* that should be removed from this XSLElement.
*
* @return a boolean indicating if this XSLElement was modified
*/
public boolean removeDestinations(Destination [] dests) {
if (dests == null || dests.length == 0) {
throw new IllegalArgumentException("Destination array cannot be null or empty!");
}
boolean changed = false;
for (int i = 0; i < dests.length; i++) {
if (removeDestination(dests[i])) {
changed = true;
}
}
return changed;
}
/**
* Removes multiple destinations from this XSLElement
*
* @param dests a Set of Strings representing destination file
* names that should be removed from this XSLElement.
*
* @return a boolean indicating if this XSLElement was modified
*/
public boolean removeDestinations(Set dests) {
if (dests == null || dests.size() == 0) {
throw new IllegalArgumentException("Destination set cannot be null or empty!");
}
return destinations.removeAll(dests);
}
/**
* Determines whether the specified XSLElement is equal to this XSLElement
*
* @param xsl The Object representing the XSLElement to compare to
* this XSLElement.
*
* @return true if the specified XSLElement is equal to this
* XSLElement, and false otherwise.
*/
public boolean equals(Object xsl) {
if (xsl instanceof XSLElement) {
return this.xsl.equals(((XSLElement)xsl).getXSL()) && (this.interval == ((XSLElement)xsl).getInterval()) && this.destinations.equals(((XSLElement)xsl).getDestinations());
}
return false;
}
/**
* Returns the hashCode value of this XSLElement
*
* @return the int hashCode value of this XSLElement
*/
public int hashCode() {
return xsl.hashCode() + (interval * 7) + destinations.hashCode();
}
/**
* Returns a deep clone of this XSLElement
*
* @return an Object that is a deep clone of this XSLElement
*/
public Object clone() {
XSLElement XSLElementCopy = null;
try {
XSLElementCopy = (XSLElement) super.clone();
} catch (CloneNotSupportedException e) {
// this should never happen, so throw an Error which no one should be catching anyway
throw new InternalError(e.toString());
}
XSLElementCopy.setXSL(this.getXSL());
XSLElementCopy.setInterval(this.getInterval());
// make a deep copy of the Destination set
Set destinationSetCopy = new HashSet();
for (Iterator i = destinations.iterator(); i.hasNext();) {
Destination nextDestination = (Destination) i.next();
destinationSetCopy.add((Destination) nextDestination.clone());
}
XSLElementCopy.setDestinations(destinationSetCopy);
return XSLElementCopy;
}
}