KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > dataview > MapFormat


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.dataview;
57
58 import java.text.*;
59 import java.util.*;
60 import java.lang.reflect.*;
61
62 public class MapFormat extends Format {
63   private LinkedHashMap formatMap;
64   private Object JavaDoc[] values;
65   private String JavaDoc[] formats;
66   private String JavaDoc entryDelimiter = "|";
67   private String JavaDoc valueFormatDelimiter = "#";
68   private String JavaDoc nullFormat = "";
69   private String JavaDoc nullValueDesignation = "null";
70
71   public MapFormat() {
72   }
73
74   public MapFormat(String JavaDoc pattern, Class JavaDoc valueClass) {
75     applyPattern(pattern, valueClass);
76   }
77
78   public MapFormat(Object JavaDoc[] values, String JavaDoc[] formats) {
79     setMap(values, formats);
80   }
81
82   public void setEntryDelimiter(char delimiter) {
83     entryDelimiter = String.valueOf(delimiter);
84   }
85
86   public char getEntryDelimiter() {
87     return entryDelimiter.charAt(0);
88   }
89
90   public void setValueFormatDelimiter(char delimiter) {
91     valueFormatDelimiter = String.valueOf(delimiter);
92   }
93
94   public char getValueFormatDelimiter() {
95     return valueFormatDelimiter.charAt(0);
96   }
97
98   public void setNullValueDesignation(String JavaDoc nullValueDesignation) {
99     this.nullValueDesignation = nullValueDesignation;
100   }
101
102   public String JavaDoc getNullValueDesignation() {
103     return nullValueDesignation;
104   }
105
106   public String JavaDoc getNullFormat() {
107     return nullFormat;
108   }
109
110   public void setMap(Object JavaDoc[] values, String JavaDoc[] formats) {
111     this.values = new Object JavaDoc[values.length];
112     this.formats = new String JavaDoc[formats.length];
113     System.arraycopy(values, 0, this.values, 0, formats.length);
114     System.arraycopy(formats, 0, this.formats, 0, formats.length);
115     formatMap = new LinkedHashMap(this.values.length + 1);
116     for (int i = 0; i < this.values.length; i++) {
117       if (this.formats[i] == null)
118         throw new NullPointerException JavaDoc("format cannot be null: " + values[i]);
119       if (this.values[i] == null)
120         nullFormat = this.formats[i];
121       else
122         formatMap.put(this.values[i], this.formats[i]);
123     }
124   }
125
126   public Object JavaDoc[] getValues() {
127     return values;
128   }
129
130   public String JavaDoc[] getFormats() {
131     return formats;
132   }
133
134   public void applyPattern(String JavaDoc pattern) {
135     applyPattern(pattern, String JavaDoc.class);
136   }
137
138   public void applyPattern(String JavaDoc pattern, Class JavaDoc valueClass) {
139     formatMap = new LinkedHashMap();
140     Constructor stringConstructor;
141     try {
142       stringConstructor =
143           valueClass.getConstructor(new Class JavaDoc[] {String JavaDoc.class});
144     } catch (Exception JavaDoc ex) {
145       throw new IllegalArgumentException JavaDoc(valueClass + " has no String cunstructor");
146     }
147     StringTokenizer parser = new StringTokenizer(pattern, entryDelimiter);
148     ArrayList valueList = new ArrayList();
149     ArrayList formatList = new ArrayList();
150     while (parser.hasMoreTokens()) {
151       String JavaDoc pair = parser.nextToken().trim();
152       int delimIndex = pair.indexOf(valueFormatDelimiter);
153       Object JavaDoc value;
154       String JavaDoc format = "";
155       try {
156         String JavaDoc valueStr;
157         if (delimIndex < 0 || delimIndex >= pair.length() - 1) {
158           valueStr = pair;
159         } else {
160           valueStr = pair.substring(0, delimIndex);
161           format = pair.substring(delimIndex + 1);
162         }
163         if (nullValueDesignation.equals(valueStr)) {
164           nullFormat = format;
165           valueList.add(null);
166           formatList.add(nullFormat);
167         } else {
168           value = stringConstructor.newInstance(new Object JavaDoc[] {valueStr});
169           formatMap.put(value, format);
170           valueList.add(value);
171           formatList.add(format);
172         }
173       }
174       catch (InstantiationException JavaDoc ex) {
175         throw new IllegalArgumentException JavaDoc(valueClass + " " + ex);
176       }catch (IllegalAccessException JavaDoc ex) {
177         throw new IllegalArgumentException JavaDoc(valueClass + " " + ex);
178       }catch (IllegalArgumentException JavaDoc ex) {
179         throw new IllegalArgumentException JavaDoc(valueClass + " " + ex);
180       }catch (InvocationTargetException ex) {
181         throw new IllegalArgumentException JavaDoc(pattern + " incorrect pattern: " + pair);
182       }
183     }
184     values = valueList.toArray();
185     formats = (String JavaDoc[])formatList.toArray(new String JavaDoc[formatList.size()]);
186   }
187
188   public Object JavaDoc parseObject(String JavaDoc text, ParsePosition status) {
189     int start = status.getIndex();
190     int furthest = start;
191     Object JavaDoc bestObject = null;
192     Object JavaDoc tempObject = null;
193     for (Iterator i = formatMap.entrySet().iterator(); i.hasNext();) {
194       Map.Entry entry = (Map.Entry)i.next();
195       String JavaDoc tempString = (String JavaDoc)entry.getValue();
196       if (text.regionMatches(start, tempString, 0, tempString.length())) {
197         status.setIndex(start + tempString.length());
198         tempObject = entry.getKey();
199         if (status.getIndex() > furthest) {
200           furthest = status.getIndex();
201           bestObject = tempObject;
202           if (furthest == text.length()) break;
203         }
204       }
205     }
206     if (nullFormat != null &&
207         text.regionMatches(start, nullFormat, 0, nullFormat.length())) {
208       status.setIndex(start + nullFormat.length());
209       if (status.getIndex() > furthest) {
210         furthest = status.getIndex();
211         bestObject = null;
212       }
213     }
214     status.setIndex(furthest);
215     if (status.getIndex() == start) {
216       status.setErrorIndex(furthest);
217     }
218     return bestObject;
219   }
220
221   public StringBuffer JavaDoc format(Object JavaDoc obj, StringBuffer JavaDoc toAppendTo, FieldPosition pos) {
222     if (obj == null) {
223       if (nullFormat == null)
224         throw new NullPointerException JavaDoc("object to format cannot be null");
225       else return toAppendTo.append(nullFormat);
226     }
227     String JavaDoc formatStr = (String JavaDoc)formatMap.get(obj);
228     if (formatStr == null)
229       throw new IllegalArgumentException JavaDoc("cannot format the object " + obj);
230     toAppendTo.append(formatStr);
231     return toAppendTo;
232   }
233
234   public String JavaDoc toPattern() {
235     StringBuffer JavaDoc pattern = new StringBuffer JavaDoc();
236     boolean notFirst = false;
237     for (Iterator i = formatMap.entrySet().iterator(); i.hasNext(); ) {
238       Map.Entry entry = (Map.Entry)i.next();
239       if (notFirst) {
240         pattern.append(entryDelimiter);
241       } else notFirst = true;
242       pattern.append(entry.getKey()).append(valueFormatDelimiter).append(entry.getValue());
243     }
244     if (nullValueDesignation != null && nullFormat != null) {
245       if (notFirst)
246         pattern.append(entryDelimiter);
247       pattern.append(nullValueDesignation).append(valueFormatDelimiter).append(nullFormat);
248     }
249     return pattern.toString();
250   }
251 }
Popular Tags