2362N/A * Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2362N/A * or visit www.oracle.com if you need additional information or have any 0N/A * A class for parsing LDAP search filters (defined in RFC 1960, 2254) 0N/A * @author Rosanna Lee 0N/A // %%% "filter" and "pos" are not declared "private" due to bug 4064984. 0N/A protected static final boolean debug =
false;
0N/A // Returns true if targetAttrs passes the filter 0N/A * Utility routines used by member classes 0N/A // does some pre-processing on the string to make it look exactly lik 0N/A // what the parser expects. This only needs to be called once. 0N/A // Sometimes, search filters don't have "(" and ")" - add them 0N/A // this would be a good place to strip whitespace if desired 0N/A // make sure every filter starts with "(" 0N/A // skip past the "(" 0N/A // use the next character to determine the type of filter 0N/A // make sure every filter ends with ")" 0N/A // skip past the ")" 0N/A throw e;
// just rethrow these 0N/A // catch all - any uncaught exception while parsing will end up here 0N/A "character " +
pos +
" in \""+
0N/A * A class for dealing with compound filters ("and" & "or" filters). 0N/A }
/* CompoundFilter */ 0N/A * A class for dealing with NOT filters 0N/A // note: declared here since member classes can't have static variables 0N/A * A class for dealing wtih atomic filters 0N/A //determine the match type 0N/A //update our position 0N/A // used for substring comparisons (where proto has "*" wildcards 0N/A // simple case 1: "*" means attribute presence is being tested 0N/A // simple case 2: if there are no wildcards, call String.equals() 0N/A // do the work: make sure all the substrings are present 0N/A // do we need to begin with the first token? 0N/A // do we need to end with the last token? 0N/A }
/* AtomicFilter */ 0N/A // ----- static methods for producing string filters given attribute set 0N/A // ----- or object array 0N/A * Creates an LDAP filter as a conjuction of the attributes supplied. 0N/A return "objectClass=*";
0N/A // only checking presence of attribute 0N/A //System.out.println("filter: " + answer); 0N/A // Writes the hex representation of a byte to a StringBuffer. 0N/A c = (
char) ((x >>
4) &
0xf);
0N/A c = (
char) ((c-
10) +
'A');
0N/A c = (
char)(c +
'0');
0N/A c = (
char) (x &
0xf);
0N/A c = (
char)((c-
10) +
'A');
0N/A c = (
char)(c +
'0');
0N/A * Returns the string representation of an object (such as an attr value). 0N/A * If obj is a byte array, encode each item as \xx, where xx is hex encoding 0N/A * of the byte value. 0N/A * Else, if obj is not a String, use its string representation (toString()). 0N/A * Special characters in obj (or its string representation) are then 0N/A * encoded appropriately according to RFC 2254. 0N/A // binary data must be encoded as \hh where hh is a hex char 0N/A for (
int i =
0; i <
len; i++) {
0N/A * Finds the first occurrence of <tt>ch</tt> in <tt>val</tt> starting 0N/A * from position <tt>start</tt>. It doesn't count if <tt>ch</tt> 0N/A * has been escaped by a backslash (\) 0N/A // if at start of string, or not there at all, or if not escaped 0N/A // start search after escaped star 0N/A * Formats the expression <tt>expr</tt> using arguments from the array 0N/A * <code>{i}</code> specifies the <code>i</code>'th element from 0N/A * the array <code>args</code> is to be substituted for the 0N/A * string "<code>{i}</code>". 0N/A * To escape '{' or '}' (or any other character), use '\'. 0N/A * Uses getEncodedStringRep() to do encoding. 0N/A // at this point, pend should be pointing at '}' 0N/A "integer expected inside {}: " +
expr);
0N/A "number exceeds argument list: " +
param);
0N/A * returns an Attributes instance containing only attributeIDs given in 0N/A * "attributeIDs" whose values come from the given DSContext. 0N/A/* For testing filter 0N/A public static void main(String[] args) { 0N/A Attributes attrs = new BasicAttributes(LdapClient.caseIgnore); 0N/A attrs.put("cn", "Rosanna Lee"); 0N/A attrs.put("sn", "Lee"); 0N/A attrs.put("fn", "Rosanna"); 0N/A attrs.put("id", "10414"); 0N/A attrs.put("machine", "jurassic"); 0N/A System.out.println(format(attrs)); 0N/A String expr = "(&(Age = {0})(Account Balance <= {1}))"; 0N/A Object[] fargs = new Object[2]; 0N/A // fill in the parameters 0N/A fargs[0] = new Integer(65); 0N/A fargs[1] = new Float(5000); 0N/A System.out.println(format(expr, fargs)); 0N/A System.out.println(format("bin={0}", 0N/A new Object[] {new byte[] {0, 1, 2, 3, 4, 5}})); 0N/A System.out.println(format("bin=\\{anything}", null)); 0N/A } catch (NamingException e) { 0N/A e.printStackTrace();