KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > externalizer > ExternalizerTagsHandler


1 /*
2  * Copyright (c) 2001, 2002 The XDoclet team
3  * All rights reserved.
4  */

5 package xdoclet.modules.externalizer;
6
7 import xdoclet.DocletContext;
8 import xdoclet.DocletTask;
9 import xdoclet.XDocletException;
10 import xdoclet.XDocletTagSupport;
11 import xdoclet.util.Translator;
12
13 /**
14  * Tags used in generating & using externalised property resource bundles.
15  *
16  * @author Ara Abrahamian (ara_e_w@yahoo.com)
17  * @created May 30, 2002
18  * @xdoclet.taghandler namespace="Externalizer"
19  * @version $Revision: 1.7 $
20  */

21 public class ExternalizerTagsHandler extends XDocletTagSupport
22 {
23     private String JavaDoc currentKey;
24     private String JavaDoc currentValue;
25
26     /**
27      * Convert native encoding character to escaped character.
28      *
29      * @param str string with native characters
30      * @return string with unicode escapes
31      */

32     private static String JavaDoc convertToUnicodeEscape(String JavaDoc str)
33     {
34         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
35
36         buf.setLength(0);
37
38         int pos = 0;
39
40         while (pos < str.length()) {
41             char c = str.charAt(pos);
42
43             pos++;
44             if (c >= 0x100) {
45                 // Only characters >8 bit -- leave iso-8859-x alone
46
String JavaDoc hex = Integer.toHexString(c);
47
48                 buf.append("\\u0000".substring(0, 6 - hex.length()));
49                 buf.append(hex);
50             }
51             else {
52                 switch (c) {
53                 case '\t':
54                     buf.append("\\t");
55                     break;
56                 default:
57                     buf.append(c);
58                 }
59             }
60         }
61         return buf.toString();
62     }
63
64     /**
65      * Evaluate the body for all field tags. Works only in context of {@link ExternalizerSubTask externalizer} subtask.
66      *
67      * @param template The body of the block tag
68      * @exception XDocletException
69      * @doc.tag type="block"
70      */

71     public void forAllFieldTags(String JavaDoc template) throws XDocletException
72     {
73         ExternalizerSubTask.Combination combination = ((ExternalizerSubTask) getDocletContext().getActiveSubTask()).getCurrentCombination();
74
75         for (int i = 0; i < combination.keys.size(); i++) {
76             currentKey = (String JavaDoc) combination.keys.get(i);
77             currentValue = (String JavaDoc) combination.values.get(i);
78
79             generate(template);
80         }
81
82         currentKey = null;
83         currentValue = null;
84     }
85
86     /**
87      * Current resource bundle name, will be called by {@link PropertiesTranslatorSubTask propertiestranslator} subtask.
88      *
89      * @return bundle name
90      * @exception XDocletException
91      * @doc.tag type="content"
92      */

93     public String JavaDoc bundleKey() throws XDocletException
94     {
95         if (DocletContext.getInstance().isSubTaskDefined(DocletTask.getSubTaskName(ExternalizerSubTask.class))) {
96             return ((ExternalizerSubTask) DocletContext.getInstance().getSubTaskBy(DocletTask.getSubTaskName(ExternalizerSubTask.class))).getBundleKey(getCurrentClass());
97         }
98         else {
99             throw new XDocletException(Translator.getString("xdoclet.modules.externalizer.ExternalizerSubtaskMessages",
100                 ExternalizerSubtaskMessages.TRANSLATOR_DEPENDS_ON_EXTERNALIZER));
101         }
102     }
103
104     /**
105      * The current key.
106      *
107      * @return key
108      * @exception XDocletException
109      * @doc.tag type="content"
110      */

111     public String JavaDoc key() throws XDocletException
112     {
113         return currentKey;
114     }
115
116     /**
117      * The current value, with Unicode escapes where necessary.
118      *
119      * @return value
120      * @exception XDocletException
121      * @doc.tag type="content"
122      */

123     public String JavaDoc value() throws XDocletException
124     {
125         return convertToUnicodeEscape(currentValue);
126     }
127 }
128
Popular Tags