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.codec.search; 21 22 23 import java.nio.ByteBuffer; 24 import java.util.ArrayList; 25 import java.util.List; 26 27 import org.apache.directory.api.asn1.DecoderException; 28 import org.apache.directory.api.asn1.EncoderException; 29 import org.apache.directory.api.i18n.I18n; 30 31 32 /** 33 * This Filter abstract class is used to store a set of filters used by 34 * OR/AND/NOT filters. 35 * 36 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 37 */ 38 public abstract class ConnectorFilter extends Filter 39 { 40 // ~ Instance fields 41 // ---------------------------------------------------------------------------- 42 43 /** The set of filters used by And/Or filters */ 44 protected List<Filter> filterSet; 45 46 /** The filters length */ 47 protected int filtersLength; 48 49 50 // ~ Constructors 51 // ------------------------------------------------------------------------------- 52 53 /** 54 * The constructor. We wont initialize the ArrayList as it may not be used. 55 */ 56 public ConnectorFilter( int tlvId ) 57 { 58 super( tlvId ); 59 } 60 61 62 /** 63 * The constructor. We wont initialize the ArrayList as it may not be used. 64 */ 65 public ConnectorFilter() 66 { 67 super(); 68 } 69 70 71 // ~ Methods 72 // ------------------------------------------------------------------------------------ 73 74 /** 75 * Add a new Filter to the list. 76 * 77 * @param filter The filter to add 78 */ 79 public void addFilter( Filter filter ) throws DecoderException 80 { 81 82 if ( filterSet == null ) 83 { 84 filterSet = new ArrayList<Filter>(); 85 } 86 87 filterSet.add( filter ); 88 } 89 90 91 /** 92 * Get the list of filters stored in the composite filter 93 * 94 * @return And array of filters 95 */ 96 public List<Filter> getFilterSet() 97 { 98 return filterSet; 99 } 100 101 102 /** 103 * Compute the ConnectorFilter length Length(ConnectorFilter) = 104 * sum(filterSet.computeLength()) 105 */ 106 public int computeLength() 107 { 108 int connectorFilterLength = 0; 109 110 if ( ( filterSet != null ) && ( filterSet.size() != 0 ) ) 111 { 112 for ( Filter filter : filterSet ) 113 { 114 connectorFilterLength += filter.computeLength(); 115 } 116 } 117 118 return connectorFilterLength; 119 } 120 121 122 /** 123 * Encode the ConnectorFilter message to a PDU. 124 * 125 * ConnectorFilter : 126 * filter.encode() ... filter.encode() 127 * 128 * @param buffer The buffer where to put the PDU 129 * @return The PDU. 130 */ 131 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException 132 { 133 if ( buffer == null ) 134 { 135 throw new EncoderException( I18n.err( I18n.ERR_04023 ) ); 136 } 137 138 // encode each filter 139 if ( ( filterSet != null ) && ( filterSet.size() != 0 ) ) 140 { 141 for ( Filter filter : filterSet ) 142 { 143 filter.encode( buffer ); 144 } 145 } 146 147 return buffer; 148 } 149 150 151 /** 152 * Return a string compliant with RFC 2254 representing a composite filter, 153 * one of AND, OR and NOT 154 * 155 * @return The composite filter string 156 */ 157 public String toString() 158 { 159 StringBuffer sb = new StringBuffer(); 160 161 if ( ( filterSet != null ) && ( filterSet.size() != 0 ) ) 162 { 163 for ( Filter filter : filterSet ) 164 { 165 sb.append( '(' ).append( filter ).append( ')' ); 166 } 167 } 168 169 return sb.toString(); 170 } 171 }