package org.mandarax.xkb;

/*
 * Copyright (C) 1999-2004 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.jdom.Element;
import org.mandarax.kernel.LogicFactory;

/**
 * Abstract super class for XKB drivers.
 * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
 * @version 3.3.1 <9 August 2004>
 * @since 1.4
 */
public abstract class AbstractXKBDriver implements XKBDriver {

    protected LogicFactory lfactory = LogicFactory.getDefaultFactory ();

    /**
     * Constructor.
     */
    public AbstractXKBDriver() {
        super ();
    }

    /**
     * Get a list of all child elements
     * nested directly (one level deep) within this element
     * with one of the given names. Preserve the order of children.
     * @return a list of children with the respective names
     * @param e an element
     * @param name1 a name
     * @param name2 a name
     */
    protected List collectChildren(Element e, String name1, String name2) {
        List    children  = e.getChildren ();
        List    collected = new Vector (children.size ());
        Element next      = null;

        for(Iterator it = children.iterator (); it.hasNext (); ) {
            next = (Element) it.next ();

            if(compare (name1, next.getName ())
                    || compare (name2, next.getName ())) {
                collected.add (next);
            }
        }

        return collected;
    }

    /**
     * Get a list of all child elements
     * nested directly (one level deep) within this element
     * with one of the given names. Preserve the order of children.
     * @return a list of children with the respective names
     * @param e an element
     * @param name1 a name
     * @param name2 a name
     * @param name3 a nam
     */
    protected List collectChildren(Element e, String name1, String name2,
                                   String name3) {
        List    children  = e.getChildren ();
        List    collected = new Vector (children.size ());
        Element next      = null;

        for(Iterator it = children.iterator (); it.hasNext (); ) {
            next = (Element) it.next ();

            if(compare (name1, next.getName ())
                    || compare (name2, next.getName ())
                    || compare (name3, next.getName ())) {
                collected.add (next);
            }
        }

        return collected;
    }

    /**
     * Compare two strings representing tag names.
     * @return a boolean
     * @param s1 string 1
     * @param s2 string 2
     */
    protected boolean compare(String s1, String s2) {
        return(s1 == null)
              ? s2 == null
              : s1.equalsIgnoreCase (s2);
    }

    /**
     * Get the number of all child elements
     * nested directly (one level deep) within this element
     * with the given name.
     * @return the number of children
     * @param e an element
     * @param name a name
     */
    protected int countChildren(Element e, String name) {
        return((e == null) || (name == null))
              ? 0
              : e.getChildren (name).size ();
    }

    /**
     * Get the number of all child elements
     * nested directly (one level deep) within this element
     * with one of the given names.
     * @return the number of children
     * @param e an element
     * @param name1 a name
     * @param name2 a name
     */
    protected int countChildren(Element e, String name1, String name2) {
        if(e == null) {
            return 0;
        }

        int n = (name1 == null)
                ? 0
                : e.getChildren (name1).size ();

        n = (name2 == null)
            ? n
            : n + e.getChildren (name2).size ();

        return n;
    }

    /**
     * Get the number of all child elements
     * nested directly (one level deep) within this element
     * with one of the given names.
     * @return the number of children
     * @param e an element
     * @param name1 a name
     * @param name2 a name
     * @param name3 a name
     */
    protected int countChildren(Element e, String name1, String name2,
                                String name3) {
        if(e == null) {
            return 0;
        }

        int n = (name1 == null)
                ? 0
                : e.getChildren (name1).size ();

        n = (name2 == null)
            ? n
            : n + e.getChildren (name2).size ();
        n = (name3 == null)
            ? n
            : n + e.getChildren (name3).size ();

        return n;
    }

    /**
     * Get the name of the driver.
     * @return a text
     */
    public String getName() {
        return getClass ().getName ();
    }
}
