View Javadoc

1   //
2   // Copyright (c) 2004, International Decision Systems
3   // all rights reserved
4   /*
5    * Copyright (c) 2004 International Decision Systems, Inc.  All Rights Reserved.
6    *
7    * By using this Software, You acknowledge that the Software is a valuable asset
8    * and trade secret of either International Decision Systems, Inc. ("IDSI") or a
9    * third party supplier of IDSI and constitutes confidential and proprietary
10   * information.
11   *
12   * NEITHER IDSI NOR ANY AGENT OR PERSON ACTING FOR OR WITH IDSI HAS MADE OR DOES
13   * MAKE ANY STATEMENTS, AFFIRMATIONS, REPRESENTATIONS OR WARRANTIES WHATSOEVER
14   * TO YOU, WHETHER EXPRESS OR IMPLIED, AS TO THE SOFTWARE, THE QUALITY OR
15   * CONDITION OF THE SOFTWARE, OR THE OPERATING CHARACTERISTICS OR RELIABILITY OF
16   * THE SOFTWARE, OR ITS SUITABILITY FOR ANY GENERAL OR PARTICULAR PURPOSE, OR AS
17   * TO ANY OTHER MATTER WHATSOEVER; ANY AND ALL OTHER WARRANTIES INCLUDING
18   * WITHOUT LIMITATION ANY WARRANTIES IMPLIED BY LAW, SUCH AS THE IMPLIED
19   * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND TITLE,
20   * USE AND NON-INFRINGEMENT; ARE HEREBY EXPRESSLY DISCLAIMED AND EXCLUDED.
21  */
22  package net.sourceforge.addam.ddlrun.utils;
23  
24  
25  
26  
27  /**
28   * Used to retrieve a ClassLoaderReader.
29   *
30   * @author TIM3
31   * @since Mar 5, 2005
32   */
33  public class ClassLoaderReaderFactory implements ResourceReaderFactory {
34  
35      /**
36       * Constructs a ClassLoaderReaderFactory with no base path
37       */
38      public ClassLoaderReaderFactory() {
39          basePath = "";
40      }
41  
42      /**
43       * Constructs a ClassLoaderReaderFactory the provided base path, which is
44       * prepended to all non-absolute requests (i.e. those not beginning with '/')
45       *
46       * @param basePath the path to prepend to relative paths passed to getReader
47       */
48      public ClassLoaderReaderFactory(String basePath) {
49          if (!basePath.endsWith(SEPARATOR) && basePath.length() > 0) {
50              // make sure the path ends with a separator
51              basePath += SEPARATOR;
52          }
53          this.basePath = basePath;
54      }
55  
56      public ResourceReader getReader(String resource) {
57          return this.getReader("", resource);
58      }
59  
60      public ResourceReader getReader(String path, String resource) {
61          String pathToUse;
62          if (path.startsWith(SEPARATOR)) {
63              // don't prepend basePath on absolute requests
64              pathToUse = path;
65          } else {
66              pathToUse = basePath + path;
67          }
68          if (!pathToUse.endsWith(SEPARATOR)) {
69              // make sure the path ends with a separator
70              pathToUse += SEPARATOR;
71          }
72          return new ClassLoaderReader(pathToUse, resource);
73      }
74  
75      private static final String SEPARATOR = "/";
76      private final String basePath;
77  }