sandbox/src/net/sf/xframe/xs/parser AttributesImpl.java,NONE,1.1 XSModelParser.java,NONE,1.1

Kurt Riede <[email protected]> Mon, 21 Mar 2005 18:13:22 +0000
Newsgroups gmane.text.xml.xframe.xsddoc.devel
Message-ID <[email protected]>
Update of /cvsroot/xframe/sandbox/src/net/sf/xframe/xs/parser
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27005/src/net/sf/xframe/xs/parser

Added Files:
	AttributesImpl.java XSModelParser.java 
Log Message:
initial upload of classes for working with XSModel classes of Apache XercesJ 2.6.2

--- NEW FILE: XSModelParser.java ---
/*
This file is part of the xframe software package
hosted at http://xframe.sourceforge.net

Copyright (c) 2003 Kurt Riede.

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.1 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
[...1358 lines suppressed...]
    }

    /**
     * @param object
     * @return
     */
    private String translateStringList(final StringList object) {
        if (object == null) {
            return "";
        }
        final StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < object.getLength(); i++) {
            if (i > 0) {
                buffer.append(' ');
            }
            buffer.append(object.item(i));
        }
        return buffer.toString();
    }
}

--- NEW FILE: AttributesImpl.java ---
/*
This file is part of the xframe software package
hosted at http://xframe.sourceforge.net

Copyright (c) 2003 Kurt Riede.

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.1 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
*/
package net.sf.xframe.xs.parser;

import org.apache.xerces.util.XMLSymbols;
import org.xml.sax.Attributes;

/**
 * An Attributes implementation that can perform more operations than the
 * attribute list helper supplied with the standard SAX2 distribution.
 * @author <a href="mailto:[email protected]">Kurt Riede </a>
 */
public final class AttributesImpl implements Attributes {

    /** Head node. */
    private ListNode head;

    /** Tail node. */
    private ListNode tail;

    /** Number of attributes. */
    private int length;

    /**
     * Returns the number of attributes.
     * @return number of attributes
     */
    public int getLength() {
        return length;
    }

    /**
     * Returns the index of the specified attribute.
     * @param raw raw name of attribute
     * @return index of attribute
     */
    public int getIndex(final String raw) {
        ListNode place = head;
        int index = 0;
        while (place != null) {
            if (place.getRaw().equals(raw)) {
                return index;
            }
            index++;
            place = place.getNext();
        }
        return -1;
    }

    /**
     * Returns the index of the specified attribute.
     * @param uri namespace uri of attribute
     * @param local local name of attribute
     * @return index of attribute
     */
    public int getIndex(final String uri, final String local) {
        ListNode place = head;
        int index = 0;
        while (place != null) {
            if (place.getUri().equals(uri) && place.getLocal().equals(local)) {
                return index;
            }
            index++;
            place = place.getNext();
        }
        return -1;
    }

    /**
     * Returns the attribute URI by index.
     * @param index index of attribute
     * @return namespace uri og attribute
     */
    public String getURI(final int index) {
        ListNode node = getListNodeAt(index);
        return node != null ? node.getUri() : null;
    }

    /**
     * Returns the attribute local name by index.
     * @param index index of attribute
     * @return local name of attribute
     */
    public String getLocalName(final int index) {
        ListNode node = getListNodeAt(index);
        return node != null ? node.getLocal() : null;
    }

    /**
     * Returns the attribute raw name by index.
     * @param index index of attribute
     * @return QName of attribute
     */
    public String getQName(final int index) {
        ListNode node = getListNodeAt(index);
        return node != null ? node.getRaw() : null;
    }

    /**
     * Returns the attribute type by index.
     * @param index index of attribute
     * @return type of attribute
     */
    public String getType(final int index) {
        ListNode node = getListNodeAt(index);
        return (node != null) ? node.getType() : null;
    }

    /**
     * Returns the attribute type by uri and local.
     * @param uri namespace uri of attribute
     * @param local local name of attribute
     * @return type of attribute
     */
    public String getType(final String uri, final String local) {
        ListNode node = getListNode(uri, local);
        return (node != null) ? node.getType() : null;
    }

    /**
     * Returns the attribute type by raw name.
     * @param raw raw name of attribute
     * @return type of attribute
     */
    public String getType(final String raw) {
        ListNode node = getListNode(raw);
        return (node != null) ? node.getType() : null;
    }

    /**
     * Returns the attribute value by index.
     * @param index index of attribute
     * @return value of attribute
     */
    public String getValue(final int index) {
        ListNode node = getListNodeAt(index);
        return (node != null) ? node.getValue() : null;
    }

    /**
     * Returns the attribute value by uri and local.
     * @param uri namespace uri of attribute
     * @param local local name of attribute
     * @return value of attribute
     */
    public String getValue(final String uri, final String local) {
        ListNode node = getListNode(uri, local);
        return (node != null) ? node.getValue() : null;
    }

    /**
     * Returns the attribute value by raw name.
     * @param raw raw name of attrobute
     * @return value of attribute
     */
    public String getValue(final String raw) {
        ListNode node = getListNode(raw);
        return (node != null) ? node.getValue() : null;
    }

    /**
     * Adds an attribute.
     * @param raw raw name of attribute
     * @param value value of attribute
     */
    public void addAttribute(final String raw, final String value) {
        addAttribute(raw, XMLSymbols.fCDATASymbol, value);
    }

    /**
     * Adds an attribute.
     * @param raw raw name of attribute
     * @param type type of attribute
     * @param value value of attribute
     */
    public void addAttribute(final String raw, final String type, final String value) {
        addAttribute(null, null, raw, type, value);
    }

