📄 searchmodel.java
字号:
package com.ca.directory.jxplorer.search;
import java.io.*;
import java.util.*;
import java.util.logging.Logger;
import com.ca.commons.cbutil.*;
import com.ca.directory.jxplorer.*;
/**
* This class saves filters to and reads filters from the property file "search_filters.txt".
* A user specified title is appended to "JXFilter." to form the name the filter. For example
* "JXFilter.myFilter". Filters can be stored in this file as raw filters, for example
* "JXFilter.myFilter=(sn=f*)", or filters can be stored as groups of filters, for example
* "JXFilter.myNewFilter=!&JXFilter.myFilterJXFilter.myOtherFilter". In the last example you
* may notice the "!&" symbols at the beinging of the value. These tell us what joins the filters
* and if the filter has a 'not'. Possible combinations are: '!&', '!|', '&', '|' or nothing if there
* is only one filter.
* <p>
* This class also allows the saving of text filters (ones that the user types or pastes into the text
* filter tab). These differ from the prior two in that they may not follow the same syntax rules and
* therefore may not be able to be loaded into the build and/or join tabs. These types of filters are
* saved with the 'JXTextFilter' prefix.
*/
public class SearchModel
{
public static ReturnAttributesDisplay rat =null;
protected Properties properties;
protected String localDir; //TE: the local directory where the property file is located.
protected ArrayList dependantFilters = new ArrayList();
protected ArrayList tempList = new ArrayList();
protected String searchFilterConfig; //CB: the name of the property file to save to.
static final String SEARCH_FILTER_FILENAME = "search_filters.txt"; //TE: the property file.
static final String NAME = "JXFilter."; //TE: the filter name prefix e.g. 'JXFilter.name'.\
static final String TEXTNAME = "JXTextFilter."; //TE: the filter name prefix e.g. 'JXTextFilter.name'.
/**
* A flag used to indicate build filters for example '(cn=f*)'.
*/
public static final int BUILDFILTER = 1;
/**
* A flag used to indicate all filters, not including 'JX' prefix.
*/
public static final int ALLFILTERS = 2;
/**
* A flag used to indicate join filters for example
* 'JXFilter.myFilter=!|JXFilter.aFilter1JXFilter.aFilter2'.
*/
public static final int JOINFILTER = 3;
/**
* A flag used to indicate text filters for example
* 'JXTextFilter.myFilter=(cn=f*)'.
*/
public static final int TEXTFILTER = 4;
/**
* A flag used to indicate all filters, including 'JX' prefix.
*/
public static final int FULLNAMES = 5;
/**
* A flag used to indicate build and join filters only.
*/
public static final int BUILDJOIN = 6;
/**
* Name used to save the base DN in the property file.
*/
public static final String BASEDN = "baseDN";
/**
* Name used to save the return attribute list name in the property file.
*/
public static final String RETATTRS = "retAttrs";
/**
* Name used to save the search level in the property file.
*/
public static final String SEARCHLEVEL = "searchLevel";
/**
* Name used to save the state of the find alias check box in the property file.
*/
public static final String FIND = "find";
/**
* Name used to save the state of the find alias check box in the property file.
*/
public static final String SEARCH = "search";
private static Logger log = Logger.getLogger(SearchModel.class.getName());
/**
* Constructor that sets up the property file.
*/
public SearchModel()
{
properties = new Properties();
searchFilterConfig = CBUtility.getPropertyConfigPath(SEARCH_FILTER_FILENAME);
if (new File(searchFilterConfig).exists()==false) { log.info("no search filter config file found at: " + searchFilterConfig); return;}
properties = CBUtility.readPropertyFile(searchFilterConfig);
if (properties.size()==0) { log.info("Initialising config file: " + searchFilterConfig); return;}
}
/**
* Opens the Return Attributes Dialog.
* @param jx JXplorer (main frame).
* @param attrNames the names of the return attributes.
* @param ds the DataSource being used.
*/
public void openRetAttrDisplay(JXplorer jx, String[] attrNames, DataSource ds)
{
rat = new ReturnAttributesDisplay(jx, attrNames);
rat.registerDataSource(ds);
}
/**
* Returns all of the filters in the property file "search_filters.txt".
* @return all of the filters in the property file "search_filters.txt".
*/
protected Enumeration getAllFilters()
{
properties = CBUtility.readPropertyFile(searchFilterConfig);
return properties.propertyNames();
}
/**
* Returns an array list of the names from the property file (search_filters.txt) depending on the integer that is
* supplied as a parameter.
* @param type one of JOINFILTER, ALLFILTERS, BUILDFILTER, TEXTFILTER, FULLNAMES.
* @return the list of filter names.
*/
public ArrayList getFilterNames(int type)
{
Enumeration list = getAllFilters();
ArrayList loadList = new ArrayList();
while (list.hasMoreElements())
{
String temp = list.nextElement().toString();
switch (type)
{
case BUILDFILTER : { if (properties.get(temp).toString().startsWith("(") && temp.startsWith(NAME)) loadList.add(temp.substring(temp.indexOf(NAME)+9)); break; } //TE: get raw filter names.
case ALLFILTERS : { if (temp.startsWith(NAME)) loadList.add(temp.substring(temp.indexOf(NAME)+9)); else if (temp.startsWith(TEXTNAME)) loadList.add(temp.substring(temp.indexOf(NAME)+14)); break; } //TE: get all filter names, not including prefix.
case JOINFILTER : { if (!properties.get(temp).toString().startsWith("(") && temp.startsWith(NAME)) loadList.add(temp.substring(temp.indexOf(NAME)+9)); break; } //TE: get joined filter names.
case TEXTFILTER : { if (temp.startsWith(TEXTNAME)) loadList.add(temp.substring(temp.indexOf(NAME)+14)); break; }
case FULLNAMES : { if(temp.startsWith(NAME) || temp.startsWith(TEXTNAME)) loadList.add(temp); break; } //TE: get all filter names, including prefix.
case BUILDJOIN : { if (temp.startsWith(NAME)) loadList.add(temp.substring(temp.indexOf(NAME)+9)); break; } //TE: get all build and join filters.
default: loadList.add("");
}
}
return loadList;
}
/**
* Returns the value from the property file of a given key. If the filter name prefix 'JXFilter.' isn't supplied with
* the name of the filter, it is inserted.
* @param name the key of the value that is being returned (can be either 'JXFilter.myFilter' or 'myFilter').
* @return the value of the key i.e. the filter.
*/
public String getFilter(String name)
{
if(name.startsWith("JXFilter."))
return properties.getProperty(name); //TE: filter name prefix has been supplied.
else
return properties.getProperty(NAME+name); //TE: inserts the filter name prefix 'JXFilter'.
}
/**
* Returns the value from the property file of a given text filter.
* @param name the key of the value that is being returned (e.g. 'myFilter').
* @return the value of the key i.e. the filter.
*/
public String getTextFilter(String name)
{
return properties.getProperty(TEXTNAME+name); //TE: inserts the filter name prefix 'JXTextFilter'.
}
/**
* Returns the LDAP filter for a given filter name. It expects the filter name prefix 'JXFilter.' or
* 'JXTextFilter' is included in the name of the filter.
* @param name the key of the value that is being returned (can be either 'JXFilter.myFilter' or 'JXTextFilter').
* @return the LDAP filter.
*/
public String getLDAPFilter(String name)
{
// if(name.startsWith("!"))
// name = name.substring(1);
// if(name.startsWith("&"))
// name = name.substring(1);
// else if(name.startsWith("|"))
// name = name.substring(1); //TE: ???? why did I need this???
ArrayList list = getFilterNames(BUILDFILTER);
if (list.contains(name.substring(9)))
return properties.getProperty(name); //TE: is a raw filter.
list.clear();
list = getFilterNames(JOINFILTER);
if (list.contains(name.substring(9)))
return getJoinFilter(properties.getProperty(name)); //TE: is a join filter.
list.clear();
list = getFilterNames(TEXTFILTER);
return properties.getProperty(name); //TE: is a text filter.
}
/**
* Counts the number of times a substring occurs within a string.
* @param string the string that is getting checked for the occurence of a substring.
* @param substring the substring that is being checked for.
* @return the number of times the substring occurs within the string.
*/
protected int getOccurences(String string, String substring)
{
int pos = -1;
int count = 0;
while ((pos = string.indexOf(substring, pos+1))!=-1)
count++;
return count;
}
/**
* Returns an array list of the names of the filters that make up the supplied filter
* for example, 'aFilter1' & 'aFilter2' from '!|JXFilter.aFilter1JXFilter.aFilter2'.
* @param filter the filter value that we want to extract the subfilter names from for example,
* '!|JXFilter.aFilter1JXFilter.aFilter2'.
* @return the list of the subfilter names.
*/
protected ArrayList getJoinFilterNames(String filter)
{
ArrayList list = new ArrayList();
String names;
int num = getOccurences(filter, "JXFilter");
for(int i=0; i<num; i++)
{
try
{
names = filter.substring(filter.indexOf(NAME)+9); //TE: make a substring of the filter from after the first '.' to the end of the filter for example 'aFilter1JXFilter.aFilter2'.
names = names.substring(0, names.indexOf(NAME)); //TE: make a substring of the name up to the first occurance of 'JXFilter.' for example 'aFilter1'.
}
catch(Exception e)
{
names = filter.substring(filter.indexOf(NAME)+9); //TE: XXXXXX arr... there must have been a reason for this??
}
filter = filter.substring(filter.indexOf(names)+ names.length()); //TE: make a substring of the filter from the end of the last name.
list.add(names);
}
return list;
}
/**
* Returns the raw filter of a filter that is created using the Join method.
* For example (!(&(cn=f*)(sn=f*)(|(cn=f*)(sn=f*)))).
* @param filter the filter value for example: '&JXFilter.myFilter1JXFilter.myFilter2'.
* @return the raw filter as a string for example: (!(&(cn=f*)(sn=f*)(|(cn=f*)(sn=f*)))).
*/
protected String getJoinFilter(String filter)
{
StringBuffer buffy = new StringBuffer();
getOperator(buffy, filter);
return buffy.toString();
}
/**
* Takes a filter value for example '&JXFilter.myFilter1JXFilter.myFilter2' and appends the operator
* to the string buffer. In this example '&'. Other possible values are '!&', '!|'. '|' or nothing.
* Removes these values from the filter string then appends the value or raw filter for example '(cn=f*)'
* of each subfilter to the buffer.
* <br>The only filters that should use this method are filters that are NOT raw i.e filters that are a
* conbination of filters.<br>
* @param buffy the string buffer which is used to append the filter parts.
* @param filter e.g. '&JXFilter.myFilter1JXFilter.myFilter2'.
*/
public void getOperator(StringBuffer buffy, String filter)
{
if (filter==null) { log.warning("Unexpected error in processing a search filter: no filter supplied."); return;}
int count = 0; //TE: keeps count of the number of ')' to be appended at the end.
if (filter.startsWith("!"))
{
buffy.append("(!"); //TE: append '(!'.
count++;
filter = filter.substring(1); //TE: remove ! from beginning of filter.
}
if (filter.startsWith("&"))
{
buffy.append("(&"); //TE: append '(&'.
count++;
filter = filter.substring(1); //TE: remove & from beginning of filter.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -