KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > datatype > FlowJXPathSelectionList


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.woody.datatype;
17
18 import java.util.Iterator JavaDoc;
19 import java.util.Locale JavaDoc;
20
21 import org.apache.avalon.framework.context.Context;
22 import org.apache.cocoon.components.ContextHelper;
23 import org.apache.cocoon.components.flow.FlowHelper;
24 import org.apache.cocoon.woody.Constants;
25 import org.apache.cocoon.xml.AttributesImpl;
26 import org.apache.commons.jxpath.JXPathContext;
27 import org.apache.commons.jxpath.Pointer;
28 import org.apache.excalibur.xml.sax.XMLizable;
29
30 import org.xml.sax.ContentHandler JavaDoc;
31 import org.xml.sax.SAXException JavaDoc;
32
33 /**
34  * A selection list that takes its values from the flow page data.
35  *
36  * @see org.apache.cocoon.woody.datatype.FlowJXPathSelectionListBuilder
37  * @author <a HREF="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
38  * @version CVS $Id: FlowJXPathSelectionList.java 30932 2004-07-29 17:35:38Z vgritsenko $
39  */

40 public class FlowJXPathSelectionList implements SelectionList {
41
42     private Context context;
43     private String JavaDoc listPath;
44     private String JavaDoc valuePath;
45     private String JavaDoc labelPath;
46     private Datatype datatype;
47     private Object JavaDoc model;
48
49     public FlowJXPathSelectionList(Context context, String JavaDoc listPath, String JavaDoc valuePath, String JavaDoc labelPath, Datatype datatype) {
50         this.context = context;
51         this.listPath = listPath;
52         this.valuePath = valuePath;
53         this.labelPath = labelPath;
54         this.datatype = datatype;
55     }
56
57     /**
58      * Builds a dynamic selection list from an in-memory collection.
59      * @see org.apache.cocoon.woody.formmodel.Field#setSelectionList(Object model, String valuePath, String labelPath)
60      * @param model The collection used as a model for the selection list.
61      * @param valuePath An XPath expression referring to the attribute used
62      * to populate the values of the list's items.
63      * @param labelPath An XPath expression referring to the attribute used
64      * to populate the labels of the list's items.
65      * @param datatype
66      */

67     public FlowJXPathSelectionList(Object JavaDoc model, String JavaDoc valuePath, String JavaDoc labelPath, Datatype datatype) {
68         this.model = model;
69         this.valuePath = valuePath;
70         this.labelPath = labelPath;
71         this.datatype = datatype;
72     }
73
74     public Datatype getDatatype() {
75         return this.datatype;
76     }
77
78     public void generateSaxFragment(ContentHandler JavaDoc contentHandler, Locale JavaDoc locale) throws SAXException JavaDoc {
79         JXPathContext ctx = null;
80         Iterator JavaDoc iter = null;
81         if (model == null) {
82             Object JavaDoc flowData = FlowHelper.getContextObject(ContextHelper.getObjectModel(this.context));
83             if (flowData == null) {
84                 throw new SAXException JavaDoc("No flow data to produce selection list");
85             }
86
87             // Move to the list location
88
ctx = JXPathContext.newContext(flowData);
89
90             // Iterate on all elements of the list
91
iter = ctx.iteratePointers(this.listPath);
92         } else {
93             // Move to the list location
94
ctx = JXPathContext.newContext(model);
95
96             // Iterate on all elements of the list
97
iter = ctx.iteratePointers(".");
98         }
99
100         // Start the selection-list
101
contentHandler.startElement(Constants.WI_NS, SELECTION_LIST_EL, Constants.WI_PREFIX_COLON + SELECTION_LIST_EL, Constants.EMPTY_ATTRS);
102
103         while(iter.hasNext()) {
104             String JavaDoc stringValue = "";
105             Object JavaDoc label = null;
106
107             // Get a context on the current item
108
Pointer ptr = (Pointer)iter.next();
109             if (ptr.getValue() != null) {
110                 JXPathContext itemCtx = ctx.getRelativeContext(ptr);
111
112                 // Get the value as a string
113
Object JavaDoc value = itemCtx.getValue(this.valuePath);
114
115                 // List may contain null value, and (per contract with convertors),
116
// convertors are not invoked on nulls.
117
if (value != null) {
118                     stringValue = this.datatype.convertToString(value, locale);
119                 }
120
121                 // Get the label (can be ommitted)
122
itemCtx.setLenient(true);
123                 label = itemCtx.getValue(this.labelPath);
124                 if (label == null) {
125                     label = stringValue;
126                 }
127             }
128
129             // Output this item
130
AttributesImpl itemAttrs = new AttributesImpl();
131             itemAttrs.addCDATAAttribute("value", stringValue);
132             contentHandler.startElement(Constants.WI_NS, ITEM_EL, Constants.WI_PREFIX_COLON + ITEM_EL, itemAttrs);
133             if (label != null) {
134                 contentHandler.startElement(Constants.WI_NS, LABEL_EL, Constants.WI_PREFIX_COLON + LABEL_EL, Constants.EMPTY_ATTRS);
135                 if (label instanceof XMLizable) {
136                     ((XMLizable)label).toSAX(contentHandler);
137                 } else {
138                     String JavaDoc stringLabel = label.toString();
139                     contentHandler.characters(stringLabel.toCharArray(), 0, stringLabel.length());
140                 }
141                 contentHandler.endElement(Constants.WI_NS, LABEL_EL, Constants.WI_PREFIX_COLON + LABEL_EL);
142             }
143             contentHandler.endElement(Constants.WI_NS, ITEM_EL, Constants.WI_PREFIX_COLON + ITEM_EL);
144         }
145
146         // End the selection-list
147
contentHandler.endElement(Constants.WI_NS, SELECTION_LIST_EL, Constants.WI_PREFIX_COLON + SELECTION_LIST_EL);
148     }
149 }
150
Popular Tags