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.util.ArrayList;
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Map;
023:
024: import javax.enterprise.deploy.model.DDBean;
025: import javax.enterprise.deploy.model.DDBeanRoot;
026: import javax.enterprise.deploy.model.XpathListener;
027:
028: import org.apache.xmlbeans.XmlCursor;
029:
030: /**
031: *
032: *
033: * @version $Rev: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
034: */
035: public class DDBeanImpl implements DDBean {
036: protected final DDBeanRoot root;
037: protected final String xpath;
038: protected final Map children;
039: protected final String content;
040: protected final Map attributeMap;
041: protected final DDBean parent;
042:
043: public DDBeanImpl(DDBeanRoot root, DDBean parent, String xpath,
044: XmlCursor c) {
045: this .root = root;
046: this .parent = parent;
047: this .xpath = xpath;
048: this .children = new HashMap();
049: this .attributeMap = new HashMap();
050: content = c.getTextValue();
051: c.push();
052: if (c.toFirstAttribute()) {
053: do {
054: attributeMap.put(c.getName().getLocalPart(), c
055: .getTextValue());
056: } while (c.toNextAttribute());
057: }
058: c.pop();
059: c.push();
060: if (c.toFirstChild()) {
061: do {
062: String name = c.getName().getLocalPart();
063: List nodes = (List) children.get(name);
064: if (nodes == null) {
065: nodes = new ArrayList();
066: children.put(name, nodes);
067: }
068: nodes.add(new DDBeanImpl(root, this ,
069: xpath + "/" + name, c));
070: } while (c.toNextSibling());
071: }
072: c.pop();
073: }
074:
075: DDBeanImpl(DDBeanImpl source, String xpath) {
076: this .xpath = xpath;
077: this .root = source.root;
078: this .parent = source.parent;
079: this .children = source.children;
080: this .content = source.content;
081: this .attributeMap = source.attributeMap;
082: }
083:
084: public DDBeanRoot getRoot() {
085: return root;
086: }
087:
088: public String getXpath() {
089: return xpath;
090: }
091:
092: public String getText() {
093: return content;
094: }
095:
096: public String getId() {
097: return getAttributeValue("id");
098: }
099:
100: public String getAttributeValue(String attrName) {
101: String value = (String) attributeMap.get(attrName);
102: if (value == null || value.length() == 0) {
103: return null;
104: }
105: return value;
106: }
107:
108: public String[] getText(String xpath) {
109: DDBean[] beans = getChildBean(xpath);
110: if (beans == null) {
111: return null;
112: }
113:
114: String[] text = new String[beans.length];
115: for (int i = 0; i < beans.length; i++) {
116: text[i] = beans[i].getText();
117: }
118: return text;
119: }
120:
121: public DDBean[] getChildBean(String xpath) {
122: if (xpath.startsWith("/")) {
123: return getRoot().getChildBean(xpath.substring(1));
124: } else if (xpath.equals(".")) {
125: return new DDBean[] { this };
126: } else if (xpath.startsWith("./")) {
127: return getChildBean(xpath.substring(2));
128: } else if (xpath.startsWith("..")) {
129: if (xpath.length() == 2) {
130: return new DDBean[] { parent };
131: } else {
132: return parent.getChildBean(xpath.substring(3));
133: }
134: }
135: int index = xpath.indexOf('/');
136: if (index == -1) {
137: List beans = (List) children.get(xpath);
138: if (beans == null) {
139: return null;
140: }
141: DDBean[] newDDBeans = (DDBean[]) beans
142: .toArray(new DDBean[beans.size()]);
143: for (int i = 0; i < newDDBeans.length; i++) {
144: newDDBeans[i] = new DDBeanImpl(
145: (DDBeanImpl) newDDBeans[i], xpath);
146: }
147: return newDDBeans;
148: } else {
149: List childBeans = (List) children.get(xpath.substring(0,
150: index));
151: if (childBeans == null) {
152: return null;
153: }
154: String path = xpath.substring(index + 1);
155: List beans = new ArrayList();
156: for (Iterator i = childBeans.iterator(); i.hasNext();) {
157: DDBean bean = (DDBean) i.next();
158: DDBean[] childs = bean.getChildBean(path);
159: if (childs != null) {
160: for (int j = 0; j < childs.length; j++) {
161: beans.add(new DDBeanImpl(
162: (DDBeanImpl) childs[j], xpath));
163: }
164: }
165: }
166: return beans.size() > 0 ? (DDBean[]) beans
167: .toArray(new DDBean[beans.size()]) : null;
168: }
169: }
170:
171: public String[] getAttributeNames() {
172: return (String[]) attributeMap.keySet().toArray(
173: new String[attributeMap.size()]);
174: }
175:
176: public void addXpathListener(String xpath, XpathListener xpl) {
177: }
178:
179: public void removeXpathListener(String xpath, XpathListener xpl) {
180: }
181:
182: public boolean equals(Object other) {
183: if (other.getClass() != DDBeanImpl.class) {
184: return false;
185: }
186: DDBeanImpl otherdd = (DDBeanImpl) other;
187: return xpath.equals(otherdd.xpath)
188: && children.equals(otherdd.children)
189: && attributeMap.equals(otherdd.attributeMap)
190: && root.equals(otherdd.root);
191: }
192:
193: public int hashCode() {
194: return xpath.hashCode() ^ attributeMap.hashCode()
195: ^ root.hashCode();
196: }
197: }
|