001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.config;
022:
023: import com.db4o.foundation.ArgumentNullException;
024:
025: /**
026: * Wildcard Alias functionality to create aliases for packages,
027: * namespaces or multiple similar named classes. One single '*'
028: * wildcard character is supported in the names.
029: * <br><br>See {@link Alias} for concrete examples.
030: */
031: public class WildcardAlias implements Alias {
032:
033: private final WildcardPattern _storedPattern;
034:
035: private final WildcardPattern _runtimePattern;
036:
037: /**
038: * Create a WildcardAlias with two patterns, the
039: * stored pattern and the pattern that is to be used
040: * at runtime. One single '*' is allowed as a wildcard
041: * character.
042: */
043: public WildcardAlias(String storedPattern, String runtimePattern) {
044:
045: if (null == storedPattern)
046: throw new ArgumentNullException("storedPattern");
047: if (null == runtimePattern)
048: throw new ArgumentNullException("runtimePattern");
049:
050: _storedPattern = new WildcardPattern(storedPattern);
051: _runtimePattern = new WildcardPattern(runtimePattern);
052: }
053:
054: /**
055: * resolving is done through simple pattern matching
056: */
057: public String resolveRuntimeName(String runtimeTypeName) {
058: return resolve(_runtimePattern, _storedPattern, runtimeTypeName);
059: }
060:
061: /**
062: * resolving is done through simple pattern matching
063: */
064:
065: public String resolveStoredName(String storedTypeName) {
066: return resolve(_storedPattern, _runtimePattern, storedTypeName);
067: }
068:
069: private String resolve(final WildcardPattern from,
070: final WildcardPattern to, String typeName) {
071: String match = from.matches(typeName);
072: return match != null ? to.inject(match) : null;
073: }
074:
075: static class WildcardPattern {
076: private String _head;
077: private String _tail;
078:
079: public WildcardPattern(String pattern) {
080: String[] parts = split(pattern);
081:
082: _head = parts[0];
083: _tail = parts[1];
084: }
085:
086: public String inject(String s) {
087: return _head + s + _tail;
088: }
089:
090: public String matches(String s) {
091: if (!s.startsWith(_head) || !s.endsWith(_tail))
092: return null;
093: return s.substring(_head.length(), s.length()
094: - _tail.length());
095: }
096:
097: private void invalidPattern() {
098: throw new IllegalArgumentException("only one '*' character");
099: }
100:
101: String[] split(String pattern) {
102: int index = pattern.indexOf('*');
103: if (-1 == index || index != pattern.lastIndexOf('*'))
104: invalidPattern();
105: return new String[] { pattern.substring(0, index),
106: pattern.substring(index + 1) };
107: }
108: }
109:
110: }
|