Source code: com/aendvari/tethys/context/ContextMap.java
1 /*
2 * ContextMap.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 import java.util.Collection;
13
14 import com.aendvari.common.osm.*;
15
16 /**
17 * <p>Associates {@link Context} objects with their location in the hierarchy.</p>
18 *
19 * @author Scott Milne
20 *
21 */
22
23 public class ContextMap
24 {
25 /* Variables */
26
27 /** Stores {@link Context} objects in a hierarchy. */
28 private Osm contextMap;
29
30 /** The default context of the hierarchy. */
31 private Context defaultContext;
32
33
34 /* Constructor */
35
36 /**
37 * Build a <code>ContextMap</code> object. No default context will be available.
38 *
39 */
40
41 public ContextMap()
42 {
43 contextMap = new Osm();
44 defaultContext = null;
45 }
46
47 /**
48 * Build a <code>ContextMap</code> object.
49 *
50 * @param context A {@link Context} object that sets the default
51 * context for the request to use
52 *
53 */
54
55 public ContextMap(Context context)
56 {
57 contextMap = new Osm();
58 defaultContext = context;
59 }
60
61 /**
62 * Returns the default context for this map.
63 *
64 * @return The default context object.
65 *
66 */
67
68 public Context getDefaultContext()
69 {
70 return defaultContext;
71 }
72
73 /**
74 * Returns the {@link Context} for the specified location.
75 *
76 * @param location The location of the context.
77 * @param returnDefault True returns the default context if search unsuccessful, false returns null.
78 *
79 * @return The {@link Context} associated with the specified location.
80 *
81 */
82
83 public Context getContext(String location, boolean returnDefault)
84 {
85 // check for empty location
86 if (location.length() > 0)
87 {
88 // remove redundant '/'
89 if (location.startsWith("/") || location.startsWith("."))
90 {
91 location = location.substring(1);
92
93 // check for root
94 if (location.length() == 0)
95 {
96 return defaultContext;
97 }
98 }
99
100 // search for the specified context
101 OsmNode node = SimpleOsmPath.getNode(contextMap, location, false);
102
103 // select this context only if exists
104 if (node != null)
105 {
106 // return the context assigned to the node found
107 return (Context)node.getNodeValue();
108 }
109 }
110
111 // context not found
112
113 // check for default context
114 if (returnDefault)
115 {
116 return defaultContext;
117 }
118 else
119 {
120 return null;
121 }
122 }
123
124 /**
125 * Returns all {@link Context Contexts} in this map.
126 *
127 * @return An {@link Osm} containing {@link Context Contexts}.
128 *
129 */
130
131 public Osm getContexts()
132 {
133 return contextMap;
134 }
135
136 /**
137 * Adds the given context to this map.
138 *
139 * @param context The context to add to the map.
140 *
141 */
142
143 public void addContext(Context context)
144 {
145 // find/create node to place context
146 OsmNode node = SimpleOsmPath.getNode(contextMap, context.getLocation(), true);
147
148 // place context into node
149 node.setNodeValue(context);
150 }
151
152 /**
153 * Sets the given context in this map. Synonym for {@link #addContext}.
154 *
155 * @param context The context to add to the map.
156 *
157 */
158
159 public void setContext(Context context)
160 {
161 addContext(context);
162 }
163 }
164