KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > 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.forms.util;
17
18 import java.util.Arrays JavaDoc;
19
20 import org.apache.cocoon.transformation.I18nTransformer;
21 import org.apache.cocoon.xml.AttributesImpl;
22 import org.apache.cocoon.xml.XMLUtils;
23 import org.apache.commons.lang.ObjectUtils;
24 import org.apache.excalibur.xml.sax.XMLizable;
25 import org.xml.sax.ContentHandler JavaDoc;
26 import org.xml.sax.SAXException JavaDoc;
27
28 /**
29  * A XMLizable implementation that will produce SAX events for the
30  * I18nTransformer in its toSAX method, based on the information
31  * given in the constructor.
32  *
33  * <p>This generates an autonomous SAX-blurb, i.e. all necessary namespace
34  * declarations will be made, and no start/endDocument events will be generated.
35  *
36  * @version $Id: I18nMessage.java 177978 2005-05-23 13:27:51Z sylvain $
37  */

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

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

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

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

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

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

100     public I18nMessage(String JavaDoc key, String JavaDoc[] parameters, boolean[] keys, String JavaDoc catalogue) {
101         this.key = key;
102         this.parameters = parameters;
103         this.keys = keys;
104         this.catalogue = catalogue;
105     }
106
107     public void toSAX(ContentHandler JavaDoc contentHandler) throws SAXException JavaDoc {
108         contentHandler.startPrefixMapping("i18n", I18nTransformer.I18N_NAMESPACE_URI);
109         if (parameters != null) {
110             contentHandler.startElement(I18nTransformer.I18N_NAMESPACE_URI, I18nTransformer.I18N_TRANSLATE_ELEMENT, "i18n:" + I18nTransformer.I18N_TRANSLATE_ELEMENT, XMLUtils.EMPTY_ATTRIBUTES);
111         }
112
113         AttributesImpl i18nAttrs = new AttributesImpl();
114         if (catalogue != null) {
115             i18nAttrs.addCDATAAttribute(I18nTransformer.I18N_NAMESPACE_URI, I18nTransformer.I18N_CATALOGUE_ATTRIBUTE, "i18n:" + I18nTransformer.I18N_CATALOGUE_ATTRIBUTE, catalogue);
116         }
117
118         contentHandler.startElement(I18nTransformer.I18N_NAMESPACE_URI, I18nTransformer.I18N_TEXT_ELEMENT, "i18n:" + I18nTransformer.I18N_TEXT_ELEMENT, i18nAttrs);
119         contentHandler.characters(key.toCharArray(), 0, key.length());
120         contentHandler.endElement(I18nTransformer.I18N_NAMESPACE_URI, I18nTransformer.I18N_TEXT_ELEMENT, "i18n:" + I18nTransformer.I18N_TEXT_ELEMENT);
121
122         // the parameters
123
if (parameters != null) {
124             for (int i = 0; i < parameters.length; i++) {
125                 contentHandler.startElement(I18nTransformer.I18N_NAMESPACE_URI, I18nTransformer.I18N_PARAM_ELEMENT, "i18n:" + I18nTransformer.I18N_PARAM_ELEMENT, XMLUtils.EMPTY_ATTRIBUTES);
126                 if (keys != null && keys[i])
127                     contentHandler.startElement(I18nTransformer.I18N_NAMESPACE_URI, I18nTransformer.I18N_TEXT_ELEMENT, "i18n:" + I18nTransformer.I18N_TEXT_ELEMENT, i18nAttrs);
128                 contentHandler.characters(parameters[i].toCharArray(), 0, parameters[i].length());
129                 if (keys != null && keys[i])
130                     contentHandler.endElement(I18nTransformer.I18N_NAMESPACE_URI, I18nTransformer.I18N_TEXT_ELEMENT, "i18n:" + I18nTransformer.I18N_TEXT_ELEMENT);
131                 contentHandler.endElement(I18nTransformer.I18N_NAMESPACE_URI, I18nTransformer.I18N_PARAM_ELEMENT, "i18n:" + I18nTransformer.I18N_PARAM_ELEMENT);
132             }
133             contentHandler.endElement(I18nTransformer.I18N_NAMESPACE_URI, I18nTransformer.I18N_TRANSLATE_ELEMENT, "i18n:" + I18nTransformer.I18N_TRANSLATE_ELEMENT);
134         }
135         contentHandler.endPrefixMapping("i18n");
136     }
137     
138     public boolean equals(Object JavaDoc obj) {
139         if (obj instanceof I18nMessage) {
140             I18nMessage other = (I18nMessage)obj;
141             return ObjectUtils.equals(this.catalogue, other.catalogue) &&
142                    ObjectUtils.equals(this.key, other.key) &&
143                    Arrays.equals(this.keys, other.keys) &&
144                    Arrays.equals(this.parameters, other.parameters);
145         } else {
146             return false;
147         }
148     }
149 }
150
Popular Tags