Return-Path: Delivered-To: apmail-xml-cocoon-cvs-archive@xml.apache.org Received: (qmail 99506 invoked by uid 500); 25 Oct 2001 20:24:05 -0000 Mailing-List: contact cocoon-cvs-help@xml.apache.org; run by ezmlm Precedence: bulk Reply-To: cocoon-dev@xml.apache.org list-help: list-unsubscribe: list-post: Delivered-To: mailing list cocoon-cvs@xml.apache.org Received: (qmail 99496 invoked by uid 500); 25 Oct 2001 20:24:05 -0000 Delivered-To: apmail-xml-cocoon2-cvs@apache.org Date: 25 Oct 2001 20:15:38 -0000 Message-ID: <20011025201538.10735.qmail@icarus.apache.org> From: bloritsch@apache.org To: xml-cocoon2-cvs@apache.org Subject: cvs commit: xml-cocoon2/src/org/apache/cocoon/transformation I18nTransformer.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N bloritsch 01/10/25 13:15:38 Modified: src/org/apache/cocoon/transformation I18nTransformer.java Log: Optimize critical path And fix indentation Revision Changes Path 1.23 +494 -495 xml-cocoon2/src/org/apache/cocoon/transformation/I18nTransformer.java Index: I18nTransformer.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/org/apache/cocoon/transformation/I18nTransformer.java,v retrieving revision 1.22 retrieving revision 1.23 diff -u -r1.22 -r1.23 --- I18nTransformer.java 2001/10/11 07:28:24 1.22 +++ I18nTransformer.java 2001/10/25 20:15:38 1.23 @@ -183,17 +183,17 @@ * @author Michael Enke */ public class I18nTransformer extends AbstractTransformer - implements Composable, Poolable, Configurable { + implements Composable, Poolable, Configurable { - private static final String FILE = "file:"; - + private static final String FILE = "file:"; + protected ComponentManager manager; /** * The namespace for i18n is "http://apache.org/cocoon/i18n/2.0" */ public static final String I18N_NAMESPACE_URI = - "http://apache.org/cocoon/i18n/2.0"; + "http://apache.org/cocoon/i18n/2.0"; // // Dictionary elements and attributes @@ -310,7 +310,7 @@ /** * Message formatter for param substitution. */ - private MessageFormat formatter = new MessageFormat(""); + private MessageFormat formatter = new MessageFormat( "" ); /** * Current language id. @@ -344,43 +344,43 @@ /** * Configure this transformer. */ - public void configure(Configuration conf) - throws ConfigurationException { - if (conf != null) { + public void configure( Configuration conf ) + throws ConfigurationException { + if ( conf != null ) { // read in the config options from the transformer definition // obtain the base name of the message catalogue - Configuration child = conf.getChild(I18N_CATALOGUE_NAME); - catalogueName = child.getValue(null); - debug("Default catalogue name is " + catalogueName); + Configuration child = conf.getChild( I18N_CATALOGUE_NAME ); + catalogueName = child.getValue( null ); + debug( "Default catalogue name is " + catalogueName ); // obtain the directory location of message catalogues - child = conf.getChild(I18N_CATALOGUE_LOCATION); - catalogueLocation = child.getValue(null); - debug("Default catalogue location is " + catalogueLocation); + child = conf.getChild( I18N_CATALOGUE_LOCATION ); + catalogueLocation = child.getValue( null ); + debug( "Default catalogue location is " + catalogueLocation ); // check our mandatory parameters - if (catalogueName == null || catalogueLocation == null) - throw new ConfigurationException( - "I18nTransformer requires the name and location of " + - "the message catalogues" - ); + if ( catalogueName == null || catalogueLocation == null ) + throw new ConfigurationException( + "I18nTransformer requires the name and location of " + + "the message catalogues" + ); // obtain default text to use for untranslated messages - child = conf.getChild(I18N_UNTRANSLATED); - untranslated = child.getValue(I18N_UNTRANSLATED); - debug("Default untranslated text is '" + untranslated + "'"); + child = conf.getChild( I18N_UNTRANSLATED ); + untranslated = child.getValue( I18N_UNTRANSLATED ); + debug( "Default untranslated text is '" + untranslated + "'" ); // obtain config option, whether to cache messages at startup time - child = conf.getChild(I18N_CACHE_STARTUP); - cacheAtStartup = child.getValueAsBoolean(false); - debug((cacheAtStartup ? "will" : "won't") + - " cache messages during startup, by default" + child = conf.getChild( I18N_CACHE_STARTUP ); + cacheAtStartup = child.getValueAsBoolean( false ); + debug( ( cacheAtStartup ? "will" : "won't" ) + + " cache messages during startup, by default" ); // activate resource bundle logging - factory.setLogger(getLogger()); + factory.setLogger( getLogger() ); } } @@ -388,42 +388,42 @@ * Uses org.apache.cocoon.acting.LocaleAction.getLocale() * to get language user has selected. */ - public void setup(SourceResolver resolver, Map objectModel, String source, - Parameters parameters) - throws ProcessingException, SAXException, IOException { + public void setup( SourceResolver resolver, Map objectModel, String source, + Parameters parameters ) + throws ProcessingException, SAXException, IOException { try { // Set current language and locale - String lc = LocaleAction.getLocale(objectModel); + String lc = LocaleAction.getLocale( objectModel ); // configure the factory - _setup(resolver); + _setup( resolver ); // setup everything for the current locale - String[] matches = new RE("_").split(lc); + String[] matches = new RE( "_" ).split( lc ); String l = matches.length > 0 - ? matches[0] : Locale.getDefault().getLanguage(); + ? matches[0] : Locale.getDefault().getLanguage(); String c = matches.length > 1 ? matches[1] : ""; String v = matches.length > 2 ? matches[2] : ""; - Locale locale = new Locale(l, c, v); + Locale locale = new Locale( l, c, v ); - debug("using locale " + locale.toString()); + debug( "using locale " + locale.toString() ); dictionary = - (XMLResourceBundle) factory.select(catalogueName, locale); + (XMLResourceBundle) factory.select( catalogueName, locale ); //FIXME(DIMS): Why should i do this explicitly? Is there an alternative? - dictionary.compose(this.manager); + dictionary.compose( this.manager ); - debug("selected dictionary " + dictionary); + debug( "selected dictionary " + dictionary ); - setLocale(locale); + setLocale( locale ); - } catch(Exception e) { - debug("exception generated, leaving unconfigured"); - throw new ProcessingException(e.getMessage(), e); + } catch ( Exception e ) { + debug( "exception generated, leaving unconfigured" ); + throw new ProcessingException( e.getMessage(), e ); } } @@ -433,369 +433,369 @@ * REVISIT: when we can get the resolver anywhere, we can pass the * configuration object directly to XMLResourceBundle. */ - private void _setup(SourceResolver resolver) throws Exception { + private void _setup( SourceResolver resolver ) throws Exception { // configure the factory to log correctly and cache catalogues DefaultConfiguration configuration = - new DefaultConfiguration("name", "location"); + new DefaultConfiguration( "name", "location" ); DefaultConfiguration cacheConf = - new DefaultConfiguration( - XMLResourceBundleFactory.ConfigurationKeys.CACHE_AT_STARTUP, - "location" - ); - cacheConf.setValue(new Boolean(cacheAtStartup).toString()); - configuration.addChild(cacheConf); + new DefaultConfiguration( + XMLResourceBundleFactory.ConfigurationKeys.CACHE_AT_STARTUP, + "location" + ); + cacheConf.setValue( new Boolean( cacheAtStartup ).toString() ); + configuration.addChild( cacheConf ); // set the root location for message catalogues DefaultConfiguration dirConf = - new DefaultConfiguration( - XMLResourceBundleFactory.ConfigurationKeys.ROOT_DIRECTORY, - "location" - ); + new DefaultConfiguration( + XMLResourceBundleFactory.ConfigurationKeys.ROOT_DIRECTORY, + "location" + ); - debug("catalog location:" + catalogueLocation); - Source source = resolver.resolve(catalogueLocation); + debug( "catalog location:" + catalogueLocation ); + Source source = resolver.resolve( catalogueLocation ); String systemId = source.getSystemId(); - if (!systemId.startsWith(FILE)) { - throw new ResourceNotFoundException(systemId + " does not denote a directory"); + if ( !systemId.startsWith( FILE ) ) { + throw new ResourceNotFoundException( systemId + " does not denote a directory" ); } - debug("catalog directory:" + systemId); - dirConf.setValue(systemId); + debug( "catalog directory:" + systemId ); + dirConf.setValue( systemId ); - configuration.addChild(dirConf); - factory.configure(configuration); + configuration.addChild( dirConf ); + factory.configure( configuration ); - debug("configured"); + debug( "configured" ); } - public void compose(ComponentManager manager) { + public void compose( ComponentManager manager ) { this.manager = manager; } - public void startElement(String uri, String name, String raw, - Attributes attr) throws SAXException { + public void startElement( String uri, String name, String raw, + Attributes attr ) throws SAXException { - if (I18N_NAMESPACE_URI.equals(uri)) { - debug("Starting i18n element: " + name); - startI18NElement(name, attr); + if ( I18N_NAMESPACE_URI.equals( uri ) ) { + debug( "Starting i18n element: " + name ); + startI18NElement( name, attr ); return; } - super.startElement(uri, name, raw, translateAttributes(name, attr)); + super.startElement( uri, name, raw, translateAttributes( name, attr ) ); } - public void endElement(String uri, String name, String raw) - throws SAXException { + public void endElement( String uri, String name, String raw ) + throws SAXException { - if (I18N_NAMESPACE_URI.equals(uri)) { - endI18NElement(name); + if ( I18N_NAMESPACE_URI.equals( uri ) ) { + endI18NElement( name ); return; } - super.endElement(uri, name, raw); + super.endElement( uri, name, raw ); } - public void characters(char[] ch, int start, int len) throws SAXException { + public void characters( char[] ch, int start, int len ) throws SAXException { - if (current_state != STATE_OUTSIDE) { - i18nCharacters(ch, start, len); + if ( current_state != STATE_OUTSIDE ) { + i18nCharacters( ch, start, len ); return; } - super.characters(ch, start, len); + super.characters( ch, start, len ); } // My own content handlers - private void startI18NElement(String name, Attributes attr) - throws SAXException { - debug("Start i18n element: " + name); + private void startI18NElement( String name, Attributes attr ) + throws SAXException { + debug( "Start i18n element: " + name ); try { - if (I18N_TEXT_ELEMENT.equals(name)) { - if (current_state != STATE_OUTSIDE + if ( I18N_TEXT_ELEMENT.equals( name ) ) { + if ( current_state != STATE_OUTSIDE && current_state != STATE_INSIDE_PARAM - && current_state != STATE_INSIDE_TRANSLATE) { - throw new SAXException(this.getClass().getName() - + ": nested i18n:text elements are not allowed. Current state: " + current_state); + && current_state != STATE_INSIDE_TRANSLATE ) { + throw new SAXException( this.getClass().getName() + + ": nested i18n:text elements are not allowed. Current state: " + current_state ); } prev_state = current_state; current_state = STATE_INSIDE_TEXT; - current_key = attr.getValue(I18N_NAMESPACE_URI, I18N_KEY_ATTRIBUTE); - } else if (I18N_TRANSLATE_ELEMENT.equals(name)) { - if (current_state != STATE_OUTSIDE) { - throw new SAXException(this.getClass().getName() - + ": i18n:translate element must be used " - + "outside of other i18n elements. Current state: " + current_state); + current_key = attr.getValue( I18N_NAMESPACE_URI, I18N_KEY_ATTRIBUTE ); + } else if ( I18N_TRANSLATE_ELEMENT.equals( name ) ) { + if ( current_state != STATE_OUTSIDE ) { + throw new SAXException( this.getClass().getName() + + ": i18n:translate element must be used " + + "outside of other i18n elements. Current state: " + current_state ); } current_state = STATE_INSIDE_TRANSLATE; - } else if (I18N_PARAM_ELEMENT.equals(name)) { - if (current_state != STATE_INSIDE_TRANSLATE) { - throw new SAXException(this.getClass().getName() - + ": i18n:param element can be used only inside " - + "i18n:translate element. Current state: " + current_state); + } else if ( I18N_PARAM_ELEMENT.equals( name ) ) { + if ( current_state != STATE_INSIDE_TRANSLATE ) { + throw new SAXException( this.getClass().getName() + + ": i18n:param element can be used only inside " + + "i18n:translate element. Current state: " + current_state ); } - setFormattingParams(attr); + setFormattingParams( attr ); current_state = STATE_INSIDE_PARAM; - } else if (I18N_DATE_ELEMENT.equals(name)) { - if (current_state != STATE_OUTSIDE) { - throw new SAXException(this.getClass().getName() - + ": i18n:date elements are not allowed " - + "inside of other i18n elements."); + } else if ( I18N_DATE_ELEMENT.equals( name ) ) { + if ( current_state != STATE_OUTSIDE ) { + throw new SAXException( this.getClass().getName() + + ": i18n:date elements are not allowed " + + "inside of other i18n elements." ); } - setFormattingParams(attr); + setFormattingParams( attr ); current_state = STATE_INSIDE_DATE; - } else if (I18N_DATE_TIME_ELEMENT.equals(name)) { - if (current_state != STATE_OUTSIDE) { - throw new SAXException(this.getClass().getName() - + ": i18n:date-time elements are not allowed " - + "inside of other i18n elements."); + } else if ( I18N_DATE_TIME_ELEMENT.equals( name ) ) { + if ( current_state != STATE_OUTSIDE ) { + throw new SAXException( this.getClass().getName() + + ": i18n:date-time elements are not allowed " + + "inside of other i18n elements." ); } - setFormattingParams(attr); + setFormattingParams( attr ); current_state = STATE_INSIDE_DATE_TIME; - } else if (I18N_TIME_ELEMENT.equals(name)) { - if (current_state != STATE_OUTSIDE) { - throw new SAXException(this.getClass().getName() - + ": i18n:date elements are not allowed " - + "inside of other i18n elements."); + } else if ( I18N_TIME_ELEMENT.equals( name ) ) { + if ( current_state != STATE_OUTSIDE ) { + throw new SAXException( this.getClass().getName() + + ": i18n:date elements are not allowed " + + "inside of other i18n elements." ); } - setFormattingParams(attr); + setFormattingParams( attr ); current_state = STATE_INSIDE_TIME; - } else if (I18N_NUMBER_ELEMENT.equals(name)) { - if (current_state != STATE_OUTSIDE) { - throw new SAXException(this.getClass().getName() - + ": i18n:number elements are not allowed " - + "inside of other i18n elements."); + } else if ( I18N_NUMBER_ELEMENT.equals( name ) ) { + if ( current_state != STATE_OUTSIDE ) { + throw new SAXException( this.getClass().getName() + + ": i18n:number elements are not allowed " + + "inside of other i18n elements." ); } - setFormattingParams(attr); + setFormattingParams( attr ); current_state = STATE_INSIDE_NUMBER; } - } catch (Exception e) { + } catch ( Exception e ) { // we need it to avoid further errors if an exception occurs current_state = STATE_OUTSIDE; - throw new SAXException(this.getClass().getName() - + ": error in format", e); + throw new SAXException( this.getClass().getName() + + ": error in format", e ); } } /** * Get src-pattern, pattern and value attribute values and store in a Map */ - private void setFormattingParams(Attributes attr) throws SAXException { - formattingParams = new HashMap(3); + private void setFormattingParams( Attributes attr ) throws SAXException { + formattingParams = new HashMap( 3 ); - String attr_value = attr.getValue(I18N_SRC_PATTERN_ATTRIBUTE); - if (attr_value != null) { - formattingParams.put(I18N_SRC_PATTERN_ATTRIBUTE, attr_value); + String attr_value = attr.getValue( I18N_SRC_PATTERN_ATTRIBUTE ); + if ( attr_value != null ) { + formattingParams.put( I18N_SRC_PATTERN_ATTRIBUTE, attr_value ); } - attr_value = attr.getValue(I18N_PATTERN_ATTRIBUTE); - if (attr_value != null) { - formattingParams.put(I18N_PATTERN_ATTRIBUTE, attr_value); + attr_value = attr.getValue( I18N_PATTERN_ATTRIBUTE ); + if ( attr_value != null ) { + formattingParams.put( I18N_PATTERN_ATTRIBUTE, attr_value ); } - attr_value = attr.getValue(I18N_VALUE_ATTRIBUTE); - if (attr_value != null) { - formattingParams.put(I18N_VALUE_ATTRIBUTE, attr_value); + attr_value = attr.getValue( I18N_VALUE_ATTRIBUTE ); + if ( attr_value != null ) { + formattingParams.put( I18N_VALUE_ATTRIBUTE, attr_value ); } - attr_value = attr.getValue(I18N_LOCALE_ATTRIBUTE); - if (attr_value != null) { - formattingParams.put(I18N_LOCALE_ATTRIBUTE, attr_value); + attr_value = attr.getValue( I18N_LOCALE_ATTRIBUTE ); + if ( attr_value != null ) { + formattingParams.put( I18N_LOCALE_ATTRIBUTE, attr_value ); } - attr_value = attr.getValue(I18N_SRC_LOCALE_ATTRIBUTE); - if (attr_value != null) { - formattingParams.put(I18N_SRC_LOCALE_ATTRIBUTE, attr_value); + attr_value = attr.getValue( I18N_SRC_LOCALE_ATTRIBUTE ); + if ( attr_value != null ) { + formattingParams.put( I18N_SRC_LOCALE_ATTRIBUTE, attr_value ); } - attr_value = attr.getValue(I18N_TYPE_ATTRIBUTE); - if (attr_value != null) { - formattingParams.put(I18N_TYPE_ATTRIBUTE, attr_value); + attr_value = attr.getValue( I18N_TYPE_ATTRIBUTE ); + if ( attr_value != null ) { + formattingParams.put( I18N_TYPE_ATTRIBUTE, attr_value ); } - attr_value = attr.getValue(I18N_SUB_TYPE_ATTRIBUTE); - if (attr_value != null) { - formattingParams.put(I18N_SUB_TYPE_ATTRIBUTE, attr_value); + attr_value = attr.getValue( I18N_SUB_TYPE_ATTRIBUTE ); + if ( attr_value != null ) { + formattingParams.put( I18N_SUB_TYPE_ATTRIBUTE, attr_value ); } } - private void endI18NElement(String name) throws SAXException { - debug("End i18n element: " + name); + private void endI18NElement( String name ) throws SAXException { + debug( "End i18n element: " + name ); try { - switch (current_state) { - case STATE_INSIDE_TEXT: - { - endTextElement(); - break; - } - case STATE_INSIDE_TRANSLATE: - { - endTranslateElement(); - break; - } - case STATE_INSIDE_PARAM: - { - endParamElement(); - break; - } - case STATE_INSIDE_DATE: - case STATE_INSIDE_DATE_TIME: - case STATE_INSIDE_TIME: - { - endDate_TimeElement(); - break; - } - case STATE_INSIDE_NUMBER: - { - endNumberElement(); - break; - } + switch ( current_state ) { + case STATE_INSIDE_TEXT: + { + endTextElement(); + break; + } + case STATE_INSIDE_TRANSLATE: + { + endTranslateElement(); + break; + } + case STATE_INSIDE_PARAM: + { + endParamElement(); + break; + } + case STATE_INSIDE_DATE: + case STATE_INSIDE_DATE_TIME: + case STATE_INSIDE_TIME: + { + endDate_TimeElement(); + break; + } + case STATE_INSIDE_NUMBER: + { + endNumberElement(); + break; + } } - } catch (Exception e) { + } catch ( Exception e ) { // we need it to avoid further errors if an exception occurs current_state = STATE_OUTSIDE; - throw new SAXException(this.getClass().getName() - + ": error in format", e); + throw new SAXException( this.getClass().getName() + + ": error in format", e ); } } /* */ - private String stripWhitespace(String s) { + private String stripWhitespace( String s ) { // FIXME (KP) Must be a better way to determine whitespace-only nodes. // trim() function does not remove spaces if string does not contain // anything else. - if (s == null) { + if ( s == null ) { return null; } - String result = (s + "!").trim(); - return result.substring(0, result.length() - 1); + String result = ( s + "!" ).trim(); + return result.substring( 0, result.length() - 1 ); } - private void i18nCharacters(char[] ch, int start, int len) - throws SAXException { + private void i18nCharacters( char[] ch, int start, int len ) + throws SAXException { - String key = new String(ch, start, len); - key = stripWhitespace(key); - if (key == null || key.length() == 0) { + String key = new String( ch, start, len ); + key = stripWhitespace( key ); + if ( key == null || key.length() == 0 ) { return; } - debug("i18n message key = '" + key + "'"); + debug( "i18n message key = '" + key + "'" ); - switch (current_state) { - case STATE_INSIDE_TEXT: - { - if (current_key != null) { - try { - translated_text = getString(current_key); - } catch (MissingResourceException e) { - translated_text = untranslated; - } - if (translated_text == null) { - translated_text = key; - } - current_key = null; - } else { - try { - translated_text = getString(key); - } catch (MissingResourceException e) { - translated_text = untranslated; + switch ( current_state ) { + case STATE_INSIDE_TEXT: + { + if ( current_key != null ) { + try { + translated_text = getString( current_key ); + } catch ( MissingResourceException e ) { + translated_text = untranslated; + } + if ( translated_text == null ) { + translated_text = key; + } + current_key = null; + } else { + try { + translated_text = getString( key ); + } catch ( MissingResourceException e ) { + translated_text = untranslated; + } } - } - break; - } - case STATE_INSIDE_TRANSLATE: - { - // Store text for param substitution (do not translate) - if (substitute_text == null) { - substitute_text = key; + break; } - break; - } - case STATE_INSIDE_PARAM: - { - // Store translation for param substitution - if (param_value == null) { - param_value = key; + case STATE_INSIDE_TRANSLATE: + { + // Store text for param substitution (do not translate) + if ( substitute_text == null ) { + substitute_text = key; + } + break; } - break; - } - case STATE_INSIDE_DATE: - case STATE_INSIDE_DATE_TIME: - case STATE_INSIDE_TIME: - case STATE_INSIDE_NUMBER: - { - if (formattingParams != null) { - if (formattingParams.get(I18N_VALUE_ATTRIBUTE) == null) { - formattingParams.put(I18N_VALUE_ATTRIBUTE, key); - } else { - // how to use the text inside of date element? + case STATE_INSIDE_PARAM: + { + // Store translation for param substitution + if ( param_value == null ) { + param_value = key; } + break; + } + case STATE_INSIDE_DATE: + case STATE_INSIDE_DATE_TIME: + case STATE_INSIDE_TIME: + case STATE_INSIDE_NUMBER: + { + if ( formattingParams != null ) { + if ( formattingParams.get( I18N_VALUE_ATTRIBUTE ) == null ) { + formattingParams.put( I18N_VALUE_ATTRIBUTE, key ); + } else { + // how to use the text inside of date element? + } + } + break; } - break; - } - default: - { - throw new SAXException(this.getClass().getName() - + "Something's really wrong!!!"); - } + default: + { + throw new SAXException( this.getClass().getName() + + "Something's really wrong!!!" ); + } } } - private Attributes translateAttributes(String name, Attributes attr) - throws SAXException { - if (attr == null) { + private Attributes translateAttributes( String name, Attributes attr ) + throws SAXException { + if ( attr == null ) { return attr; } - AttributesImpl temp_attr = new AttributesImpl(attr); + AttributesImpl temp_attr = new AttributesImpl( attr ); // Translate all attributes from i18n:attr="name1 name2 ..." // using their values as keys int i18n_attr_index = - temp_attr.getIndex(I18N_NAMESPACE_URI, I18N_ATTR_ATTRIBUTE); + temp_attr.getIndex( I18N_NAMESPACE_URI, I18N_ATTR_ATTRIBUTE ); - if (i18n_attr_index != -1) { + if ( i18n_attr_index != -1 ) { StringTokenizer st = - new StringTokenizer(temp_attr.getValue(i18n_attr_index)); + new StringTokenizer( temp_attr.getValue( i18n_attr_index ) ); // remove the i18n:attr attribute - we don't need it - temp_attr.removeAttribute(i18n_attr_index); - while (st.hasMoreElements()) { + temp_attr.removeAttribute( i18n_attr_index ); + while ( st.hasMoreElements() ) { // translate all listed attributes String attr_name = st.nextToken(); - int attr_index = temp_attr.getIndex(attr_name); + int attr_index = temp_attr.getIndex( attr_name ); - if (attr_index != -1) { - String text2translate = temp_attr.getValue(attr_index); + if ( attr_index != -1 ) { + String text2translate = temp_attr.getValue( attr_index ); String result; try { - result = getString(text2translate); - } catch (MissingResourceException e) { + result = getString( text2translate ); + } catch ( MissingResourceException e ) { result = untranslated; } // set the translated value - if (result != null) { - temp_attr.setValue(attr_index, result); + if ( result != null ) { + temp_attr.setValue( attr_index, result ); } else { - getLogger().warn("translation not found for attribute " - + attr_name + " in element: " + name); + getLogger().warn( "translation not found for attribute " + + attr_name + " in element: " + name ); } } else { - getLogger().warn("i18n attribute '" + attr_name - + "' not found in element: " + name); + getLogger().warn( "i18n attribute '" + attr_name + + "' not found in element: " + name ); } } return temp_attr; @@ -805,30 +805,30 @@ } private void endTextElement() throws SAXException { - debug("End text element, translated_text: " + translated_text); - switch (prev_state) { - case STATE_OUTSIDE: - { - // simply translate text (key translation already performed) - if (translated_text != null) { - super.contentHandler.characters(translated_text.toCharArray(), - 0, translated_text.length()); - } else { - // else - translation not found - debug("--- Translation not found! ---"); + debug( "End text element, translated_text: " + translated_text ); + switch ( prev_state ) { + case STATE_OUTSIDE: + { + // simply translate text (key translation already performed) + if ( translated_text != null ) { + super.contentHandler.characters( translated_text.toCharArray(), + 0, translated_text.length() ); + } else { + // else - translation not found + debug( "--- Translation not found! ---" ); + } + break; } - break; - } - case STATE_INSIDE_TRANSLATE: - { - substitute_text = translated_text; - break; - } - case STATE_INSIDE_PARAM: - { - param_value = translated_text; - break; - } + case STATE_INSIDE_TRANSLATE: + { + substitute_text = translated_text; + break; + } + case STATE_INSIDE_PARAM: + { + param_value = translated_text; + break; + } } translated_text = null; current_state = prev_state; @@ -836,327 +836,326 @@ } private void endParamElement() throws SAXException { - debug("Substitution param: " + param_value); - if (formattingParams != null) { - String paramType = (String)formattingParams.get(I18N_TYPE_ATTRIBUTE); - if (paramType != null) { - debug("Param type: " + paramType); - if (formattingParams.get(I18N_VALUE_ATTRIBUTE) == null - && param_value != null) { - debug("Put param value: " + param_value); - formattingParams.put(I18N_VALUE_ATTRIBUTE, param_value); - } - if ("date".equals(paramType) || - "date-time".equals(paramType) || - "time".equals(paramType)) { - debug("Formatting date_time param: " + formattingParams); - param_value = formatDate_Time(formattingParams); - } else if ("number".equals(paramType)) { - debug("Formatting number param: " + formattingParams); - param_value = formatNumber(formattingParams); + debug( "Substitution param: " + param_value ); + if ( formattingParams != null ) { + String paramType = (String) formattingParams.get( I18N_TYPE_ATTRIBUTE ); + if ( paramType != null ) { + debug( "Param type: " + paramType ); + if ( formattingParams.get( I18N_VALUE_ATTRIBUTE ) == null + && param_value != null ) { + debug( "Put param value: " + param_value ); + formattingParams.put( I18N_VALUE_ATTRIBUTE, param_value ); + } + if ( "date".equals( paramType ) || + "date-time".equals( paramType ) || + "time".equals( paramType ) ) { + debug( "Formatting date_time param: " + formattingParams ); + param_value = formatDate_Time( formattingParams ); + } else if ( "number".equals( paramType ) ) { + debug( "Formatting number param: " + formattingParams ); + param_value = formatNumber( formattingParams ); } } } - debug("Added substitution param: " + param_value); - indexedParams.add(param_value); + debug( "Added substitution param: " + param_value ); + indexedParams.add( param_value ); param_value = null; current_state = STATE_INSIDE_TRANSLATE; } private void endTranslateElement() throws SAXException { - if (substitute_text == null) { + if ( substitute_text == null ) { return; } String result; - if (indexedParams.size() > 0 && substitute_text.length() > 0) { - debug("Text for susbtitution: " + substitute_text); - result = formatter.format(substitute_text, indexedParams.toArray()); - debug("Result of susbtitution: " + result); + if ( indexedParams.size() > 0 && substitute_text.length() > 0 ) { + debug( "Text for susbtitution: " + substitute_text ); + result = formatter.format( substitute_text, indexedParams.toArray() ); + debug( "Result of susbtitution: " + result ); } else { result = substitute_text; } - super.contentHandler.characters(result.toCharArray(), 0, result.length()); + super.contentHandler.characters( result.toCharArray(), 0, result.length() ); indexedParams.clear(); substitute_text = null; current_state = STATE_OUTSIDE; } private void endDate_TimeElement() throws SAXException { - String result = formatDate_Time(formattingParams); - super.contentHandler.characters(result.toCharArray(), 0, result.length()); + String result = formatDate_Time( formattingParams ); + super.contentHandler.characters( result.toCharArray(), 0, result.length() ); current_state = STATE_OUTSIDE; } - private Locale getLocale(Map params, String attribute) { - Locale locale = this.locale; + private Locale getLocale( Map params, String attribute ) { + Locale locale = this.locale; // the specific locale value - String lc = (String)params.get(attribute); - if(lc != null) try { - - String[] matches = new RE("_").split(lc); - String l = matches.length > 0 - ? matches[0] : Locale.getDefault().getLanguage(); - String c = matches.length > 1 ? matches[1] : ""; - String v = matches.length > 2 ? matches[2] : ""; - locale = new Locale(l, c, v); - } - catch(Exception e) {} - return locale; + String lc = (String) params.get( attribute ); + if ( lc != null ) + try { + + String[] matches = new RE( "_" ).split( lc ); + String l = matches.length > 0 + ? matches[0] : Locale.getDefault().getLanguage(); + String c = matches.length > 1 ? matches[1] : ""; + String v = matches.length > 2 ? matches[2] : ""; + locale = new Locale( l, c, v ); + } catch ( Exception e ) { + } + return locale; } - - private String formatDate_Time(Map params) throws SAXException { + + private String formatDate_Time( Map params ) throws SAXException { SimpleDateFormat to_fmt = null, from_fmt; - String element = null; - int srcStyle = DateFormat.SHORT, style = DateFormat.SHORT; - boolean realPattern = false, realSrcPattern = false; + String element = null; + int srcStyle = DateFormat.SHORT, style = DateFormat.SHORT; + boolean realPattern = false, realSrcPattern = false; - if (params == null) { - throw new SAXException(this.getClass().getName() - + ": i18n:"+element+" - error in element attributes."); + if ( params == null ) { + throw new SAXException( this.getClass().getName() + + ": i18n:" + element + " - error in element attributes." ); } - loc = getLocale(params, I18N_LOCALE_ATTRIBUTE); - srcLoc = getLocale(params, I18N_SRC_LOCALE_ATTRIBUTE); + loc = getLocale( params, I18N_LOCALE_ATTRIBUTE ); + srcLoc = getLocale( params, I18N_SRC_LOCALE_ATTRIBUTE ); // from pattern - String srcPattern = (String)params.get(I18N_SRC_PATTERN_ATTRIBUTE); + String srcPattern = (String) params.get( I18N_SRC_PATTERN_ATTRIBUTE ); // to pattern - String pattern = (String)params.get(I18N_PATTERN_ATTRIBUTE); + String pattern = (String) params.get( I18N_PATTERN_ATTRIBUTE ); // the date value - String value = (String)params.get(I18N_VALUE_ATTRIBUTE); + String value = (String) params.get( I18N_VALUE_ATTRIBUTE ); - if(srcPattern == null) { - srcStyle = DateFormat.DEFAULT; - } - else { - if(srcPattern.equalsIgnoreCase("short")) - srcStyle = DateFormat.SHORT; - else if(srcPattern.equalsIgnoreCase("medium")) - srcStyle = DateFormat.MEDIUM; - else if(srcPattern.equalsIgnoreCase("long")) - srcStyle = DateFormat.LONG; - else if(srcPattern.equalsIgnoreCase("full")) - srcStyle = DateFormat.FULL; - /* really a src-pattern */ - else - realSrcPattern = true; - } - if(pattern == null) { - style = DateFormat.DEFAULT; - } - else { - if(pattern.equalsIgnoreCase("short")) - style = DateFormat.SHORT; - else if(pattern.equalsIgnoreCase("medium")) - style = DateFormat.MEDIUM; - else if(pattern.equalsIgnoreCase("long")) - style = DateFormat.LONG; - else if(pattern.equalsIgnoreCase("full")) - style = DateFormat.FULL; - /* really a pattern */ - else - realPattern = true; - } - - if (current_state == STATE_INSIDE_DATE) { - element = I18N_DATE_ELEMENT; - to_fmt = (SimpleDateFormat)DateFormat. - getDateInstance(style, loc); - from_fmt = (SimpleDateFormat)DateFormat. - getDateInstance(srcStyle, srcLoc); - } - else if (current_state == STATE_INSIDE_DATE_TIME) { - element = I18N_DATE_TIME_ELEMENT; - to_fmt = (SimpleDateFormat)DateFormat. - getDateTimeInstance(style, style, loc); - from_fmt = (SimpleDateFormat)DateFormat. - getDateTimeInstance(srcStyle, srcStyle, srcLoc); - } - else { /* STATE_INSIDE_TIME */ - element = I18N_TIME_ELEMENT; - to_fmt = (SimpleDateFormat)DateFormat. - getTimeInstance(style, loc); - from_fmt = (SimpleDateFormat)DateFormat. - getTimeInstance(srcStyle, srcLoc); - } + if ( srcPattern == null ) { + srcStyle = DateFormat.DEFAULT; + } else { + if ( srcPattern.equalsIgnoreCase( "short" ) ) + srcStyle = DateFormat.SHORT; + else if ( srcPattern.equalsIgnoreCase( "medium" ) ) + srcStyle = DateFormat.MEDIUM; + else if ( srcPattern.equalsIgnoreCase( "long" ) ) + srcStyle = DateFormat.LONG; + else if ( srcPattern.equalsIgnoreCase( "full" ) ) + srcStyle = DateFormat.FULL; + /* really a src-pattern */ + else + realSrcPattern = true; + } + if ( pattern == null ) { + style = DateFormat.DEFAULT; + } else { + if ( pattern.equalsIgnoreCase( "short" ) ) + style = DateFormat.SHORT; + else if ( pattern.equalsIgnoreCase( "medium" ) ) + style = DateFormat.MEDIUM; + else if ( pattern.equalsIgnoreCase( "long" ) ) + style = DateFormat.LONG; + else if ( pattern.equalsIgnoreCase( "full" ) ) + style = DateFormat.FULL; + /* really a pattern */ + else + realPattern = true; + } + + if ( current_state == STATE_INSIDE_DATE ) { + element = I18N_DATE_ELEMENT; + to_fmt = (SimpleDateFormat) DateFormat. + getDateInstance( style, loc ); + from_fmt = (SimpleDateFormat) DateFormat. + getDateInstance( srcStyle, srcLoc ); + } else if ( current_state == STATE_INSIDE_DATE_TIME ) { + element = I18N_DATE_TIME_ELEMENT; + to_fmt = (SimpleDateFormat) DateFormat. + getDateTimeInstance( style, style, loc ); + from_fmt = (SimpleDateFormat) DateFormat. + getDateTimeInstance( srcStyle, srcStyle, srcLoc ); + } else { + /* STATE_INSIDE_TIME */ + element = I18N_TIME_ELEMENT; + to_fmt = (SimpleDateFormat) DateFormat. + getTimeInstance( style, loc ); + from_fmt = (SimpleDateFormat) DateFormat. + getTimeInstance( srcStyle, srcLoc ); + } // parsed date object Date dateValue = null; - // pattern overwrites locale format - if (realSrcPattern) { - from_fmt.applyPattern(srcPattern); + // pattern overwrites locale format + if ( realSrcPattern ) { + from_fmt.applyPattern( srcPattern ); } - if (realPattern) { - to_fmt.applyPattern(pattern); + if ( realPattern ) { + to_fmt.applyPattern( pattern ); } // get current date and time by default - if (value == null) { + if ( value == null ) { dateValue = new Date(); } else { try { - dateValue = from_fmt.parse(value); - } catch (ParseException pe) { - throw new SAXException(this.getClass().getName() - + "i18n:date - parsing error.", pe); + dateValue = from_fmt.parse( value ); + } catch ( ParseException pe ) { + throw new SAXException( this.getClass().getName() + + "i18n:date - parsing error.", pe ); } } // we have all necessary data here: do formatting. - String result = to_fmt.format(dateValue); - debug("i18n:"+element+" result: " + result); + String result = to_fmt.format( dateValue ); + debug( "i18n:" + element + " result: " + result ); return result; } private void endNumberElement() throws SAXException { - String result = formatNumber(formattingParams); - super.contentHandler.characters(result.toCharArray(), 0, result.length()); + String result = formatNumber( formattingParams ); + super.contentHandler.characters( result.toCharArray(), 0, result.length() ); current_state = STATE_OUTSIDE; } - private String formatNumber(Map params) throws SAXException { - if (params == null) { - throw new SAXException(this.getClass().getName() - + ": i18n:number - error in element attributes."); + private String formatNumber( Map params ) throws SAXException { + if ( params == null ) { + throw new SAXException( this.getClass().getName() + + ": i18n:number - error in element attributes." ); } // from pattern - String srcPattern = (String)params.get(I18N_SRC_PATTERN_ATTRIBUTE); + String srcPattern = (String) params.get( I18N_SRC_PATTERN_ATTRIBUTE ); // to pattern - String pattern = (String)params.get(I18N_PATTERN_ATTRIBUTE); + String pattern = (String) params.get( I18N_PATTERN_ATTRIBUTE ); // the number value - String value = (String)params.get(I18N_VALUE_ATTRIBUTE); + String value = (String) params.get( I18N_VALUE_ATTRIBUTE ); // sub-type - String subType = (String)params.get(I18N_SUB_TYPE_ATTRIBUTE); + String subType = (String) params.get( I18N_SUB_TYPE_ATTRIBUTE ); // parsed number Number numberValue = null; - // locale, may be switched locale - loc = getLocale(params, I18N_LOCALE_ATTRIBUTE); - srcLoc = getLocale(params, I18N_SRC_LOCALE_ATTRIBUTE); + // locale, may be switched locale + loc = getLocale( params, I18N_LOCALE_ATTRIBUTE ); + srcLoc = getLocale( params, I18N_SRC_LOCALE_ATTRIBUTE ); // src format - DecimalFormat from_fmt = (DecimalFormat)NumberFormat.getInstance(srcLoc); - int int_currency = 0; + DecimalFormat from_fmt = (DecimalFormat) NumberFormat.getInstance( srcLoc ); + int int_currency = 0; - // src-pattern overwrites locale format - if (srcPattern != null) { - from_fmt.applyPattern(srcPattern); + // src-pattern overwrites locale format + if ( srcPattern != null ) { + from_fmt.applyPattern( srcPattern ); } - // to format + // to format DecimalFormat to_fmt = null; char dec = from_fmt.getDecimalFormatSymbols().getDecimalSeparator(); int decAt = 0; boolean appendDec = false; - if (subType == null) { - to_fmt = (DecimalFormat)NumberFormat.getInstance(loc); - to_fmt.setMaximumFractionDigits(309); - for(int i=value.length()-1; - i>=0 && value.charAt(i)!=dec;i--,decAt++); - if(decAt < value.length()) to_fmt.setMinimumFractionDigits(decAt); + if ( subType == null ) { + to_fmt = (DecimalFormat) NumberFormat.getInstance( loc ); + to_fmt.setMaximumFractionDigits( 309 ); + for ( int i = value.length() - 1; + i >= 0 && value.charAt( i ) != dec; i--, decAt++ ) + ; + if ( decAt < value.length() ) to_fmt.setMinimumFractionDigits( decAt ); decAt = 0; - for(int i = 0; i < value.length() && value.charAt(i) != dec; i++) { - if(Character.isDigit(value.charAt(i))) decAt++; + for ( int i = 0; i < value.length() && value.charAt( i ) != dec; i++ ) { + if ( Character.isDigit( value.charAt( i ) ) ) decAt++; } - to_fmt.setMinimumIntegerDigits(decAt); - if(value.charAt(value.length()-1) == dec) appendDec = true; - } else if (subType.equals("currency")) { - to_fmt = (DecimalFormat)NumberFormat.getCurrencyInstance(loc); - } else if (subType.equals("int-currency")) { - to_fmt = (DecimalFormat)NumberFormat.getCurrencyInstance(loc); - int_currency = 1; - for(int i=0;i 0) - numberValue = new Double(numberValue.doubleValue()/ - int_currency); + numberValue = from_fmt.parse( value ); + if ( int_currency > 0 ) + numberValue = new Double( numberValue.doubleValue() / + int_currency ); else { } - } catch (ParseException pe) { - throw new SAXException(this.getClass().getName() - + "i18n:number - parsing error.", pe); + } catch ( ParseException pe ) { + throw new SAXException( this.getClass().getName() + + "i18n:number - parsing error.", pe ); } } // we have all necessary data here: do formatting. - String result = to_fmt.format(numberValue); - if(appendDec) result = result + dec; - debug("i18n:number result: " + result); + String result = to_fmt.format( numberValue ); + if ( appendDec ) result = result + dec; + debug( "i18n:number result: " + result ); return result; } /** * Helper method to retrieve a message from the dictionary */ - private String getString(String key) { + private String getString( String key ) { - return dictionary.getString( - I18N_CATALOGUE_PREFIX + "[@key='" + key + "']" - ); + return dictionary.getString( + new StringBuffer( I18N_CATALOGUE_PREFIX ).append( "[@key='" ).append( key ).append( "']" ).toString() + ); } - private void setLocale(Locale locale) { + private void setLocale( Locale locale ) { this.locale = locale; lang = locale.getLanguage(); - formatter.setLocale(locale); + formatter.setLocale( locale ); } /** * Helper method to debug messages */ - private void debug(String msg) { - getLogger().debug("I18nTransformer: " + msg); + private void debug( String msg ) { + getLogger().debug( "I18nTransformer: " + msg ); } /** * - static public void main(String[] args) { + static public void main(String[] args) { - Locale locale = null; + Locale locale = null; - Locale[] locales = Locale.getAvailableLocales(); - for (int i = 0; i < locales.length; i++) { - locale = locales[i]; - SimpleDateFormat fmt = (SimpleDateFormat)DateFormat.getDateTimeInstance( - DateFormat.DEFAULT, DateFormat.DEFAULT, locale - ); - - String localized = fmt.format(new Date()); - - NumberFormat n_fmt = NumberFormat.getCurrencyInstance(locale); - String money = n_fmt.format(1210.5); - - System.out.println("Locale [" - + locale.getLanguage() + ", " - + locale.getCountry() + ", " - + locale.getVariant() + "] : " - + locale.getDisplayName() - + " \t Date: " + localized - + " \t Money: " + money); - } - } + Locale[] locales = Locale.getAvailableLocales(); + for (int i = 0; i < locales.length; i++) { + locale = locales[i]; + SimpleDateFormat fmt = (SimpleDateFormat)DateFormat.getDateTimeInstance( + DateFormat.DEFAULT, DateFormat.DEFAULT, locale + ); + + String localized = fmt.format(new Date()); + + NumberFormat n_fmt = NumberFormat.getCurrencyInstance(locale); + String money = n_fmt.format(1210.5); + + System.out.println("Locale [" + + locale.getLanguage() + ", " + + locale.getCountry() + ", " + + locale.getVariant() + "] : " + + locale.getDisplayName() + + " \t Date: " + localized + + " \t Money: " + money); + } + } */ } ---------------------------------------------------------------------- In case of troubles, e-mail: webmaster@xml.apache.org To unsubscribe, e-mail: cocoon-cvs-unsubscribe@xml.apache.org For additional commands, e-mail: cocoon-cvs-help@xml.apache.org