1 16 17 package org.apache.xerces.dom; 18 19 import org.apache.xerces.util.URI; 20 import org.w3c.dom.DocumentType ; 21 import org.w3c.dom.EntityReference ; 22 import org.w3c.dom.NamedNodeMap ; 23 import org.w3c.dom.Node ; 24 25 82 public class EntityReferenceImpl 83 extends ParentNode 84 implements EntityReference { 85 86 90 91 static final long serialVersionUID = -7381452955687102062L; 92 93 97 98 protected String name; 99 100 protected String baseURI; 101 102 103 104 106 107 109 113 114 public EntityReferenceImpl(CoreDocumentImpl ownerDoc, String name) { 115 super(ownerDoc); 116 this.name = name; 117 isReadOnly(true); 118 needsSyncChildren(true); 119 } 120 121 125 129 public short getNodeType() { 130 return Node.ENTITY_REFERENCE_NODE; 131 } 132 133 136 public String getNodeName() { 137 if (needsSyncData()) { 138 synchronizeData(); 139 } 140 return name; 141 } 142 143 144 public Node cloneNode(boolean deep) { 145 EntityReferenceImpl er = (EntityReferenceImpl)super.cloneNode(deep); 146 er.setReadOnly(true, deep); 147 return er; 148 } 149 150 158 public String getBaseURI() { 159 if (needsSyncData()) { 160 synchronizeData(); 161 } 162 if (baseURI == null) { 163 DocumentType doctype; 164 NamedNodeMap entities; 165 EntityImpl entDef; 166 if (null != (doctype = getOwnerDocument().getDoctype()) && 167 null != (entities = doctype.getEntities())) { 168 169 entDef = (EntityImpl)entities.getNamedItem(getNodeName()); 170 if (entDef !=null) { 171 return entDef.getBaseURI(); 172 } 173 } 174 } else if (baseURI != null && baseURI.length() != 0 ) { try { 176 return new URI(baseURI).toString(); 177 } 178 catch (org.apache.xerces.util.URI.MalformedURIException e){ 179 return null; 181 } 182 } 183 return baseURI; 184 } 185 186 187 188 public void setBaseURI(String uri){ 189 if (needsSyncData()) { 190 synchronizeData(); 191 } 192 baseURI = uri; 193 } 194 195 202 protected String getEntityRefValue (){ 203 if (needsSyncChildren()){ 204 synchronizeChildren(); 205 } 206 207 String value = ""; 208 if (firstChild != null){ 209 if (firstChild.getNodeType() == Node.ENTITY_REFERENCE_NODE){ 210 value = ((EntityReferenceImpl)firstChild).getEntityRefValue(); 211 } 212 else if (firstChild.getNodeType() == Node.TEXT_NODE){ 213 value = firstChild.getNodeValue(); 214 } 215 else { 216 return null; 218 } 219 220 if (firstChild.nextSibling == null){ 221 return value; 222 } 223 else { 224 StringBuffer buff = new StringBuffer (value); 225 ChildNode next = firstChild.nextSibling; 226 while (next != null){ 227 228 if (next.getNodeType() == Node.ENTITY_REFERENCE_NODE){ 229 value = ((EntityReferenceImpl)next).getEntityRefValue(); 230 } 231 else if (next.getNodeType() == Node.TEXT_NODE){ 232 value = next.getNodeValue(); 233 } 234 else { 235 return null; 237 } 238 buff.append(value); 239 next = next.nextSibling; 240 241 } 242 return buff.toString(); 243 } 244 } 245 return ""; 246 } 247 248 254 protected void synchronizeChildren() { 255 needsSyncChildren(false); 257 258 DocumentType doctype; 259 NamedNodeMap entities; 260 EntityImpl entDef; 261 if (null != (doctype = getOwnerDocument().getDoctype()) && 262 null != (entities = doctype.getEntities())) { 263 264 entDef = (EntityImpl)entities.getNamedItem(getNodeName()); 265 266 if (entDef == null) 268 return; 269 270 isReadOnly(false); 272 for (Node defkid = entDef.getFirstChild(); 273 defkid != null; 274 defkid = defkid.getNextSibling()) { 275 Node newkid = defkid.cloneNode(true); 276 insertBefore(newkid, null); 277 } 278 setReadOnly(true, true); 279 } 280 } 281 282 283 288 public void setReadOnly(boolean readOnly, boolean deep) { 289 290 if (needsSyncData()) { 291 synchronizeData(); 292 } 293 if (deep) { 294 295 if (needsSyncChildren()) { 296 synchronizeChildren(); 297 } 298 for (ChildNode mykid = firstChild; 300 mykid != null; 301 mykid = mykid.nextSibling) { 302 303 mykid.setReadOnly(readOnly,true); 304 305 } 306 } 307 isReadOnly(readOnly); 308 } 310 311 320 321 347 397 398 399 } | Popular Tags |