KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > i18n > I18nString


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
21 package org.netbeans.modules.i18n;
22
23
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26 import org.netbeans.api.java.classpath.ClassPath;
27 import org.openide.filesystems.FileObject;
28
29 import org.openide.loaders.DataObject;
30 import org.openide.util.MapFormat;
31
32
33 /**
34  * This object represent i18n values which will be used by actual
35  * i18n-izing of found hard coded string. I.e. resource where will be stored
36  * new key-value pair, actual key-value pair and replace code wich will
37  * replace found hard coded string.
38  * <p>
39  * It also prescribes that each subclass MUST have <b>copy constuctor</b>
40  * calling its superclass copy constructor. The copy constructor MUST be then
41  * called during <b>cloning</b>. All subclasses must also support oposite
42  * process <b>becoming</b>
43  *
44  * @author Peter Zavadsky
45  * @author Petr Kuzel
46  */

47 public class I18nString {
48
49     /**
50      * Support for this i18n string istance.
51      * It contains implementation.
52      */

53     protected I18nSupport support;
54     
55     /** The key value according the hard coded string will be i18n-ized. */
56     protected String JavaDoc key;
57     
58     /** The "value" value which will be stored to resource. */
59     protected String JavaDoc value;
60     
61     /** Comment for key-value pair stored in resource. */
62     protected String JavaDoc comment;
63     
64     /** Replace format. */
65     protected String JavaDoc replaceFormat;
66
67     
68     /**
69      * Creates new I18nString.
70      * @param support <code>I18nSupport</code> linked to this instance,
71      * has to be non-null
72      */

73     protected I18nString(I18nSupport support) {
74         if(support == null) throw new NullPointerException JavaDoc();
75
76         this.support = support;
77         
78         //??? what is this
79
replaceFormat = I18nUtil.getOptions().getReplaceJavaCode();
80     }
81
82     /**
83      * Copy contructor.
84      */

85     protected I18nString(I18nString copy) {
86         this.key = copy.key;
87         this.value = copy.value;
88         this.comment = copy.comment;
89         this.replaceFormat = copy.replaceFormat;
90         this.support = copy.support;
91     }
92     
93     /**
94      * Let this instance take its state from passed one.
95      * All subclasses must extend it.
96      */

97     public void become(I18nString copy) {
98         this.key = copy.key;
99         this.value = copy.value;
100         this.comment = copy.comment;
101         this.replaceFormat = copy.replaceFormat;
102         this.support = copy.support;
103     }
104     
105     /**
106      * Cloning must use copy contructors.
107      */

108     public Object JavaDoc clone() {
109         return new I18nString(this);
110     }
111     
112     /** Getter for <code>support</code>. */
113     public I18nSupport getSupport() {
114         return support;
115     }
116     
117     /** Getter for <code>key</code>. */
118     public String JavaDoc getKey() {
119         return key;
120     }
121
122     /** Setter for <code>key</code>. */
123     public void setKey(String JavaDoc key) {
124         if(this.key == key || (this.key != null && this.key.equals(key)))
125             return;
126
127         this.key = key;
128     }
129     
130     /** Getter for <code>value</code>. */
131     public String JavaDoc getValue() {
132         return value;
133     }
134
135     /** Setter for <code>value</code>. */
136     public void setValue(String JavaDoc value) {
137         if(this.value == value || (this.value != null && this.value.equals(value)))
138             return;
139
140         this.value = value;
141     }
142
143     /** Getter for <code>comment</code>. */
144     public String JavaDoc getComment() {
145         return comment;
146     }
147
148     /** Setter for <code>comment</code>. */
149     public void setComment(String JavaDoc comment) {
150         if(this.comment == comment || (this.comment != null && this.comment.equals(comment)))
151             return;
152
153         this.comment = comment;
154     }
155
156     /** Getter for replace format property. */
157     public String JavaDoc getReplaceFormat() {
158         return replaceFormat;
159     }
160     
161     /** Setter for replace format property. */
162     public void setReplaceFormat(String JavaDoc replaceFormat) {
163         this.replaceFormat = replaceFormat;
164     }
165
166     /**
167      * Derive replacing string. The method substitutes parameters into
168      * a format string using <code>MapFormat.format</code>. If you
169      * override this method, you must not call <code>.format</code>
170      * on the return value because values substituted in the previous
171      * round can contain control codes. All replacements
172      * must take place simultaneously in a single, the first, call. Thus, if you
173      * need to substitute some additional parameters not substituted by
174      * default, use
175      * the provided hook {@link #fillFormatMap}.
176      *
177      * @return replacing string or null if this instance is invalid
178      */

179     public String JavaDoc getReplaceString() {
180         if(getKey() == null || getSupport() == null || getSupport().getResourceHolder().getResource() == null)
181             return null;
182         
183         if(replaceFormat == null)
184             replaceFormat = I18nUtil.getOptions().getReplaceJavaCode();
185
186         // Create map.
187

188         DataObject sourceDataObject = getSupport().getSourceDataObject();
189
190         FileObject fo = getSupport().getResourceHolder().getResource().getPrimaryFile();
191         ClassPath cp = Util.getExecClassPath(sourceDataObject.getPrimaryFile(), fo);
192         Map JavaDoc map = new HashMap JavaDoc(4);
193
194         map.put("key", getKey()); // NOI18N
195
map.put("bundleNameSlashes", cp.getResourceName( fo, '/', false ) ); // NOI18N
196
map.put("bundleNameDots", cp.getResourceName( fo, '.', false ) ); // NOI18N
197
map.put("sourceFileName", sourceDataObject == null ? "" : sourceDataObject.getPrimaryFile().getName()); // NOI18N
198

199         fillFormatMap(map);
200
201         return MapFormat.format(replaceFormat, map);
202     }
203
204     /**
205      * Hook for filling in additional format key/value pair in
206      * subclasses. Within the method, the provided substituion map can
207      * be arbitrarilly modified.
208      * @param subst Map to be filled in with key/value pairs
209      */

210     protected void fillFormatMap(Map JavaDoc subst) {
211     }
212 }
213
Popular Tags