use of com.cosylab.logging.engine.Filter.Constraint in project ACS by ACS-Community.
the class FiltersVector method loadFilters.
/**
* Load filters
* In case of errors an exception is thrown
*
* @param f The xml file to parse (java.io.File)
* @param eraseFilters If true existing filters will be deleted before loading
* @param fileName Is the name of the file (it is usually null and the name is
* read from the parameter f. However when this method is called
* recursively we need to pass the name because the file we're reading
* is a temporary file (generated by converting the original file)
* @throws <code>Exception</code> In case of error loading the filters
*/
public void loadFilters(File f, boolean eraseFilters, String fileName) throws Exception {
Document doc;
FileInputStream fStream;
fStream = new FileInputStream(f);
JAXPDOMAdapter adapter = new JAXPDOMAdapter();
DOMBuilder builder = new DOMBuilder();
doc = builder.build(adapter.getDocument(fStream, false));
Element root = doc.getRootElement();
if (root.getName().compareToIgnoreCase("FILTER_LIST") != 0 && root.getName().compareToIgnoreCase("FILTERS") != 0) {
// We show a message to the user and abort
throw new Exception("Wrong xml file: the root is " + root.getName() + " instead of FILTER_LIST");
} else if (root.getName().compareToIgnoreCase("FILTER_LIST") == 0) {
// The file is an old version (show a message to the user to explain what's
// happening and how to avoid this message appears again in future
System.err.println("The format of this filter file is deprecated. \nSave the filters to avoid this message appears in future.");
// Convert the format of the file
File newFormatFile = convertOldFilterFile(f);
if (newFormatFile == null) {
// Something went wrong while converting: abort
throw new Exception("Error converting the file to the new format");
} else {
// Recursively call loadFilters but now the file has
// the new format
loadFilters(newFormatFile, eraseFilters, f.getAbsolutePath());
return;
}
}
Element filtersElement = root.getChild("FILTER_LIST");
// No filters defined
if (filtersElement == null)
return;
List children = filtersElement.getChildren("FILTER");
// Check if the vector has elements
if (children == null || children.size() == 0) {
return;
}
if (eraseFilters) {
// The user whish to substitute the existing filters
// with those he's loading
clear();
}
// Read all the filters from the file
Iterator it = children.iterator();
while (it.hasNext()) {
// Temporary Strings to store values read from the file
String lethalStr = null;
String notStr = null;
String minStr = null;
String maxStr = null;
String exactStr = null;
String wcharStr = null;
String fieldStr = null;
Boolean enabled = null;
Element element = (Element) it.next();
String type = element.getAttributeValue("type");
Constraint constraint = Constraint.fromName(type);
Element lethalElement = element.getChild("LETHAL");
if (lethalElement != null) {
lethalStr = lethalElement.getText();
}
Element notElement = element.getChild("APPLYNOT");
if (notElement != null) {
notStr = notElement.getText();
}
Element minElement = element.getChild("MIN");
String minType = null;
if (minElement != null) {
minStr = minElement.getText();
minType = minElement.getAttributeValue("class");
}
Element maxElement = element.getChild("MAX");
String maxType = null;
if (maxElement != null) {
maxStr = maxElement.getText();
maxType = maxElement.getAttributeValue("class");
}
Element exactElement = element.getChild("EXACT");
String exactType = null;
if (exactElement != null) {
exactStr = exactElement.getText();
exactType = exactElement.getAttributeValue("class");
}
Element wcharElement = element.getChild("WILDCHAR");
if (wcharElement != null) {
wcharStr = wcharElement.getText();
}
Element fieldElement = element.getChild("FIELD");
if (fieldElement != null) {
fieldStr = fieldElement.getText();
}
// Build the Field.
LogField field = LogField.fromName(fieldStr);
if (field == null) {
// Ooops the field has not been found
// Check if this String contains an Integer representing the
// position of this field in the enum LogField (it was in the old format)
Integer i = Integer.parseInt(fieldStr);
field = LogField.values()[i];
}
Element enabledElement = element.getChild("ENABLED");
if (enabledElement != null) {
enabled = new Boolean(enabledElement.getText());
} else {
// Tag not found: enable the filter per default
enabled = Boolean.TRUE;
}
// Build the filter
Filter filter = Filter.buildFilter(constraint, field, lethalStr, notStr, minStr, minType, maxStr, maxType, exactStr, exactType, wcharStr);
// bulidFilter throws an exception but ner return a null filter
if (filter == null) {
throw new IllegalStateException("The filter should not be null");
}
addFilter(filter, enabled);
}
}
Aggregations