[jboss-svn-commits] JBL Code SVN: r5273 - in labs/shotoku/trunk: shotoku-base/src/java/org/jboss/shotoku/tools shotoku-feeds/src/java/org/jboss/shotoku/feeds shotoku-feeds/src/java/org/jboss/shotoku/feeds/tag shotoku-feeds/src/java/org/jboss/shotoku/feeds/tools
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Tue Jul 25 10:53:10 EDT 2006
Author: adamw
Date: 2006-07-25 10:53:07 -0400 (Tue, 25 Jul 2006)
New Revision: 5273
Added:
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tag/AttributesTagHandler.java
Modified:
labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/tools/Tools.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/FeedsDescriptor.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tag/AggregatedFeedTagHandler.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tag/FeedTagHandler.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tag/FeedTagHandlerManager.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tag/RemoteFeedTagHandler.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tag/ShotokuFeedTagHandler.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tag/TagFeedTagHandler.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tools/FeedsConstants.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tools/FeedsTools.java
Log:
http://jira.jboss.org/jira/browse/JBSHOTOKU-36
Modified: labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/tools/Tools.java
===================================================================
--- labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/tools/Tools.java 2006-07-25 13:12:53 UTC (rev 5272)
+++ labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/tools/Tools.java 2006-07-25 14:53:07 UTC (rev 5273)
@@ -242,6 +242,19 @@
}
/**
+ * Converts the given object to a String in a null-safe way.
+ * @param o Object to convert.
+ * @return Result of o.toString() or null if o is null.
+ */
+ public static String toString(Object o) {
+ if (o == null) {
+ return null;
+ }
+
+ return o.toString();
+ }
+
+ /**
* Checks if two objects are equal - either both null, or
* their equals method returns true.
* @param obj1 First object to compare.
Modified: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/FeedsDescriptor.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/FeedsDescriptor.java 2006-07-25 13:12:53 UTC (rev 5272)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/FeedsDescriptor.java 2006-07-25 14:53:07 UTC (rev 5273)
@@ -69,7 +69,11 @@
*/
private Map<String, Map<String, SpecialFeed>> specialFeeds;
- private Map<String, Map<String, Object>> properties;
+ /**
+ * Properties: sets of attributes which can be used in
+ * feeds.
+ */
+ private Map<String, Map<String, Object>> attributes;
private VariableResolverManager vrm;
@@ -98,7 +102,7 @@
// location in Shotoku repositories.
Properties props = getProperties();
- // From properties read from Shotoku, getting the content manager
+ // From attributes read from Shotoku, getting the content manager
// configuration for this feeds id.
ContentManager cm = ContentManager.getContentManager(
props.getProperty(id + FeedsConstants.FEEDID),
@@ -111,6 +115,7 @@
// Creating the maps that will hold the feeds.
feeds = new HashMap<String, Map<String, NormalFeed>>();
specialFeeds = new HashMap<String, Map<String, SpecialFeed>>();
+ attributes = new HashMap<String, Map<String, Object>>();
// Looking for file containing feed definitons and filling the maps.
for (org.jboss.shotoku.Node feedNode :
@@ -205,6 +210,15 @@
}
/**
+ * Adds an attributes map with the given name.
+ * @param name
+ * @param props
+ */
+ public void addAttributes(String name, Map<String, Object> props) {
+ attributes.put(name, props);
+ }
+
+ /**
* Tries to generate a feed with the given name. If the feed is absent,
* or is being already generated (this case indicates a loop in feed
* definitions), no actions are taken.
@@ -275,6 +289,14 @@
return ret;
}
+ /**
+ * @param name
+ * @return An attributes map with the given name.
+ */
+ public Map<String, Object> getAttributes(String name) {
+ return attributes.get(name);
+ }
+
public VariableResolverManager getVariableResolverManager() {
return vrm;
}
Modified: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tag/AggregatedFeedTagHandler.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tag/AggregatedFeedTagHandler.java 2006-07-25 13:12:53 UTC (rev 5272)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tag/AggregatedFeedTagHandler.java 2006-07-25 14:53:07 UTC (rev 5273)
@@ -23,13 +23,17 @@
super(feedName, group, definition, s);
}
- public boolean generate(FeedsDescriptor fd) {
+ public boolean generate(FeedsDescriptor fd) {
+ if (!generateAttributes(fd)) {
+ return false;
+ }
+
String feedName = getFeedName();
String feedType = getFeedType();
Node definition = getDefinition();
// Reading all attributes and making the substitution on them.
- Map<String, String> attributes = getAttributes();
+ Map<String, Object> attributes = getAttributes();
// First, getting (and if need be, generating) the feeds to be
// aggregated as written in the tag.
@@ -76,13 +80,15 @@
}
NormalFeed feed = FeedFactory.getNewNormalFeed(feedName,
- attributes.get(FeedsConstants.DISP_NAME_ATTR),
+ Tools.toString(attributes.get(FeedsConstants.DISP_NAME_ATTR)),
feedType);
try {
feed.generate(toAggregate.toArray(new NormalFeed[0]),
- attributes.get("title"), attributes.get("description"),
- attributes.get("author"), generateFeedLink(fd, true, feedName, feedType));
+ Tools.toString(attributes.get("title")),
+ Tools.toString(attributes.get("description")),
+ Tools.toString(attributes.get("author")),
+ generateFeedLink(fd, true, feedName, feedType));
} catch (IOException e) {
log.error("Can't add aggregated feed " + feedName + " : " + feedType + ".", e);
}
Added: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tag/AttributesTagHandler.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tag/AttributesTagHandler.java 2006-07-25 13:12:53 UTC (rev 5272)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tag/AttributesTagHandler.java 2006-07-25 14:53:07 UTC (rev 5273)
@@ -0,0 +1,43 @@
+package org.jboss.shotoku.feeds.tag;
+
+import org.apache.log4j.Logger;
+import org.apache.velocity.VelocityContext;
+import org.jboss.shotoku.feeds.FeedsDescriptor;
+import org.jboss.shotoku.feeds.FeedFactory;
+import org.jboss.shotoku.feeds.data.NormalFeed;
+import org.jboss.shotoku.feeds.tools.FeedsConstants;
+import org.jboss.shotoku.feeds.tools.FeedsTools;
+import org.jboss.shotoku.feeds.variables.Substitution;
+import org.jboss.shotoku.search.*;
+import org.jboss.shotoku.tools.Tools;
+import org.jboss.shotoku.ContentManager;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+import java.lang.reflect.Constructor;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Date;
+import java.io.Writer;
+
+/**
+ * @author Adam Warski (adamw at aster.pl)
+ */
+public class AttributesTagHandler extends FeedTagHandler {
+ private static final Logger log = Logger.getLogger(FeedsDescriptor.class);
+
+ public AttributesTagHandler(String feedName, String group, Node definition,
+ Substitution s) {
+ super(feedName, group, definition, s);
+ }
+
+ public boolean generate(FeedsDescriptor fd) {
+ if (!generateAttributes(fd)) {
+ return false;
+ }
+
+ fd.addAttributes(getFeedName(), getAttributes());
+
+ return true;
+ }
+}
Modified: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tag/FeedTagHandler.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tag/FeedTagHandler.java 2006-07-25 13:12:53 UTC (rev 5272)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tag/FeedTagHandler.java 2006-07-25 14:53:07 UTC (rev 5273)
@@ -4,9 +4,12 @@
import org.jboss.shotoku.feeds.variables.Substitution;
import org.jboss.shotoku.feeds.FeedsDescriptor;
import org.jboss.shotoku.feeds.tools.FeedsConstants;
+import org.jboss.shotoku.feeds.tools.FeedsTools;
import org.jboss.shotoku.tools.Tools;
+import org.apache.log4j.Logger;
import java.util.Map;
+import java.util.HashMap;
/**
* A base class for classes which handle generation of various feed types,
@@ -14,11 +17,13 @@
* @author Adam Warski (adamw at aster.pl)
*/
public abstract class FeedTagHandler {
+ private static final Logger log = Logger.getLogger(FeedsDescriptor.class);
+
private String feedName;
private String group;
private Node definition;
private Substitution s;
- private Map<String, String> attributes;
+ private Map<String, Object> attributes;
public FeedTagHandler(String feedName, String group, Node definition,
Substitution s) {
@@ -27,8 +32,11 @@
this.definition = definition;
this.s = s;
- attributes = Tools.getMapFromNodeAttributes(definition);
- s.make(attributes);
+ Map<String, String> attributesStrings =
+ Tools.getMapFromNodeAttributes(definition);
+ s.make(attributesStrings);
+
+ attributes = FeedsTools.generateAttributes(attributesStrings);
}
public String getFeedName() {
@@ -51,11 +59,44 @@
return s.getSubstitutionFor(FeedsConstants.TYPE_VARIABLE);
}
- public Map<String, String> getAttributes() {
+ public Map<String, Object> getAttributes() {
return attributes;
}
/**
+ * Generates attributes for this tag basing on the ones in the node
+ * and the ones in any possible super-attributes map.
+ * @param fd
+ * @return True iff generation was successfull, that is, all dependencies
+ * have been met.
+ */
+ public boolean generateAttributes(FeedsDescriptor fd) {
+ Object extendsObj = attributes.get(FeedsConstants.EXTENDS_ATTR);
+ if (extendsObj == null) {
+ return true;
+ }
+
+ String extendsStr = extendsObj.toString();
+ Map<String, Object> superAttributes = fd.getAttributes(extendsStr);
+
+ if (superAttributes == null) {
+ if (!fd.generateFeed(extendsStr)) {
+ log.error("Unable to read super attributes set: " + extendsStr);
+ return false;
+ }
+
+ superAttributes = fd.getAttributes(extendsStr);
+ }
+
+ HashMap<String, Object> tempAttrs =
+ new HashMap<String, Object>(superAttributes);
+ tempAttrs.putAll(attributes);
+ attributes = tempAttrs;
+
+ return true;
+ }
+
+ /**
* Generates a feed handled by this instance. The generated feed should
* be added to the FeedsDescriptor by calling the FeedsDescriptor.addFeed()
* method.
@@ -65,17 +106,17 @@
public abstract boolean generate(FeedsDescriptor fd);
/**
- * Generates a link to a specified feed. If the link is to be full, an
- * absolute address is returned. Otherwise, a relative one.
+ * Generates a link to a specified feed. If the link is to be full, an
+ * absolute address is returned. Otherwise, a relative one.
* @param fd
- * @param full
- * @param feedName
- * @param feedType
- * @return Link to the given feed.
- */
- protected String generateFeedLink(FeedsDescriptor fd, boolean full, String feedName,
+ * @param full
+ * @param feedName
+ * @param feedType
+ * @return Link to the given feed.
+ */
+ protected String generateFeedLink(FeedsDescriptor fd, boolean full, String feedName,
String feedType) {
- return (full ? getAttributes().get(FeedsConstants.BASE_ADDR_ATTR) : "")
+ return (full ? getAttributes().get(FeedsConstants.BASE_ADDR_ATTR) : "")
+ "/feeds/" + feedName + "/" + feedType;
- }
+ }
}
Modified: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tag/FeedTagHandlerManager.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tag/FeedTagHandlerManager.java 2006-07-25 13:12:53 UTC (rev 5272)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tag/FeedTagHandlerManager.java 2006-07-25 14:53:07 UTC (rev 5273)
@@ -45,6 +45,7 @@
addHandler(FeedsConstants.REM_FEED_TAG, RemoteFeedTagHandler.class);
addHandler(FeedsConstants.SHO_FEED_TAG, ShotokuFeedTagHandler.class);
addHandler(FeedsConstants.TAG_FEED_TAG, TagFeedTagHandler.class);
+ addHandler(FeedsConstants.ATTRS_TAG, AttributesTagHandler.class);
}
/**
Modified: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tag/RemoteFeedTagHandler.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tag/RemoteFeedTagHandler.java 2006-07-25 13:12:53 UTC (rev 5272)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tag/RemoteFeedTagHandler.java 2006-07-25 14:53:07 UTC (rev 5273)
@@ -6,6 +6,7 @@
import org.jboss.shotoku.feeds.FeedFactory;
import org.jboss.shotoku.feeds.tools.FeedsConstants;
import org.jboss.shotoku.feeds.FeedsDescriptor;
+import org.jboss.shotoku.tools.Tools;
import org.apache.log4j.Logger;
import java.net.URL;
@@ -21,11 +22,16 @@
}
public boolean generate(FeedsDescriptor fd) {
+ if (!generateAttributes(fd)) {
+ return false;
+ }
+
String feedName = getFeedName();
String feedType = getFeedType();
- String address = getAttributes().get("address");
- String displayName = getAttributes().get(FeedsConstants.DISP_NAME_ATTR);
+ String address = Tools.toString(getAttributes().get("address"));
+ String displayName = Tools.toString(getAttributes().get(
+ FeedsConstants.DISP_NAME_ATTR));
try {
NormalFeed feed = FeedFactory.getNewNormalFeed(feedName, displayName, feedType);
Modified: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tag/ShotokuFeedTagHandler.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tag/ShotokuFeedTagHandler.java 2006-07-25 13:12:53 UTC (rev 5272)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tag/ShotokuFeedTagHandler.java 2006-07-25 14:53:07 UTC (rev 5273)
@@ -108,21 +108,22 @@
}
public boolean generate(FeedsDescriptor fd) {
+ if (!generateAttributes(fd)) {
+ return false;
+ }
+
// Reading all attributes and making the substitution on them.
- Map<String, String> attributes = getAttributes();
+ Map<String, Object> attributes = getAttributes();
- String template = attributes.get("template");
- String displayName = attributes.get(FeedsConstants.DISP_NAME_ATTR);
+ String template = Tools.toString(attributes.get("template"));
+ String displayName = Tools.toString(attributes.get(
+ FeedsConstants.DISP_NAME_ATTR));
- ContentManager cm = ContentManager.getContentManager(attributes.get("id"),
- attributes.get("prefix"));
+ ContentManager cm = ContentManager.getContentManager(
+ Tools.toString(attributes.get("id")),
+ Tools.toString(attributes.get("prefix")));
- // Creating a velocity context with parts that won't change in all
- // generated feeds.
- Map<String, Object> velocityAttributes =
- FeedsTools.generateVelocityAttributes(attributes);
-
- VelocityContext vc = new VelocityContext(velocityAttributes);
+ VelocityContext vc = new VelocityContext(attributes);
FeedsTools.prepareVelocityContext(attributes, vc);
NodeList feedNodeChildren = getDefinition().getChildNodes();
Modified: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tag/TagFeedTagHandler.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tag/TagFeedTagHandler.java 2006-07-25 13:12:53 UTC (rev 5272)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tag/TagFeedTagHandler.java 2006-07-25 14:53:07 UTC (rev 5273)
@@ -21,15 +21,16 @@
}
public boolean generate(FeedsDescriptor fd) {
+ if (!generateAttributes(fd)) {
+ return false;
+ }
+
// Reading all attributes and making the substitution on them.
- Map<String, String> attributes = getAttributes();
+ Map<String, Object> attributes = getAttributes();
- Map<String, Object> velocityAttributes =
- FeedsTools.generateVelocityAttributes(attributes);
-
// Creating a velocity context with parts that won't change in all
// generated feeds.
- VelocityContext vc = new VelocityContext(velocityAttributes);
+ VelocityContext vc = new VelocityContext(attributes);
FeedsTools.prepareVelocityContext(attributes, vc);
String feedName = getFeedName();
Modified: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tools/FeedsConstants.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tools/FeedsConstants.java 2006-07-25 13:12:53 UTC (rev 5272)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tools/FeedsConstants.java 2006-07-25 14:53:07 UTC (rev 5273)
@@ -21,6 +21,7 @@
public final static String AGG_FEED_TAG = "aggregated-feed";
public final static String SHO_FEED_TAG = "shotoku-feed";
public final static String TAG_FEED_TAG = "tag-feed";
+ public final static String ATTRS_TAG = "attributes";
/*
* Date format compatible with RFC822.
@@ -78,6 +79,7 @@
public static final String NAME_ATTR = "name";
public static final String DISP_NAME_ATTR = "displayName";
public static final String BASE_ADDR_ATTR = "baseServerAddress";
+ public static final String EXTENDS_ATTR = "extends";
/**
* Xml node attribute prefix denoting an attribute for which
Modified: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tools/FeedsTools.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tools/FeedsTools.java 2006-07-25 13:12:53 UTC (rev 5272)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tools/FeedsTools.java 2006-07-25 14:53:07 UTC (rev 5273)
@@ -6,6 +6,7 @@
import org.jboss.shotoku.feeds.FeedsService;
import org.jboss.shotoku.feeds.service.FeedsServiceImpl;
import org.jboss.shotoku.ContentManager;
+import org.jboss.shotoku.tools.Tools;
import org.jboss.mx.util.MBeanProxyExt;
import org.jboss.mx.util.MBeanServerLocator;
@@ -25,17 +26,19 @@
* to be loaded, puts in the resulting map a new (key, object of that
* class) binding, and otherwise, just copies the entry from the
* original map.
+ * @param initial Initial attributes to which new ones shall be
+ * added.
* @param attributes Original attributes to be copied/ expanded.
* @return A map of key-value bindings copied from the original map,
* or key-object of class taken from key bindings.
*/
- public static Map<String, Object> generateVelocityAttributes(
+ public static Map<String, Object> generateAttributes(
+ Map<String, Object> initial,
Map<String, String> attributes) {
- Map<String, Object> velocityAttributes = new HashMap<String, Object>();
for (String key : attributes.keySet()) {
if (key.startsWith(FeedsConstants.CLASS_ATTR_PREFIX)) {
try {
- velocityAttributes.put(
+ initial.put(
key.substring(FeedsConstants.CLASS_ATTR_PREFIX.length()),
Class.forName(attributes.get(key)).newInstance());
} catch (Exception e) {
@@ -43,14 +46,29 @@
"velocity context: " + key, e);
}
} else {
- velocityAttributes.put(key, attributes.get(key));
+ initial.put(key, attributes.get(key));
}
}
- return velocityAttributes;
+ return initial;
}
/**
+ * For each attribute from the given map, if its key denotes a class
+ * to be loaded, puts in the resulting map a new (key, object of that
+ * class) binding, and otherwise, just copies the entry from the
+ * original map.
+ * @param attributes Original attributes to be copied/ expanded.
+ * @return A map of key-value bindings copied from the original map,
+ * or key-object of class taken from key bindings.
+ */
+ public static Map<String, Object> generateAttributes(
+ Map<String, String> attributes) {
+ return generateAttributes(new HashMap<String, Object>(),
+ attributes);
+ }
+
+ /**
* Basing on the given attributes, puts in the given velocity context
* common attributez: date formats, current date and a text-escaping
* object.
@@ -58,11 +76,13 @@
* should be set
* @param vc Velocity context to fill.
*/
- public static void prepareVelocityContext(Map<String, String> attributes,
+ public static void prepareVelocityContext(Map<String, Object> attributes,
VelocityContext vc) {
- String dateFormat = attributes.get("dateFormat");
- vc.put("dateFormat", new SimpleDateFormat(dateFormat == null ? "MM/dd/yy" : dateFormat));
- vc.put("rssDateFormat", new SimpleDateFormat(FeedsConstants.RFC_822_DATE));
+ String dateFormat = Tools.toString(attributes.get("dateFormat"));
+ vc.put("dateFormat", new SimpleDateFormat(
+ dateFormat == null ? "MM/dd/yy" : dateFormat));
+ vc.put("rssDateFormat",
+ new SimpleDateFormat(FeedsConstants.RFC_822_DATE));
vc.put("rdfDateFormat", new RdfDateFormat());
vc.put("now", Calendar.getInstance().getTime());
vc.put("escape", new TextEscaping());
More information about the jboss-svn-commits
mailing list