Search in sources :

Example 1 with DOMBuilder

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);
    }
}
Also used : Constraint(com.cosylab.logging.engine.Filter.Constraint) Element(org.jdom.Element) Document(org.jdom.Document) LogField(com.cosylab.logging.engine.log.LogField) FileInputStream(java.io.FileInputStream) DOMBuilder(org.jdom.input.DOMBuilder) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) JAXPDOMAdapter(org.jdom.adapters.JAXPDOMAdapter) Iterator(java.util.Iterator) List(java.util.List) File(java.io.File)

Example 2 with DOMBuilder

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;
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilder(javax.xml.parsers.DocumentBuilder) EditorColorsSchemeImpl(com.intellij.openapi.editor.colors.impl.EditorColorsSchemeImpl) Element(org.jdom.Element) StringReader(java.io.StringReader) EditorColorsScheme(com.intellij.openapi.editor.colors.EditorColorsScheme) Document(org.w3c.dom.Document) DOMBuilder(org.jdom.input.DOMBuilder)

Example 3 with DOMBuilder

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;
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Element(org.jdom.Element) Document(org.w3c.dom.Document) DOMBuilder(org.jdom.input.DOMBuilder) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with DOMBuilder

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;
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.jdom.Element) Document(org.w3c.dom.Document) DOMBuilder(org.jdom.input.DOMBuilder) Assert.assertNotNull(org.junit.Assert.assertNotNull) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with DOMBuilder

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());
}
Also used : XMLOutputter(org.jdom.output.XMLOutputter) DOMBuilder(org.jdom.input.DOMBuilder)

Aggregations

DOMBuilder (org.jdom.input.DOMBuilder)5 Element (org.jdom.Element)4 Document (org.w3c.dom.Document)3 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 NotNull (org.jetbrains.annotations.NotNull)2 Constraint (com.cosylab.logging.engine.Filter.Constraint)1 LogField (com.cosylab.logging.engine.log.LogField)1 EditorColorsScheme (com.intellij.openapi.editor.colors.EditorColorsScheme)1 EditorColorsSchemeImpl (com.intellij.openapi.editor.colors.impl.EditorColorsSchemeImpl)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 Iterator (java.util.Iterator)1 List (java.util.List)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 Document (org.jdom.Document)1 JAXPDOMAdapter (org.jdom.adapters.JAXPDOMAdapter)1