use of org.jdom.input.DOMBuilder 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);
}
}
use of org.jdom.input.DOMBuilder in project intellij-community by JetBrains.
the class EditorColorSchemeTestCase method loadScheme.
protected static EditorColorsScheme loadScheme(@NotNull String docText) throws ParserConfigurationException, IOException, SAXException {
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource inputSource = new InputSource(new StringReader(docText));
Document doc = docBuilder.parse(inputSource);
Element root = new DOMBuilder().build(doc.getDocumentElement());
EditorColorsScheme defaultScheme = EditorColorsManager.getInstance().getScheme(EditorColorsScheme.DEFAULT_SCHEME_NAME);
EditorColorsScheme targetScheme = new EditorColorsSchemeImpl(defaultScheme);
targetScheme.readExternal(root);
return targetScheme;
}
use of org.jdom.input.DOMBuilder in project intellij-plugins by StepicOrg.
the class StepikProjectManager method toElement.
@NotNull
private static Element toElement(ByteArrayOutputStream out) throws ParserConfigurationException, SAXException, IOException {
if (factory == null) {
factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
builder = factory.newDocumentBuilder();
domBuilder = new DOMBuilder();
}
try (ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray())) {
Document doc = builder.parse(in);
org.jdom.Document document = domBuilder.build(doc);
Element root = document.getRootElement();
document.removeContent(root);
Element element = new Element("element");
element.addContent(root);
return element;
}
}
use of org.jdom.input.DOMBuilder in project intellij-plugins by StepicOrg.
the class TestUtils method readXmlFile.
@NotNull
public static Element readXmlFile(Class<?> clazz, @NotNull String filename) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(clazz.getResourceAsStream(filename));
DOMBuilder domBuilder = new DOMBuilder();
Element root = domBuilder.build(doc).getRootElement();
assertNotNull("Failed read test xml data file", root);
return root;
}
use of org.jdom.input.DOMBuilder in project tdi-studio-se by Talend.
the class SOAPUtil method Doc2StringWithoutDeclare.
private String Doc2StringWithoutDeclare(Document doc) {
DOMBuilder builder = new DOMBuilder();
org.jdom.Document jdomDoc = builder.build(doc);
XMLOutputter outputter = new XMLOutputter();
return outputter.outputString(jdomDoc.getRootElement());
}
Aggregations