View Javadoc

1   package org.apache.commons.digester3;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static java.lang.String.format;
23  
24  import org.xml.sax.Attributes;
25  
26  /**
27   * @since 3.0
28   */
29  final class PatternRuleMatcher
30      implements RuleMatcher
31  {
32  
33      private final String pattern;
34  
35      private String namespaceURI;
36  
37      public PatternRuleMatcher( String pattern )
38      {
39          this( pattern, null );
40      }
41  
42      public PatternRuleMatcher( String pattern, /* @Nullable */String namespaceURI )
43      {
44          if ( pattern == null )
45          {
46              throw new IllegalArgumentException( "Input pattern must be not null" );
47          }
48  
49          this.pattern = pattern;
50          this.namespaceURI = namespaceURI;
51      }
52  
53      /**
54       * {@inheritDoc}
55       */
56      public boolean match( String namespace, String pattern, String name, Attributes attributes )
57      {
58          if ( namespaceURI != null && !namespace.equals( namespaceURI ) )
59          {
60              return false;
61          }
62          return this.pattern.equals( pattern );
63      }
64  
65      public String getPattern()
66      {
67          return pattern;
68      }
69  
70      public String getNamespaceURI()
71      {
72          return namespaceURI;
73      }
74  
75      /**
76       * {@inheritDoc}
77       */
78      @Override
79      public int hashCode()
80      {
81          final int prime = 31;
82          int result = 1;
83          result = prime * result + ( ( namespaceURI == null ) ? 0 : namespaceURI.hashCode() );
84          result = prime * result + pattern.hashCode();
85          return result;
86      }
87  
88      @Override
89      public boolean equals( Object obj )
90      {
91          if ( this == obj )
92          {
93              return true;
94          }
95  
96          if ( obj == null )
97          {
98              return false;
99          }
100 
101         if ( getClass() != obj.getClass() )
102         {
103             return false;
104         }
105 
106         PatternRuleMatcher other = (PatternRuleMatcher) obj;
107         if ( namespaceURI == null )
108         {
109             if ( other.getNamespaceURI() != null )
110             {
111                 return false;
112             }
113         }
114         else if ( !namespaceURI.equals( other.getNamespaceURI() ) )
115         {
116             return false;
117         }
118 
119         if ( !pattern.equals( other.getPattern() ) )
120         {
121             return false;
122         }
123 
124         return true;
125     }
126 
127     /**
128      * {@inheritDoc}
129      */
130     @Override
131     public String toString()
132     {
133         return format( "%s (%s)", pattern, namespaceURI );
134     }
135 
136 }