1 16 package org.apache.cocoon.forms.util; 17 18 import java.util.Arrays ; 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 ; 26 import org.xml.sax.SAXException ; 27 28 38 public class I18nMessage implements XMLizable { 39 private String key; 40 private String catalogue; 41 private String [] parameters; 42 private boolean[] keys; 43 44 47 public I18nMessage(String key) { 48 this(key, (String ) null); 49 } 50 51 55 public I18nMessage(String key, String catalogue) { 56 this.key = key; 57 this.catalogue = catalogue; 58 } 59 60 65 public I18nMessage(String key, String [] parameters) { 66 this(key, parameters, (String )null); 67 } 68 69 75 public I18nMessage(String key, String [] parameters, String catalogue) { 76 this.key = key; 77 this.parameters = parameters; 78 this.catalogue = catalogue; 79 } 80 81 88 public I18nMessage(String key, String [] parameters, boolean[] keys) { 89 this(key, parameters, keys, null); 90 } 91 92 100 public I18nMessage(String key, String [] parameters, boolean[] keys, String catalogue) { 101 this.key = key; 102 this.parameters = parameters; 103 this.keys = keys; 104 this.catalogue = catalogue; 105 } 106 107 public void toSAX(ContentHandler contentHandler) throws SAXException { 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 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 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 |