View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.model.filter;
21  
22  
23  import java.text.ParseException;
24  import java.util.Comparator;
25  import java.util.List;
26  import java.util.Set;
27  import java.util.TreeSet;
28  
29  import org.apache.directory.api.ldap.model.schema.SchemaManager;
30  
31  
32  /**
33   * Visitor which traverses a filter tree while normalizing the branch node
34   * order. Filter expressions can change the order of expressions in branch nodes
35   * without effecting the logical meaning of the expression. This visitor orders
36   * the children of expression tree branch nodes consistantly. It is really
37   * useful for comparing expression trees which may be altered for performance or
38   * altered because of codec idiosyncracies: for example the SNACC4J codec uses a
39   * hashmap to store expressions in a sequence which rearranges the order of
40   * children based on object hashcodes. We need this visitor to remove such
41   * inconsitancies in order hence normalizing the branch node's child order.
42   * 
43   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
44   */
45  public class BranchNormalizedVisitor implements FilterVisitor
46  {
47      /**
48       * {@inheritDoc}
49       */
50      @Override
51      public Object visit( ExprNode node )
52      {
53          if ( !( node instanceof BranchNode ) )
54          {
55              return null;
56          }
57  
58          BranchNode branch = ( BranchNode ) node;
59  
60          Comparator<ExprNode> nodeComparator = new NodeComparator();
61  
62          Set<ExprNode> set = new TreeSet<>( nodeComparator );
63  
64          List<ExprNode> children = branch.getChildren();
65  
66          for ( ExprNode child : branch.getChildren() )
67          {
68              if ( !child.isLeaf() )
69              {
70                  ExprNode newChild = ( ExprNode ) visit( child );
71  
72                  if ( newChild != null )
73                  {
74                      set.add( newChild );
75                  }
76              }
77              else
78              {
79                  set.add( child );
80              }
81          }
82  
83          children.clear();
84  
85          children.addAll( set );
86  
87          return branch;
88      }
89  
90  
91      /**
92       * {@inheritDoc}
93       */
94      @Override
95      public boolean canVisit( ExprNode node )
96      {
97          return node instanceof BranchNode;
98      }
99  
100 
101     /**
102      * {@inheritDoc}
103      */
104     @Override
105     public boolean isPrefix()
106     {
107         return false;
108     }
109 
110 
111     /**
112      * {@inheritDoc}
113      */
114     @Override
115     public List<ExprNode> getOrder( BranchNode node, List<ExprNode> children )
116     {
117         return children;
118     }
119 
120 
121     /**
122      * Normalizes a filter expression to a canonical representation while
123      * retaining logical meaning of the expression.
124      * 
125      * @param schemaManager The SchemaManager
126      * @param filter the filter to normalize
127      * @return the normalized version of the filter
128      * @throws java.text.ParseException if the filter is malformed
129      */
130     public static String getNormalizedFilter( SchemaManager schemaManager, String filter ) throws ParseException
131     {
132         ExprNode originalNode = FilterParser.parse( schemaManager, filter );
133 
134         return getNormalizedFilter( originalNode );
135     }
136 
137 
138     /**
139      * Normalizes a filter expression to a canonical representation while
140      * retaining logical meaning of the expression.
141      * 
142      * @param filter the filter to normalize
143      * @return the normalized String version of the filter
144      */
145     public static String getNormalizedFilter( ExprNode filter )
146     {
147         BranchNormalizedVisitor visitor = new BranchNormalizedVisitor();
148 
149         ExprNode result = ( ExprNode ) visitor.visit( filter );
150 
151         return result.toString().trim();
152     }
153 
154     static class NodeComparator implements Comparator<ExprNode>
155     {
156         @Override
157         public int compare( ExprNode o1, ExprNode o2 )
158         {
159             StringBuilder buf = new StringBuilder();
160 
161             buf.setLength( 0 );
162 
163             String s1;
164 
165             buf.append( o1.toString() );
166 
167             s1 = buf.toString();
168 
169             buf.setLength( 0 );
170 
171             String s2;
172 
173             buf.append( o2.toString() );
174 
175             s2 = buf.toString();
176 
177             return s1.compareTo( s2 );
178         }
179     }
180 }