hivemind-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hls...@apache.org
Subject cvs commit: jakarta-hivemind/framework/src/java/org/apache/hivemind Resource.java
Date Fri, 18 Jun 2004 13:50:34 GMT
hlship      2004/06/18 06:50:34

  Modified:    framework/src/java/org/apache/hivemind/ant
                        ConstructRegistry.java
               framework/src/java/org/apache/hivemind/util
                        LocalizedResourceFinder.java AbstractResource.java
                        ClasspathResource.java UtilStrings.properties
                        UtilMessages.java URLResource.java
                        LocalizedResource.java
               framework/src/java/org/apache/hivemind Resource.java
  Added:       framework/src/java/org/apache/hivemind/util
                        LocalizedFileResourceFinder.java FileResource.java
               framework/src/test/org/apache/hivemind/util
                        TestFileResource.java
               framework/src/test-data/TestFileResource
                        localizable_fr.properties localizable.properties
                        localizable_en.properties
  Log:
  Add FileResource implementatin of Resource.
  
  Revision  Changes    Path
  1.7       +2 -2      jakarta-hivemind/framework/src/java/org/apache/hivemind/ant/ConstructRegistry.java
  
  Index: ConstructRegistry.java
  ===================================================================
  RCS file: /home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/ant/ConstructRegistry.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ConstructRegistry.java	17 Jun 2004 15:16:14 -0000	1.6
  +++ ConstructRegistry.java	18 Jun 2004 13:50:33 -0000	1.7
  @@ -32,6 +32,7 @@
   import org.apache.hivemind.Resource;
   import org.apache.hivemind.impl.RegistryBuilder;
   import org.apache.hivemind.sdl.SDLDocumentAdaptor;
  +import org.apache.hivemind.util.FileResource;
   import org.apache.hivemind.util.URLResource;
   import org.apache.tools.ant.BuildException;
   import org.apache.tools.ant.Task;
  @@ -173,8 +174,7 @@
               return;
           }
   
  -        URL fileURL = file.toURL();
  -        Resource r = new URLResource(fileURL);
  +        Resource r = new FileResource(file.getPath());
   
           enqueue(r);
       }
  
  
  
  1.2       +0 -1      jakarta-hivemind/framework/src/java/org/apache/hivemind/util/LocalizedResourceFinder.java
  
  Index: LocalizedResourceFinder.java
  ===================================================================
  RCS file: /home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/util/LocalizedResourceFinder.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- LocalizedResourceFinder.java	26 Feb 2004 23:07:55 -0000	1.1
  +++ LocalizedResourceFinder.java	18 Jun 2004 13:50:33 -0000	1.2
  @@ -25,7 +25,6 @@
    * 
    *
    * @author Howard Lewis Ship
  - * @version $Id$
    */
   public class LocalizedResourceFinder
   {
  
  
  
  1.3       +6 -3      jakarta-hivemind/framework/src/java/org/apache/hivemind/util/AbstractResource.java
  
  Index: AbstractResource.java
  ===================================================================
  RCS file: /home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/util/AbstractResource.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- AbstractResource.java	5 Jun 2004 19:09:12 -0000	1.2
  +++ AbstractResource.java	18 Jun 2004 13:50:33 -0000	1.3
  @@ -20,6 +20,9 @@
   
   /**
    * Abstract implementation of {@link org.apache.hivemind.Resource}.
  + * 
  + * <p>Resource instances act as a kind of factory for new instances
  + * of the same type, via {@link #newResource(String)}.
    *
    * @author Howard Lewis Ship
    */
  @@ -56,7 +59,7 @@
               if (name.equals(_path))
                   return this;
   
  -            return buildNewResourceLocation(name);
  +            return newResource(name);
           }
   
           if (_folderPath == null)
  @@ -65,7 +68,7 @@
           if (name.equals(_name))
               return this;
   
  -        return buildNewResourceLocation(_folderPath + name);
  +        return newResource(_folderPath + name);
       }
   
       public String getPath()
  @@ -79,7 +82,7 @@
       }
   
   
  -    protected abstract Resource buildNewResourceLocation(String path);
  +    protected abstract Resource newResource(String path);
   
       private void split()
       {
  
  
  
  1.4       +1 -1      jakarta-hivemind/framework/src/java/org/apache/hivemind/util/ClasspathResource.java
  
  Index: ClasspathResource.java
  ===================================================================
  RCS file: /home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/util/ClasspathResource.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ClasspathResource.java	5 Jun 2004 19:09:12 -0000	1.3
  +++ ClasspathResource.java	18 Jun 2004 13:50:33 -0000	1.4
  @@ -90,7 +90,7 @@
           return 4783 & getPath().hashCode();
       }
   
  -    protected Resource buildNewResourceLocation(String path)
  +    protected Resource newResource(String path)
       {
           return new ClasspathResource(_resolver, path);
       }
  
  
  
  1.3       +3 -0      jakarta-hivemind/framework/src/java/org/apache/hivemind/util/UtilStrings.properties
  
  Index: UtilStrings.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/util/UtilStrings.properties,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- UtilStrings.properties	15 Jun 2004 13:42:33 -0000	1.2
  +++ UtilStrings.properties	18 Jun 2004 13:50:33 -0000	1.3
  @@ -22,3 +22,6 @@
   read-failure=Unable to read property {0} of object {1}: {2}
   null-object=Attempt to read or update properties of null.
   unable-to-introspect=Unable to introspect properties of class {0}: {1}
  +
  +bad-file-url=Error retrieving URL for file {0}: {1}
  +
  
  
  
  1.5       +10 -13    jakarta-hivemind/framework/src/java/org/apache/hivemind/util/UtilMessages.java
  
  Index: UtilMessages.java
  ===================================================================
  RCS file: /home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/util/UtilMessages.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- UtilMessages.java	17 Jun 2004 15:16:15 -0000	1.4
  +++ UtilMessages.java	18 Jun 2004 13:50:33 -0000	1.5
  @@ -14,6 +14,7 @@
   
   package org.apache.hivemind.util;
   
  +import java.io.File;
   import java.lang.reflect.Constructor;
   
   import org.apache.hivemind.HiveMind;
  @@ -48,7 +49,7 @@
               "invoke-failed",
               constructor.getDeclaringClass().getName(),
               HiveMind.getLocationString(location),
  -            _formatter.extractMessage(cause));
  +            cause);
       }
   
       public static String noPropertyWriter(String propertyName, Object target)
  @@ -58,9 +59,7 @@
   
       public static String writeFailure(String propertyName, Object target, Throwable cause)
       {
  -        return _formatter.format(
  -            "write-failure",
  -            new Object[] { propertyName, target, _formatter.extractMessage(cause)});
  +        return _formatter.format("write-failure", new Object[] { propertyName, target,
cause });
       }
   
       public static String noReader(String propertyName, Object target)
  @@ -70,11 +69,7 @@
   
       public static String readFailure(String propertyName, Object target, Throwable cause)
       {
  -        return _formatter.format(
  -            "read-failure",
  -            propertyName,
  -            target,
  -            _formatter.extractMessage(cause));
  +        return _formatter.format("read-failure", propertyName, target, cause);
       }
   
       public static String nullObject()
  @@ -84,9 +79,11 @@
   
       public static String unableToIntrospect(Class targetClass, Throwable cause)
       {
  -        return _formatter.format(
  -            "unable-to-introspect",
  -            targetClass.getName(),
  -            _formatter.extractMessage(cause));
  +        return _formatter.format("unable-to-introspect", targetClass.getName(), cause);
  +    }
  +
  +    public static String badFileURL(String path, Throwable cause)
  +    {
  +        return _formatter.format("bad-file-url", path, cause);
       }
   }
  
  
  
  1.3       +1 -1      jakarta-hivemind/framework/src/java/org/apache/hivemind/util/URLResource.java
  
  Index: URLResource.java
  ===================================================================
  RCS file: /home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/util/URLResource.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- URLResource.java	5 Jun 2004 19:09:12 -0000	1.2
  +++ URLResource.java	18 Jun 2004 13:50:33 -0000	1.3
  @@ -50,7 +50,7 @@
           return getPath();
       }
   
  -    protected Resource buildNewResourceLocation(String path)
  +    protected Resource newResource(String path)
       {
           return new URLResource(path);
       }
  
  
  
  1.2       +0 -2      jakarta-hivemind/framework/src/java/org/apache/hivemind/util/LocalizedResource.java
  
  Index: LocalizedResource.java
  ===================================================================
  RCS file: /home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/util/LocalizedResource.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- LocalizedResource.java	26 Feb 2004 23:07:55 -0000	1.1
  +++ LocalizedResource.java	18 Jun 2004 13:50:33 -0000	1.2
  @@ -21,8 +21,6 @@
    *  This object is immutable.
    * 
    *  @author Mindbridge
  - *  @version $Id$
  - *  @since 3.0
    */
   public class LocalizedResource
   {
  
  
  
  1.1                  jakarta-hivemind/framework/src/java/org/apache/hivemind/util/LocalizedFileResourceFinder.java
  
  Index: LocalizedFileResourceFinder.java
  ===================================================================
  //  Copyright 2004 The Apache Software Foundation
  //
  // 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.apache.org/licenses/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.apache.hivemind.util;
  
  import java.io.File;
  import java.util.Locale;
  
  
  /**
   * Used by {@link org.apache.tapestry.resource.FileResource}
   * to locate localizations of a given file.
   *
   * @author Howard Lewis Ship
   */
  public class LocalizedFileResourceFinder
  {
      public String findLocalizedPath(String path, Locale locale)
      {
          int dotx = path.lastIndexOf('.');
          LocalizedNameGenerator g =
              new LocalizedNameGenerator(path.substring(0, dotx), locale, path.substring(dotx));
  
          while (g.more())
          {
              String candidate = g.next();
  
              File f = new File(candidate);
  
              if (f.exists())
                  return candidate;
          }
  
          return path;
      }
  }
  
  
  
  1.1                  jakarta-hivemind/framework/src/java/org/apache/hivemind/util/FileResource.java
  
  Index: FileResource.java
  ===================================================================
  //  Copyright 2004 The Apache Software Foundation
  //
  // 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.apache.org/licenses/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.apache.hivemind.util;
  
  import java.io.File;
  import java.net.MalformedURLException;
  import java.net.URL;
  import java.util.Locale;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.apache.hivemind.Resource;
  
  /**
   * An implementation of {@link org.apache.hivemind.Resource} built around
   * {@link java.io.File}.
   *
   * @author Howard Lewis Ship
   */
  public class FileResource extends AbstractResource
  {
      private static final Log LOG = LogFactory.getLog(FileResource.class);
  
      public FileResource(String path)
      {
          super(path);
      }
  
      public FileResource(String path, Locale locale)
      {
          super(path, locale);
      }
  
      protected Resource newResource(String path)
      {
          return new FileResource(path);
      }
  
      private File getFile()
      {
          return new File(getPath());
      }
  
      public URL getResourceURL()
      {
          File file = getFile();
  
          try
          {
              if (file == null || !file.exists())
                  return null;
  
              return file.toURL();
          }
          catch (MalformedURLException ex)
          {
              LOG.error(UtilMessages.badFileURL(getPath(), ex), ex);
              return null;
          }
      }
  
      public Resource getLocalization(Locale locale)
      {
          LocalizedFileResourceFinder f = new LocalizedFileResourceFinder();
  
          String path = getPath();
  
          String finalPath = f.findLocalizedPath(path, locale);
  
          if (finalPath.equals(path))
              return this;
  
          return newResource(finalPath);
      }
  
      public String toString()
      {
          return getPath();
      }
  
  }
  
  
  
  1.1                  jakarta-hivemind/framework/src/test/org/apache/hivemind/util/TestFileResource.java
  
  Index: TestFileResource.java
  ===================================================================
  //  Copyright 2004 The Apache Software Foundation
  //
  // 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.apache.org/licenses/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.apache.hivemind.util;
  
  import hivemind.test.FrameworkTestCase;
  
  import java.io.File;
  import java.net.URL;
  import java.util.Locale;
  
  import org.apache.hivemind.Resource;
  
  /**
   * Tests for {@link org.apache.hivemind.util.FileResource} and
   * {@link org.apache.hivemind.util.LocalizedFileResourceFinder}.
   *
   * @author Howard Lewis Ship
   */
  public class TestFileResource extends FrameworkTestCase
  {
      private static final String DIR = "src/test-data/TestFileResource";
      private static final String BASEFILE = DIR + "/localizable.properties";
  
      public void testFindLocalization() throws Exception
      {
          String path = getFrameworkPath(BASEFILE);
  
          // validate that the test is running properly
  
          File file = new File(path);
  
          if (!file.exists())
              throw new IllegalArgumentException(
                  "Configuration error: base path ("
                      + path
                      + ") evaluates to "
                      + file.getCanonicalPath()
                      + ", which does not exist. Set the correct working directory or FRAMEWORK_DIR
system property.");
  
          LocalizedFileResourceFinder f = new LocalizedFileResourceFinder();
  
          String enPath = getFrameworkPath(DIR + "/localizable_en.properties");
  
          assertEquals(enPath, f.findLocalizedPath(path, Locale.ENGLISH));
  
          String frPath = getFrameworkPath(DIR + "/localizable_fr.properties");
  
          assertEquals(frPath, f.findLocalizedPath(path, Locale.FRANCE));
      }
  
      public void testNoLocalization()
      {
          String path = getFrameworkPath(BASEFILE);
          LocalizedFileResourceFinder f = new LocalizedFileResourceFinder();
  
          assertEquals(path, f.findLocalizedPath(path, Locale.GERMAN));
      }
  
      public void testExisting() throws Exception
      {
          String path = getFrameworkPath(BASEFILE);
  
          Resource r = new FileResource(path);
  
          File file = new File(path);
          URL expected = file.toURL();
  
          assertEquals(expected, r.getResourceURL());
      }
  
      public void testMissing() throws Exception
      {
          String path = getFrameworkPath(DIR + "/does-not-exist");
  
          Resource r = new FileResource(path);
  
          assertEquals(null, r.getResourceURL());
      }
  
      public void testCreateWithLocale() throws Exception
      {
          String path = getFrameworkPath(DIR + "/localizable_fr.properties");
  
          Resource r = new FileResource(path, Locale.CANADA_FRENCH);
  
          assertEquals(path, r.getPath());
          assertEquals(Locale.CANADA_FRENCH, r.getLocale());
      }
  
      public void testGetLocalization()
      {
          String path = getFrameworkPath(BASEFILE);
  
          Resource base = new FileResource(path);
          Resource localized = base.getLocalization(Locale.FRANCE);
  
          String expected = getFrameworkPath(DIR + "/localizable_fr.properties");
  
          assertEquals(expected, localized.getPath());
      }
  
      public void testLocalizationMissing()
      {
          String path = getFrameworkPath(BASEFILE);
  
          Resource base = new FileResource(path);
          Resource localized = base.getLocalization(Locale.CHINA);
  
          assertSame(base, localized);
      }
  
      public void testToString()
      {
          String path = getFrameworkPath(BASEFILE);
  
          Resource r = new FileResource(path);
  
          assertEquals(path, r.toString());
      }
  }
  
  
  
  1.1                  jakarta-hivemind/framework/src/test-data/TestFileResource/localizable_fr.properties
  
  	<<Binary file>>
  
  
  1.1                  jakarta-hivemind/framework/src/test-data/TestFileResource/localizable.properties
  
  	<<Binary file>>
  
  
  1.1                  jakarta-hivemind/framework/src/test-data/TestFileResource/localizable_en.properties
  
  	<<Binary file>>
  
  
  1.2       +0 -1      jakarta-hivemind/framework/src/java/org/apache/hivemind/Resource.java
  
  Index: Resource.java
  ===================================================================
  RCS file: /home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/Resource.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Resource.java	26 Feb 2004 23:07:56 -0000	1.1
  +++ Resource.java	18 Jun 2004 13:50:33 -0000	1.2
  @@ -43,7 +43,6 @@
    * Folders must be represented with a trailing slash.
    *
    * @author Howard Lewis Ship
  - * @version $Id$
    *
    **/
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: hivemind-cvs-unsubscribe@jakarta.apache.org
For additional commands, e-mail: hivemind-cvs-help@jakarta.apache.org


Mime
View raw message