KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > config > types > PathPatternType


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.config.types;
30
31 import com.caucho.config.ConfigException;
32 import com.caucho.log.Log;
33 import com.caucho.util.CharBuffer;
34 import com.caucho.util.L10N;
35
36 import javax.annotation.PostConstruct;
37 import java.util.logging.Logger JavaDoc;
38 import java.util.regex.Pattern JavaDoc;
39 import java.util.regex.PatternSyntaxException JavaDoc;
40
41 /**
42  * Abstract type building a path pattern. The pattern follows ant.
43  */

44 public class PathPatternType {
45   static final L10N L = new L10N(PathPatternType.class);
46   static final Logger JavaDoc log = Log.open(PathPatternType.class);
47
48   private Pattern JavaDoc _pattern;
49
50   public PathPatternType()
51   {
52   }
53
54   public PathPatternType(String JavaDoc pattern)
55     throws ConfigException, PatternSyntaxException JavaDoc
56   {
57     setName(pattern);
58   }
59
60   /**
61    * Sets the pattern name.
62    */

63   public void setName(String JavaDoc pattern)
64     throws ConfigException, PatternSyntaxException JavaDoc
65   {
66     CharBuffer cb = new CharBuffer();
67
68     cb.append("^");
69
70     int i = 0;
71     int length = pattern.length();
72
73     while (i < length && pattern.charAt(i) == '/')
74       i++;
75
76     for (; i < length; i++) {
77       char ch = pattern.charAt(i);
78
79       if (ch == '/')
80     cb.append('/');
81       else if (ch != '*')
82     cb.append(ch);
83       else if (length <= i + 1 || pattern.charAt(i + 1) != '*')
84     cb.append("[^/]*");
85       else if (i > 0 && pattern.charAt(i - 1) != '/')
86     throw new ConfigException(L.l("'{0}' is an invalid pattern at '**'",
87                       pattern));
88       else if (i + 2 < length && pattern.charAt(i + 2) == '/') {
89     cb.append("([^/]*/)*");
90     i += 2;
91       }
92       else if (i + 2 < length)
93     throw new ConfigException(L.l("'{0}' is an invalid pattern at '**'",
94                       pattern));
95       else {
96     cb.append(".*");
97     i++;
98       }
99     }
100
101     cb.append("$");
102
103     _pattern = Pattern.compile(cb.toString());
104   }
105
106   /**
107    * Sets the pattern name.
108    */

109   public void addText(String JavaDoc text)
110     throws ConfigException, PatternSyntaxException JavaDoc
111   {
112     text = text.trim();
113
114     if (! text.equals(""))
115       setName(text);
116   }
117
118   /**
119    * initialize the pattern.
120    */

121   @PostConstruct
122   public void init()
123     throws ConfigException
124   {
125     if (_pattern == null)
126       throw new ConfigException(L.l("pattern requires 'name' attribute."));
127   }
128
129   /**
130    * Check for a match.
131    */

132   public boolean isMatch(String JavaDoc path)
133   {
134     return _pattern.matcher(path).matches();
135   }
136
137   public String JavaDoc toString()
138   {
139     return "PathPatternType[" + _pattern.pattern() + "]";
140   }
141 }
142
Popular Tags