cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jo...@apache.org
Subject svn commit: r202298 - in /cocoon/trunk/src/java/org/apache/cocoon/i18n: XMLResourceBundle.java XMLResourceBundleFactory.java
Date Tue, 28 Jun 2005 22:59:14 GMT
Author: joerg
Date: Tue Jun 28 15:59:13 2005
New Revision: 202298

URL: http://svn.apache.org/viewcvs?rev=202298&view=rev
Log:
made the sourceURI of an XMLBundle unmodifiable,
i.e. sourceURI can only be set via init() and no longer changed in load() or update()
http://marc.theaimsgroup.com/?t=111978020800002&r=1&w=4

Modified:
    cocoon/trunk/src/java/org/apache/cocoon/i18n/XMLResourceBundle.java
    cocoon/trunk/src/java/org/apache/cocoon/i18n/XMLResourceBundleFactory.java

Modified: cocoon/trunk/src/java/org/apache/cocoon/i18n/XMLResourceBundle.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/i18n/XMLResourceBundle.java?rev=202298&r1=202297&r2=202298&view=diff
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/i18n/XMLResourceBundle.java (original)
+++ cocoon/trunk/src/java/org/apache/cocoon/i18n/XMLResourceBundle.java Tue Jun 28 15:59:13
2005
@@ -53,7 +53,7 @@
  * </pre>
  *
  * Value can be any well formed XML snippet and it will be cached by the key specified
- * in the attrbute <code>key</code>. Objects returned by this {@link Bundle}
implementation
+ * in the attribute <code>key</code>. Objects returned by this {@link Bundle}
implementation
  * are instances of the {@link ParamSaxBuffer} class.
  *
  * @author <a href="mailto:dev@cocoon.apache.org">Apache Cocoon Team</a>
@@ -82,11 +82,10 @@
      */
     public static final String AT_KEY = "key";
 
-
     /**
-     * Bundle name
+     * Source URI of the bundle
      */
-    private String name;
+    private String sourceURI;
 
     /**
      * Bundle validity
@@ -147,7 +146,7 @@
         }
 
         public void startElement(String ns, String localName, String qName, Attributes atts)
throws SAXException {
-            switch (state) {
+            switch (this.state) {
                 case 0:
                     // <i18n:catalogue>
                     if (!"".equals(ns) && !NS.equals(ns)) {
@@ -158,7 +157,7 @@
                         throw new SAXException("Root element must be <" + EL_CATALOGUE
+ ">.");
                     }
                     this.namespace = ns;
-                    state ++;
+                    this.state++;
                     break;
                 case 1:
                     // <i18n:message>
@@ -175,12 +174,12 @@
                         throw new SAXException("<" + EL_MESSAGE + "> must have '" +
                                                AT_KEY + "' attribute.");
                     }
-                    buffer = new ParamSaxBuffer();
-                    values.put(key, buffer);
-                    state ++;
+                    this.buffer = new ParamSaxBuffer();
+                    this.values.put(key, this.buffer);
+                    this.state++;
                     break;
                 case 2:
-                    buffer.startElement(ns, localName, qName, atts);
+                    this.buffer.startElement(ns, localName, qName, atts);
                     break;
                 default:
                     throw new SAXException("Internal error: Invalid state");
@@ -188,20 +187,20 @@
         }
 
         public void endElement(String ns, String localName, String qName) throws SAXException
{
-            switch (state) {
+            switch (this.state) {
                 case 0:
                     break;
                 case 1:
                     // </i18n:catalogue>
-                    state --;
+                    this.state--;
                     break;
                 case 2:
                     if (this.namespace.equals(ns) && EL_MESSAGE.equals(localName))
{
                         // </i18n:message>
                         this.buffer = null;
-                        state --;
+                        this.state--;
                     } else {
-                        buffer.endElement(ns, localName, qName);
+                        this.buffer.endElement(ns, localName, qName);
                     }
                     break;
                 default:
@@ -210,26 +209,26 @@
         }
 
         public void startPrefixMapping(String prefix, String uri) throws SAXException {
-            if (buffer != null) {
-                buffer.startPrefixMapping(prefix, uri);
+            if (this.buffer != null) {
+                this.buffer.startPrefixMapping(prefix, uri);
             }
         }
 
         public void endPrefixMapping(String prefix) throws SAXException {
-            if (buffer != null) {
-                buffer.endPrefixMapping(prefix);
+            if (this.buffer != null) {
+                this.buffer.endPrefixMapping(prefix);
             }
         }
 
         public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException
{
-            if (buffer != null) {
-                buffer.ignorableWhitespace(ch, start, length);
+            if (this.buffer != null) {
+                this.buffer.ignorableWhitespace(ch, start, length);
             }
         }
 
         public void characters(char[] ch, int start, int length) throws SAXException {
-            if (buffer != null) {
-                buffer.characters(ch, start, length);
+            if (this.buffer != null) {
+                this.buffer.characters(ch, start, length);
             }
         }
     }
@@ -254,8 +253,7 @@
     /**
      * Initalize the bundle
      *
-     * @param name name of the bundle
-     * @param sourceURL source URL of the XML bundle
+     * @param sourceURI source URI of the XML bundle
      * @param locale locale
      * @param parent parent bundle of this bundle
      *
@@ -263,38 +261,31 @@
      * @throws ProcessingException if an error occurs while loading the bundle
      * @throws SAXException if an error occurs while loading the bundle
      */
