Search in sources :

Example 31 with Element

use of org.jdom.Element 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 32 with Element

use of org.jdom.Element in project intellij-community by JetBrains.

the class HTMLControls method loadControls.

@SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
private static Control[] loadControls() {
    Element element;
    try {
        // use temporary bytes stream because otherwise inputStreamSkippingBOM will fail
        // on ZipFileInputStream used in jar files
        final InputStream stream = HTMLControls.class.getResourceAsStream("HtmlControls.xml");
        final byte[] bytes = FileUtilRt.loadBytes(stream);
        stream.close();
        final UnsyncByteArrayInputStream bytesStream = new UnsyncByteArrayInputStream(bytes);
        element = JDOMUtil.load(CharsetToolkit.inputStreamSkippingBOM(bytesStream));
        bytesStream.close();
    } catch (Exception e) {
        LOG.error(e);
        return new Control[0];
    }
    if (!element.getName().equals("htmlControls")) {
        LOG.error("HTMLControls storage is broken");
        return new Control[0];
    }
    return XmlSerializer.deserialize(element, Control[].class);
}
Also used : UnsyncByteArrayInputStream(com.intellij.util.io.UnsyncByteArrayInputStream) InputStream(java.io.InputStream) Element(org.jdom.Element) UnsyncByteArrayInputStream(com.intellij.util.io.UnsyncByteArrayInputStream)

Example 33 with Element

use of org.jdom.Element in project intellij-community by JetBrains.

the class I18nInspection method writeSettings.

@Override
public void writeSettings(@NotNull Element node) throws WriteExternalException {
    super.writeSettings(node);
    if (ignoreForEnumConstants) {
        final Element e = new Element("option");
        e.setAttribute("name", SKIP_FOR_ENUM);
        e.setAttribute("value", Boolean.toString(ignoreForEnumConstants));
        node.addContent(e);
    }
}
Also used : Element(org.jdom.Element)

Example 34 with Element

use of org.jdom.Element in project intellij-community by JetBrains.

the class MavenJDOMUtil method findChildValueByPath.

public static String findChildValueByPath(@Nullable Element element, String path, String defaultValue) {
    Element child = findChildByPath(element, path);
    if (child == null)
        return defaultValue;
    String childValue = child.getTextTrim();
    return childValue.isEmpty() ? defaultValue : childValue;
}
Also used : Element(org.jdom.Element)

Example 35 with Element

use of org.jdom.Element in project intellij-community by JetBrains.

the class MavenJDOMUtil method collectChildren.

private static List<Element> collectChildren(@Nullable Element container, String subPath) {
    if (container == null)
        return Collections.emptyList();
    int firstDot = subPath.indexOf('.');
    if (firstDot == -1) {
        return container.getChildren(subPath);
    }
    String childName = subPath.substring(0, firstDot);
    String pathInChild = subPath.substring(firstDot + 1);
    List<Element> result = new ArrayList<>();
    for (Element each : container.getChildren(childName)) {
        Element child = findChildByPath(each, pathInChild);
        if (child != null)
            result.add(child);
    }
    return result;
}
Also used : Element(org.jdom.Element)

Aggregations

Element (org.jdom.Element)1994 BioPaxObject (org.vcell.pathway.BioPaxObject)143 ArrayList (java.util.ArrayList)141 NotNull (org.jetbrains.annotations.NotNull)103 IOException (java.io.IOException)102 Document (org.jdom.Document)102 Nullable (org.jetbrains.annotations.Nullable)98 List (java.util.List)84 File (java.io.File)78 GroupObject (org.vcell.pathway.GroupObject)75 VirtualFile (com.intellij.openapi.vfs.VirtualFile)67 JDOMException (org.jdom.JDOMException)57 Expression (cbit.vcell.parser.Expression)47 SAXBuilder (org.jdom.input.SAXBuilder)47 ExpressionException (cbit.vcell.parser.ExpressionException)45 Iterator (java.util.Iterator)45 PsiElement (com.intellij.psi.PsiElement)44 Attribute (org.jdom.Attribute)42 XMLOutputter (org.jdom.output.XMLOutputter)34 Namespace (org.jdom.Namespace)32