KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > datatype > FlowJXPathSelectionListBuilder


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 org.apache.avalon.framework.context.Context;
19 import org.apache.avalon.framework.context.ContextException;
20 import org.apache.avalon.framework.context.Contextualizable;
21 import org.apache.cocoon.forms.FormsConstants;
22 import org.apache.cocoon.forms.util.DomHelper;
23 import org.w3c.dom.Element JavaDoc;
24
25 import java.util.Map JavaDoc;
26
27 /**
28  * Builds a selection list that will take its values from the flow page data.
29  * The items list and, for each item, its value and label, are fetched using
30  * JXPath expressions.
31  *
32  * <p>If an item has no label, its value is used as the label.
33  *
34  * <p>Example:
35  * <pre>
36  * &lt;fd:selection-list type="flow-jxpath"
37  * list-path="selectList" value-path="value" label-path="label"/gt;
38  * </pre>
39  * Flow script:
40  * <pre>
41  * var data = {
42  * selectList: [{value:3, label:"three"}, {value:4}]
43  * };
44  * form.showForm("form.html", data);
45  * </pre>
46  *
47  * @see org.apache.cocoon.forms.datatype.FlowJXPathSelectionList
48  * @author <a HREF="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
49  * @version $Id: FlowJXPathSelectionListBuilder.java 326838 2005-10-20 06:26:53Z sylvain $
50  */

51 public class FlowJXPathSelectionListBuilder implements SelectionListBuilder, Contextualizable {
52
53     private Context context;
54
55     public void contextualize(Context context) throws ContextException {
56         this.context = context;
57     }
58
59     public SelectionList build(Element JavaDoc selectionListElement, Datatype datatype) throws Exception JavaDoc {
60
61         String JavaDoc listPath = DomHelper.getAttribute(selectionListElement, "list-path");
62         String JavaDoc valuePath = DomHelper.getAttribute(selectionListElement, "value-path");
63         Map JavaDoc nspfx = DomHelper.getInheritedNSDeclarations(selectionListElement);
64         String JavaDoc i18nPfx = FormsConstants.I18N_PREFIX;
65         if (nspfx != null) {
66             i18nPfx = (String JavaDoc)nspfx.get( FormsConstants.I18N_NS );
67             if (i18nPfx == null ) {
68                 i18nPfx = FormsConstants.I18N_PREFIX;
69             }
70         }
71         String JavaDoc labelPath = DomHelper.getAttribute(selectionListElement, "label-path", null);
72         boolean labelIsI18nKey = false;
73         if( labelPath == null )
74         {
75             labelPath = DomHelper.getAttribute(selectionListElement, i18nPfx + ":label-path");
76             labelIsI18nKey = true;
77         }
78         String JavaDoc nullText = DomHelper.getAttribute(selectionListElement, "null-text", null);
79         boolean nullTextIsI18nKey = false;
80         if( nullText == null ) {
81             nullText = DomHelper.getAttribute(selectionListElement, i18nPfx + ":null-text", null);
82             if( nullText != null ) {
83                 nullTextIsI18nKey = true;
84             }
85         }
86         
87         String JavaDoc i18nCatalog = DomHelper.getAttribute(selectionListElement, "catalogue", null);
88
89
90         return new FlowJXPathSelectionList(context,
91                                            listPath,
92                                            valuePath,
93                                            labelPath,
94                                            datatype,
95                                            nullText,
96                                            nullTextIsI18nKey,
97                                            i18nCatalog,
98                                            labelIsI18nKey);
99     }
100
101 }
102
Popular Tags