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.util.ArrayList; 21 import java.util.HashMap; 22 import java.util.Iterator; 23 import java.util.List; 24 import java.util.Map; 25 26 import javax.enterprise.deploy.model.DDBean; 27 import javax.enterprise.deploy.model.DDBeanRoot; 28 import javax.enterprise.deploy.model.XpathListener; 29 30 import org.apache.xmlbeans.XmlCursor; 31 32 /** 33 * 34 * 35 * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $ 36 */ 37 public class DDBeanImpl implements DDBean { 38 protected final DDBeanRoot root; 39 protected final String xpath; 40 protected final Map children; 41 protected final String content; 42 protected final Map attributeMap; 43 protected final DDBean parent; 44 45 public DDBeanImpl(DDBeanRoot root, DDBean parent, String xpath, XmlCursor c) { 46 this.root = root; 47 this.parent = parent; 48 this.xpath = xpath; 49 this.children = new HashMap(); 50 this.attributeMap = new HashMap(); 51 content = c.getTextValue(); 52 c.push(); 53 if (c.toFirstAttribute()) { 54 do { 55 attributeMap.put(c.getName().getLocalPart(), c.getTextValue()); 56 } while (c.toNextAttribute()); 57 } 58 c.pop(); 59 c.push(); 60 if (c.toFirstChild()) { 61 do { 62 String name = c.getName().getLocalPart(); 63 List nodes = (List) children.get(name); 64 if (nodes == null) { 65 nodes = new ArrayList(); 66 children.put(name, nodes); 67 } 68 nodes.add(new DDBeanImpl(root, this, xpath + "/" + name, c)); 69 } while (c.toNextSibling()); 70 } 71 c.pop(); 72 } 73 74 DDBeanImpl(DDBeanImpl source, String xpath) { 75 this.xpath = xpath; 76 this.root = source.root; 77 this.parent = source.parent; 78 this.children = source.children; 79 this.content = source.content; 80 this.attributeMap = source.attributeMap; 81 } 82 83 public DDBeanRoot getRoot() { 84 return root; 85 } 86 87 public String getXpath() { 88 return xpath; 89 } 90 91 public String getText() { 92 return content; 93 } 94 95 public String getId() { 96 return getAttributeValue("id"); 97 } 98 99 public String getAttributeValue(String attrName) { 100 String value = (String) attributeMap.get(attrName); 101 if (value == null || value.length() == 0) { 102 return null; 103 } 104 return value; 105 } 106 107 public String[] getText(String xpath) { 108 DDBean[] beans = getChildBean(xpath); 109 if (beans == null) { 110 return null; 111 } 112 113 String[] text = new String[beans.length]; 114 for (int i = 0; i < beans.length; i++) { 115 text[i] = beans[i].getText(); 116 } 117 return text; 118 } 119 120 public DDBean[] getChildBean(String xpath) { 121 if (xpath.startsWith("/")) { 122 return getRoot().getChildBean(xpath.substring(1)); 123 } else if(xpath.equals(".")) { 124 return new DDBean[]{this}; 125 } else if(xpath.startsWith("./")) { 126 return getChildBean(xpath.substring(2)); 127 } else if(xpath.startsWith("..")) { 128 if(xpath.length() == 2) { 129 return new DDBean[]{parent}; 130 } else { 131 return parent.getChildBean(xpath.substring(3)); 132 } 133 } 134 int index = xpath.indexOf('/'); 135 if (index == -1) { 136 List beans = (List) children.get(xpath); 137 if (beans == null) { 138 return null; 139 } 140 DDBean[] newDDBeans = (DDBean[]) beans.toArray(new DDBean[beans.size()]); 141 for (int i = 0; i < newDDBeans.length; i++) { 142 newDDBeans[i] = new DDBeanImpl((DDBeanImpl) newDDBeans[i], xpath); 143 } 144 return newDDBeans; 145 } else { 146 List childBeans = (List) children.get(xpath.substring(0, index)); 147 if (childBeans == null) { 148 return null; 149 } 150 String path = xpath.substring(index + 1); 151 List beans = new ArrayList(); 152 for (Iterator i = childBeans.iterator(); i.hasNext();) { 153 DDBean bean = (DDBean) i.next(); 154 DDBean[] childs = bean.getChildBean(path); 155 if (childs != null) { 156 for (int j = 0; j < childs.length; j++) { 157 beans.add(new DDBeanImpl((DDBeanImpl) childs[j], xpath)); 158 } 159 } 160 } 161 return beans.size() > 0 ? (DDBean[]) beans.toArray(new DDBean[beans.size()]) : null; 162 } 163 } 164 165 public String[] getAttributeNames() { 166 return (String[]) attributeMap.keySet().toArray(new String[attributeMap.size()]); 167 } 168 169 public void addXpathListener(String xpath, XpathListener xpl) { 170 } 171 172 public void removeXpathListener(String xpath, XpathListener xpl) { 173 } 174 175 public boolean equals(Object other) { 176 if (other.getClass() != DDBeanImpl.class) { 177 return false; 178 } 179 DDBeanImpl otherdd = (DDBeanImpl) other; 180 return xpath.equals(otherdd.xpath) 181 && children.equals(otherdd.children) 182 && attributeMap.equals(otherdd.attributeMap) 183 && root.equals(otherdd.root); 184 } 185 186 public int hashCode() { 187 return xpath.hashCode() ^ attributeMap.hashCode() ^ root.hashCode(); 188 } 189 }