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.tools;
19  
20  import java.io.InputStream;
21  import java.net.URL;
22  
23  import javax.enterprise.deploy.model.DDBean;
24  import javax.enterprise.deploy.model.DDBeanRoot;
25  import javax.enterprise.deploy.model.DeployableObject;
26  import javax.enterprise.deploy.model.XpathListener;
27  import javax.enterprise.deploy.model.exceptions.DDBeanCreateException;
28  import javax.enterprise.deploy.shared.ModuleType;
29  
30  import org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil;
31  import org.apache.xmlbeans.XmlCursor;
32  import org.apache.xmlbeans.XmlObject;
33  
34  /**
35   * @version $Rev: 438104 $ $Date: 2006-08-29 08:50:48 -0700 (Tue, 29 Aug 2006) $
36   */
37  public class DDBeanRootImpl implements DDBeanRoot {
38      private final DeployableObject deployable;
39      private final DDBean docBean;
40  
41      public DDBeanRootImpl(DeployableObject deployable, URL descriptor) throws DDBeanCreateException {
42          this.deployable = deployable;
43          InputStream is = null;
44          try {
45              is = descriptor.openStream();
46              try {
47                  XmlObject xmlObject = XmlBeansUtil.parse(is);
48                  XmlCursor c = xmlObject.newCursor();
49                  try {
50                      c.toStartDoc();
51                      c.toFirstChild();
52                      docBean = new DDBeanImpl(this, this, "/" + c.getName().getLocalPart(), c);
53                  } finally {
54                      c.dispose();
55                  }
56              } finally {
57                  is.close();
58              }
59          } catch (Exception e) {
60              throw (DDBeanCreateException) new DDBeanCreateException("problem").initCause(e);
61          }
62      }
63  
64      public DDBeanRoot getRoot() {
65          return this;
66      }
67  
68      public String getXpath() {
69          return "/";
70      }
71  
72      public String getDDBeanRootVersion() {
73          return docBean.getAttributeValue("version");
74      }
75  
76      public DeployableObject getDeployableObject() {
77          return deployable;
78      }
79  
80      public String getFilename() {
81          throw new UnsupportedOperationException();
82      }
83  
84      public String getModuleDTDVersion() {
85          throw new UnsupportedOperationException();
86      }
87  
88      public ModuleType getType() {
89          return deployable.getType();
90      }
91  
92      public String getId() {
93          return null;
94      }
95  
96      public String getText() {
97          return null;
98      }
99  
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 }