// ldapsearch.java
import java.io.*;
import java.util.*;
import javax.naming.*;
import javax.naming.directory.*;
/**
ldapsearch.java NOTES
Test LDAP directory of genome data at http://euGenes.org/
This script fetches and prints sequence and associated gene
features from an ldap directory of genome annotations at euGenes.org
The species, chromosome, feature kind and base range can be specified.
Search of high level directories provide information on contents
of the specific attributes available in species-chromosome directories.
Compile with 'javac ldapsearch.java'
Run with 'java ldapsearch'
* @author Don Gilbert
* @version 1.0 05/01/2002
*/
public class ldapsearch
{
public static void main(String[] args)
{
int ns= 0;
ldapsearch ldap= new ldapsearch();
for (int i=0; i50000)(stop=<80000))"
+"?sizelimit=10,deref=always" // other ldap options
;
this.search( url );
System.exit(0);
}
public int search(String url)
{
out.println("Search URL: " +url); out.println();
if (helping) return 0;
int counter= 0;
try {
// ldap://host:port/dn?attributes?scope?filter?extensions
com.sun.jndi.ldap.LdapURL lu= new com.sun.jndi.ldap.LdapURL(url);
String aurl= lu.toString();
int q= aurl.indexOf('?'); if (q>0) aurl= aurl.substring(0,q);
Properties env= new Properties();
env.put( Context.PROVIDER_URL, aurl);
env.put( Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
String dn= lu.getDN();
String base= "";
String scope= lu.getScope();
String filter= lu.getFilter();
String[] attr= splitString( lu.getAttributes(),",;");
SearchControls scontrols = new SearchControls();
int iscope= SearchControls.OBJECT_SCOPE; // should this be default?
if ("sub".equalsIgnoreCase(scope)) iscope= SearchControls.SUBTREE_SCOPE;
else if ("base".equalsIgnoreCase(scope)) iscope= SearchControls.OBJECT_SCOPE;
else if ("one".equalsIgnoreCase(scope)) iscope= SearchControls.ONELEVEL_SCOPE;
scontrols.setSearchScope(iscope);
if (attr.length>0) scontrols.setReturningAttributes(attr);
if (filter==null) filter="(objectClass=*)";
String[] extn= splitString( lu.getExtensions(),"=,;");
for (int i=0; i0) out.print(",");
out.print(base);
}
out.println();
Attributes attrs= si.getAttributes();
if (attrs.size()>0) out.println( attrs); // otherwise get "No attributes"
out.println();
}
String[] splitString(String s, String del) {
if (s==null) return new String[0];
StringTokenizer st= new StringTokenizer(s, del);
int n= st.countTokens(); String[] ss= new String[n];
for (int i=0; i