= das: Data Structures Xephyrus Tag Libraries
Version 1.5 Usage Guide
Introduction

The Xephyrus Data Structures Tag Library (das) provides an easy way to create and manipulate the contents of common Java data-structures such as maps and lists using JSP tags.

Architecture

Within the das tag library there are two classes of tags: container tags and worker tags. Container tags define the specific structure being manipulated, including everything needed to create the underlying data object if needed. Worker tags do the data manipulation. Worker tags must be nested in container tags.

CONTAINER TAG
WORKER TAG
 
<das:map id="mymap">
<das:set key="thekey" value="thevalue" />
</das:map>

The hierarchy of the code underlying the tags is also classified by these types. The container tag hierarchical tree looks like so:

Container Class Hierarchy

Similarly the worker tag hierarchical tree is like this:

Worker Class Hierarchy


 
List Container Use Cases
Simple List Use Case
References: das:list, das:add, das:get

In the simplest case of using a list, one would put some values into the list, then get them out. In this simple example we create a list with a list tag populate it with the names of some colors. That list is then used with the get tag to populate this drop down box with those color names.

<%-- create & populate a list --%> <das:list id="colors"> <das:add value="black" /> <das:add value="blue" /> <das:add value="green" /> <das:add value="red" /> <das:add value="cyan" /> <das:add value="purple" /> <das:add value="yellow" /> <das:add value="white" /> </das:list> <%-- build a form --%> <p> <select name="selectcolor"> <das:list id="colors"> <das:get loop="true" var="oneColor"> <option>${pageScope.oneColor}</option> </das:get> </das:list> </select> </p>

Getting List Values
References: das:list, das:get, EL⇒

There are a few details and variations on getting a value from a list. For example, you could get an item the get tag or using the JSP Expression Language or even (shudder) using Java scripting within the JSP. Here we reference list elements in various ways for grins and giggles.

Via a das:get tag:

<das:list id="colors"> <das:get index="0" /> </das:list>

Via an indexed EL statement:

${pageScope.colors[1]}

Via a das:get tag using an intermediary variable:

<das:list id="colors"> <das:get index="2" var="myvar" /> </das:list> ${pageScope.myvar}

Via a looped das:get tag using a looping variable:

<das:list id="colors"> <das:get loop="true" var="innerbit"> ${pageScope.innerbit} </das:get> </das:list>

Removing List Values
References: das:list, das:remove

To remove an element from a list, the das:remove tag is used. Since the element removed is retrieved during the removal, that element can be spewn to the output or stashed in a variable. To remove an element silently, assign it to a junk variable. If you haven't already noticed, elements in lists use a zero-based index. That won't be a surprise to Java programmers.

Remove the 5th element and write it to the output stream:

<das:list id="colors"> <das:remove index="4" /> </das:list>

Remove the 7th element and stash it in a variable:

<das:list id="colors"> <das:remove index="6" var="gone" /> </das:list>

Getting the Size of a List
References: das:list, das:size

The size of any given list can be spouted directly to the output stream or stuffed in a variable. Here's an example of spoutage:

<das:list id="colors"> The size of the <b>colors</b> list is: <das:size />. </das:list>

Sorting Lists
References: das:list, das:add, das:get

Here we will throw a list together which is sorted. Since sorting happens in the container itself, all other operations work the same way as if the list wasn't sorted (except insofar as the order of the list elements is concerned, of course).

<%-- create & populate a list --%> <das:list id="colors" sort="ascending"> <das:add value="black" /> <das:add value="blue" /> <das:add value="green" /> <das:add value="red" /> <das:add value="cyan" /> <das:add value="purple" /> <das:add value="yellow" /> <das:add value="white" /> </das:list> <%-- build a form --%> <p> <select name="selectcolor"> <das:list id="colors"> <das:get loop="true" var="oneColor"> <option>${pageScope.oneColor}</option> </das:get> </das:list> </select> </p>

Using a Custom List Object

Any external class can be referenced as a list by the das tags as long as it implements the Collections interface. To do so, just create an object using the jsp:useBean tag, or even (shudder) java scripting. Then reference it from any das:list tag for all the das coolness. Like so:

<%-- create the list --%> <jsp:useBean id="grins" class="java.util.Vector" /> <%-- populate the list --%> <das:list id="grins"> <das:add value="Duchenne Smile" /> <das:add value="Pan American Smile" /> <das:add value="Cheshire Grin" /> <das:add value="Columbian Grin" /> </das:list> <%-- build a form --%> <p> <select name="selectgrin"> <das:list id="grins"> <das:get loop="true" var="oneGrin"> <option>${pageScope.oneGrin}</option> </das:get> </das:list> </select> </p>


 
Map Container Use Cases
Simple Map Use Case
References: das:map, das:add, das:get

