001: /*
002: * Copyright 2004-2006 Fouad HAMDI.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.csvbeans.specs;
017:
018: import java.util.HashMap;
019: import java.util.Iterator;
020: import java.util.Map;
021: import java.util.Set;
022:
023: import org.csvbeans.converters.Converter;
024: import org.csvbeans.utils.MessagesBundle;
025:
026: /**
027: * Specifications of a converter.
028: *
029: * @author Fouad Hamdi
030: * @since 0.7
031: */
032: public class ConverterSpecificationImpl implements
033: ConverterSpecification {
034: private String id;
035:
036: private String ref;
037:
038: private String className;
039:
040: private Map properties = new HashMap();
041:
042: private SpecificationsFile specifications;
043:
044: public void addProperty(PropertySpecification property) {
045: properties.put(property.getName(), property.getValue());
046: }
047:
048: /* (non-Javadoc)
049: * @see org.csvbeans.specs.ConverterSpecification#getId()
050: */
051: public String getId() {
052: return id;
053: }
054:
055: /**
056: * @param id
057: * The id to set.
058: */
059: public void setId(String id) {
060: this .id = id;
061: }
062:
063: /* (non-Javadoc)
064: * @see org.csvbeans.specs.ConverterSpecification#getProperties()
065: */
066: public Map getProperties() {
067: return properties;
068: }
069:
070: /**
071: * @param properties
072: * The properties to set.
073: */
074: public void setProperties(Map properties) {
075: this .properties = properties;
076: }
077:
078: /* (non-Javadoc)
079: * @see org.csvbeans.specs.ConverterSpecification#getRef()
080: */
081: public String getRef() {
082: return ref;
083: }
084:
085: /**
086: * @param ref
087: * The ref to set.
088: */
089: public void setRef(String ref) {
090: this .ref = ref;
091: }
092:
093: /* (non-Javadoc)
094: * @see org.csvbeans.specs.ConverterSpecification#getClassName()
095: */
096: public String getClassName() {
097: return className;
098: }
099:
100: /**
101: * @param className
102: * The className to set.
103: */
104: public void setClassName(String className) {
105: this .className = className;
106: }
107:
108: /* (non-Javadoc)
109: * @see org.csvbeans.specs.ConverterSpecification#buildConverter()
110: */
111: public Converter buildConverter() {
112: try {
113: Converter converter = (Converter) Class.forName(className)
114: .newInstance();
115: Set keys = properties.keySet();
116: if (keys != null) {
117: for (Iterator it = keys.iterator(); it.hasNext();) {
118: String key = (String) it.next();
119: converter.addProperty(key, (String) properties
120: .get(key));
121: }
122: }
123: converter.init(specifications);
124: return converter;
125: } catch (Exception e) {
126: throw new IllegalArgumentException(MessagesBundle
127: .getMessage("converter.impossible.creation",
128: new Object[] { className, e }));
129: }
130: }
131:
132: public void setSpecifications(SpecificationsFile specifications) {
133: this.specifications = specifications;
134: }
135: }
|