KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > 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.forms.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.forms.FormsConstants;
25 import org.apache.cocoon.forms.util.I18nMessage;
26 import org.apache.cocoon.xml.AttributesImpl;
27 import org.apache.cocoon.xml.XMLUtils;
28 import org.apache.commons.jxpath.JXPathContext;
29 import org.apache.commons.jxpath.Pointer;
30 import org.apache.excalibur.xml.sax.XMLizable;
31
32 import org.xml.sax.ContentHandler JavaDoc;
33 import org.xml.sax.SAXException JavaDoc;
34
35 /**
36  * A selection list that takes its values from the flow page data.
37  *
38  * @see org.apache.cocoon.forms.datatype.FlowJXPathSelectionListBuilder
39  * @author <a HREF="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
40  * @version $Id: FlowJXPathSelectionList.java 326838 2005-10-20 06:26:53Z sylvain $
41  */

42 public class FlowJXPathSelectionList implements SelectionList {
43
44     private Context context;
45     private String JavaDoc listPath;
46     private String JavaDoc valuePath;
47     private String JavaDoc labelPath;
48     private Datatype datatype;
49     private Object JavaDoc model;
50     private boolean nullable = false;
51     private String JavaDoc nullText;
52     private boolean nullTextIsI18nKey = false;
53     private String JavaDoc i18nCatalog;
54     private boolean labelIsI18nKey = false;
55
56     public FlowJXPathSelectionList(Context context,
57                                    String JavaDoc listPath,
58                                    String JavaDoc valuePath,
59                                    String JavaDoc labelPath,
60                                    Datatype datatype,
61                                    String JavaDoc nullText,
62                                    boolean nullTextIsI18nKey,
63                                    String JavaDoc i18nCatalog,
64                                    boolean labelIsI18nKey) {
65         this.context = context;
66         this.listPath = listPath;
67         this.valuePath = valuePath;
68         this.labelPath = labelPath;
69         this.datatype = datatype;
70         this.nullText = nullText;
71         this.nullable = (nullText != null);
72         this.nullTextIsI18nKey = nullTextIsI18nKey;
73         this.i18nCatalog =i18nCatalog;
74         this.labelIsI18nKey = labelIsI18nKey;
75     }
76
77     /**
78      * Builds a dynamic selection list from an in-memory collection.
79      * @see org.apache.cocoon.forms.formmodel.Field#setSelectionList(Object model, String valuePath, String labelPath)
80      * @param model The collection used as a model for the selection list.
81      * @param valuePath An XPath expression referring to the attribute used
82      * to populate the values of the list's items.
83      * @param labelPath An XPath expression referring to the attribute used
84      * to populate the labels of the list's items.
85      * @param datatype
86      */

87     public FlowJXPathSelectionList(Object JavaDoc model, String JavaDoc valuePath, String JavaDoc labelPath, Datatype datatype) {
88         this.model = model;
89         this.valuePath = valuePath;
90         this.labelPath = labelPath;
91         this.datatype = datatype;
92     }
93
94     public Datatype getDatatype() {
95         return this.datatype;
96     }
97
98     public void generateSaxFragment(ContentHandler JavaDoc contentHandler, Locale JavaDoc locale) throws SAXException JavaDoc {
99         JXPathContext ctx = null;
100         Iterator JavaDoc iter = null;
101         if (model == null) {
102             Object JavaDoc flowData = FlowHelper.getContextObject(ContextHelper.getObjectModel(this.context));
103             if (flowData == null) {
104                 throw new SAXException JavaDoc("No flow data to produce selection list");
105             }
106
107             // Move to the list location
108
ctx = JXPathContext.newContext(flowData);
109
110             // Iterate on all elements of the list
111
iter = ctx.iteratePointers(this.listPath);
112         } else {
113             // Move to the list location
114
ctx = JXPathContext.newContext(model);
115
116             // Iterate on all elements of the list
117
iter = ctx.iteratePointers(".");
118         }
119
120         // Start the selection-list
121
contentHandler.startElement(FormsConstants.INSTANCE_NS, SELECTION_LIST_EL, FormsConstants.INSTANCE_PREFIX_COLON + SELECTION_LIST_EL, XMLUtils.EMPTY_ATTRIBUTES);
122         if( this.nullable ) {
123             final AttributesImpl voidAttrs = new AttributesImpl( );
124             voidAttrs.addCDATAAttribute( "value", "" );
125             contentHandler.startElement( FormsConstants.INSTANCE_NS, ITEM_EL,
126                                          FormsConstants.INSTANCE_PREFIX_COLON +
127                                          ITEM_EL, voidAttrs );
128
129             if( this.nullText != null ) {
130                 contentHandler.startElement( FormsConstants.INSTANCE_NS, LABEL_EL,
131                                              FormsConstants.INSTANCE_PREFIX_COLON +
132                                              LABEL_EL, XMLUtils.EMPTY_ATTRIBUTES );
133
134                 if( this.nullTextIsI18nKey ) {
135                     if( ( this.i18nCatalog != null ) &&
136                         ( this.i18nCatalog.trim( ).length( ) > 0 ) ) {
137                         new I18nMessage( this.nullText, this.i18nCatalog ).toSAX( contentHandler );
138                     } else {
139                         new I18nMessage( this.nullText ).toSAX( contentHandler );
140                     }
141                 } else {
142                     contentHandler.characters( this.nullText.toCharArray( ), 0,
143                                                this.nullText.length( ) );
144                 }
145
146                 contentHandler.endElement( FormsConstants.INSTANCE_NS, LABEL_EL,
147                                            FormsConstants.INSTANCE_PREFIX_COLON +
148                                            LABEL_EL );
149             }
150
151             contentHandler.endElement( FormsConstants.INSTANCE_NS, ITEM_EL,
152                                        FormsConstants.INSTANCE_PREFIX_COLON +
153                                        ITEM_EL );
154         }
155
156         while(iter.hasNext()) {
157             String JavaDoc stringValue = "";
158             Object JavaDoc label = null;
159
160             // Get a context on the current item
161
Pointer ptr = (Pointer)iter.next();
162             if (ptr.getValue() != null) {
163                 JXPathContext itemCtx = ctx.getRelativeContext(ptr);
164
165                 // Get the value as a string
166
Object JavaDoc value = itemCtx.getValue(this.valuePath);
167
168                 // List may contain null value, and (per contract with convertors),
169
// convertors are not invoked on nulls.
170
if (value != null) {
171                     stringValue = this.datatype.convertToString(value, locale);
172                 }
173
174                 // Get the label (can be ommitted)
175
itemCtx.setLenient(true);
176                 label = itemCtx.getValue(this.labelPath);
177                 if (label == null) {
178                     label = stringValue;
179                 }
180             }
181
182             // Output this item
183
AttributesImpl itemAttrs = new AttributesImpl();
184             itemAttrs.addCDATAAttribute("value", stringValue);
185             contentHandler.startElement(FormsConstants.INSTANCE_NS, ITEM_EL, FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL, itemAttrs);
186             if (label != null) {
187                 contentHandler.startElement(FormsConstants.INSTANCE_NS, LABEL_EL, FormsConstants.INSTANCE_PREFIX_COLON + LABEL_EL, XMLUtils.EMPTY_ATTRIBUTES);
188                 if (label instanceof XMLizable) {
189                     ((XMLizable)label).toSAX(contentHandler);
190                 } else if( this.labelIsI18nKey ) {
191                     String JavaDoc stringLabel = label.toString();
192
193                     if( ( this.i18nCatalog != null ) &&
194                         ( this.i18nCatalog.trim( ).length( ) > 0 ) ) {
195                         new I18nMessage( stringLabel, this.i18nCatalog ).toSAX( contentHandler );
196                     } else {
197                         new I18nMessage( stringLabel ).toSAX( contentHandler );
198                     }
199                 } else {
200                     String JavaDoc stringLabel = label.toString();
201                     contentHandler.characters(stringLabel.toCharArray(), 0, stringLabel.length());
202                 }
203                 contentHandler.endElement(FormsConstants.INSTANCE_NS, LABEL_EL, FormsConstants.INSTANCE_PREFIX_COLON + LABEL_EL);
204             }
205             contentHandler.endElement(FormsConstants.INSTANCE_NS, ITEM_EL, FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL);
206         }
207
208         // End the selection-list
209
contentHandler.endElement(FormsConstants.INSTANCE_NS, SELECTION_LIST_EL, FormsConstants.INSTANCE_PREFIX_COLON + SELECTION_LIST_EL);
210     }
211 }
212
Popular Tags