001/* 002 * Copyright 2012 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.ametys.core.util.dom; 018 019import java.util.Map; 020 021import org.w3c.dom.Attr; 022import org.w3c.dom.DOMException; 023import org.w3c.dom.Element; 024import org.w3c.dom.NamedNodeMap; 025import org.w3c.dom.Node; 026import org.w3c.dom.NodeList; 027import org.w3c.dom.TypeInfo; 028 029/** 030 * Basic implementation of {@link Element}. 031 */ 032public abstract class AbstractAmetysElement extends AbstractAmetysNode implements Element 033{ 034 /** The parent Element or null if none. */ 035 protected Element _parent; 036 037 private Map<String, AmetysAttribute> _attsMap; 038 private String _tagName; 039 040 /** 041 * Constructor. 042 */ 043 public AbstractAmetysElement() 044 { 045 // empty contructor 046 } 047 048 /** 049 * Constructor. 050 * @param tagName the tag name. 051 */ 052 public AbstractAmetysElement(String tagName) 053 { 054 _tagName = tagName; 055 } 056 057 /** 058 * Constructor. 059 * @param parent the parent {@link Element}, if any. 060 */ 061 public AbstractAmetysElement(Element parent) 062 { 063 _parent = parent; 064 } 065 066 /** 067 * Constructor. 068 * @param tagName the tag name. 069 * @param parent the parent {@link Element}, if any. 070 */ 071 public AbstractAmetysElement(String tagName, Element parent) 072 { 073 _tagName = tagName; 074 _parent = parent; 075 } 076 077 @Override 078 public String getTagName() 079 { 080 return _tagName; 081 } 082 083 @Override 084 public String getNodeName() 085 { 086 return getTagName(); 087 } 088 089 @Override 090 public String getLocalName() 091 { 092 return getTagName(); 093 } 094 095 @Override 096 public short getNodeType() 097 { 098 return Node.ELEMENT_NODE; 099 } 100 101 @Override 102 public Node getParentNode() 103 { 104 return _parent; 105 } 106 107 @Override 108 public NamedNodeMap getAttributes() 109 { 110 if (_attsMap == null) 111 { 112 _attsMap = _lookupAttributes(); 113 } 114 115 return new AmetysNamedNodeMap(_attsMap); 116 } 117 118 @Override 119 public String getAttribute(String name) 120 { 121 if (_attsMap == null) 122 { 123 _attsMap = _lookupAttributes(); 124 } 125 126 Attr attr = _attsMap.get(name); 127 128 if (attr == null || attr.getValue() == null || attr.getValue().isEmpty()) 129 { 130 return ""; 131 } 132 133 return attr.getValue(); 134 } 135 136 @Override 137 public Attr getAttributeNode(String name) 138 { 139 if (_attsMap == null) 140 { 141 _attsMap = _lookupAttributes(); 142 } 143 144 return _attsMap.get(name); 145 } 146 147 @Override 148 public String getAttributeNS(String namespaceURI, String localName) throws DOMException 149 { 150 return getAttribute(localName); 151 } 152 153 @Override 154 public Attr getAttributeNodeNS(String namespaceURI, String localName) throws DOMException 155 { 156 return getAttributeNode(localName); 157 } 158 159 @Override 160 public boolean hasAttributes() 161 { 162 if (_attsMap == null) 163 { 164 _attsMap = _lookupAttributes(); 165 } 166 167 return !_attsMap.isEmpty(); 168 } 169 170 @Override 171 public boolean hasAttribute(String name) 172 { 173 if (_attsMap == null) 174 { 175 _attsMap = _lookupAttributes(); 176 } 177 178 return _attsMap.containsKey(name); 179 } 180 181 @Override 182 public boolean hasAttributeNS(String namespaceURI, String localName) throws DOMException 183 { 184 return hasAttribute(localName); 185 } 186 187 @Override 188 public String getTextContent() throws DOMException 189 { 190 StringBuilder sb = new StringBuilder(); 191 192 NodeList children = getChildNodes(); 193 for (int i = 0; i < children.getLength(); i++) 194 { 195 sb.append(children.item(i).getTextContent()); 196 } 197 198 return sb.toString(); 199 } 200 201 /** 202 * Returns a Map<name, value> corresponding to the attributes.<br> 203 * @return the name/value pairs 204 */ 205 protected abstract Map<String, AmetysAttribute> _lookupAttributes(); 206 207 // Unsupported methods 208 209 @Override 210 public TypeInfo getSchemaTypeInfo() 211 { 212 throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "getSchemaTypeInfo"); 213 } 214 215 @Override 216 public NodeList getElementsByTagName(String name) 217 { 218 throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "getElementsByTagName"); 219 } 220 221 @Override 222 public NodeList getElementsByTagNameNS(String namespaceURI, String localName) throws DOMException 223 { 224 throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "getElementsByTagNameNS"); 225 } 226 227 @Override 228 public void setAttribute(String name, String value) throws DOMException 229 { 230 throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "setAttribute"); 231 } 232 233 @Override 234 public void removeAttribute(String name) throws DOMException 235 { 236 throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "removeAttribute"); 237 } 238 239 @Override 240 public Attr setAttributeNode(Attr newAttr) throws DOMException 241 { 242 throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "setAttributeNode"); 243 } 244 245 @Override 246 public Attr removeAttributeNode(Attr oldAttr) throws DOMException 247 { 248 throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "removeAttributeNode"); 249 } 250 251 @Override 252 public void setAttributeNS(String namespaceURI, String qualifiedName, String value) throws DOMException 253 { 254 throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "setAttributeNS"); 255 } 256 257 @Override 258 public void removeAttributeNS(String namespaceURI, String localName) throws DOMException 259 { 260 throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "removeAttributeNS"); 261 } 262 263 @Override 264 public Attr setAttributeNodeNS(Attr newAttr) throws DOMException 265 { 266 throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "setAttributeNodeNS"); 267 } 268 269 @Override 270 public void setIdAttribute(String name, boolean isId) throws DOMException 271 { 272 throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "setIdAttribute"); 273 } 274 275 @Override 276 public void setIdAttributeNS(String namespaceURI, String localName, boolean isId) throws DOMException 277 { 278 throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "setIdAttributeNS"); 279 } 280 281 @Override 282 public void setIdAttributeNode(Attr idAttr, boolean isId) throws DOMException 283 { 284 throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "setIdAttributeNode"); 285 } 286}