001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.forms.datatype.convertor;
018:
019: import org.apache.avalon.framework.CascadingRuntimeException;
020: import org.apache.commons.collections.map.ReferenceMap;
021: import org.apache.commons.jxpath.JXPathContext;
022:
023: import org.xml.sax.ContentHandler;
024: import org.xml.sax.SAXException;
025:
026: import java.util.Locale;
027: import java.util.Map;
028:
029: /**
030: * Converts String representation of beans to bean instances and vice versa.
031: *
032: * <p>
033: * Sometimes the toString() method doesn't give a good representation of a
034: * Java Bean suited for selection list IDs. For this an optional
035: * <fd:id-path>jx-path</fd:id-path> attribute can be specified to
036: * have this convertor to use a different string representation.
037: * </p>
038: *
039: * @version $Id$
040: */
041: public class BeanConvertor implements Convertor {
042: //~ Instance fields --------------------------------------------------------
043:
044: private Class m_class;
045:
046: private Map m_objects = new ReferenceMap();
047:
048: private String m_idPath;
049:
050: //~ Constructors -----------------------------------------------------------
051:
052: /**
053: * Construct a new BeanConvertor for a class
054: *
055: * @param className The package-qualified name of the class implementing
056: * the typesafe enum pattern.
057: * @param idPath Path to the identity field of the bean
058: *
059: * @throws CascadingRuntimeException If the class cannot be found
060: */
061: public BeanConvertor(final String className, final String idPath) {
062: try {
063: m_class = Class.forName(className);
064: } catch (ClassNotFoundException e) {
065: throw new CascadingRuntimeException("Class " + className
066: + " not found", e);
067: }
068:
069: m_idPath = idPath;
070: }
071:
072: //~ Methods ----------------------------------------------------------------
073:
074: /**
075: * @see org.apache.cocoon.forms.datatype.convertor.Convertor#getTypeClass()
076: */
077: public Class getTypeClass() {
078: return m_class;
079: }
080:
081: /**
082: * @see org.apache.cocoon.forms.datatype.convertor.Convertor#convertFromString(java.lang.String,
083: * java.util.Locale,
084: * org.apache.cocoon.forms.datatype.convertor.Convertor.FormatCache)
085: */
086: public ConversionResult convertFromString(final String value,
087: final Locale locale, final FormatCache formatCache) {
088: return new ConversionResult(m_objects.get(value));
089: }
090:
091: /**
092: * @see org.apache.cocoon.forms.datatype.convertor.Convertor#convertToString(java.lang.Object,
093: * java.util.Locale,
094: * org.apache.cocoon.forms.datatype.convertor.Convertor.FormatCache)
095: */
096: public String convertToString(final Object value,
097: final Locale locale, final FormatCache formatCache) {
098: String idValue = "";
099:
100: if (null != value) {
101: if (m_idPath != null) {
102: final JXPathContext ctx = JXPathContext
103: .newContext(value);
104: idValue = ctx.getValue(m_idPath).toString();
105: } else {
106: idValue = value.toString();
107: }
108: }
109:
110: m_objects.put(idValue, value);
111:
112: return idValue;
113: }
114:
115: /**
116: * We do not enerate any SAX events
117: *
118: * @param contentHandler The contentHandler
119: * @param locale The locale
120: *
121: * @throws SAXException Just in case of failure that could never happen
122: */
123: public void generateSaxFragment(
124: final ContentHandler contentHandler, final Locale locale)
125: throws SAXException {
126: // intentionally empty
127: }
128: }
|