alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.vcs.advanced.variables;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.*;

/**
 * This class provides input/output of conditions from/to xml file.
 *
 * @author  Martin Entlicher
 */
public class ConditionIO extends Object {
    
    public static final String CONDITION_TAG = "condition";                      // NOI18N
    public static final String CONDITION_VAR_ATTR = "var";                       // NOI18N
    public static final String VAR_TAG = "var";                                  // NOI18N
    public static final String VAR_NAME_ATTR = "name";                           // NOI18N
    public static final String VAR_VALUE_ATTR = "value";                         // NOI18N
    public static final String VAR_VALUE_IGNORE_CASE_ATTR = "valueIgnoreCase";   // NOI18N
    public static final String VAR_VALUE_CONTAINS_ATTR = "valueContains";        // NOI18N
    public static final String VAR_VALUE_CONTAINS_IGNORE_CASE_ATTR = "valueContainsIgnoreCase"; // NOI18N
    public static final String AND_TAG = "and";                                  // NOI18N
    public static final String OR_TAG = "or";                                    // NOI18N
    public static final String NOT_TAG = "not";                                  // NOI18N

    /** Creates a new instance of ConditionIO */
    private ConditionIO() {
    }
    
    /**
     * Read a list of conditions from the specified document.
     */
    public static Condition[] readConditions(Document doc) throws DOMException {
        List conditions = new ArrayList();
        Element rootElem = doc.getDocumentElement();
        if (!VariableIO.CONFIG_ROOT_ELEM.equals(rootElem.getNodeName())) return null;
        NodeList conditionsList = rootElem.getElementsByTagName(CONDITION_TAG);
        int n = conditionsList.getLength();
        for (int i = 0; i < n; i++) {
            Element conditionElement = (Element) conditionsList.item(i);
            NamedNodeMap attrs = conditionElement.getAttributes();
            if (attrs.getLength() == 0) continue;
            Node varAttrNode = attrs.getNamedItem(CONDITION_VAR_ATTR);
            if (varAttrNode == null) continue;
            String varName = varAttrNode.getNodeValue();
            Condition c = new Condition(varName);
            readCondition(c, conditionElement);
            conditions.add(c);
        }
        return (Condition[]) conditions.toArray(new Condition[conditions.size()]);
    }
    
    private static void readCondition(Condition c, Element conditionElement) throws DOMException {
        String[] varValuePtr = new String[1];
        int[] compareValuePtr = new int[1];
        NodeList children = conditionElement.getChildNodes();
        int n = children.getLength();
        List elements = new ArrayList();
        for (int i = 0; i < n; i++) {
            Node node = children.item(i);
            if (Node.ELEMENT_NODE != node.getNodeType()) {
                continue;
            }
            elements.add(node);
        }
        n = elements.size();
        for (int i = 0; i < n; i++) {
            Element elem = (Element) elements.get(i);
            String tagName = elem.getTagName();
            if (VAR_TAG.equals(tagName)) {
                String name = readVariable(elem, varValuePtr, compareValuePtr);
                if (name == null) continue;
                c.addVar(name, varValuePtr[0], compareValuePtr[0], true);
            } else if (NOT_TAG.equals(tagName)) {
                NodeList notChildren = elem.getChildNodes();
                int nn = notChildren.getLength();
                for (int ni = 0; ni < nn; ni++) {
                    Node nnode = notChildren.item(ni);
                    if (Node.ELEMENT_NODE != nnode.getNodeType()) continue;
                    Element nvar = (Element) nnode;
                    String name = readVariable(nvar, varValuePtr, compareValuePtr);
                    if (name == null) continue;
                    c.addVar(name, varValuePtr[0], compareValuePtr[0], false);
                }
            } else if (AND_TAG.equals(tagName)) {
                if (n == 1) { // just  tag is here
                    c.setLogicalOperation(Condition.LOGICAL_AND);
                    readCondition(c, elem);
                } else {
                    Condition inner = new Condition("Anonymous inner");
                    c.addCondition(inner, true);
                    inner.setLogicalOperation(Condition.LOGICAL_AND);
                    readCondition(inner, elem);
                }
            } else if (OR_TAG.equals(tagName)) {
                if (n == 1) { // just  tag is here
                    c.setLogicalOperation(Condition.LOGICAL_OR);
                    readCondition(c, elem);
                } else {
                    Condition inner = new Condition("Anonymous inner");
                    c.addCondition(inner, true);
                    inner.setLogicalOperation(Condition.LOGICAL_OR);
                    readCondition(inner, elem);
                }
            }
        }
    }
    
