cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vgritse...@apache.org
Subject cvs commit: cocoon-2.1/src/java/org/apache/cocoon/transformation I18nTransformer.java
Date Thu, 27 Nov 2003 03:40:41 GMT
vgritsenko    2003/11/26 19:40:41

  Modified:    src/java/org/apache/cocoon/transformation
                        I18nTransformer.java
  Log:
  Remove deprecated configuration syntax. Code cleanup.
  
  Revision  Changes    Path
  1.16      +194 -319  cocoon-2.1/src/java/org/apache/cocoon/transformation/I18nTransformer.java
  
  Index: I18nTransformer.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/transformation/I18nTransformer.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- I18nTransformer.java	13 Nov 2003 13:59:46 -0000	1.15
  +++ I18nTransformer.java	27 Nov 2003 03:40:41 -0000	1.16
  @@ -105,6 +105,7 @@
    * {@link org.apache.cocoon.acting.LocaleAction#getLocaleAttribute(Map, String) } for details.
    * It then attempts to find a <strong>message catalogue</strong> that satisifies
    * the particular locale, and use it for for text replacement within i18n markup.
  + * 
    * <p>Catalogues are maintained in separate files, with a naming convention
    * similar to that of ResourceBundle (See java.util.ResourceBundle).
    * ie.
  @@ -189,10 +190,11 @@
    *  <li><strong>cache-at-startup</strong>: flag whether to cache
    *      messages at startup (false by default).
    * </ul>
  - * <p>Note: before using multiple catalogues was supported, the catalogue name
  - * and location was specified using elements named <code>catalogue-name</code>
and
  - * <code>catalogue-location</code>. This syntax is still supported, but can't
be used
  - * concurrently with the new syntax.
  + * 
  + * <p><strong>NOTE:</strong> before using multiple catalogues was supported,
  + * the catalogue name and location was specified using elements named
  + * <code>catalogue-name</code> and <code>catalogue-location</code>.
This syntax is
  + * <strong>NOT</strong> supported anymore.
    *
    * <p>To use the transformer in a pipeline, simply specify it in a particular
    * transform. eg:
  @@ -253,7 +255,7 @@
    */
   public class I18nTransformer extends AbstractTransformer
           implements CacheableProcessingComponent,
  -        Composable, Configurable, Disposable {
  +                   Composable, Configurable, Disposable {
   
       /**
        * The namespace for i18n is "http://apache.org/cocoon/i18n/2.1".
  @@ -268,7 +270,7 @@
               "http://apache.org/cocoon/i18n/2.0";
   
       /**
  -     * Did we already encountered a old namespace ? This is static to ensure that
  +     * Did we already encountered an old namespace? This is static to ensure that
        * the associated message will be logged only once.
        */
       private static boolean deprecationFound = false;
  @@ -718,12 +720,6 @@
        */
       public static final String I18N_FRACTION_DIGITS_ATTRIBUTE = "fraction-digits";
   
  -    // NOP Validity object: i18n transformer is cached forever
  -    // FIXME (KP): Cache validity should be generated by
  -    // Bundle implementations.
  -    private static final SourceValidity I18N_NOP_VALIDITY =
  -            new org.apache.excalibur.source.impl.validity.NOPValidity();
  -
       // States of the transformer
       private static final int STATE_OUTSIDE                       = 0;
       private static final int STATE_INSIDE_TEXT                   = 10;
  @@ -737,6 +733,7 @@
       private static final int STATE_INSIDE_TIME                   = 62;
       private static final int STATE_INSIDE_NUMBER                 = 63;
   
  +
       // All date-time related parameter types and element names
       private static final Set dateTypes;
   
  @@ -748,14 +745,14 @@
   
       static {
           // initialize date types set
  -        HashSet set = new HashSet();
  +        HashSet set = new HashSet(5);
           set.add(I18N_DATE_ELEMENT);
           set.add(I18N_TIME_ELEMENT);
           set.add(I18N_DATE_TIME_ELEMENT);
           dateTypes = Collections.unmodifiableSet(set);
   
           // initialize number types set
  -        set = new HashSet();
  +        set = new HashSet(9);
           set.add(I18N_NUMBER_ELEMENT);
           set.add(I18N_PERCENT_ELEMENT);
           set.add(I18N_CURRENCY_ELEMENT);
  @@ -765,7 +762,7 @@
           numberTypes = Collections.unmodifiableSet(set);
   
           // Initialize date patterns map
  -        Map map = new HashMap();
  +        Map map = new HashMap(7);
           map.put("SHORT", new Integer(DateFormat.SHORT));
           map.put("MEDIUM", new Integer(DateFormat.MEDIUM));
           map.put("LONG", new Integer(DateFormat.LONG));
  @@ -773,6 +770,7 @@
           datePatterns = Collections.unmodifiableMap(map);
       }
   
  +
       // Component manager for this component
       protected ComponentManager manager;
   
  @@ -849,12 +847,6 @@
       // i18n configuration variables
       //
   
  -    // Catalogue name value
  -    private String catalogueName;
  -
  -    // Catalogue location value
  -    private String catalogueLocation;
  -
       // id of the default catalogue
       private String defaultCatalogueId;
   
  @@ -870,6 +862,7 @@
       // Helper variable used to hold the old untranslated value
       private String globalUntranslated;
   
  +
       /**
        * Returns the current locale setting of this transformer instance.
        * @return current Locale object
  @@ -883,7 +876,22 @@
        * Generates unique key for the current locale.
        */
       public java.io.Serializable getKey() {
  -        return catalogueLocation + '/' + catalogueName + '?' + this.locale;
  +        // TODO: Key should be composed out of used catalogues locations, and locale.
  +        //       Right now it is hardcoded only to default catalogue location.
  +        CatalogueInfo catalogueInfo = (CatalogueInfo)catalogues.get(defaultCatalogueId);
  +        StringBuffer key = new StringBuffer();
  +        if (catalogueInfo != null) {
  +            key.append(catalogueInfo.resolvedLocation);
  +        }
  +        key.append("?");
  +        if (locale != null) {
  +            key.append(locale.getLanguage());
  +            key.append("_");
  +            key.append(locale.getCountry());
  +            key.append("_");
  +            key.append(locale.getVariant());
  +        }
  +        return key.toString();
       }
   
       /**
  @@ -892,21 +900,22 @@
        * if this instance is not cacheable.
        */
       public SourceValidity getValidity() {
  -        return I18N_NOP_VALIDITY;
  +        // FIXME (KP): Cache validity should be generated by
  +        // Bundle implementations.
  +        return org.apache.excalibur.source.impl.validity.NOPValidity.SHARED_INSTANCE;
       }
   
       /**
        * Implementation of composable interface.
        * Looksup the Bundle Factory to be used.
        */
  -    public void compose(ComponentManager manager) {
  +    public void compose(ComponentManager manager) throws ComponentException {
           this.manager = manager;
           try {
               this.factory = (BundleFactory)manager.lookup(BundleFactory.ROLE);
  -
  -            debug("BundleFactory is obtained");
  -        } catch (ComponentException ce) {
  -            getLogger().error("BundleFactory is not loaded", ce);
  +        } catch (ComponentException e) {
  +            getLogger().debug("Failed to load BundleFactory", e);
  +            throw e;
           }
       }
   
  @@ -915,80 +924,44 @@
        * Configure this transformer.
        */
       public void configure(Configuration conf) throws ConfigurationException {
  -        if (factory == null) {
  -            throw new ConfigurationException("BundleFactory component is not found.");
  -        }
  -        
           // read in the config options from the transformer definition
  -
  -        // there are two possible configuration methods:
  -        //  (1) the one dating from before multiple catalogues were supported:
  -        //      in this case the elements catalogue-name and catalogue-location specify
  -        //      the catalogue to be used
  -        //  (2) the new one supporting mulitple catalogues
  -
  -        // obtain the base name of the message catalogue
  -        Configuration child = conf.getChild(I18N_CATALOGUE_NAME);
  -        catalogueName = child.getValue(null);
  -
  -        // obtain the directory location of message catalogues
  -        child = conf.getChild(I18N_CATALOGUE_LOCATION);
  -        catalogueLocation = child.getValue(null);
  -
           Configuration cataloguesConf = conf.getChild("catalogues", false);
   
  -        if ((catalogueName != null || catalogueLocation != null) && cataloguesConf
!= null) {
  -            // if old and new style configuration are used at the same time...
  -            throw new ConfigurationException("I18nTransformer: old and new configuration
style " +
  -                                             "are used at the same time. Use either the
'catalogue-name' " +
  -                                             "and 'catalogue-location' elements or use
the 'catalogues' element, " +
  -                                             "but don't use both at the same time.");
  -        } else if (catalogueName != null || catalogueLocation != null) {
  -            if (!(catalogueName != null && catalogueLocation != null))
  -                throw new ConfigurationException("I18nTransformer: catalogue-name and "
+
  -                                                 "catalogue-location must both be specified");
  -            if (getLogger().isDebugEnabled())
  -                getLogger().debug("using old-style configuration: name = " + catalogueName
  -                        + ", location = " + catalogueLocation);
  -        } else if (cataloguesConf == null) {
  -            // if both old and new style configuration are missing ...
  -            throw new ConfigurationException("Missing configuration for the I18nTransformer:
" +
  -                                             "a 'catalogues' element specifying the catalogues
is required.");
  -        } else {
  -            // new configuration style
  -            Configuration[] catalogueConfs = cataloguesConf.getChildren("catalogue");
  -            for (int i = 0; i < catalogueConfs.length; i++) {
  -                String id = catalogueConfs[i].getAttribute("id");
  -                String name = catalogueConfs[i].getAttribute("name");
  -                String location = catalogueConfs[i].getAttribute("location");
  -                CatalogueInfo newCatalogueInfo;
  -                try {
  -                    newCatalogueInfo = new CatalogueInfo(name, location);
  -                } catch (PatternException e) {
  -                    throw new ConfigurationException("I18nTransformer: error in name or
location " +
  -                                                     "attribute on catalogue element with
id " + id, e);
  -                }
  -                catalogues.put(id, newCatalogueInfo);
  -            }
  +        // new configuration style
  +        Configuration[] catalogueConfs = cataloguesConf.getChildren("catalogue");
  +        for (int i = 0; i < catalogueConfs.length; i++) {
  +            String id = catalogueConfs[i].getAttribute("id");
  +            String name = catalogueConfs[i].getAttribute("name");
  +            String location = catalogueConfs[i].getAttribute("location");
  +            CatalogueInfo newCatalogueInfo;
  +            try {
  +                newCatalogueInfo = new CatalogueInfo(name, location);
  +            } catch (PatternException e) {
  +                throw new ConfigurationException("I18nTransformer: error in name or location
" +
  +                                                 "attribute on catalogue element with id
" + id, e);
  +            }
  +            catalogues.put(id, newCatalogueInfo);
  +        }
   
  -            defaultCatalogueId = cataloguesConf.getAttribute("default");
  -            if (!catalogues.containsKey(defaultCatalogueId)) {
  -                throw new ConfigurationException("I18nTransformer: default catalogue id
'" +
  -                                                 defaultCatalogueId + "' denotes a nonexisting
catalogue");
  -            }
  +        this.defaultCatalogueId = cataloguesConf.getAttribute("default");
  +        if (!catalogues.containsKey(this.defaultCatalogueId)) {
  +            throw new ConfigurationException("I18nTransformer: default catalogue id '"
+
  +                                             this.defaultCatalogueId + "' denotes a nonexisting
catalogue");
           }
   
           // obtain default text to use for untranslated messages
  -        child = conf.getChild(I18N_UNTRANSLATED);
  -        untranslated = child.getValue(null);
  -        debug("Default untranslated text is '" + untranslated + "'");
  +        untranslated = conf.getChild(I18N_UNTRANSLATED).getValue(null);
  +        if (getLogger().isDebugEnabled()) {
  +            getLogger().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") +
  +        cacheAtStartup = conf.getChild(I18N_CACHE_STARTUP).getValueAsBoolean(false);
  +        if (getLogger().isDebugEnabled()) {
  +            getLogger().debug((cacheAtStartup ? "will" : "won't") +
                 " cache messages during startup, by default"
  -        );
  +            );
  +        }
       }
   
       /**
  @@ -996,25 +969,18 @@
        */
       public void setup(SourceResolver resolver, Map objectModel, String source,
                         Parameters parameters)
  -            throws ProcessingException, SAXException, IOException {
  +    throws ProcessingException, SAXException, IOException {
   
           this.sourceResolver = resolver;
           this.objectModel = objectModel;
   
           try {
               // check parameters to see if anything has been locally overloaded
  -            String localCatLocation = null;
  -            String localCatName = null;
               String localUntranslated = null;
               String lc = null;
               String localDefaultCatalogueId = null;
   
               if (parameters != null) {
  -                // localCatLocation and localCatName are to support old-style configuration
  -                localCatLocation =
  -                        parameters.getParameter(I18N_CATALOGUE_LOCATION, null);
  -                localCatName =
  -                        parameters.getParameter(I18N_CATALOGUE_NAME, null);
                   localUntranslated =
                           parameters.getParameter(I18N_UNTRANSLATED, null);
                   lc = parameters.getParameter(I18N_LOCALE, null);
  @@ -1035,8 +1001,9 @@
   
               // Get current locale
               Locale locale = I18nUtils.parseLocale(lc);
  -            if (getLogger().isDebugEnabled())
  -                debug("using locale " + locale.toString());
  +            if (getLogger().isDebugEnabled()) {
  +                getLogger().debug("Using locale " + locale.toString());
  +            }
   
               // Initialize instance state variables
               this.locale             = locale;
  @@ -1057,21 +1024,7 @@
   
               // give the defaultCatalogue variable its value -- first look if it's locally
overridden
               // and otherwise use the component-wide defaults.
  -            if (localCatLocation != null || localCatName != null) {
  -                // first local old configuration style
  -                localCatName = localCatName != null ? localCatName : catalogueName;
  -                localCatLocation = localCatLocation != null ? localCatLocation : catalogueLocation;
  -                if (localCatName == null || localCatLocation == null)
  -                    throw new ProcessingException("I18nTransformer: incorrect usage: either
" +
  -                                                  "catalogue-name or catalogue-location
are not specified.");
  -                defaultCatalogue = getCatalogue(localCatName, localCatLocation);
  -            } else if ((localDefaultCatalogueId != null || localCatLocation != null) &&
localCatName != null) {
  -                // throw error if old and new configuration style are used at the same
time
  -                throw new ProcessingException("I18nTransformer: either specify 'catalogue-name'
" +
  -                                              "and 'catalogue-location' or specify 'default-catalogue-id',
" +
  -                                              "but don't mix the two configuration styles.");
  -            } else if (localDefaultCatalogueId != null) {
  -                // then if new local configuration style
  +            if (localDefaultCatalogueId != null) {
                   CatalogueInfo catalogueInfo = (CatalogueInfo)catalogues.get(localDefaultCatalogueId);
                   if (catalogueInfo == null) {
                       throw new ProcessingException("I18nTransformer: '" +
  @@ -1079,83 +1032,30 @@
                                                     "' is not an existing catalogue id.");
                   }
                   defaultCatalogue = catalogueInfo.getCatalogue();
  -            } else if (catalogueName != null && catalogueLocation != null) {
  -                // then global old configuration style
  -                defaultCatalogue = getCatalogue(catalogueName, catalogueLocation);
               } else {
  -                // then global new configuration style
                   defaultCatalogue = ((CatalogueInfo)catalogues.get(defaultCatalogueId)).getCatalogue();
               }
  -            if (getLogger().isDebugEnabled())
  -                getLogger().debug("using default catalogue " + defaultCatalogue);
  +            if (getLogger().isDebugEnabled()) {
  +                getLogger().debug("Using default catalogue " + defaultCatalogue);
  +            }
   
               // Create and initialize a formatter
               this.formatter = new MessageFormat("");
               this.formatter.setLocale(locale);
  -
           } catch (Exception e) {
  -            debug("exception generated, leaving unconfigured");
  +            getLogger().debug("exception generated, leaving unconfigured");
               throw new ProcessingException(e.getMessage(), e);
           }
       }
   
  -    private Bundle getCatalogue(String name, String location) throws Exception
  -    {
  -        synchronized (factory) {
  -            configureFactory(location);
  -            return (Bundle)factory.select(name, locale);
  -        }
  -    }
  -
  -    /**
  -     * Internal setup of XML resource factory.
  -     *
  -     * REVISIT (MC): when we can get the resolver anywhere, we can pass the
  -     * configuration object directly to XMLResourceBundle.
  -     */
  -    protected void configureFactory(String location) throws Exception {
  -        // configure the factory to log correctly and cache catalogues
  -        DefaultConfiguration configuration =
  -                new DefaultConfiguration("name", "location");
  -        DefaultConfiguration cacheConf =
  -                new DefaultConfiguration(
  -                        BundleFactory.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(
  -                        BundleFactory.ConfigurationKeys.ROOT_DIRECTORY,
  -                        "location"
  -                );
  -
  -        debug("catalog location:" + location);
  -        Source source = null;
  -        try {
  -            source = sourceResolver.resolveURI(location);
  -            String systemId = source.getURI();
  -            debug("catalog directory:" + systemId);
  -            dirConf.setValue(systemId);
  -            configuration.addChild(dirConf);
  -        } finally {
  -            if (source != null)
  -                sourceResolver.release(source);
  -        }
  -
  -        // Pass created configuration object to the factory
  -        ((Configurable)factory).configure(configuration);
  -        debug("factory configured");
  -    }
   
       //
       // Standard SAX event handlers
       //
   
       public void startElement(String uri, String name, String raw,
  -                             Attributes attr) throws SAXException {
  +                             Attributes attr)
  +    throws SAXException {
   
           // Handle previously buffered characters
           if (current_state != STATE_OUTSIDE && strBuffer != null) {
  @@ -1164,21 +1064,27 @@
           }
   
           // Process start element event
  -        if (I18N_OLD_NAMESPACE_URI.equals(uri)) {
  +        if (I18N_NAMESPACE_URI.equals(uri)) {
  +            if (getLogger().isDebugEnabled()) {
  +                getLogger().debug("Starting i18n element: " + name);
  +            }
  +            startI18NElement(name, attr);
  +
  +        } else if (I18N_OLD_NAMESPACE_URI.equals(uri)) {
               if (!deprecationFound) {
                   deprecationFound = true;
                   getLogger().warn("The namespace '" +
                                    I18N_OLD_NAMESPACE_URI +
  -                                 "' for i18n is not supported any more, use: '" +
  +                                 "' for i18n is deprecated, use: '" +
                                    I18N_NAMESPACE_URI + "'");
               }
  -            debug("Starting deprecated i18n element: " + name);
  +            if (getLogger().isDebugEnabled()) {
  +                getLogger().debug("Starting deprecated i18n element: " + name);
  +            }
               startI18NElement(name, attr);
   
  -        } else if (I18N_NAMESPACE_URI.equals(uri)) {
  -            debug("Starting i18n element: " + name);
  -            startI18NElement(name, attr);
  -        } else { // We have a non i18n element event
  +        } else {
  +            // We have a non i18n element event
               if (current_state == STATE_OUTSIDE) {
                   super.startElement(uri, name, raw,
                                      translateAttributes(name, attr));
  @@ -1245,7 +1151,9 @@
       private void startI18NElement(String name, Attributes attr)
               throws SAXException {
   
  -        debug("Start i18n element: " + name);
  +        if (getLogger().isDebugEnabled()) {
  +            getLogger().debug("Start i18n element: " + name);
  +        } 
   
           if (I18N_TEXT_ELEMENT.equals(name)) {
               if (current_state != STATE_OUTSIDE
  @@ -1261,14 +1169,14 @@
   
               prev_state = current_state;
               current_state = STATE_INSIDE_TEXT;
  -            
  +
               current_key = attr.getValue("", I18N_KEY_ATTRIBUTE);
               if (current_key == null) {
                   // Try the namespaced attribute
                   current_key = attr.getValue(I18N_NAMESPACE_URI, I18N_KEY_ATTRIBUTE);
                   if (current_key == null) {
                       // Try the old namespace
  -                    current_key = attr.getValue(I18N_OLD_NAMESPACE_URI, I18N_KEY_ATTRIBUTE);
  
  +                    current_key = attr.getValue(I18N_OLD_NAMESPACE_URI, I18N_KEY_ATTRIBUTE);
                   }
               }
   
  @@ -1277,15 +1185,14 @@
                   // Try the namespaced attribute
                   currentCatalogueId = attr.getValue(I18N_NAMESPACE_URI, I18N_CATALOGUE_ATTRIBUTE);
               }
  -            if (prev_state != STATE_INSIDE_PARAM)
  +            if (prev_state != STATE_INSIDE_PARAM) {
                   tr_text_recorder = null;
  +            }
   
               if (current_key != null) {
                   tr_text_recorder = getMirrorRecorder(current_key, null);
  -                //debug("Got translation: " + tr_text_recorder);
               }
   
  -
           } else if (I18N_TRANSLATE_ELEMENT.equals(name)) {
               if (current_state != STATE_OUTSIDE) {
                   throw new SAXException(
  @@ -1362,7 +1269,9 @@
   
                   // Perform soft locale matching
                   if (this.locale.toString().startsWith(locale)) {
  -                    debug("Locale matching: " + locale);
  +                    if (getLogger().isDebugEnabled()) {
  +                        getLogger().debug("Locale matching: " + locale);
  +                    }
                       translate_copy = true;
                   }
               }
  @@ -1379,9 +1288,10 @@
                   );
               }
   
  -            debug("Matching any locale");
  -            if (!translate_end)
  +            getLogger().debug("Matching any locale");
  +            if (!translate_end) {
                   translate_copy = true;
  +            }
   
               prev_state = current_state;
               current_state = STATE_INSIDE_OTHERWISE;
  @@ -1491,7 +1401,9 @@
       }
   
       private void endI18NElement(String name) throws SAXException {
  -        debug("End i18n element: " + name);
  +        if (getLogger().isDebugEnabled()) {
  +            getLogger().debug("End i18n element: " + name);
  +        }
           switch (current_state) {
               case STATE_INSIDE_TEXT:
                   endTextElement();
  @@ -1527,7 +1439,7 @@
       }
   
       private void i18nCharacters(String textValue)
  -            throws SAXException {
  +    throws SAXException {
   
           if (textValue == null) {
               return;
  @@ -1537,7 +1449,9 @@
           if (textValue.length() == 0)
               return;
   
  -        debug( "i18n message text = '" + textValue + "'" );
  +        if (getLogger().isDebugEnabled()) {
  +            getLogger().debug( "i18n message text = '" + textValue + "'" );
  +        }
   
           switch (current_state) {
               case STATE_INSIDE_TEXT:
  @@ -1562,7 +1476,7 @@
   
               case STATE_INSIDE_CHOOSE:
                   // No characters allowed. Send an exception ?
  -                debug("No characters allowed inside <i18n:choose> tags");
  +                getLogger().debug("No characters allowed inside <i18n:choose> tags");
                   break;
   
               case STATE_INSIDE_DATE:
  @@ -1587,15 +1501,15 @@
       // Translate all attributes that are listed in i18n:attr attribute
       private Attributes translateAttributes(String name, Attributes attr) {
   
  -        if(attr == null)
  +        if (attr == null) {
               return 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);
  -        
           if (i18n_attr_index == -1) {
               // Try the old namespace
               i18n_attr_index = temp_attr.getIndex(I18N_OLD_NAMESPACE_URI,I18N_ATTR_ATTRIBUTE);
  @@ -1618,8 +1532,7 @@
                       // the colon denotes a catalogue id
                       int colonPos = text2translate.indexOf(":");
                       String catalogueId = null;
  -                    if (colonPos != -1)
  -                    {
  +                    if (colonPos != -1) {
                           catalogueId = text2translate.substring(0, colonPos);
                           text2translate = text2translate.substring(colonPos + 1, text2translate.length());
                       }
  @@ -1659,8 +1572,9 @@
                           tr_text_recorder = getMirrorRecorder(text_recorder.text(), text_recorder);
                       } else {
                           // We have the key, but couldn't find a transltation
  -                        if (getLogger().isDebugEnabled())
  -                            debug("translation not found for key " + current_key);
  +                        if (getLogger().isDebugEnabled()) {
  +                            getLogger().debug("translation not found for key " + current_key);
  +                        }
                           // use the untranslated-text only when the content of the i18n:text
                           // element was empty
                           if (text_recorder.empty() && untranslatedRecorder != null)
{
  @@ -1714,30 +1628,39 @@
           if (paramType != null) {
               // We have a typed parameter
   
  -            debug("Param type: " + paramType);
  -            if (formattingParams.get(I18N_VALUE_ATTRIBUTE) == null &&
  -                    param_value != null) {
  -
  -                debug("Put param value: " + param_value);
  +            if (getLogger().isDebugEnabled()) {
  +                getLogger().debug("Param type: " + paramType);
  +            }
  +            if (formattingParams.get(I18N_VALUE_ATTRIBUTE) == null && param_value
!= null) {
  +                if (getLogger().isDebugEnabled()) {
  +                    getLogger().debug("Put param value: " + param_value);
  +                }
                   formattingParams.put(I18N_VALUE_ATTRIBUTE, param_value);
               }
   
               // Check if we have a date or a number parameter
               if (dateTypes.contains(paramType)) {
  -                debug("Formatting date_time param: " + formattingParams);
  +                if (getLogger().isDebugEnabled()) {
  +                    getLogger().debug("Formatting date_time param: " + formattingParams);
  +                }
                   param_value = formatDate_Time(formattingParams);
               } else if (numberTypes.contains(paramType)) {
  -                debug("Formatting number param: " + formattingParams);
  +                if (getLogger().isDebugEnabled()) {
  +                    getLogger().debug("Formatting number param: " + formattingParams);
  +                }
                   param_value = formatNumber(formattingParams);
               }
  -            debug("Added substitution param: " + param_value);
  +            if (getLogger().isDebugEnabled()) {
  +                getLogger().debug("Added substitution param: " + param_value);
  +            }
           }
   
           param_value = null;
           current_state = STATE_INSIDE_TRANSLATE;
   
  -        if(param_recorder == null)
  +        if(param_recorder == null) {
               return;
  +        }
   
           indexedParams.put(param_name, param_recorder.clone());
       }
  @@ -1745,8 +1668,8 @@
       private void endTranslateElement() throws SAXException {
           if (tr_text_recorder != null) {
               if (getLogger().isDebugEnabled()) {
  -                debug("End of translate with params");
  -                debug("Fragment for substitution : " + tr_text_recorder.text());
  +                getLogger().debug("End of translate with params");
  +                getLogger().debug("Fragment for substitution : " + tr_text_recorder.text());
               }
               tr_text_recorder.send(super.contentHandler, indexedParams);
               tr_text_recorder = null;
  @@ -1905,8 +1828,10 @@
           }
   
           // we have all necessary data here: do formatting.
  -        debug("### Formatting date: " + dateValue + " with localized pattern "
  -              + to_fmt.toLocalizedPattern() + " for locale: " + locale);
  +        if (getLogger().isDebugEnabled()) {
  +            getLogger().debug("### Formatting date: " + dateValue + " with localized pattern
" +
  +                              to_fmt.toLocalizedPattern() + " for locale: " + locale);
  +        }
           return to_fmt.format(dateValue);
       }
   
  @@ -1945,15 +1870,15 @@
           // type
           String type = (String)params.get(I18N_TYPE_ATTRIBUTE);
   
  -         // fraction-digits
  -         int fractionDigits = -1;
  -         try {
  -             String fd = (String)params.get(I18N_FRACTION_DIGITS_ATTRIBUTE);
  -             if (fd != null)
  -                 fractionDigits = Integer.parseInt(fd);
  -         } catch (NumberFormatException nfe) {
  -             getLogger().warn("Error in number format with fraction-digits", nfe);
  -         }
  +        // fraction-digits
  +        int fractionDigits = -1;
  +        try {
  +            String fd = (String)params.get(I18N_FRACTION_DIGITS_ATTRIBUTE);
  +            if (fd != null)
  +                fractionDigits = Integer.parseInt(fd);
  +        } catch (NumberFormatException nfe) {
  +            getLogger().warn("Error in number format with fraction-digits", nfe);
  +        }
   
           // parsed number
           Number numberValue = null;
  @@ -1993,7 +1918,6 @@
               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);
  @@ -2077,28 +2001,31 @@
           // we have all necessary data here: do formatting.
           String result = to_fmt.format(numberValue);
           if (appendDec) result = result + dec;
  -        debug("i18n:number result: " + result);
  +        if (getLogger().isDebugEnabled()) {
  +            getLogger().debug("i18n:number result: " + result);
  +        }
           return result;
       }
   
       //-- Dictionary handling routins
   
       /**
  -     * Helper method to retrieve a message from the dictionary.
  -     * mattam: now only used for i:attr.
  -     * A default value is returned if message is not found
  +     * Helper method to retrieve a message Node from the dictionary.
  +     * A default value is returned if message is not found.
        *
        * @param catalogueId if not null, this catalogue will be used instead of the default
one.
        */
  -    private String getString(String key, String catalogueId, String defaultValue) {
  +    private Node getNode(String catalogueId, String key) {
           try {
               Bundle catalogue = defaultCatalogue;
               if (catalogueId != null) {
                   CatalogueInfo catalogueInfo = (CatalogueInfo)catalogues.get(catalogueId);
                   if (catalogueInfo == null) {
  -                    if (getLogger().isDebugEnabled())
  -                        debug("Catalogue not found: " + catalogueId + ", could not translate
key " + key);
  -                    return defaultValue;
  +                    if (getLogger().isDebugEnabled()) {
  +                        getLogger().debug("Catalogue not found: " + catalogueId +
  +                                          ", could not translate key " + key);
  +                    }
  +                    return null;
                   }
                   try {
                       catalogue = catalogueInfo.getCatalogue();
  @@ -2107,17 +2034,30 @@
                                         " from location " + catalogueInfo.getLocation() +
                                         " for locale " + locale +
                                         ", will not translate key " + key);
  -                    return defaultValue;
  +                    return null;
                   }
               }
  -            Node res = (Node)catalogue.getObject(
  -                    I18N_CATALOGUE_PREFIX + "[@key='" + key + "']");
  -
  -            String value = getTextValue(res);
  -            return value != null ? value : defaultValue;
  +            return (Node)catalogue.getObject(I18N_CATALOGUE_PREFIX +
  +                                             "[@key='" + key + "']");
           } catch (MissingResourceException e)  {
  +            getLogger().debug("Untranslated key: '" + key + "'");
  +            return null;
  +        }
  +    }
  +
  +    /**
  +     * Helper method to retrieve a message from the dictionary.
  +     * mattam: now only used for i:attr.
  +     * A default value is returned if message is not found
  +     *
  +     * @param catalogueId if not null, this catalogue will be used instead of the default
one.
  +     */
  +    private String getString(String key, String catalogueId, String defaultValue) {
  +        final Node res = getNode(catalogueId, key);
  +        if (res == null) {
               return defaultValue;
           }
  +        return getTextValue(res);
       }
   
       // Helper method to get the text value of the node.
  @@ -2148,51 +2088,19 @@
        * A default value is returned if message is not found.
        */ 
       private MirrorRecorder getMirrorRecorder(String key, MirrorRecorder defaultValue) {
  -        try {
  -            Bundle catalogue = defaultCatalogue;
  -            if (currentCatalogueId != null) {
  -                CatalogueInfo catalogueInfo = (CatalogueInfo)catalogues.get(currentCatalogueId);
  -                if (catalogueInfo == null) {
  -                    if (getLogger().isDebugEnabled()) {
  -                        debug("Catalogue not found: " + currentCatalogueId +
  -                              ", could not translate key " + key);
  -                    }
  -                    return defaultValue;
  -                }
  -                try {
  -                    catalogue = catalogueInfo.getCatalogue();
  -                } catch (Exception e) {
  -                    getLogger().error("Error getting catalogue " + catalogueInfo.getName()
+
  -                                      " from location " + catalogueInfo.getLocation() +
  -                                      " for locale " + locale + ", will not translate key
" + key);
  -                    return defaultValue;
  -                }
  -            }
  -            MirrorRecorder value = new MirrorRecorder (
  -                    (Node)catalogue.getObject(
  -                            I18N_CATALOGUE_PREFIX + "[@key='" + key + "']"));
  -
  -            if (value == null) {
  -                return defaultValue;
  -            }
  -
  -            return value;
  -
  -        } catch (MissingResourceException e) {
  -            debug("Untranslated key: '" + key + "'");
  +        Node value = getNode(currentCatalogueId, key);
  +        if (value == null) {
               return defaultValue;
           }
  -    }
   
  -    // Helper method to debug messages
  -    private void debug(String msg) {
  -        getLogger().debug("I18nTransformer: " + msg);
  +        return new MirrorRecorder(value);
       }
   
       public void recycle() {
           // restore untranslated-text if necessary
  -        if (globalUntranslated != null)
  +        if (globalUntranslated != null) {
               untranslated = globalUntranslated;
  +        }
           untranslatedRecorder = null;
   
           // clean up default catalogue
  @@ -2201,15 +2109,13 @@
   
           // clean up the other catalogues
           Iterator catalogueIt = catalogues.values().iterator();
  -        while (catalogueIt.hasNext())
  -        {
  +        while (catalogueIt.hasNext()) {
               CatalogueInfo catalogueInfo = (CatalogueInfo)catalogueIt.next();
               catalogueInfo.releaseCatalog();
           }
   
           sourceResolver = null;
           objectModel = null;
  -
           super.recycle();
       }
   
  @@ -2245,61 +2151,30 @@
               return resolvedLocation;
           }
   
  -        private void resolve() throws PatternException {
  -            if (resolvedLocation == null)
  +        private void resolve() throws Exception {
  +            if (resolvedLocation == null) {
                   resolvedLocation = location.resolve(null, objectModel);
  -            if (resolvedName == null)
  +            }
  +            if (resolvedName == null) {
                   resolvedName = name.resolve(null, objectModel);
  +            }
           }
   
           public Bundle getCatalogue() throws Exception {
               if (catalogue == null) {
                   resolve();
  -                synchronized(factory) {
  -                    configureFactory(resolvedLocation);
  -                    catalogue = (Bundle)factory.select(resolvedName, locale);
  -                }
  +                catalogue = (Bundle) factory.select(resolvedLocation, resolvedName, locale);
               }
               return catalogue;
           }
   
           public void releaseCatalog() {
  -            // would it be necessary to first configure the factory here?
  -            if (catalogue != null)
  +            if (catalogue != null) {
                   factory.release(catalogue);
  +            }
               catalogue = null;
               resolvedName = null;
               resolvedLocation = null;
           }
       }
  -
  -/*
  -    static public void main(String[] args) {
  -        Locale locale = null;
  -        Locale[] locales = Locale.getAvailableLocales();
  -
  -        for (int i = 0; i < locales.length; i++) {
  -            locale = locales[i];n
  -            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);
  -        }
  -    }
  -*/
   }
  
  
  

Mime
View raw message