View Javadoc

1   /**
2    *
3    * Copyright 2003-2004 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  
18  package org.apache.geronimo.deployment.plugin;
19  
20  import javax.enterprise.deploy.model.DDBean;
21  import javax.enterprise.deploy.model.XpathEvent;
22  import javax.enterprise.deploy.spi.DConfigBean;
23  import javax.enterprise.deploy.spi.exceptions.BeanNotFoundException;
24  import javax.enterprise.deploy.spi.exceptions.ConfigurationException;
25  
26  import org.apache.xmlbeans.XmlObject;
27  
28  /**
29   *
30   *
31   * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
32   */
33  public abstract class DConfigBeanSupport extends XmlBeanSupport implements DConfigBean {
34      private DDBean ddBean;
35  
36      public DConfigBeanSupport(DDBean ddBean, XmlObject xmlObject) {
37          super(xmlObject);
38          this.ddBean = ddBean;
39      }
40  
41      protected void setParent(DDBean ddBean, XmlObject xmlObject) {
42          this.ddBean = ddBean;
43          setXmlObject(xmlObject);
44      }
45  
46      public DDBean getDDBean() {
47          return ddBean;
48      }
49  
50      public DConfigBean getDConfigBean(DDBean bean) throws ConfigurationException {
51          throw new ConfigurationException("No DConfigBean matching DDBean "+bean);
52      }
53  
54      public String[] getXpaths() {
55          return null;
56      }
57  
58      public void removeDConfigBean(DConfigBean bean) throws BeanNotFoundException {
59          throw new BeanNotFoundException("No children");
60      }
61  
62      public void notifyDDChange(XpathEvent event) {
63      }
64  
65      protected String[] getXPathsWithPrefix(String prefix, String[][] xpathSegments) {
66          String[] result = new String[xpathSegments.length];
67          for (int i = 0; i < xpathSegments.length; i++) {
68              String[] xpathSegmentArray = xpathSegments[i];
69              StringBuffer xpath = new StringBuffer();
70              for (int j = 0; j < xpathSegmentArray.length; j++) {
71                  String segment = xpathSegmentArray[j];
72                  if (prefix != null) {
73                      xpath.append(prefix).append(":");
74                  }
75                  xpath.append(segment);
76                  if (j < xpathSegmentArray.length -1) {
77                      xpath.append("/");
78                  }
79              }
80              result[i] = xpath.toString();
81          }
82          return result;
83      }
84  
85      protected String[] getXPathsFromNamespace(String uri, String[][] xpathSegments) {
86          String[] attributeNames = ddBean.getRoot().getAttributeNames();
87          for (int i = 0; i < attributeNames.length; i++) {
88              String attributeName = attributeNames[i];
89              if (attributeName.startsWith("xmlns")) {
90                  if (ddBean.getRoot().getAttributeValue(attributeName).equals(uri)) {
91                      if (attributeName.equals("xmlns")) {
92                          return getXPathsWithPrefix(null , xpathSegments);
93                      }
94                      return getXPathsWithPrefix(attributeName.substring(6), xpathSegments);
95                  }
96              }
97          }
98          //we can't determine the namespace from looking at attributes, since the namespace is not an attribute.
99          //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 }