-    public void init(String name, String sourceURL, Locale locale, Bundle parent)
+    public void init(String sourceURI, Locale locale, Bundle parent)
     throws IOException, ProcessingException, SAXException {
-        if (getLogger().isDebugEnabled()) {
-            getLogger().debug("Loading XML bundle: " + name + ", locale: " + locale);
-        }
-
-        this.name = name;
+        this.sourceURI = sourceURI;
         this.locale = locale;
         this.parent = parent;
         this.values = new HashMap();
-        load(sourceURL);
+        load();
     }
 
     /**
-     * Load the XML bundle, based on the source URL.
-     *
-     * @param sourceURL source URL of the XML bundle
+     * Load the XML bundle, based on the source URI.
      *
      * @exception IOException if an IO error occurs while reading the file
      * @exception ProcessingException if no parser is configured
      * @exception SAXException if an error occurs while parsing the file
      */
-    protected void load(String sourceURL)
-    throws IOException, ProcessingException, SAXException {
+    protected void load() throws IOException, ProcessingException, SAXException {
         Source source = null;
         SourceResolver resolver = null;
         try {
-            int valid = this.validity == null? SourceValidity.INVALID: this.validity.isValid();
+            int valid = this.validity == null ? SourceValidity.INVALID : this.validity.isValid();
             if (valid != SourceValidity.VALID) {
                 // Saved validity is not valid, get new source and validity
                 resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE);
-                source = resolver.resolveURI(sourceURL);
+                source = resolver.resolveURI(this.sourceURI);
                 SourceValidity sourceValidity = source.getValidity();
                 if (valid == SourceValidity.INVALID || this.validity.isValid(sourceValidity)
!= SourceValidity.VALID) {
                     HashMap values = new HashMap();
@@ -302,14 +293,14 @@
                     this.validity = sourceValidity;
                     this.values = values;
                     if (getLogger().isDebugEnabled()) {
-                        getLogger().debug("Loaded XML bundle: " + this.name + ", locale:
" + this.locale);
+                        getLogger().debug("Loaded XML bundle: " + this.sourceURI + ", locale:
" + this.locale);
                     }
                 }
             }
         } catch (ServiceException e) {
             throw new ProcessingException("Can't lookup source resolver", e);
         } catch (MalformedURLException e) {
-            throw new SourceNotFoundException("Invalid resource URL: " + sourceURL, e);
+            throw new SourceNotFoundException("Invalid resource URL: " + this.sourceURI,
e);
         } finally {
             if (source != null) {
                 resolver.release(source);
@@ -319,15 +310,6 @@
     }
 
     /**
-     * Gets the name of the bundle.
-     *
-     * @return the name
-     */
-    public String getName() {
-        return this.name;
-    }
-
-    /**
      * Gets the validity of the bundle.
      *
      * @return the validity
@@ -356,7 +338,7 @@
             return null;
         }
 
-        Object value = values.get(key);
+        Object value = this.values.get(key);
         if (value == null && this.parent != null) {
             value = this.parent.getObject(key);
         }
@@ -397,16 +379,14 @@
     }
 
     /**
-     * Reload this bundle if URI's timestam is newer than ours
-     *
-     * @param sourceURL source URL of the XML bundle
+     * Reload this bundle if URI's timestamp is newer than ours.
      */
-    public void update(String sourceURL) {
+    public void update() {
         try {
-            load(sourceURL);
+            load();
         } catch (Exception e) {
-            getLogger().info("Resource update failed. " + this.name + ", locale: " + this.locale
+
-                             " Exception: " + e);
+            getLogger().info("Resource update failed. " + this.sourceURI + ", locale: " +
this.locale
+                             + " Exception: " + e);
         }
     }
 }

Modified: cocoon/trunk/src/java/org/apache/cocoon/i18n/XMLResourceBundleFactory.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/i18n/XMLResourceBundleFactory.java?rev=202298&r1=202297&r2=202298&view=diff
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/i18n/XMLResourceBundleFactory.java (original)
+++ cocoon/trunk/src/java/org/apache/cocoon/i18n/XMLResourceBundleFactory.java Tue Jun 28
15:59:13 2005
@@ -53,12 +53,12 @@
        implements BundleFactory, Serviceable, Configurable, Disposable, ThreadSafe, LogEnabled
{
 
     /**
-     * Cache of the bundles by file name
+     * Cache of the bundles
      */
     protected final Map cache = Collections.synchronizedMap(new HashMap());
 
     /**
-     * Cache for the file names of the bundles that were not found
+     * Cache for the bundles that were not found
      */
     protected final Map cacheNotFound = new HashMap();
 
@@ -152,7 +152,7 @@
      * @return the directory path
      */
     protected String getDirectory() {
-        return directory;
+        return this.directory;
     }
 
     /**
@@ -161,7 +161,7 @@
      * @return true if pre-loading all resources; false otherwise
      */
     protected boolean cacheAtStartup() {
-        return cacheAtStartup;
+        return this.cacheAtStartup;
     }
 
     /**
@@ -255,12 +255,11 @@
         }
 
         final String cacheKey = getCacheKey(directories, index, name, locale);
-        final String fileName = getFileName(directories[index], name, locale);
 
-        XMLResourceBundle bundle = selectCached(cacheKey, fileName);
+        XMLResourceBundle bundle = selectCached(cacheKey);
         if (bundle == null) {
             synchronized (this) {
-                bundle = selectCached(cacheKey, fileName);
+                bundle = selectCached(cacheKey);
                 if (bundle == null) {
                     boolean localeAvailable = (locale != null && !locale.getLanguage().equals(""));
                     index++;
@@ -276,6 +275,7 @@
                     }
 
                     if (!isNotFoundBundle(cacheKey)) {
+                        final String fileName = getFileName(directories[index - 1], name,
locale);
                         bundle = _loadBundle(name, fileName, locale, parentBundle);
                         updateCache(cacheKey, bundle);
                     }
@@ -309,7 +309,7 @@
             bundle = new XMLResourceBundle();
             bundle.enableLogging(this.logger);
             bundle.service(this.manager);
-            bundle.init(name, fileName, locale, parentBundle);
+            bundle.init(fileName, locale, parentBundle);
             return bundle;
         } catch (ResourceNotFoundException e) {
             getLogger().info("Resource not found: " + name + ", locale: " + locale +
@@ -416,13 +416,12 @@
      * Selects a bundle from the cache.
      *
      * @param cacheKey          caching key of the bundle
-     * @param fileName          file name of the bundle
      * @return                  the cached bundle; null, if not found
      */
-    protected XMLResourceBundle selectCached(String cacheKey, String fileName) {
+    protected XMLResourceBundle selectCached(String cacheKey) {
         XMLResourceBundle bundle = (XMLResourceBundle) this.cache.get(cacheKey);
         if (bundle != null) {
-            bundle.update(fileName);
+            bundle.update();
         }
 
         if (getLogger().isDebugEnabled()) {



Mime
View raw message