001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.java;
043:
044: import java.util.*;
045:
046: import org.openide.util.MapFormat;
047:
048: /**
049: * The message formatter, which uses map's keys in place of numbers.
050: * This class extends functionality of MessageFormat by allowing the user to
051: * specify strings in place of MessageFormatter's numbers. It then uses given
052: * map to translate keys to values. It also handles conditional expressions.
053: *
054: * You will usually use this formatter as follows:
055: * <code>JMapFormat.format("Hello {name}", map);</code>
056: *
057: * For conditional expression:
058: * <code>JMapFormat.format("Hello {name,old_suffix,new_suffix,no_match}"); </code>
059: * Last three parameters are optional. At first, name is translated. If it then has
060: * suffix old_suffix it is replaced by te new_suffix else whole parrent is replaced by
061: * no_match string.
062: *
063: * Notes: if map does not contain value for key specified, it substitutes
064: * the value by <code>"null"</code> word to qualify that something goes wrong.
065: *
066: * @author Slavek Psenicka, Martin Ryzl
067: * @version 1.0, March 11. 1999
068: */
069:
070: public class JMapFormat extends MapFormat {
071:
072: private String cdel = ","; // NOI18N
073:
074: static final long serialVersionUID = 5503640004816201285L;
075:
076: public JMapFormat(Map map) {
077: super (map);
078: }
079:
080: protected Object processKey(String key) {
081: // System.out.println("key = " + key); // NOI18N
082: StringTokenizer st = new StringTokenizer(key, cdel, true);
083: String data[] = new String[4];
084: String temp;
085:
086: for (int i = 0; (i < 4) && st.hasMoreTokens();) {
087: temp = st.nextToken();
088: if (temp.equals("$"))
089: i++; // NOI18N
090: else
091: data[i] = temp;
092: }
093:
094: Object obj = super .processKey(data[0]);
095: if (obj instanceof String) {
096: if (data[1] != null) {
097: String name = (String) obj;
098: if (name.endsWith(data[1])) {
099: if (data[2] == null)
100: data[2] = ""; // NOI18N
101: return name.substring(0, name.length()
102: - data[1].length())
103: + data[2];
104: } else {
105: if (data[3] != null)
106: return data[3];
107: else
108: return obj;
109: }
110: }
111: }
112: return obj;
113: }
114:
115: public String getCondDelimiter() {
116: return cdel;
117: }
118:
119: public void setCondDelimiter(String cdel) {
120: this.cdel = cdel;
121: }
122:
123: }
|