    /**
     * Read a variable.
     * @param var The variable element
     * @param valuePtr The return value of the variable
     * @param compareValuePtr The compare value
     * @return name of the variable
     */
    private static String readVariable(Element var, String[] valuePtr, int[] compareValuePtr) {
        NamedNodeMap attrs = var.getAttributes();
        Node nameAttr = attrs.getNamedItem(VAR_NAME_ATTR);
        String name = null;
        if (nameAttr != null) {
            name = nameAttr.getNodeValue();
            Node value;
            if ((value = attrs.getNamedItem(VAR_VALUE_ATTR)) != null) {
                valuePtr[0] = value.getNodeValue();
                compareValuePtr[0] = Condition.COMPARE_VALUE_EQUALS;
            } else if ((value = attrs.getNamedItem(VAR_VALUE_IGNORE_CASE_ATTR)) != null) {
                valuePtr[0] = value.getNodeValue();
                compareValuePtr[0] = Condition.COMPARE_VALUE_EQUALS_IGNORE_CASE;
            } else if ((value = attrs.getNamedItem(VAR_VALUE_CONTAINS_ATTR)) != null) {
                valuePtr[0] = value.getNodeValue();
                compareValuePtr[0] = Condition.COMPARE_VALUE_CONTAINS;
            } else if ((value = attrs.getNamedItem(VAR_VALUE_CONTAINS_IGNORE_CASE_ATTR)) != null) {
                valuePtr[0] = value.getNodeValue();
                compareValuePtr[0] = Condition.COMPARE_VALUE_CONTAINS_IGNORE_CASE;
            } else {
                name = null;
            }
        }
        return name;
    }
    
    /* ---------------------------------------------------------------------- */
    
    /**
     * Write the commands definitions to the document from the tree of commands.
     */
    public static boolean writeConditions(Document doc, Condition[] conditions) throws DOMException {
        Element rootElem = doc.getDocumentElement();
        if (!VariableIO.CONFIG_ROOT_ELEM.equals(rootElem.getNodeName())) return false;
        for (int i = 0; i < conditions.length; i++) {
            Element conditionElm = doc.createElement(CONDITION_TAG);
            conditionElm.setAttribute(CONDITION_VAR_ATTR, conditions[i].getName());
            setupConditionElement(doc, conditionElm, conditions[i]);
            rootElem.appendChild(conditionElm);
        }
        return true;
    }
    
    private static void setupConditionElement(Document doc, Element conditionElm, Condition c) {
        if (CONDITION_TAG.equals(conditionElm.getNodeName())) {
            int lop = c.getLogicalOperation();
            if (lop == Condition.LOGICAL_OR) {
                Element orElem = doc.createElement(OR_TAG);
                conditionElm.appendChild(orElem);
                setupConditionElement(doc, orElem, c);
                return ;
            }
        }
        Condition.Var[] vars = c.getVars();
        for (int i = 0; i < vars.length; i++) {
            Element cElem;
            if (!c.isPositiveTest(vars[i])) {
                Element not = doc.createElement(NOT_TAG);
                conditionElm.appendChild(not);
                cElem = not;
            } else {
                cElem = conditionElm;
            }
            addVarElement(doc, cElem, vars[i]);
        }
        Condition[] subConditions = c.getConditions();
        for (int i = 0; i < subConditions.length; i++) {
            Element cElem;
            if (!c.isPositiveTest(subConditions[i])) {
                Element not = doc.createElement(NOT_TAG);
                conditionElm.appendChild(not);
                cElem = not;
            } else {
                cElem = conditionElm;
            }
            int lop = subConditions[i].getLogicalOperation();
            Element subcElem;
            if (Condition.LOGICAL_AND == lop) {
                subcElem = doc.createElement(AND_TAG);
                cElem.appendChild(subcElem);
            } else if (Condition.LOGICAL_OR == lop) {
                subcElem = doc.createElement(OR_TAG);
                cElem.appendChild(subcElem);
            } else {
                continue;
            }
            setupConditionElement(doc, subcElem, subConditions[i]);
        }
    }
    
    private static void addVarElement(Document doc, Element conditionElm, Condition.Var var) throws DOMException {
        Element varElem = doc.createElement(VAR_TAG);
        conditionElm.appendChild(varElem);
        varElem.setAttribute(VAR_NAME_ATTR, var.getName());
        int compareValue = var.getCompareValue();
        if (Condition.COMPARE_VALUE_EQUALS == compareValue) {
            varElem.setAttribute(VAR_VALUE_ATTR, var.getValue());
        } else if (Condition.COMPARE_VALUE_EQUALS_IGNORE_CASE == compareValue) {
            varElem.setAttribute(VAR_VALUE_IGNORE_CASE_ATTR, var.getValue());
        } else if (Condition.COMPARE_VALUE_CONTAINS == compareValue) {
            varElem.setAttribute(VAR_VALUE_CONTAINS_ATTR, var.getValue());
        } else if (Condition.COMPARE_VALUE_CONTAINS_IGNORE_CASE == compareValue) {
            varElem.setAttribute(VAR_VALUE_CONTAINS_IGNORE_CASE_ATTR, var.getValue());
        }
    }
    
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.