001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.forms.util;
018:
019: import java.util.Arrays;
020:
021: import org.apache.cocoon.transformation.I18nTransformer;
022: import org.apache.cocoon.xml.AttributesImpl;
023: import org.apache.cocoon.xml.XMLUtils;
024: import org.apache.commons.lang.ObjectUtils;
025: import org.apache.excalibur.xml.sax.XMLizable;
026: import org.xml.sax.ContentHandler;
027: import org.xml.sax.SAXException;
028:
029: /**
030: * A XMLizable implementation that will produce SAX events for the
031: * I18nTransformer in its toSAX method, based on the information
032: * given in the constructor.
033: *
034: * <p>This generates an autonomous SAX-blurb, i.e. all necessary namespace
035: * declarations will be made, and no start/endDocument events will be generated.
036: *
037: * @version $Id: I18nMessage.java 449149 2006-09-23 03:58:05Z crossley $
038: */
039: public class I18nMessage implements XMLizable {
040: private String key;
041: private String catalogue;
042: private String[] parameters;
043: private boolean[] keys;
044:
045: /**
046: * @param key a message key, to be translated by the I18nTransformer
047: */
048: public I18nMessage(String key) {
049: this (key, (String) null);
050: }
051:
052: /**
053: * @param key a message key, to be translated by the I18nTransformer
054: * @param catalogue a named I18nTransformer catalogue to use
055: */
056: public I18nMessage(String key, String catalogue) {
057: this .key = key;
058: this .catalogue = catalogue;
059: }
060:
061: /**
062: * @param key a message key, to be translated by the I18nTransformer
063: * @param parameters parameters to be substituted in the errorMessage (will be
064: * done by the I18nTransformer)
065: */
066: public I18nMessage(String key, String[] parameters) {
067: this (key, parameters, (String) null);
068: }
069:
070: /**
071: * @param key a message key, to be translated by the I18nTransformer
072: * @param parameters parameters to be substituted in the errorMessage (will be
073: * done by the I18nTransformer)
074: * @param catalogue a named I18nTransformer catalogue to use
075: */
076: public I18nMessage(String key, String[] parameters, String catalogue) {
077: this .key = key;
078: this .parameters = parameters;
079: this .catalogue = catalogue;
080: }
081:
082: /**
083: * @param key a message key, to be translated by the I18nTransformer
084: * @param parameters parameters to be substituted in the errorMessage (will be
085: * done by the I18nTransformer)
086: * @param keys Each element in the keys array corresponds to a string in the parameters array
087: * and indicates whether that parameter is in itself again a key.
088: */
089: public I18nMessage(String key, String[] parameters, boolean[] keys) {
090: this (key, parameters, keys, null);
091: }
092:
093: /**
094: * @param key a message key, to be translated by the I18nTransformer
095: * @param parameters parameters to be substituted in the errorMessage (will be
096: * done by the I18nTransformer)
097: * @param keys Each element in the keys array corresponds to a string in the parameters array
098: * and indicates whether that parameter is in itself again a key.
099: * @param catalogue a named I18nTransformer catalogue to use
100: */
101: public I18nMessage(String key, String[] parameters, boolean[] keys,
102: String catalogue) {
103: this .key = key;
104: this .parameters = parameters;
105: this .keys = keys;
106: this .catalogue = catalogue;
107: }
108:
109: public void toSAX(ContentHandler contentHandler)
110: throws SAXException {
111: contentHandler.startPrefixMapping("i18n",
112: I18nTransformer.I18N_NAMESPACE_URI);
113: if (parameters != null) {
114: contentHandler.startElement(
115: I18nTransformer.I18N_NAMESPACE_URI,
116: I18nTransformer.I18N_TRANSLATE_ELEMENT, "i18n:"
117: + I18nTransformer.I18N_TRANSLATE_ELEMENT,
118: XMLUtils.EMPTY_ATTRIBUTES);
119: }
120:
121: AttributesImpl i18nAttrs = new AttributesImpl();
122: if (catalogue != null) {
123: i18nAttrs.addCDATAAttribute(
124: I18nTransformer.I18N_NAMESPACE_URI,
125: I18nTransformer.I18N_CATALOGUE_ATTRIBUTE, "i18n:"
126: + I18nTransformer.I18N_CATALOGUE_ATTRIBUTE,
127: catalogue);
128: }
129:
130: contentHandler.startElement(I18nTransformer.I18N_NAMESPACE_URI,
131: I18nTransformer.I18N_TEXT_ELEMENT, "i18n:"
132: + I18nTransformer.I18N_TEXT_ELEMENT, i18nAttrs);
133: contentHandler.characters(key.toCharArray(), 0, key.length());
134: contentHandler.endElement(I18nTransformer.I18N_NAMESPACE_URI,
135: I18nTransformer.I18N_TEXT_ELEMENT, "i18n:"
136: + I18nTransformer.I18N_TEXT_ELEMENT);
137:
138: // the parameters
139: if (parameters != null) {
140: for (int i = 0; i < parameters.length; i++) {
141: contentHandler.startElement(
142: I18nTransformer.I18N_NAMESPACE_URI,
143: I18nTransformer.I18N_PARAM_ELEMENT, "i18n:"
144: + I18nTransformer.I18N_PARAM_ELEMENT,
145: XMLUtils.EMPTY_ATTRIBUTES);
146: if (keys != null && keys[i]) {
147: contentHandler
148: .startElement(
149: I18nTransformer.I18N_NAMESPACE_URI,
150: I18nTransformer.I18N_TEXT_ELEMENT,
151: "i18n:"
152: + I18nTransformer.I18N_TEXT_ELEMENT,
153: i18nAttrs);
154: }
155: final String aParam = String.valueOf(parameters[i]);
156: contentHandler.characters(aParam.toCharArray(), 0,
157: aParam.length());
158: if (keys != null && keys[i]) {
159: contentHandler
160: .endElement(
161: I18nTransformer.I18N_NAMESPACE_URI,
162: I18nTransformer.I18N_TEXT_ELEMENT,
163: "i18n:"
164: + I18nTransformer.I18N_TEXT_ELEMENT);
165: }
166: contentHandler.endElement(
167: I18nTransformer.I18N_NAMESPACE_URI,
168: I18nTransformer.I18N_PARAM_ELEMENT, "i18n:"
169: + I18nTransformer.I18N_PARAM_ELEMENT);
170: }
171: contentHandler.endElement(
172: I18nTransformer.I18N_NAMESPACE_URI,
173: I18nTransformer.I18N_TRANSLATE_ELEMENT, "i18n:"
174: + I18nTransformer.I18N_TRANSLATE_ELEMENT);
175: }
176: contentHandler.endPrefixMapping("i18n");
177: }
178:
179: public boolean equals(Object obj) {
180: if (obj instanceof I18nMessage) {
181: I18nMessage other = (I18nMessage) obj;
182: return ObjectUtils.equals(this .catalogue, other.catalogue)
183: && ObjectUtils.equals(this .key, other.key)
184: && Arrays.equals(this .keys, other.keys)
185: && Arrays.equals(this .parameters, other.parameters);
186: } else {
187: return false;
188: }
189: }
190: }
|