Similarly to the Simple List Use Case, in this simple example we create a map with a map tag and populate it with names and web-codes of some colors. That map is then used with the get tag to populate this drop down box with those color names and values.

<%-- create & populate a map --%> <das:map id="colors"> <das:add key="black" value="#000000" /> <das:add key="blue" value="#0000FF" /> <das:add key="green" value="#00FF00" /> <das:add key="red" value="#FF0000" /> <das:add key="cyan" value="#00FFFF" /> <das:add key="purple" value="#FF00FF" /> <das:add key="yellow" value="#FFFF00" /> <das:add key="white" value="#FFFFFF" /> </das:map> <%-- build a form --%> <p> <select name="selectcolor"> <das:map id="colors"> <das:get loop="true" var="colorKey"> <das:get loop="false" key="${pageScope.colorKey}" var="colorCode" /> <option value="${pageScope.colorCode}">${pageScope.colorKey}</option> </das:get> </das:map> </select> </p>

Getting Map Values
References: das:map, das:get, EL⇒

One of the most important aspects of any given container, be that container physical, logical or even metaphorical, is the ability to retreive what which has previously been placed in that container. That is to say, of what use is a container if the items placed within that container cannot be retreived. Well, surely one could find a use for such a container, but then that container would very likely not be called a container in the first place but something else entirely since the very defining qualities that would have made it a container are not present within it.

But I digress...

In this instance, the instance of a map container within the das tag library, there are several ways through which one could retreive any given value. Following are several examples:

Via a das:get tag:

<das:map id="colors"> <das:get key="black" /> </das:map>

Via a keyed EL expression:

${pageScope.colors['blue']}

Via a das:get tag and an intermediary variable:

<das:map id="colors"> <das:get key="green" var="intermediary" /> </das:map> ${pageScope.intermediary}

Via a looped das:get tag and an looping variable:

<das:map id="colors"> <das:get loop="true" var="innerbit"> ${pageScope.innerbit} </das:get> </das:map>

Removing Map Mappings
References: das:map, das:remove

The following examples demonstrate removals. When you remove a mapping from a map container, that mapping is also retreived. So, you can either spew it out to the world, or shove it into a variable. If the mapping had more than one value associated with it ALL of the values will be removed using these methods.

Remove the mapping named "green" and write it to the output stream:

<das:map id="colors"> <das:remove key="green" /> </das:map>

Remove the mapping named "purple" and shove it into a nice little variable:

<das:map id="colors"> <das:remove key="red" var="gone" /> </das:map>

Getting the Size of a Map
References: das:map, das:size

The size of a map is determined to be the number of keys in that map. That count can be spewed directly to the output stream or stuffed into a variable. For example:

<das:map id="colors"> The size of the <b>colors</b> map is: <das:size />. </das:map>

Sorting Maps
References: das:map, das:add, das:get

Here we will throw together a map that is sorted. Maps are sorted by key. This map we will sort in descending order. Since this sorting happens within the container itself, all other operations work the same way as if the map were not sorted.

<%-- create & populate a list --%> <das:map id="colors" sort="descending"> <das:add key="black" value="#000000" /> <das:add key="blue" value="#0000FF" /> <das:add key="green" value="#00FF00" /> <das:add key="red" value="#FF0000" /> <das:add key="cyan" value="#00FFFF" /> <das:add key="purple" value="#FF00FF" /> <das:add key="yellow" value="#FFFF00" /> <das:add key="white" value="#FFFFFF" /> </das:map> <%-- build a form --%> <p> <select name="selectcolor"> <das:map id="colors"> <das:get loop="true" var="colorKey"> <das:get loop="false" key="${pageScope.colorKey}" var="colorCode" /> <option value="${pageScope.colorCode}">${pageScope.colorKey}</option> </das:get> </das:map> </select> </p>

Using a Custom Map Object

Any external class can be referenced as a map from the das tags as long as it implements the Map interface. To do this, create an using the jsp:useBean tag. (You could do it from a Java script block if you really wanted, and you could sell drugs to school kids while you're at it.) Once that object is created by whatever faustian bargain you manage to obtain you can reference it from any das:map tag. Like so:

<%-- create the list --%> <jsp:useBean id="grins" class="java.util.HashMap" /> <%-- populate the list --%> <das:map id="grins"> <das:add key="involuntary" value="Duchenne Smile" /> <das:add key="corporate" value="Pan American Smile" /> <das:add key="mischievious" value="Cheshire Grin" /> <das:add key="deadly" value="Columbian Grin" /> </das:map> <%-- build a form --%> <p> <select name="selectgrin"> <das:map id="grins"> <das:get loop="true" var="grinKey"> <das:get loop="false" key="${pageScope.grinKey}" var="grinCode" /> <option value="${pageScope.grinKey}">${pageScope.grinCode}</option> </das:get> </das:map> </select> </p>