KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > service > namespace > RegexQNamePattern


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.service.namespace;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22
23 /**
24  * Provides matching between {@link org.alfresco.service.namespace.QName qnames} using
25  * regular expression matching.
26  * <p>
27  * A simple {@link #MATCH_ALL convenience} pattern matcher is also provided that
28  * will match any qname.
29  *
30  * @see java.lang.String#matches(java.lang.String)
31  *
32  * @author Derek Hulley
33  */

34 public class RegexQNamePattern implements QNamePattern
35 {
36     private static final Log logger = LogFactory.getLog(RegexQNamePattern.class);
37     
38     /** A helper pattern matcher that will match <i>all</i> qnames */
39     public static final QNamePattern MATCH_ALL = new QNamePattern()
40         {
41             public boolean isMatch(QName qname)
42             {
43                 return true;
44             }
45         };
46     
47     private String JavaDoc namespaceUriPattern;
48     private String JavaDoc localNamePattern;
49     private String JavaDoc combinedPattern;
50     
51     /**
52      * @param namespaceUriPattern a regex pattern that will be applied to the namespace URI
53      * @param localNamePattern a regex pattern that will be applied to the local name
54      */

55     public RegexQNamePattern(String JavaDoc namespaceUriPattern, String JavaDoc localNamePattern)
56     {
57         this.namespaceUriPattern = namespaceUriPattern;
58         this.localNamePattern = localNamePattern;
59         this.combinedPattern = null;
60     }
61     
62     /**
63      * @param combinedPattern a regex pattern that will be applied to the full qname
64      * string representation
65      *
66      * @see QName#toString()
67      */

68     public RegexQNamePattern(String JavaDoc combinedPattern)
69     {
70         this.combinedPattern = combinedPattern;
71         this.namespaceUriPattern = null;
72         this.localNamePattern = null;
73     }
74     
75     public String JavaDoc toString()
76     {
77         StringBuilder JavaDoc sb = new StringBuilder JavaDoc(56);
78         sb.append("RegexQNamePattern[");
79         if (combinedPattern != null)
80         {
81             sb.append(" pattern=").append(combinedPattern);
82         }
83         else
84         {
85             sb.append(" uri=").append(namespaceUriPattern);
86             sb.append(", localname=").append(namespaceUriPattern);
87         }
88         sb.append(" ]");
89         return sb.toString();
90     }
91
92     /**
93      * @param qname the value to check against this pattern
94      * @return Returns true if the regex pattern provided match thos of the provided qname
95      */

96     public boolean isMatch(QName qname)
97     {
98         boolean match = false;
99         if (combinedPattern != null)
100         {
101             String JavaDoc qnameStr = qname.toString();
102             match = qnameStr.matches(combinedPattern);
103         }
104         else
105         {
106             match = (qname.getNamespaceURI().matches(namespaceUriPattern) &&
107                      qname.getLocalName().matches(localNamePattern));
108         }
109         // done
110
if (logger.isDebugEnabled())
111         {
112             logger.debug("QName matching: \n" +
113                     " matcher: " + this + "\n" +
114                     " qname: " + qname + "\n" +
115                     " result: " + match);
116         }
117         return match;
118     }
119 }
120
Popular Tags