KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > aspectwerkz > expression > ExpressionNamespace


1 /**************************************************************************************
2  * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved. *
3  * http://aspectwerkz.codehaus.org *
4  * ---------------------------------------------------------------------------------- *
5  * The software in this package is published under the terms of the LGPL license *
6  * a copy of which has been included with this distribution in the license.txt file. *
7  **************************************************************************************/

8 package org.codehaus.aspectwerkz.expression;
9
10 import org.codehaus.aspectwerkz.exception.DefinitionException;
11
12 import java.util.HashMap JavaDoc;
13 import java.util.Map JavaDoc;
14 import java.util.WeakHashMap JavaDoc;
15
16 /**
17  * The expression namespace as well as a repository for the namespaces. <p/>A namespace is usually defined by the name
18  * of the class defining the expression.
19  *
20  * @author <a HREF="mailto:jboner@codehaus.org">Jonas Bonér </a>
21  */

22 public final class ExpressionNamespace {
23     /**
24      * Namespace container.
25      */

26     private static final Map JavaDoc s_namespaces = new WeakHashMap JavaDoc();
27
28     /**
29      * Map with all the expressions in the namespace, [name:expression] pairs.
30      */

31     private final Map JavaDoc m_expressions = new HashMap JavaDoc();
32
33     /**
34      * The namespace.
35      */

36     private final String JavaDoc m_namespace;
37
38     /**
39      * Creates a new expression namespace.
40      *
41      * @param namespace
42      */

43     private ExpressionNamespace(final String JavaDoc namespace) {
44         m_namespace = namespace;
45     }
46
47     /**
48      * Returns the expression namespace for a specific namespace.
49      *
50      * @param namespace the expression namespace
51      * @return the expression namespace abstraction
52      */

53     public static synchronized ExpressionNamespace getNamespace(final String JavaDoc namespace) {
54         if (!s_namespaces.containsKey(namespace)) {
55             s_namespaces.put(namespace, new ExpressionNamespace(namespace));
56         }
57         return (ExpressionNamespace) s_namespaces.get(namespace);
58     }
59
60     /**
61      * Adds an expression info to the namespace.
62      *
63      * @param name the name mapped to the expression
64      * @param expressionInfo the expression info to add
65      */

66     public void addExpressionInfo(final String JavaDoc name, final ExpressionInfo expressionInfo) {
67         m_expressions.put(name, expressionInfo);
68     }
69
70     /**
71      * Returns the expression info with a specific name or null if it could not be found.
72      *
73      * @param name the name of the expression
74      * @return the expression info
75      */

76     public ExpressionInfo getExpressionInfoOrNull(final String JavaDoc name) {
77         int index = name.lastIndexOf('.');
78         if (index != -1) {
79             // stay in the same CflowStack
80
//TODO: allow for lookup in other CflowStack providing they are in the same hierarchy
81
return getNamespace(name.substring(0, index)).getExpressionInfoOrNull(
82                     name.substring(index + 1, name.length())
83             );
84         } else {
85             final ExpressionInfo expressionInfo = ((ExpressionInfo) m_expressions.get(name));
86 // if (expressionInfo == null) {
87
// throw new DefinitionException(
88
// new StringBuffer().
89
// append("could not resolve reference to pointcut [").
90
// append(name).
91
// append("] in namespace [").
92
// append(m_namespace).
93
// append("]").toString()
94
// );
95
// }
96
return expressionInfo;
97         }
98     }
99
100     /**
101      * Returns the expression info with a specific name or throw an exception if it could not be found.
102      *
103      * @param name the name of the expression
104      * @return the expression info
105      */

106     public ExpressionInfo getExpressionInfo(final String JavaDoc name) {
107         int index = name.lastIndexOf('.');
108         if (index != -1) {
109             // stay in the same CflowStack
110
//TODO: allow for lookup in other CflowStack providing they are in the same hierarchy
111
return getNamespace(name.substring(0, index)).getExpressionInfo(name.substring(index + 1, name.length()));
112         } else {
113             final ExpressionInfo expressionInfo = ((ExpressionInfo) m_expressions.get(name));
114             if (expressionInfo == null) {
115                 throw new DefinitionException(
116                         new StringBuffer JavaDoc().
117                         append("could not resolve reference to pointcut [").
118                         append(name).
119                         append("] in namespace [").
120                         append(m_namespace).
121                         append("]").toString()
122                 );
123             }
124             return expressionInfo;
125         }
126     }
127
128     /**
129      * Returns the expression with a specific name.
130      *
131      * @param name the name of the expression
132      * @return the expression
133      */

134     public ExpressionVisitor getExpression(final String JavaDoc name) {
135         return getExpressionInfo(name).getExpression();
136     }
137
138     /**
139      * Returns the advised class expression with a specific name.
140      *
141      * @param name the name of the expression
142      * @return the expression
143      */

144     public AdvisedClassFilterExpressionVisitor getAdvisedClassExpression(final String JavaDoc name) {
145         return getExpressionInfo(name).getAdvisedClassFilterExpression();
146     }
147
148     /**
149      * Returns the name of the namespace.
150      *
151      * @return the name of the namespace
152      */

153     public String JavaDoc getName() {
154         return m_namespace;
155     }
156 }
Popular Tags