    /**
     * Adds an attribute.
     * @param uri namespace uri of attribute
     * @param local local name of attribute
     * @param raw raw name of attribute
     * @param type type of attribute
     * @param value value of attribute
     */
    public void addAttribute(final String uri, final String local, final String raw, final String type, final String value) {
        ListNode node = new ListNode(uri, local, raw, type, value);
        if (length == 0) {
            head = node;
        } else {
            tail.setNext(node);
        }
        tail = node;
        length++;
    }

    /**
     * Inserts an attribute.
     * @param index index of attribute
     * @param raw raw name of attribute
     * @param type type of attribute
     * @param value value of attribute
     */
    public void insertAttributeAt(final int index, final String raw, final String type, final String value) {
        insertAttributeAt(index, null, null, raw, type, value);
    }

    /**
     * Inserts an attribute.
     * @param index index of attribute
     * @param uri namespace uri of attribute
     * @param local local name of attribute
     * @param raw raw name of attribute
     * @param type type of attribute
     * @param value value of attribute
     */
    public void insertAttributeAt(final int index, final String uri, final String local, final String raw, final String type,
            final String value) {
        // if list is empty, add attribute
        if (length == 0 || index >= length) {
            addAttribute(uri, local, raw, type, value);
            return;
        }
        // insert at beginning of list
        ListNode node = new ListNode(uri, local, raw, type, value);
        if (index < 1) {
            node.setNext(head);
            head = node;
        } else {
            ListNode prev = getListNodeAt(index - 1);
            node.setNext(prev.getNext());
            prev.setNext(node);
        }
        length++;
    }

    /**
     * Removes an attribute.
     * @param index index of attribute
     */
    public void removeAttributeAt(final int index) {
        if (length == 0) {
            return;
        }
        if (index == 0) {
            head = head.getNext();
            if (head == null) {
                tail = null;
            }
            length--;
        } else {
            ListNode prev = getListNodeAt(index - 1);
            ListNode node = getListNodeAt(index);
            if (node != null) {
                prev.setNext(node.getNext());
                if (node == tail) {
                    tail = prev;
                }
                length--;
            }
        }
    }

    /**
     * Removes the specified attribute.
     * @param raw raw name of attribute
     */
    public void removeAttribute(final String raw) {
        removeAttributeAt(getIndex(raw));
    }

    /**
     * Removes the specified attribute.
     * @param uri namespace uri of attribute
     * @param local name of attribute
     */
    public void removeAttribute(final String uri, final String local) {
        removeAttributeAt(getIndex(uri, local));
    }

    /**
     * Returns the node at the specified index.
     * @param i index of node
     * @return internal representation of an attribute
     */
    private ListNode getListNodeAt(final int i) {
        int k = i;
        for (ListNode place = head; place != null; place = place.getNext()) {
            if (--k == -1) {
                return place;
            }
        }
        return null;
    }

    /**
     * Returns the first node with the specified uri and local.
     * @param uri namespace uri of attribute
     * @param local local name of attribute
     * @return internal representation of an attribute
     */
    public ListNode getListNode(final String uri, final String local) {
        if (uri != null && local != null) {
            ListNode place = head;
            while (place != null) {
                if (place.getUri() != null && place.getLocal() != null && place.getUri().equals(uri)
                        && place.getLocal().equals(local)) {
                    return place;
                }
                place = place.getNext();
            }
        }
        return null;
    }

    /** Returns the first node with the specified raw name. */
    private ListNode getListNode(final String raw) {
        if (raw != null) {
            for (ListNode place = head; place != null; place = place.getNext()) {
                if (place.getRaw() != null && place.getRaw().equals(raw)) {
                    return place;
                }
            }
        }
        return null;
    }

    /**
     * Returns a string representation of this object.
     * @return string representation
     */
    public String toString() {
        StringBuffer str = new StringBuffer();
        str.append('[');
        str.append("len=");
        str.append(length);
        str.append(", {");
        for (ListNode place = head; place != null; place = place.getNext()) {
            str.append(place.toString());
            if (place.getNext() != null) {
                str.append(", ");
            }
        }
        str.append("}]");
        return str.toString();
    }

    /**
     * An attribute node.
     */
    protected final class ListNode {

        /** Attribute uri. */
        private String uri;

        /** Attribute local. */
        private String local;

        /** Attribute raw. */
        private String raw;

        /** Attribute type. */
        private String type;

        /** Attribute value. */
        private String value;

        /** Next node. */
        private ListNode next;

        /**
         * Constructs a list node.
         * @param uri namespace uri of attribute
         * @param local local name of attribute
         * @param raw raw name of attribute
         * @param type type of attribute
         * @param value value of attribute
         */
        protected ListNode(final String uri, final String local, final String raw, final String type, final String value) {
            this.uri = uri;
            this.local = local;
            this.raw = raw;
            this.type = type;
            this.value = value;
        }

        /**
         * @return Returns the local name.
         */
        public String getLocal() {
            return local;
        }

        /**
         * @return Returns the next element.
         */
        public ListNode getNext() {
            return next;
        }

        /**
         * @param nextNode The next to set.
         */
        public void setNext(final ListNode nextNode) {
            this.next = nextNode;
        }

        /**
         * @return Returns the raw name.
         */
        public String getRaw() {
            return raw;
        }

        /**
         * @return Returns the type.
         */
        public String getType() {
            return type;
        }

        /**
         * @return Returns the namespace uri.
         */
        public String getUri() {
            return uri;
        }

        /**
         * @return Returns the value.
         */
        public String getValue() {
            return value;
        }

        /**
         * Returns string representation of this object.
         * @return string representation
         * */
        public String toString() {
            return raw != null ? raw : local;
        }
    }
}



-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click