Package org.apache.geronimo.deployment.tools

Source Code of org.apache.geronimo.deployment.tools.DDBeanImpl

/**
*
* Copyright 2003-2004 The Apache Software Foundation
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/

package org.apache.geronimo.deployment.tools;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.enterprise.deploy.model.DDBean;
import javax.enterprise.deploy.model.DDBeanRoot;
import javax.enterprise.deploy.model.XpathListener;
import javax.xml.namespace.QName;

import org.apache.xmlbeans.XmlCursor;

/**
*
*
* @version $Rev: 169154 $ $Date: 2005-05-08 13:35:23 -0600 (Sun, 08 May 2005) $
*/
public class DDBeanImpl implements DDBean {
    protected final DDBeanRoot root;
    protected final String xpath;
    protected final Map children;
    protected final String content;
    protected final Map attributeMap;

    public DDBeanImpl(DDBeanRoot root, String xpath, XmlCursor c) {
        this.root = root;
        this.xpath = xpath;
        this.children = new HashMap();
        this.attributeMap = new HashMap();
        content = c.getTextValue();
        c.push();
        if (c.toFirstAttribute()) {
            do {
                attributeMap.put(c.getName().getLocalPart(), c.getTextValue());
            } while (c.toNextAttribute());
        }
        c.pop();
        c.push();
        if (c.toFirstChild()) {
            do {
                String name = c.getName().getLocalPart();
                List nodes = (List) children.get(name);
                if (nodes == null) {
                    nodes = new ArrayList();
                    children.put(name, nodes);
                }
                nodes.add(new DDBeanImpl(root, xpath + "/" + name, c));
            } while (c.toNextSibling());
        }
        c.pop();
    }

    DDBeanImpl(DDBeanImpl source, String xpath) {
        this.xpath = xpath;
        this.root = source.root;
        this.children = source.children;
        this.content = source.content;
        this.attributeMap = source.attributeMap;
    }

    public DDBeanRoot getRoot() {
        return root;
    }

    public String getXpath() {
        return xpath;
    }

    public String getText() {
        return content;
    }

    public String getId() {
        return getAttributeValue("id");
    }

    public String getAttributeValue(String attrName) {
        String value = (String) attributeMap.get(attrName);
        if (value == null || value.length() == 0) {
            return null;
        }
        return value;
    }

    public String[] getText(String xpath) {
        DDBean[] beans = getChildBean(xpath);
        if (beans == null) {
            return null;
        }

        String[] text = new String[beans.length];
        for (int i = 0; i < beans.length; i++) {
            text[i] = beans[i].getText();
        }
        return text;
    }

    public DDBean[] getChildBean(String xpath) {
        if (xpath.startsWith("/")) {
            return getRoot().getChildBean(xpath.substring(1));
        }
        int index = xpath.indexOf('/');
        if (index == -1) {
            List beans = (List) children.get(xpath);
            if (beans == null) {
                return null;
            }
            DDBean[] newDDBeans = (DDBean[]) beans.toArray(new DDBean[beans.size()]);
            for (int i = 0; i < newDDBeans.length; i++) {
                newDDBeans[i] = new DDBeanImpl((DDBeanImpl) newDDBeans[i], xpath);
            }
            return newDDBeans;
        } else {
            List childBeans = (List) children.get(xpath.substring(0, index));
            if (childBeans == null) {
                return null;
            }
            String path = xpath.substring(index + 1);
            List beans = new ArrayList();
            for (Iterator i = childBeans.iterator(); i.hasNext();) {
                DDBean bean = (DDBean) i.next();
                DDBean[] childs = bean.getChildBean(path);
                if (childs != null) {
                    for (int j = 0; j < childs.length; j++) {
                        beans.add(new DDBeanImpl((DDBeanImpl) childs[j], xpath));
                    }
                }
            }
            return beans.size() > 0 ? (DDBean[]) beans.toArray(new DDBean[beans.size()]) : null;
        }
    }

    public String[] getAttributeNames() {
        return (String[]) attributeMap.keySet().toArray(new String[attributeMap.size()]);
    }

    public void addXpathListener(String xpath, XpathListener xpl) {
    }

    public void removeXpathListener(String xpath, XpathListener xpl) {
    }

    public boolean equals(Object other) {
        if (other.getClass() != DDBeanImpl.class) {
            return false;
        }
        DDBeanImpl otherdd = (DDBeanImpl) other;
        return xpath.equals(otherdd.xpath)
                && children.equals(otherdd.children)
                && attributeMap.equals(otherdd.attributeMap)
                && root.equals(otherdd.root);
    }

    public int hashCode() {
        return xpath.hashCode() ^ attributeMap.hashCode() ^ root.hashCode();
    }
}
TOP

Related Classes of org.apache.geronimo.deployment.tools.DDBeanImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.