001 /** 002 * 003 * Copyright 2003-2004 The Apache Software Foundation 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * 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 */ 017 018 package org.apache.geronimo.deployment.tools; 019 020 import java.util.ArrayList; 021 import java.util.HashMap; 022 import java.util.Iterator; 023 import java.util.List; 024 import java.util.Map; 025 026 import javax.enterprise.deploy.model.DDBean; 027 import javax.enterprise.deploy.model.DDBeanRoot; 028 import javax.enterprise.deploy.model.XpathListener; 029 030 import org.apache.xmlbeans.XmlCursor; 031 032 /** 033 * 034 * 035 * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $ 036 */ 037 public class DDBeanImpl implements DDBean { 038 protected final DDBeanRoot root; 039 protected final String xpath; 040 protected final Map children; 041 protected final String content; 042 protected final Map attributeMap; 043 protected final DDBean parent; 044 045 public DDBeanImpl(DDBeanRoot root, DDBean parent, String xpath, XmlCursor c) { 046 this.root = root; 047 this.parent = parent; 048 this.xpath = xpath; 049 this.children = new HashMap(); 050 this.attributeMap = new HashMap(); 051 content = c.getTextValue(); 052 c.push(); 053 if (c.toFirstAttribute()) { 054 do { 055 attributeMap.put(c.getName().getLocalPart(), c.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, xpath + "/" + name, c)); 069 } while (c.toNextSibling()); 070 } 071 c.pop(); 072 } 073 074 DDBeanImpl(DDBeanImpl source, String xpath) { 075 this.xpath = xpath; 076 this.root = source.root; 077 this.parent = source.parent; 078 this.children = source.children; 079 this.content = source.content; 080 this.attributeMap = source.attributeMap; 081 } 082 083 public DDBeanRoot getRoot() { 084 return root; 085 } 086 087 public String getXpath() { 088 return xpath; 089 } 090 091 public String getText() { 092 return content; 093 } 094 095 public String getId() { 096 return getAttributeValue("id"); 097 } 098 099 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 }