/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * The contents of this file are subject to the Netscape Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/NPL/ * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * The Original Code is mozilla.org code. * * The Initial Developer of the Original Code is Netscape * Communications Corporation. Portions created by Netscape are * Copyright (C) 1999 Netscape Communications Corporation. All * Rights Reserved. * * Contributor(s): */ package netscape.ldap.util; import java.io.*; import java.util.*; import java.net.*; import com.oroinc.text.regex.*; import netscape.ldap.*; /** * Represents an LDAP filter configuration file read into memory. *
*
* Once you read in a filter file to create an object of this class,
* you can access the filter information through the methods that create
* LDAPFilterList
and LDAPFilter
objects.
* (You do not need to manually construct these objects yourself.)
*
* * This class (along with the other LDAP filter classes) provide * functionality equivalent to the LDAP filter functions in the LDAP C API. *
* * The format of the file/URL/buffer must be that as defined in the * ldapfilter.conf(5) man page from the University of Michigan LDAP-3.3 * distribution.
* * The LDAP filter classes provide a powerful way to configure LDAP clients * by modifying a configuration file.
* * The following is a short example for how to use the * LDAP filter classes. * *
* * // ... Setup LDAPConnection up here ... ** * * @see LDAPFilterList * @see LDAPFilter * @version 1.0 */ public class LDAPFilterDescriptor { private Vector m_vFilterSet = new Vector(); private String m_strLine; private int m_nLine; private String m_strPrefix; private String m_strAffix; private LDAPIntFilterSet m_tmpFilterSet = null; private String m_strLastMatchPattern = null; private String m_strLastDelimiter = null; /** * The Default scope is used when a scope is not defined * in the filter file. The scope is the only "optional" parameter * in the file. */ private static final int DEFAULT_SCOPE = LDAPConnection.SCOPE_SUB; /** * Creates an LDAPFilterDescriptor object from an existing filter * configuration file. This file has the format as defined in the * ldapfilter.conf(5) man page. * * @exception netscape.ldap.util.BadFilterException * One of the filters was not generated properly. Most likely * this is due to an improperly formatted ldapfilter.conf file. */ public LDAPFilterDescriptor ( String strFile ) throws FileNotFoundException, BadFilterException { DataInputStream inputStream = new DataInputStream ( new FileInputStream ( strFile ) ); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); init( reader ); } /** * Creates an LDAPFilterDescriptor object from an existing * StringBuffer. This file has the format as defined in the * ldapfilter.conf(5) man page. * * @exception netscape.ldap.util.BadFilterException * One of the filters was not generated properly. Most likely * this is due to an improperly formatted ldapfilter.conf file. */ public LDAPFilterDescriptor ( StringBuffer strBuffer ) throws BadFilterException { init( strBuffer ); } /** * Creates an LDAPFilterDescriptor object from a URL. * This file has the format as defined in the * ldapfilter.conf(5) man page. * * @exception netscape.ldap.util.BadFilterException * One of the filters was not generated properly. Most likely * this is due to an improperly formatted ldapfilter.conf file. */ public LDAPFilterDescriptor ( URL url ) throws IOException, BadFilterException { URLConnection urlc = url.openConnection(); DataInputStream inputStream = new DataInputStream ( urlc.getInputStream() ); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); init( reader ); } /** * This function initializes the LDAPFilterDescriptor. It's * called internally, and should never be called directly by the * developer. */ private void init ( Object inputObj) throws BadFilterException { String strCommentPattern = "(?:^\\s*#|^\\s*$)"; String strDataPattern = "(?:\\s*\"([^\"]*)\"|([^\\s]*))\\s*"; Perl5Compiler compiler = new Perl5Compiler(); Perl5Pattern patComment; Perl5Pattern patData; Vector vStrings = new Vector ( 5 ); try { patComment = (Perl5Pattern)compiler.compile ( strCommentPattern ); patData = (Perl5Pattern)compiler.compile ( strDataPattern ); } catch ( MalformedPatternException e ) { // This should NEVER happen... System.out.println ( "FATAL Error, couldn't compile pattern"); System.out.println ( " " + e.getMessage() ); return; } // Setup some temporary variables. m_nLine = 0; try { if (inputObj instanceof StringBuffer) { StringBuffer ibuffer = (StringBuffer)inputObj; StringBuffer buffer = new StringBuffer(); for (int i=0; i* * LDAPFilterDescriptor filterDescriptor; *
* * // Create the LDAPFilterDescriptor given the file * // "ldapfilter.conf". * try { * filterDescriptor = new LDAPFilterDescriptor ( "ldapfilter.conf" ); *
* * // Now retrieve the Filters in the form of an * // LDAPFilterList * LDAPFilterList filterList = new filterDescriptor.getFilters("match_tag", "string_user_typed"); *
* * // For each filter, do the search. Normally, you wouldn't * // do the search if the first filter matched, but this is * // just showing the enumeration type aspects of * // LDAPFilterList * LDAPFilter filter; * while ( filterList.hasMoreElements() ) { * filter = filterList.next(); * LDAPResults results = LDAPConnection.search ( * strBase, // base DN * filter.getScope(), // scope * filter.getFilter(), // completed filter * null, // all attribs * false ); // attrsOnly? * } *
* * // ...more processing here... * } catch ( BadFilterException e ) { * System.out.println ( e.toString() ); * System.exit ( 0 ); * } catch ( IOException e ) { * // ...handle exception here... * } *
*/ public void setFilterAffixes ( String strPrefix, String strAffix ) { m_strPrefix = strPrefix; m_strAffix = strAffix; } }