KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > JMapFormat


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.java;
21
22 import java.util.*;
23
24 import org.openide.util.MapFormat;
25
26 /**
27 * The message formatter, which uses map's keys in place of numbers.
28 * This class extends functionality of MessageFormat by allowing the user to
29 * specify strings in place of MessageFormatter's numbers. It then uses given
30 * map to translate keys to values. It also handles conditional expressions.
31 *
32 * You will usually use this formatter as follows:
33 * <code>JMapFormat.format("Hello {name}", map);</code>
34 *
35 * For conditional expression:
36 * <code>JMapFormat.format("Hello {name,old_suffix,new_suffix,no_match}"); </code>
37 * Last three parameters are optional. At first, name is translated. If it then has
38 * suffix old_suffix it is replaced by te new_suffix else whole parrent is replaced by
39 * no_match string.
40 *
41 * Notes: if map does not contain value for key specified, it substitutes
42 * the value by <code>"null"</code> word to qualify that something goes wrong.
43 *
44 * @author Slavek Psenicka, Martin Ryzl
45 * @version 1.0, March 11. 1999
46 */

47
48 public class JMapFormat extends MapFormat {
49
50     private String JavaDoc cdel = ","; // NOI18N
51

52     static final long serialVersionUID =5503640004816201285L;
53     public JMapFormat(Map map) {
54         super(map);
55     }
56
57     protected Object JavaDoc processKey(String JavaDoc key) {
58         // System.out.println("key = " + key); // NOI18N
59
StringTokenizer st = new StringTokenizer(key, cdel, true);
60         String JavaDoc data[] = new String JavaDoc[4];
61         String JavaDoc temp;
62
63         for(int i = 0; (i < 4) && st.hasMoreTokens(); ) {
64             temp = st.nextToken();
65             if (temp.equals("$")) i++; // NOI18N
66
else data[i] = temp;
67         }
68
69         Object JavaDoc obj = super.processKey(data[0]);
70         if (obj instanceof String JavaDoc) {
71             if (data[1] != null) {
72                 String JavaDoc name = (String JavaDoc)obj;
73                 if (name.endsWith(data[1])) {
74                     if (data[2] == null) data[2] = ""; // NOI18N
75
return name.substring(0, name.length() - data[1].length()) + data[2];
76                 } else {
77                     if (data[3] != null) return data[3];
78                     else return obj;
79                 }
80             }
81         }
82         return obj;
83     }
84
85     public String JavaDoc getCondDelimiter() {
86         return cdel;
87     }
88
89     public void setCondDelimiter(String JavaDoc cdel) {
90         this.cdel = cdel;
91     }
92
93 }
94
Popular Tags