01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.forms.datatype.convertor;
18:
19: import org.apache.cocoon.forms.FormsConstants;
20: import org.apache.cocoon.forms.util.DomHelper;
21:
22: import org.w3c.dom.Element;
23:
24: /**
25: * Creates {@link BeanConvertor}s
26: *
27: * <p>
28: * The optional <fd:bean>FQCN</fd:bean> attribute is used to give
29: * this convertor a hint of which concrete bean class we are going to work with.
30: * If this attribute is not specified java.lang.Object is used.
31: * <p>
32: * Sometimes the toString() method doesn't give a good representation of a
33: * Java Bean suited for selection list IDs. For this an optional
34: * <fd:id-path>jx-path</fd:id-path> attribute can be specified to
35: * have this convertor to use a different string representation.
36: * </p>
37: *
38: * @version $Id$
39: */
40: public class BeanConvertorBuilder implements ConvertorBuilder {
41: //~ Methods ----------------------------------------------------------------
42:
43: /**
44: * Build a {@link BeanConvertor}
45: *
46: * @param configElement The configuration element
47: *
48: * @return An initialized {@link Convertor}
49: *
50: * @throws Exception In case of failure
51: */
52: public Convertor build(final Element configElement)
53: throws Exception {
54: if (configElement == null) {
55: return null;
56: }
57:
58: final Element beanEl = DomHelper.getChildElement(configElement,
59: FormsConstants.DEFINITION_NS, "bean", false);
60: final String clazz = ((beanEl == null) ? Object.class.getName()
61: : beanEl.getFirstChild().getNodeValue());
62: final Element idPathEl = DomHelper.getChildElement(
63: configElement, FormsConstants.DEFINITION_NS, "id-path",
64: false);
65: final String idPath = ((idPathEl != null) ? idPathEl
66: .getFirstChild().getNodeValue() : null);
67: final BeanConvertor convertor = new BeanConvertor(clazz, idPath);
68:
69: return convertor;
70: }
71: }
|