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.regexp;
008:
009: import org.codehaus.aspectwerkz.expression.ExpressionException;
010: import org.codehaus.aspectwerkz.util.Strings;
011:
012: import java.io.ObjectInputStream;
013:
014: /**
015: * Implements the regular expression pattern matcher for names.
016: *
017: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
018: */
019: public class NamePattern extends Pattern {
020: /**
021: * The name pattern.
022: */
023: protected transient com.karneim.util.collection.regex.Pattern m_namePattern;
024:
025: /**
026: * The name pattern as a string.
027: */
028: protected String m_pattern;
029:
030: /**
031: * Private constructor.
032: *
033: * @param pattern the pattern
034: */
035: NamePattern(final String pattern) {
036: m_pattern = pattern;
037: escape(m_pattern);
038: }
039:
040: /**
041: * Matches a name.
042: *
043: * @param name the name
044: * @return true if we have a matche
045: */
046: public boolean matches(final String name) {
047: if (name == null) {
048: throw new IllegalArgumentException("name can not be null");
049: }
050: if (name.equals("")) {
051: return false;
052: }
053: return m_namePattern.contains(name);
054: }
055:
056: /**
057: * Returns the pattern as a string.
058: *
059: * @return the pattern
060: */
061: public String getPattern() {
062: return m_pattern;
063: }
064:
065: /**
066: * Escapes the name pattern.
067: *
068: * @param namePattern the name pattern
069: */
070: protected void escape(String namePattern) {
071: try {
072: if (namePattern.equals(REGULAR_WILDCARD)) {
073: namePattern = "[a-zA-Z0-9_$.]+";
074: } else {
075: namePattern = Strings.replaceSubString(namePattern,
076: "*", "[a-zA-Z0-9_$]*");
077: }
078: m_namePattern = new com.karneim.util.collection.regex.Pattern(
079: namePattern);
080: } catch (Throwable e) {
081: throw new ExpressionException(
082: "type pattern is not well formed: " + namePattern,
083: e);
084: }
085: }
086:
087: /**
088: * Provides custom deserialization.
089: *
090: * @param stream the object input stream containing the serialized object
091: * @throws Exception in case of failure
092: */
093: private void readObject(final ObjectInputStream stream)
094: throws Exception {
095: ObjectInputStream.GetField fields = stream.readFields();
096: m_pattern = (String) fields.get("m_pattern", null);
097: escape(m_pattern);
098: }
099:
100: public int hashCode() {
101: int result = 17;
102: result = (37 * result) + hashCodeOrZeroIfNull(m_pattern);
103: result = (37 * result) + hashCodeOrZeroIfNull(m_namePattern);
104: return result;
105: }
106:
107: protected static int hashCodeOrZeroIfNull(final Object o) {
108: if (null == o) {
109: return 19;
110: }
111: return o.hashCode();
112: }
113:
114: public boolean equals(final Object o) {
115: if (this == o) {
116: return true;
117: }
118: if (!(o instanceof NamePattern)) {
119: return false;
120: }
121: final NamePattern obj = (NamePattern) o;
122: return areEqualsOrBothNull(obj.m_pattern, this .m_pattern)
123: && areEqualsOrBothNull(obj.m_namePattern,
124: this .m_namePattern);
125: }
126:
127: protected static boolean areEqualsOrBothNull(final Object o1,
128: final Object o2) {
129: if (null == o1) {
130: return (null == o2);
131: }
132: return o1.equals(o2);
133: }
134: }
|