KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > ddl > util > CommandFormatter


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.lib.ddl.util;
21
22 import java.text.ParseException JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 import org.openide.util.MapFormat;
29
30 /**
31 * The message formatter, It handles [] brackets as optional keys.
32 *
33 * @author Slavek Psenicka
34 */

35
36 public class CommandFormatter {
37     /** Parsed items */
38     Vector JavaDoc items;
39
40     /** Formats pattern using arguments map
41     * Returns formatted string.
42     * @param pattern String to be formatted
43     * @param arguments Argument map
44     */

45     public static String JavaDoc format(String JavaDoc pattern, Map JavaDoc arguments) throws ParseException JavaDoc, IllegalArgumentException JavaDoc {
46         CommandFormatter temp = new CommandFormatter(pattern);
47         return temp.format(arguments);
48     }
49
50     /** Constructor
51     * @param pattern String to be formatted
52     */

53     public CommandFormatter(String JavaDoc pattern) throws ParseException JavaDoc {
54         this(new StringTokenizer JavaDoc(pattern, "[]", true));
55     }
56
57     /** Constructor
58     * @param tok Used tokenizer
59     */

60     private CommandFormatter(StringTokenizer JavaDoc tok) throws ParseException JavaDoc {
61         items = scan(tok);
62     }
63
64     /** Scans data for [] brackets and prepares data for formatting
65     * using MapFormat utility
66     */

67     private Vector JavaDoc scan(StringTokenizer JavaDoc tok) throws ParseException JavaDoc {
68         Vector JavaDoc objvec = new Vector JavaDoc();
69         while (tok.hasMoreTokens()) {
70             String JavaDoc token = (String JavaDoc) tok.nextElement();
71             Object JavaDoc obj = token;
72             if (token.equals("["))
73                 obj = (Object JavaDoc)scan(tok);
74             else if (token.equals("]"))
75                 break;
76             objvec.add(obj);
77         }
78
79         return objvec;
80     }
81
82     /** Formats parsed string using given argument map
83     * @param arguments Argument map
84     */

85     public String JavaDoc format(Map JavaDoc arguments) throws IllegalArgumentException JavaDoc {
86         return format(items, arguments);
87     }
88
89     /** Formats given string vector using given argument map.
90     * Used internally only.
91     * @param arguments Argument map
92     */

93     private String JavaDoc format(Vector JavaDoc itemvec, Map JavaDoc arguments) throws IllegalArgumentException JavaDoc {
94         String JavaDoc retstr = "";
95         Enumeration JavaDoc items_e = itemvec.elements();
96         while (items_e.hasMoreElements()) {
97             Object JavaDoc e_item = items_e.nextElement();
98             if (e_item instanceof Vector JavaDoc) {
99                 try {
100                     e_item = format((Vector JavaDoc)e_item, arguments);
101                 } catch (Exception JavaDoc e) {
102                     //PENDING
103
}
104             }
105
106             if (e_item instanceof String JavaDoc) {
107                 MapFormat fmt = new MapFormat(arguments);
108                 fmt.setThrowExceptionIfKeyWasNotFound(true);
109                 String JavaDoc e_msg = fmt.format((String JavaDoc)e_item);
110                 if (e_msg != null)
111                     retstr = retstr + e_msg;
112                 else
113                     throw new IllegalArgumentException JavaDoc();
114             }
115         }
116
117         return retstr;
118     }
119 }
120
Popular Tags