[Yanel-commits] rev 34855 - in public/yanel/trunk/src/core/java/org/wyona/yanel/core: i18n source transformation

simon at wyona.com simon at wyona.com
Fri Apr 4 17:32:47 CEST 2008


Author: simon
Date: 2008-04-04 17:32:47 +0200 (Fri, 04 Apr 2008)
New Revision: 34855

Added:
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/i18n/MessageProviderFactory.java
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/YanelStreamSource.java
Modified:
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/YanelRepositoryResolver.java
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/transformation/I18nTransformer3.java
Log:
see: http://bugzilla.wyona.com/cgi-bin/bugzilla/show_bug.cgi?id=6241
thanks to josias!

Added: public/yanel/trunk/src/core/java/org/wyona/yanel/core/i18n/MessageProviderFactory.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/i18n/MessageProviderFactory.java	                        (rev 0)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/i18n/MessageProviderFactory.java	2008-04-04 15:32:47 UTC (rev 34855)
@@ -0,0 +1,117 @@
+/*
+ * Copyright 2008 Wyona
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.wyona.org/licenses/APACHE-LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.wyona.yanel.core.i18n;
+
+import java.io.InputStream;
+import java.util.HashMap;
+
+import javax.xml.transform.Source;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.URIResolver;
+import javax.xml.transform.sax.SAXSource;
+
+import org.wyona.yanel.core.source.SourceException;
+import org.wyona.yanel.core.source.YanelStreamSource;
+
+/**
+ * A MessageProviderFactory creates MessageProviders. It supports two different
+ * providers, ResourceBundleMessageProvider and XMLMessageProvider.
+ * It supports caching of XMLMessageProviders.
+ */
+public class MessageProviderFactory {
+    protected static HashMap cache = new HashMap();
+
+    /**
+     * Gets a message provider for the given catalogue. If the catalogue name is
+     * a URI, the catalogue is assumed to be an XML catalogue and an
+     * XMLMessageProvider will be instantiated. In this case the catalogue will
+     * be resolved by the resolver and the message provider will be cached if
+     * possible. Otherwise, if the catalogue name is not a URI, the catalogue is
+     * assumed to be a resource bundle catalogue and a
+     * ResourceBundleMessageProvider will be instantiated. 
+     * A ResourceBundleMessageProvider will not be cached.
+     * 
+     * @param catalogue
+     * @param resolver
+     * @return message provider
+     * @throws SourceException
+     */
+    public static synchronized MessageProvider getMessageProvider(String catalogue,
+            URIResolver resolver) throws SourceException {
+        MessageProvider messageProvider = null;
+
+        if (catalogue.indexOf(":") == -1) {
+            messageProvider = getResourceBundleMessageProvider(catalogue);
+        } else {
+            messageProvider = getXMLMessageProvider(catalogue, resolver);
+        }
+
+        return messageProvider;
+    }
+
+    public static synchronized MessageProvider getResourceBundleMessageProvider(String catalogue) {
+        return new ResourceBundleMessageProvider(catalogue);
+    }
+
+    public static synchronized MessageProvider getXMLMessageProvider(String catalogue,
+            URIResolver resolver) throws SourceException {
+        try {
+            Source source = resolver.resolve(catalogue, null);
+            long sourceLastModified = -1;
+
+            if (source instanceof YanelStreamSource) {
+                sourceLastModified = ((YanelStreamSource) source).getLastModified();
+            }
+
+            if (cache.containsKey(catalogue) && sourceLastModified != -1) {
+                // message provider is in the cache, verify validity
+                MessageProviderCacheEntry entry = (MessageProviderCacheEntry) cache.get(catalogue);
+                long cacheLastModified = entry.lastModified;
+
+                if (cacheLastModified >= sourceLastModified) {
+                    // cached message provider is valid
+                    return entry.messageProvider;
+                }
+            }
+
+            InputStream is = SAXSource.sourceToInputSource(source).getByteStream();
+            XMLMessageProvider messageProvider = new XMLMessageProvider(is);
+
+            if (sourceLastModified != -1) {
+                MessageProviderCacheEntry cacheEntry = new MessageProviderCacheEntry(
+                        messageProvider, sourceLastModified);
+                cache.put(catalogue, cacheEntry);
+            }
+
+            return messageProvider;
+
+        } catch (TransformerException e) {
+            throw new SourceException(e.getMessage(), e);
+        }
+    }
+
+    static class MessageProviderCacheEntry {
+        MessageProvider messageProvider;
+
+        long lastModified;
+
+        MessageProviderCacheEntry(MessageProvider messageProvider, long lastModified) {
+            this.messageProvider = messageProvider;
+            this.lastModified = lastModified;
+        }
+    }
+}
\ No newline at end of file

