KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > util > I18nMessage


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.woody.util;
17
18 import org.apache.cocoon.transformation.I18nTransformer;
19 import org.apache.cocoon.woody.Constants;
20 import org.apache.cocoon.xml.AttributesImpl;
21 import org.apache.excalibur.xml.sax.XMLizable;
22 import org.xml.sax.ContentHandler JavaDoc;
23 import org.xml.sax.SAXException JavaDoc;
24
25 /**
26  * A XMLizable implementation that will produce SAX events for the
27  * I18nTransformer in its toSAX method, based on the information
28  * given in the constructor.
29  *
30  * <p>This generates an autonomous SAX-blurb, i.e. all necessary namespace
31  * declarations will be made, and no start/endDocument events will be generated.
32  *
33  * @version CVS $Id: I18nMessage.java 30932 2004-07-29 17:35:38Z vgritsenko $
34  */

35 public class I18nMessage implements XMLizable {
36     private String JavaDoc key;
37     private String JavaDoc catalogue;
38     private String JavaDoc[] parameters;
39     private boolean[] keys;
40
41     /**
42      * @param key a message key, to be translated by the I18nTransformer
43      */

44     public I18nMessage(String JavaDoc key) {
45         this(key, (String JavaDoc) null);
46     }
47
48     /**
49      * @param key a message key, to be translated by the I18nTransformer
50      * @param catalogue a named I18nTransformer catalogue to use
51      */

52     public I18nMessage(String JavaDoc key, String JavaDoc catalogue) {
53         this.key = key;
54         this.catalogue = catalogue;
55     }
56
57     /**
58      * @param key a message key, to be translated by the I18nTransformer
59      * @param parameters parameters to be substituted in the errorMessage (will be
60      * done by the I18nTransformer)
61      */

62     public I18nMessage(String JavaDoc key, String JavaDoc[] parameters) {
63         this(key, parameters, (String JavaDoc)null);
64     }
65
66     /**
67      * @param key a message key, to be translated by the I18nTransformer
68      * @param parameters parameters to be substituted in the errorMessage (will be
69      * done by the I18nTransformer)
70      * @param catalogue a named I18nTransformer catalogue to use
71      */

72     public I18nMessage(String JavaDoc key, String JavaDoc[] parameters, String JavaDoc catalogue) {
73         this.key = key;
74         this.parameters = parameters;
75         this.catalogue = catalogue;
76     }
77
78     /**
79      * @param key a message key, to be translated by the I18nTransformer
80      * @param parameters parameters to be substituted in the errorMessage (will be
81      * done by the I18nTransformer)
82      * @param keys Each element in the keys array corresponds to a string in the parameters array
83      * and indicates whether that parameter is in itself again a key.
84      */

85     public I18nMessage(String JavaDoc key, String JavaDoc[] parameters, boolean[] keys) {
86         this(key, parameters, keys, null);
87     }
88
89     /**
90      * @param key a message key, to be translated by the I18nTransformer
91      * @param parameters parameters to be substituted in the errorMessage (will be
92      * done by the I18nTransformer)
93      * @param keys Each element in the keys array corresponds to a string in the parameters array
94      * and indicates whether that parameter is in itself again a key.
95      * @param catalogue a named I18nTransformer catalogue to use
96      */

97     public I18nMessage(String JavaDoc key, String JavaDoc[] parameters, boolean[] keys, String JavaDoc catalogue) {
98         this.key = key;
99         this.parameters = parameters;
100         this.keys = keys;
101         this.catalogue = catalogue;
102     }
103
104     public void toSAX(ContentHandler JavaDoc contentHandler) throws SAXException JavaDoc {
105         contentHandler.startPrefixMapping("i18n", I18nTransformer.I18N_NAMESPACE_URI);
106         if (parameters != null) {
107             contentHandler.startElement(I18nTransformer.I18N_NAMESPACE_URI, I18nTransformer.I18N_TRANSLATE_ELEMENT, "i18n:" + I18nTransformer.I18N_TRANSLATE_ELEMENT, Constants.EMPTY_ATTRS);
108         }
109
110         AttributesImpl i18nAttrs = new AttributesImpl();
111         if (catalogue != null) {
112             i18nAttrs.addCDATAAttribute(I18nTransformer.I18N_NAMESPACE_URI, I18nTransformer.I18N_CATALOGUE_ATTRIBUTE, "i18n:" + I18nTransformer.I18N_CATALOGUE_ATTRIBUTE, catalogue);
113         }
114
115         contentHandler.startElement(I18nTransformer.I18N_NAMESPACE_URI, I18nTransformer.I18N_TEXT_ELEMENT, "i18n:" + I18nTransformer.I18N_TEXT_ELEMENT, i18nAttrs);
116         contentHandler.characters(key.toCharArray(), 0, key.length());
117         contentHandler.endElement(I18nTransformer.I18N_NAMESPACE_URI, I18nTransformer.I18N_TEXT_ELEMENT, "i18n:" + I18nTransformer.I18N_TEXT_ELEMENT);
118
119         // the parameters
120
if (parameters != null) {
121             for (int i = 0; i < parameters.length; i++) {
122                 contentHandler.startElement(I18nTransformer.I18N_NAMESPACE_URI, I18nTransformer.I18N_PARAM_ELEMENT, "i18n:" + I18nTransformer.I18N_PARAM_ELEMENT, Constants.EMPTY_ATTRS);
123                 if (keys != null && keys[i])
124                     contentHandler.startElement(I18nTransformer.I18N_NAMESPACE_URI, I18nTransformer.I18N_TEXT_ELEMENT, "i18n:" + I18nTransformer.I18N_TEXT_ELEMENT, i18nAttrs);
125                 contentHandler.characters(parameters[i].toCharArray(), 0, parameters[i].length());
126                 if (keys != null && keys[i])
127                     contentHandler.endElement(I18nTransformer.I18N_NAMESPACE_URI, I18nTransformer.I18N_TEXT_ELEMENT, "i18n:" + I18nTransformer.I18N_TEXT_ELEMENT);
128                 contentHandler.endElement(I18nTransformer.I18N_NAMESPACE_URI, I18nTransformer.I18N_PARAM_ELEMENT, "i18n:" + I18nTransformer.I18N_PARAM_ELEMENT);
129             }
130             contentHandler.endElement(I18nTransformer.I18N_NAMESPACE_URI, I18nTransformer.I18N_TRANSLATE_ELEMENT, "i18n:" + I18nTransformer.I18N_TRANSLATE_ELEMENT);
131         }
132         contentHandler.endPrefixMapping("i18n");
133     }
134 }
135
Popular Tags