001    /**
002     *
003     * Copyright 2003-2004 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package org.apache.geronimo.deployment.tools;
019    
020    import java.io.InputStream;
021    import java.net.URL;
022    
023    import javax.enterprise.deploy.model.DDBean;
024    import javax.enterprise.deploy.model.DDBeanRoot;
025    import javax.enterprise.deploy.model.DeployableObject;
026    import javax.enterprise.deploy.model.XpathListener;
027    import javax.enterprise.deploy.model.exceptions.DDBeanCreateException;
028    import javax.enterprise.deploy.shared.ModuleType;
029    
030    import org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil;
031    import org.apache.xmlbeans.XmlCursor;
032    import org.apache.xmlbeans.XmlObject;
033    
034    /**
035     * @version $Rev: 438104 $ $Date: 2006-08-29 08:50:48 -0700 (Tue, 29 Aug 2006) $
036     */
037    public class DDBeanRootImpl implements DDBeanRoot {
038        private final DeployableObject deployable;
039        private final DDBean docBean;
040    
041        public DDBeanRootImpl(DeployableObject deployable, URL descriptor) throws DDBeanCreateException {
042            this.deployable = deployable;
043            InputStream is = null;
044            try {
045                is = descriptor.openStream();
046                try {
047                    XmlObject xmlObject = XmlBeansUtil.parse(is);
048                    XmlCursor c = xmlObject.newCursor();
049                    try {
050                        c.toStartDoc();
051                        c.toFirstChild();
052                        docBean = new DDBeanImpl(this, this, "/" + c.getName().getLocalPart(), c);
053                    } finally {
054                        c.dispose();
055                    }
056                } finally {
057                    is.close();
058                }
059            } catch (Exception e) {
060                throw (DDBeanCreateException) new DDBeanCreateException("problem").initCause(e);
061            }
062        }
063    
064        public DDBeanRoot getRoot() {
065            return this;
066        }
067    
068        public String getXpath() {
069            return "/";
070        }
071    
072        public String getDDBeanRootVersion() {
073            return docBean.getAttributeValue("version");
074        }
075    
076        public DeployableObject getDeployableObject() {
077            return deployable;
078        }
079    
080        public String getFilename() {
081            throw new UnsupportedOperationException();
082        }
083    
084        public String getModuleDTDVersion() {
085            throw new UnsupportedOperationException();
086        }
087    
088        public ModuleType getType() {
089            return deployable.getType();
090        }
091    
092        public String getId() {
093            return null;
094        }
095    
096        public String getText() {
097            return null;
098        }
099    
100        public String[] getAttributeNames() {
101            return docBean.getAttributeNames();
102        }
103    
104        public String getAttributeValue(String attrName) {
105            return docBean.getAttributeValue(attrName);
106        }
107    
108        public DDBean[] getChildBean(String xpath) {
109            if (xpath.startsWith("/")) {
110                xpath = xpath.substring(1);
111            }
112            int index = xpath.indexOf('/');
113            String childName = (index == -1) ? xpath : xpath.substring(0, index);
114            if (("/" + childName).equals(docBean.getXpath())) {
115                if (index == -1) {
116                    return new DDBean[]{new DDBeanImpl((DDBeanImpl) docBean, xpath)};
117                } else {
118                    DDBean[] newDDBeans = docBean.getChildBean(xpath.substring(index + 1));
119                    if (newDDBeans != null) {
120                        for (int i = 0; i < newDDBeans.length; i++) {
121                            newDDBeans[i] = new DDBeanImpl((DDBeanImpl) newDDBeans[i], xpath);
122                        }
123                    }
124                    return newDDBeans;
125                }
126            } else {
127                return null;
128            }
129        }
130    
131        public String[] getText(String xpath) {
132            DDBean[] beans = getChildBean(xpath);
133            if (beans == null) {
134                return null;
135            }
136    
137            String[] text = new String[beans.length];
138            for (int i = 0; i < beans.length; i++) {
139                text[i] = beans[i].getText();
140            }
141            return text;
142        }
143    
144        public void addXpathListener(String xpath, XpathListener xpl) {
145            throw new UnsupportedOperationException();
146        }
147    
148        public void removeXpathListener(String xpath, XpathListener xpl) {
149            throw new UnsupportedOperationException();
150        }
151    }