View Javadoc

1   /*
2    * Copyright 2004-2005, 2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * 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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package net.sf.morph.transform.converters;
17  
18  import java.util.ArrayList;
19  import java.util.Iterator;
20  import java.util.List;
21  import java.util.Locale;
22  
23  /**
24   * <p>
25   * Converts a container to a textual representation (String or StringBuffer only).
26   * The string representation is comprised of a prefix, a textual representation
27   * of the array contents, and a suffix. The textual representation of the array
28   * contents is in turn made up of the string representation of each of the
29   * elements in the array separated by a separator character.  Conversions to
30   * characters will only succeed if the result of the conversion is a single
31   * character in length.
32   * </p>
33   * 
34   * <p>
35   * For example, if the prefix is <code>{</code>, the suffix is <code>}</code>,
36   * the separator is <code>,</code> and the contents of the array are the
37   * Integers <code>1</code>,<code>2</code> and <code>3</code>, the array
38   * will be converted to the text <code>{1,2,3}</code>.
39   * </p>
40   * 
41   * @author Matt Sgarlata
42   * @since Dec 25, 2004
43   */
44  public class ContainerToPrettyTextConverter extends BaseToPrettyTextConverter {
45  	/** Default prefix */
46  	public static final String DEFAULT_PREFIX = "{";
47  	/** Default suffix */
48  	public static final String DEFAULT_SUFFIX = "}";
49  	/** Default separator */
50  	public static final String DEFAULT_SEPARATOR = ",";
51  
52  	/**
53  	 * Create a new ContainerToPrettyTextConverter.
54  	 */
55  	public ContainerToPrettyTextConverter() {
56  		setPrefix(DEFAULT_PREFIX);
57  		setSuffix(DEFAULT_SUFFIX);
58  		setSeparator(DEFAULT_SEPARATOR);
59  	}
60  
61  	/**
62  	 * {@inheritDoc}
63  	 */
64  	protected Object convertImpl(Class destinationClass, Object source,
65  		Locale locale) throws Exception {
66  
67  		boolean endsInSeparator = false;
68  		StringBuffer buffer = new StringBuffer();
69  		if (getPrefix() != null) {
70  			buffer.append(getPrefix());
71  		}
72  		Iterator iterator = getContainerReflector().getIterator(source);
73  		while (iterator != null && iterator.hasNext()) {
74  			Object next = iterator.next();
75  			if (next != null || isShowNullValues()) {
76  				String text = (String) getToTextConverter().convert(String.class,
77  					next, locale);
78  				buffer.append(text);
79  				buffer.append(getSeparator());
80  				endsInSeparator = true;
81  			}
82  		}
83  		if (endsInSeparator) {
84  			buffer.delete(buffer.length() - getSeparator().length(), buffer.length());	
85  		}
86  		if (getSuffix() != null) {
87  			buffer.append(getSuffix());
88  		}
89  		return getTextConverter().convert(destinationClass, buffer, locale);
90  	}
91  
92  	/**
93  	 * {@inheritDoc}
94  	 */
95  	protected Class[] getSourceClassesImpl() throws Exception {
96  		// the container reflector can treat an object like a container,
97  		// but for this converter we don't want objects to be valid sources
98  		// to convert objects to text, the ObjectToTextConverter should be used
99  		// instead
100 		List list = null;
101 		Class[] reflectableClasses = getContainerReflector().getReflectableClasses();
102 		for (int i = 0; i < reflectableClasses.length; i++) {
103 			if (reflectableClasses[i] == Object.class) {
104 				if (list == null) {
105 					list = new ArrayList(reflectableClasses.length - 1);
106 					for (int j = 0; j < i; j++) {
107 						list.add(j, reflectableClasses[j]);
108 					}
109 				}
110 			} else if (list != null) {
111 				list.add(reflectableClasses[i]);
112 			}
113 		}
114 		return list == null ? reflectableClasses : (Class[]) list.toArray(new Class[list.size()]);
115 	}
116 
117 }