KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

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