Modified: public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/YanelRepositoryResolver.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/YanelRepositoryResolver.java	2008-04-04 15:15:02 UTC (rev 34854)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/YanelRepositoryResolver.java	2008-04-04 15:32:47 UTC (rev 34855)
@@ -10,6 +10,7 @@
 import org.wyona.yanel.core.Resource;
 import org.wyona.yanel.core.Yanel;
 import org.wyona.yanel.core.map.Realm;
+import org.wyona.yarep.core.Node;
 import org.wyona.yarep.core.Repository;
 
 
@@ -82,8 +83,11 @@
             } else {
                 repo = realm.getRepository();
             }
-            InputStream in = repo.getNode(path).getInputStream();
-            return new StreamSource(in);
+            Node node = repo.getNode(path);
+            InputStream in = node.getInputStream();
+            YanelStreamSource source = new YanelStreamSource(in);
+            source.setLastModified(node.getLastModified());
+            return source;
         } catch (Exception e) {
             String errorMsg = "Could not resolve URI: " + href + ": " + e.toString();
             log.error(errorMsg, e);

Added: public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/YanelStreamSource.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/YanelStreamSource.java	                        (rev 0)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/YanelStreamSource.java	2008-04-04 15:32:47 UTC (rev 34855)
@@ -0,0 +1,62 @@
+package org.wyona.yanel.core.source;
+
+import java.io.File;
+import java.io.InputStream;
+import java.io.Reader;
+
+import javax.xml.transform.stream.StreamSource;
+
+import org.apache.log4j.Logger;
+
+/**
+ */
+public class YanelStreamSource extends StreamSource {
+    private static Logger log = Logger.getLogger(YanelStreamSource.class);
+
+    protected long lastModified = -1;
+    
+    public YanelStreamSource() {
+    }
+    
+    public YanelStreamSource(File f) {
+        super(f);
+        this.lastModified = f.lastModified();
+    }
+
+    public YanelStreamSource(InputStream inputStream, String systemId) {
+        super(inputStream, systemId);
+    }
+
+    public YanelStreamSource(InputStream inputStream) {
+        super(inputStream);
+    }
+
+    public YanelStreamSource(Reader reader, String systemId) {
+        super(reader, systemId);
+    }
+
+    public YanelStreamSource(Reader reader) {
+        super(reader);
+    }
+
+    public YanelStreamSource(String systemId) {
+        super(systemId);
+    }
+
+    /**
+     * Gets the last modification date of this source.
+     * @return last modification date in ms or -1 if it's unknown.
+     */
+    public long getLastModified() {
+        return this.lastModified;
+    }
+    
+    /**
+     * Sets the last modification date of this source.
+     * Only the creator of this source should call this method.
+     * @param lastModified last modification date in ms
+     */
+    public void setLastModified(long lastModified) {
+        this.lastModified = lastModified;
+    }
+}

Modified: public/yanel/trunk/src/core/java/org/wyona/yanel/core/transformation/I18nTransformer3.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/transformation/I18nTransformer3.java	2008-04-04 15:15:02 UTC (rev 34854)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/transformation/I18nTransformer3.java	2008-04-04 15:32:47 UTC (rev 34855)
@@ -14,6 +14,7 @@
 import org.apache.log4j.Category;
 import org.wyona.yanel.core.i18n.MessageManager;
 import org.wyona.yanel.core.i18n.MessageProvider;
+import org.wyona.yanel.core.i18n.MessageProviderFactory;
 import org.wyona.yanel.core.i18n.ResourceBundleMessageProvider;
 import org.wyona.yanel.core.i18n.XMLMessageProvider;
 import org.wyona.yanel.core.source.SourceException;
@@ -128,18 +129,10 @@
     }
     
     protected MessageProvider getMessageProvider(String catalogue) {
-        if (catalogue.indexOf(":") == -1) {
-            return new ResourceBundleMessageProvider(catalogue);
-        } else {
-            try {
-                Source source = resolver.resolve(catalogue, null);
-                InputStream is = SAXSource.sourceToInputSource(source).getByteStream();
-                return new XMLMessageProvider(is);
-            } catch (SourceException e) {
-                throw new RuntimeException(e.getMessage(), e);
-            } catch (TransformerException e) {
-                throw new RuntimeException(e.getMessage(), e);
-            }
+        try {
+            return MessageProviderFactory.getMessageProvider(catalogue, resolver);
+        } catch (SourceException e) {
+            throw new RuntimeException(e.getMessage(), e);
         }
     }
     



More information about the Yanel-commits mailing list