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: */package org.apache.geronimo.deployment.tools;
017:
018: import java.io.InputStream;
019: import java.net.URL;
020:
021: import javax.enterprise.deploy.model.DDBean;
022: import javax.enterprise.deploy.model.DDBeanRoot;
023: import javax.enterprise.deploy.model.DeployableObject;
024: import javax.enterprise.deploy.model.XpathListener;
025: import javax.enterprise.deploy.model.exceptions.DDBeanCreateException;
026: import javax.enterprise.deploy.shared.ModuleType;
027:
028: import org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil;
029: import org.apache.xmlbeans.XmlCursor;
030: import org.apache.xmlbeans.XmlObject;
031:
032: /**
033: * @version $Rev: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
034: */
035: public class DDBeanRootImpl implements DDBeanRoot {
036: private final DeployableObject deployable;
037: private final DDBean docBean;
038:
039: public DDBeanRootImpl(DeployableObject deployable, URL descriptor)
040: throws DDBeanCreateException {
041: this .deployable = deployable;
042: InputStream is = null;
043: try {
044: is = descriptor.openStream();
045: try {
046: XmlObject xmlObject = XmlBeansUtil.parse(is);
047: XmlCursor c = xmlObject.newCursor();
048: try {
049: c.toStartDoc();
050: c.toFirstChild();
051: docBean = new DDBeanImpl(this , this , "/"
052: + 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(
061: "problem").initCause(e);
062: }
063: }
064:
065: public DDBeanRoot getRoot() {
066: return this ;
067: }
068:
069: public String getXpath() {
070: return "/";
071: }
072:
073: public String getDDBeanRootVersion() {
074: return docBean.getAttributeValue("version");
075: }
076:
077: public DeployableObject getDeployableObject() {
078: return deployable;
079: }
080:
081: public String getFilename() {
082: throw new UnsupportedOperationException();
083: }
084:
085: public String getModuleDTDVersion() {
086: throw new UnsupportedOperationException();
087: }
088:
089: public ModuleType getType() {
090: return deployable.getType();
091: }
092:
093: public String getId() {
094: return null;
095: }
096:
097: public String getText() {
098: return null;
099: }
100:
101: public String[] getAttributeNames() {
102: return docBean.getAttributeNames();
103: }
104:
105: public String getAttributeValue(String attrName) {
106: return docBean.getAttributeValue(attrName);
107: }
108:
109: public DDBean[] getChildBean(String xpath) {
110: if (xpath.startsWith("/")) {
111: xpath = xpath.substring(1);
112: }
113: int index = xpath.indexOf('/');
114: String childName = (index == -1) ? xpath : xpath.substring(0,
115: index);
116: if (("/" + childName).equals(docBean.getXpath())) {
117: if (index == -1) {
118: return new DDBean[] { new DDBeanImpl(
119: (DDBeanImpl) docBean, xpath) };
120: } else {
121: DDBean[] newDDBeans = docBean.getChildBean(xpath
122: .substring(index + 1));
123: if (newDDBeans != null) {
124: for (int i = 0; i < newDDBeans.length; i++) {
125: newDDBeans[i] = new DDBeanImpl(
126: (DDBeanImpl) newDDBeans[i], xpath);
127: }
128: }
129: return newDDBeans;
130: }
131: } else {
132: return null;
133: }
134: }
135:
136: public String[] getText(String xpath) {
137: DDBean[] beans = getChildBean(xpath);
138: if (beans == null) {
139: return null;
140: }
141:
142: String[] text = new String[beans.length];
143: for (int i = 0; i < beans.length; i++) {
144: text[i] = beans[i].getText();
145: }
146: return text;
147: }
148:
149: public void addXpathListener(String xpath, XpathListener xpl) {
150: throw new UnsupportedOperationException();
151: }
152:
153: public void removeXpathListener(String xpath, XpathListener xpl) {
154: throw new UnsupportedOperationException();
155: }
156: }
|