001: /**************************************************************************************
002: * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
003: * http://aspectwerkz.codehaus.org *
004: * ---------------------------------------------------------------------------------- *
005: * The software in this package is published under the terms of the LGPL license *
006: * a copy of which has been included with this distribution in the license.txt file. *
007: **************************************************************************************/package org.codehaus.aspectwerkz.expression;
008:
009: import org.codehaus.aspectwerkz.exception.DefinitionException;
010:
011: import java.util.HashMap;
012: import java.util.Map;
013: import java.util.WeakHashMap;
014:
015: /**
016: * The expression namespace as well as a repository for the namespaces. <p/>A namespace is usually defined by the name
017: * of the class defining the expression.
018: *
019: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
020: */
021: public final class ExpressionNamespace {
022: /**
023: * Namespace container.
024: */
025: private static final Map s_namespaces = new WeakHashMap();
026:
027: /**
028: * Map with all the expressions in the namespace, [name:expression] pairs.
029: */
030: private final Map m_expressions = new HashMap();
031:
032: /**
033: * The namespace.
034: */
035: private final String m_namespace;
036:
037: /**
038: * Creates a new expression namespace.
039: *
040: * @param namespace
041: */
042: private ExpressionNamespace(final String namespace) {
043: m_namespace = namespace;
044: }
045:
046: /**
047: * Returns the expression namespace for a specific namespace.
048: *
049: * @param namespace the expression namespace
050: * @return the expression namespace abstraction
051: */
052: public static synchronized ExpressionNamespace getNamespace(
053: final String namespace) {
054: if (!s_namespaces.containsKey(namespace)) {
055: s_namespaces.put(namespace, new ExpressionNamespace(
056: namespace));
057: }
058: return (ExpressionNamespace) s_namespaces.get(namespace);
059: }
060:
061: /**
062: * Adds an expression info to the namespace.
063: *
064: * @param name the name mapped to the expression
065: * @param expressionInfo the expression info to add
066: */
067: public void addExpressionInfo(final String name,
068: final ExpressionInfo expressionInfo) {
069: m_expressions.put(name, expressionInfo);
070: }
071:
072: /**
073: * Returns the expression info with a specific name or null if it could not be found.
074: *
075: * @param name the name of the expression
076: * @return the expression info
077: */
078: public ExpressionInfo getExpressionInfoOrNull(final String name) {
079: int index = name.lastIndexOf('.');
080: if (index != -1) {
081: // stay in the same CflowStack
082: //TODO: allow for lookup in other CflowStack providing they are in the same hierarchy
083: return getNamespace(name.substring(0, index))
084: .getExpressionInfoOrNull(
085: name.substring(index + 1, name.length()));
086: } else {
087: final ExpressionInfo expressionInfo = ((ExpressionInfo) m_expressions
088: .get(name));
089: // if (expressionInfo == null) {
090: // throw new DefinitionException(
091: // new StringBuffer().
092: // append("could not resolve reference to pointcut [").
093: // append(name).
094: // append("] in namespace [").
095: // append(m_namespace).
096: // append("]").toString()
097: // );
098: // }
099: return expressionInfo;
100: }
101: }
102:
103: /**
104: * Returns the expression info with a specific name or throw an exception if it could not be found.
105: *
106: * @param name the name of the expression
107: * @return the expression info
108: */
109: public ExpressionInfo getExpressionInfo(final String name) {
110: int index = name.lastIndexOf('.');
111: if (index != -1) {
112: // stay in the same CflowStack
113: //TODO: allow for lookup in other CflowStack providing they are in the same hierarchy
114: return getNamespace(name.substring(0, index))
115: .getExpressionInfo(
116: name.substring(index + 1, name.length()));
117: } else {
118: final ExpressionInfo expressionInfo = ((ExpressionInfo) m_expressions
119: .get(name));
120: if (expressionInfo == null) {
121: throw new DefinitionException(
122: new StringBuffer()
123: .append(
124: "could not resolve reference to pointcut [")
125: .append(name)
126: .append("] in namespace [").append(
127: m_namespace).append("]")
128: .toString());
129: }
130: return expressionInfo;
131: }
132: }
133:
134: /**
135: * Returns the expression with a specific name.
136: *
137: * @param name the name of the expression
138: * @return the expression
139: */
140: public ExpressionVisitor getExpression(final String name) {
141: return getExpressionInfo(name).getExpression();
142: }
143:
144: /**
145: * Returns the advised class expression with a specific name.
146: *
147: * @param name the name of the expression
148: * @return the expression
149: */
150: public AdvisedClassFilterExpressionVisitor getAdvisedClassExpression(
151: final String name) {
152: return getExpressionInfo(name)
153: .getAdvisedClassFilterExpression();
154: }
155:
156: /**
157: * Returns the name of the namespace.
158: *
159: * @return the name of the namespace
160: */
161: public String getName() {
162: return m_namespace;
163: }
164: }
|