KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > config > WildcardAlias


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.config;
22
23 import com.db4o.foundation.ArgumentNullException;
24
25 /**
26  * Wildcard Alias functionality to create aliases for packages,
27  * namespaces or multiple similar named classes. One single '*'
28  * wildcard character is supported in the names.
29  * <br><br>See {@link Alias} for concrete examples.
30  */

31 public class WildcardAlias implements Alias {
32     
33     private final WildcardPattern _storedPattern;
34     
35     private final WildcardPattern _runtimePattern;
36
37     /**
38      * Create a WildcardAlias with two patterns, the
39      * stored pattern and the pattern that is to be used
40      * at runtime. One single '*' is allowed as a wildcard
41      * character.
42      */

43     public WildcardAlias(String JavaDoc storedPattern, String JavaDoc runtimePattern) {
44         
45         if (null == storedPattern) throw new ArgumentNullException("storedPattern");
46         if (null == runtimePattern) throw new ArgumentNullException("runtimePattern");
47         
48         _storedPattern = new WildcardPattern(storedPattern);
49         _runtimePattern = new WildcardPattern(runtimePattern);
50     }
51     
52     /**
53      * resolving is done through simple pattern matching
54      */

55     public String JavaDoc resolveRuntimeName(String JavaDoc runtimeTypeName) {
56         String JavaDoc match = _runtimePattern.matches(runtimeTypeName);
57         return match != null
58             ? _storedPattern.inject(match)
59             : null;
60     }
61
62     /**
63      * resolving is done through simple pattern matching
64      */

65     
66     public String JavaDoc resolveStoredName(String JavaDoc storedTypeName) {
67         String JavaDoc match = _storedPattern.matches(storedTypeName);
68         return match != null
69             ? _runtimePattern.inject(match)
70             : null;
71     }
72     
73     static class WildcardPattern {
74         private String JavaDoc _head;
75         private String JavaDoc _tail;
76
77         public WildcardPattern(String JavaDoc pattern) {
78             String JavaDoc[] parts = split(pattern);
79             
80             _head = parts[0];
81             _tail = parts[1];
82         }
83
84         public String JavaDoc inject(String JavaDoc s) {
85             return _head + s + _tail;
86         }
87
88         public String JavaDoc matches(String JavaDoc s) {
89             if (!s.startsWith(_head) || !s.endsWith(_tail)) return null;
90             return s.substring(_head.length(), s.length()-_tail.length());
91         }
92
93         private void invalidPattern() {
94             throw new IllegalArgumentException JavaDoc("only one '*' character");
95         }
96         
97         String JavaDoc[] split(String JavaDoc pattern) {
98             int index = pattern.indexOf('*');
99             if (-1 == index || index != pattern.lastIndexOf('*')) invalidPattern();
100             return new String JavaDoc[] {
101                     pattern.substring(0, index),
102                     pattern.substring(index+1)
103             };
104         }
105     }
106
107 }
108
Popular Tags