001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  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.plugin;
019    
020    import javax.enterprise.deploy.model.DDBean;
021    import javax.enterprise.deploy.model.XpathEvent;
022    import javax.enterprise.deploy.spi.DConfigBean;
023    import javax.enterprise.deploy.spi.exceptions.BeanNotFoundException;
024    import javax.enterprise.deploy.spi.exceptions.ConfigurationException;
025    
026    import org.apache.xmlbeans.XmlObject;
027    
028    /**
029     *
030     *
031     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
032     */
033    public abstract class DConfigBeanSupport extends XmlBeanSupport implements DConfigBean {
034        private DDBean ddBean;
035    
036        public DConfigBeanSupport(DDBean ddBean, XmlObject xmlObject) {
037            super(xmlObject);
038            this.ddBean = ddBean;
039        }
040    
041        protected void setParent(DDBean ddBean, XmlObject xmlObject) {
042            this.ddBean = ddBean;
043            setXmlObject(xmlObject);
044        }
045    
046        public DDBean getDDBean() {
047            return ddBean;
048        }
049    
050        public DConfigBean getDConfigBean(DDBean bean) throws ConfigurationException {
051            throw new ConfigurationException("No DConfigBean matching DDBean "+bean);
052        }
053    
054        public String[] getXpaths() {
055            return null;
056        }
057    
058        public void removeDConfigBean(DConfigBean bean) throws BeanNotFoundException {
059            throw new BeanNotFoundException("No children");
060        }
061    
062        public void notifyDDChange(XpathEvent event) {
063        }
064    
065        protected String[] getXPathsWithPrefix(String prefix, String[][] xpathSegments) {
066            String[] result = new String[xpathSegments.length];
067            for (int i = 0; i < xpathSegments.length; i++) {
068                String[] xpathSegmentArray = xpathSegments[i];
069                StringBuffer xpath = new StringBuffer();
070                for (int j = 0; j < xpathSegmentArray.length; j++) {
071                    String segment = xpathSegmentArray[j];
072                    if (prefix != null) {
073                        xpath.append(prefix).append(":");
074                    }
075                    xpath.append(segment);
076                    if (j < xpathSegmentArray.length -1) {
077                        xpath.append("/");
078                    }
079                }
080                result[i] = xpath.toString();
081            }
082            return result;
083        }
084    
085        protected String[] getXPathsFromNamespace(String uri, String[][] xpathSegments) {
086            String[] attributeNames = ddBean.getRoot().getAttributeNames();
087            for (int i = 0; i < attributeNames.length; i++) {
088                String attributeName = attributeNames[i];
089                if (attributeName.startsWith("xmlns")) {
090                    if (ddBean.getRoot().getAttributeValue(attributeName).equals(uri)) {
091                        if (attributeName.equals("xmlns")) {
092                            return getXPathsWithPrefix(null , xpathSegments);
093                        }
094                        return getXPathsWithPrefix(attributeName.substring(6), xpathSegments);
095                    }
096                }
097            }
098            //we can't determine the namespace from looking at attributes, since the namespace is not an attribute.
099            //try assuming that the ddbeans strip namespaces from their xpath handing.
100            return getXPathsWithPrefix(null , xpathSegments);
101        }
102    
103        /**
104         * Each entry in the first array is an XPath.
105         * Each entry in the enclosed array is a component of that XPath (slashes omitted).
106         * so {{"foo","bar"},{"baz","foo"}} would represent "foo/bar" and "baz/foo"
107         */ 
108        protected String[] getXPathsForJ2ee_1_4(String[][] xpathSegments) {
109            return getXPathsFromNamespace("http://java.sun.com/xml/ns/j2ee", xpathSegments);
110        }
111    }