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.woody.util;
018:
019: import org.apache.cocoon.transformation.I18nTransformer;
020: import org.apache.cocoon.woody.Constants;
021: import org.apache.cocoon.xml.AttributesImpl;
022: import org.apache.excalibur.xml.sax.XMLizable;
023: import org.xml.sax.ContentHandler;
024: import org.xml.sax.SAXException;
025:
026: /**
027: * A XMLizable implementation that will produce SAX events for the
028: * I18nTransformer in its toSAX method, based on the information
029: * given in the constructor.
030: *
031: * <p>This generates an autonomous SAX-blurb, i.e. all necessary namespace
032: * declarations will be made, and no start/endDocument events will be generated.
033: *
034: * @version CVS $Id: I18nMessage.java 433543 2006-08-22 06:22:54Z crossley $
035: */
036: public class I18nMessage implements XMLizable {
037: private String key;
038: private String catalogue;
039: private String[] parameters;
040: private boolean[] keys;
041:
042: /**
043: * @param key a message key, to be translated by the I18nTransformer
044: */
045: public I18nMessage(String key) {
046: this (key, (String) null);
047: }
048:
049: /**
050: * @param key a message key, to be translated by the I18nTransformer
051: * @param catalogue a named I18nTransformer catalogue to use
052: */
053: public I18nMessage(String key, String catalogue) {
054: this .key = key;
055: this .catalogue = catalogue;
056: }
057:
058: /**
059: * @param key a message key, to be translated by the I18nTransformer
060: * @param parameters parameters to be substituted in the errorMessage (will be
061: * done by the I18nTransformer)
062: */
063: public I18nMessage(String key, String[] parameters) {
064: this (key, parameters, (String) null);
065: }
066:
067: /**
068: * @param key a message key, to be translated by the I18nTransformer
069: * @param parameters parameters to be substituted in the errorMessage (will be
070: * done by the I18nTransformer)
071: * @param catalogue a named I18nTransformer catalogue to use
072: */
073: public I18nMessage(String key, String[] parameters, String catalogue) {
074: this .key = key;
075: this .parameters = parameters;
076: this .catalogue = catalogue;
077: }
078:
079: /**
080: * @param key a message key, to be translated by the I18nTransformer
081: * @param parameters parameters to be substituted in the errorMessage (will be
082: * done by the I18nTransformer)
083: * @param keys Each element in the keys array corresponds to a string in the parameters array
084: * and indicates whether that parameter is in itself again a key.
085: */
086: public I18nMessage(String key, String[] parameters, boolean[] keys) {
087: this (key, parameters, keys, null);
088: }
089:
090: /**
091: * @param key a message key, to be translated by the I18nTransformer
092: * @param parameters parameters to be substituted in the errorMessage (will be
093: * done by the I18nTransformer)
094: * @param keys Each element in the keys array corresponds to a string in the parameters array
095: * and indicates whether that parameter is in itself again a key.
096: * @param catalogue a named I18nTransformer catalogue to use
097: */
098: public I18nMessage(String key, String[] parameters, boolean[] keys,
099: String catalogue) {
100: this .key = key;
101: this .parameters = parameters;
102: this .keys = keys;
103: this .catalogue = catalogue;
104: }
105:
106: public void toSAX(ContentHandler contentHandler)
107: throws SAXException {
108: contentHandler.startPrefixMapping("i18n",
109: I18nTransformer.I18N_NAMESPACE_URI);
110: if (parameters != null) {
111: contentHandler.startElement(
112: I18nTransformer.I18N_NAMESPACE_URI,
113: I18nTransformer.I18N_TRANSLATE_ELEMENT, "i18n:"
114: + I18nTransformer.I18N_TRANSLATE_ELEMENT,
115: Constants.EMPTY_ATTRS);
116: }
117:
118: AttributesImpl i18nAttrs = new AttributesImpl();
119: if (catalogue != null) {
120: i18nAttrs.addCDATAAttribute(
121: I18nTransformer.I18N_NAMESPACE_URI,
122: I18nTransformer.I18N_CATALOGUE_ATTRIBUTE, "i18n:"
123: + I18nTransformer.I18N_CATALOGUE_ATTRIBUTE,
124: catalogue);
125: }
126:
127: contentHandler.startElement(I18nTransformer.I18N_NAMESPACE_URI,
128: I18nTransformer.I18N_TEXT_ELEMENT, "i18n:"
129: + I18nTransformer.I18N_TEXT_ELEMENT, i18nAttrs);
130: contentHandler.characters(key.toCharArray(), 0, key.length());
131: contentHandler.endElement(I18nTransformer.I18N_NAMESPACE_URI,
132: I18nTransformer.I18N_TEXT_ELEMENT, "i18n:"
133: + I18nTransformer.I18N_TEXT_ELEMENT);
134:
135: // the parameters
136: if (parameters != null) {
137: for (int i = 0; i < parameters.length; i++) {
138: contentHandler.startElement(
139: I18nTransformer.I18N_NAMESPACE_URI,
140: I18nTransformer.I18N_PARAM_ELEMENT, "i18n:"
141: + I18nTransformer.I18N_PARAM_ELEMENT,
142: Constants.EMPTY_ATTRS);
143: if (keys != null && keys[i])
144: contentHandler
145: .startElement(
146: I18nTransformer.I18N_NAMESPACE_URI,
147: I18nTransformer.I18N_TEXT_ELEMENT,
148: "i18n:"
149: + I18nTransformer.I18N_TEXT_ELEMENT,
150: i18nAttrs);
151: contentHandler.characters(parameters[i].toCharArray(),
152: 0, parameters[i].length());
153: if (keys != null && keys[i])
154: contentHandler
155: .endElement(
156: I18nTransformer.I18N_NAMESPACE_URI,
157: I18nTransformer.I18N_TEXT_ELEMENT,
158: "i18n:"
159: + I18nTransformer.I18N_TEXT_ELEMENT);
160: contentHandler.endElement(
161: I18nTransformer.I18N_NAMESPACE_URI,
162: I18nTransformer.I18N_PARAM_ELEMENT, "i18n:"
163: + I18nTransformer.I18N_PARAM_ELEMENT);
164: }
165: contentHandler.endElement(
166: I18nTransformer.I18N_NAMESPACE_URI,
167: I18nTransformer.I18N_TRANSLATE_ELEMENT, "i18n:"
168: + I18nTransformer.I18N_TRANSLATE_ELEMENT);
169: }
170: contentHandler.endPrefixMapping("i18n");
171: }
172: }
|