KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > faces > taglib > ConverterTag


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.faces.taglib;
17
18 import org.apache.cocoon.taglib.TagSupport;
19
20 import org.apache.cocoon.faces.FacesUtils;
21 import org.xml.sax.Attributes JavaDoc;
22 import org.xml.sax.SAXException JavaDoc;
23
24 import javax.faces.component.UIComponent;
25 import javax.faces.component.ValueHolder;
26 import javax.faces.convert.Converter;
27 import javax.faces.convert.ConverterException;
28
29 /**
30  * Base class for converter tags.
31  * Can be extended to implement custom converters.
32  *
33  * @version CVS $Id: ConverterTag.java 53791 2004-10-05 13:16:15Z vgritsenko $
34  */

35 public class ConverterTag extends TagSupport {
36
37     private String JavaDoc converterId;
38
39     public String JavaDoc getConverterId() {
40         return this.converterId;
41     }
42
43     public void setConverterId(String JavaDoc converterId) {
44         this.converterId = converterId;
45     }
46
47     public int doStartTag(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts)
48     throws SAXException JavaDoc {
49
50         UIComponentTag tag = FacesUtils.findParentUIComponentTag(this);
51         if (tag == null) {
52             throw new SAXException JavaDoc("Tag " + getClass().getName() + " have to be nested in a UIComponentTag");
53         }
54
55         if (!tag.getCreated()) {
56             return 0;
57         }
58
59         Converter converter = createConverter();
60
61         ValueHolder vh = (ValueHolder) tag.getComponentInstance();
62         vh.setConverter(converter);
63         Object JavaDoc localValue = vh.getLocalValue();
64         if (localValue instanceof String JavaDoc) {
65             try {
66                 localValue = converter.getAsObject(tag.getFacesContext(), (UIComponent) vh, (String JavaDoc) localValue);
67                 vh.setValue(localValue);
68             } catch (ConverterException ce) {
69             }
70         }
71
72         return SKIP_BODY;
73     }
74
75     /**
76      * Override to create custom validator
77      */

78     protected Converter createConverter() {
79         final UIComponentTag tag = FacesUtils.findParentUIComponentTag(this);
80         String JavaDoc converterIdVal = (String JavaDoc) tag.evaluate(converterId);
81         return tag.getApplication().createConverter(converterIdVal);
82     }
83
84     public void recycle() {
85         super.recycle();
86         this.converterId = null;
87     }
88 }
89
Popular Tags