Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/aendvari/tethys/context/Context.java


1   /*
2    * Context.java
3    *
4    * Copyright (c) 2001, 2002 Aendvari, Ltd. All Rights Reserved.
5    *
6    * See the file LICENSE for terms of use.
7    *
8    */
9   
10  package com.aendvari.tethys.context;
11  
12  /**
13   * <p>A base class for context objects. Describes the location of the context
14   * object within the hierarchy.<p>
15   *
16   * @author  Scott Milne
17   *
18   */
19  
20  public class Context
21  {
22    /* Variables */
23  
24    /** The location of this context within the hierarchy. */
25    private String location;
26  
27  
28    /* Constructors */
29  
30    /**
31     * Creates a <code>Context</code> located at the specified position.
32     *
33       * @param    location          The location of this context in the hierarchy.
34     *
35     */
36  
37    public Context(String location)
38    {
39      this.location = location;
40  
41      // normalize location path
42      this.location = this.location.replace('.', '/');
43    }
44  
45    /**
46     * Creates a <code>Context</code> located relative to the parent context.
47     *
48       * @param    location          The location of this context relative to the parent context.
49       * @param    parent            The parent context.
50     *
51     */
52  
53    public Context(String location, Context parent)
54    {
55      this.location = combinePaths(parent.getLocation(), location);
56  
57      // normalize location path
58      this.location = this.location.replace('.', '/');
59    }
60  
61  
62    /* Attributes */
63  
64    public String getLocation() { return location; }
65  
66  
67    /**
68     * Extends the base path with the supplied relative path.
69     *
70     * @param  base        The base path for the relative path.
71     * @param  relative      The relative path from the base.
72     *
73     * @return            The combined base and relative paths.
74     *
75     */
76  
77    protected String combinePaths(String base, String relative)
78    {
79      StringBuffer extended = new StringBuffer(base.length() + relative.length() + 1);
80  
81      // include root
82      extended.append(base);
83  
84      // check for preceding separator
85      if (!relative.startsWith(".") && !relative.startsWith("/"))
86      {
87        // if no separator, then add one
88        extended.append("/");
89      }
90  
91      // add relative, if not just separator
92      if (!relative.equals(".") && !relative.equals("/"))
93      {
94        // include relative path
95        extended.append(relative);
96      }
97  
98      return extended.toString();
99    }
100 
101   /**
102    * Extends the location of this context with the supplied relative path.
103    *
104    * @param  relative      The relative path to include.
105    *
106    * @return            The combined location and relative path.
107    *
108    */
109 
110   public String extendLocation(String relative)
111   {
112     return combinePaths(location, relative);
113   }
114 
115 
116   /* Debug */
117 
118   public String toString()
119   {
120     String toString = "Context=[";
121 
122     toString += "location=" + location;
123     toString += "]";
124 
125     return toString;
126   }
127 }
128