ElementImpl.java 14.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
/*
 * Decompiled with CFR 0_118.
 */
package com.adobe.xfa.dom;

import com.adobe.xfa.Attribute;
import com.adobe.xfa.Element;
import com.adobe.xfa.dom.*;
import com.adobe.xfa.ut.StringUtils;
import org.w3c.dom.*;

import java.util.Iterator;
import java.util.NoSuchElementException;

class ElementImpl
extends ParentNode
implements org.w3c.dom.Element {
    static final String ALL_NODES = "*";
    private final QNameImpl mQName;
    private AttrImpl mFirstAttr;
    private AttrImpl mLastAttr;
    private int mAttrCount = -1;
    private NamedNodeMapImpl mAttrMap;

    ElementImpl(ParentNode parent, Element newElement) {
        super(parent, newElement);
        this.mQName = new QNameImpl(this.getXFAElement().getNS(), this.getXFAElement().getPrefix(), this.getXFAElement().getLocalName());
        DocumentImpl doc = this.getDocument();
        if (parent == doc) {
            doc.setDocumentElement(this);
        }
    }

    @Override
    public String getAttribute(String name) {
        Attr attr = this.getAttributeNode(name);
        return attr == null ? null : attr.getValue();
    }

    @Override
    public String getAttributeNS(String namespaceURI, String localName) throws DOMException {
        Attr attr = this.getAttributeNodeNS(namespaceURI, localName);
        return attr == null ? null : attr.getValue();
    }

    @Override
    public Attr getAttributeNode(String name) {
        this.populateAttrMap();
        Node result = this.mAttrMap.getNamedItem(name);
        if (result == null) {
            return null;
        }
        assert (result instanceof Attr);
        return (Attr)result;
    }

    @Override
    public Attr getAttributeNodeNS(String namespaceURI, String localName) throws DOMException {
        this.populateAttrMap();
        Node result = this.mAttrMap.getNamedItemNS(namespaceURI, localName);
        if (result == null) {
            return null;
        }
        assert (result instanceof Attr);
        return (Attr)result;
    }

    @Override
    public NodeList getElementsByTagName(String name) {
        name = name.intern();
        NodeListImpl childList = new NodeListImpl(0);
        this.addChildrenByTagName(name, childList);
        return childList;
    }

    @Override
    public NodeList getElementsByTagNameNS(String namespaceURI, String localName) throws DOMException {
        namespaceURI = namespaceURI.intern();
        localName = localName.intern();
        NodeListImpl childList = new NodeListImpl(0);
        this.addChildrenByTagNameNS(namespaceURI, localName, childList);
        return childList;
    }

    @Override
    public TypeInfo getSchemaTypeInfo() {
        return null;
    }

    @Override
    public String getTagName() {
        return this.mQName.getQName();
    }

    @Override
    public boolean hasAttribute(String name) {
        return this.getAttributeNode(name) != null;
    }

    @Override
    public boolean hasAttributeNS(String namespaceURI, String localName) throws DOMException {
        return this.getAttributeNodeNS(namespaceURI, localName) != null;
    }

    @Override
    public void removeAttribute(String name) throws DOMException {
        throw new DOMException(7, "");
    }

    @Override
    public void removeAttributeNS(String namespaceURI, String localName) throws DOMException {
        throw new DOMException(7, "");
    }

    @Override
    public Attr removeAttributeNode(Attr oldAttr) throws DOMException {
        throw new DOMException(7, "");
    }

    @Override
    public void setAttribute(String name, String newValue) throws DOMException {
        throw new DOMException(7, "");
    }

    @Override
    public void setAttributeNS(String namespaceURI, String localName, String newValue) throws DOMException {
        throw new DOMException(7, "");
    }

    @Override
    public Attr setAttributeNode(Attr newAttr) throws DOMException {
        throw new DOMException(7, "");
    }

    @Override
    public Attr setAttributeNodeNS(Attr newAttr) throws DOMException {
        throw new DOMException(7, "");
    }

    @Override
    public void setIdAttribute(String name, boolean isId) throws DOMException {
        throw new DOMException(7, "");
    }

    @Override
    public void setIdAttributeNS(String namespaceURI, String localName, boolean isId) throws DOMException {
        throw new DOMException(7, "");
    }

    @Override
    public void setIdAttributeNode(Attr idAttr, boolean isId) throws DOMException {
        throw new DOMException(7, "");
    }

    @Override
    public NamedNodeMap getAttributes() {
        this.fillAllAttrs();
        NamedNodeMapImpl namedNodeMap = new NamedNodeMapImpl(this.getXFAElement().getNumAttrs());
        namedNodeMap.addNodes(this.mFirstAttr);
        return namedNodeMap;
    }

    @Override
    public String getLocalName() {
        return this.mQName.getLocalName();
    }

    @Override
    public String getNamespaceURI() {
        return this.mQName.getNamespace();
    }

    @Override
    public String getNodeName() {
        return this.mQName.getQName();
    }

    @Override
    public short getNodeType() {
        return 1;
    }

    @Override
    public String getPrefix() {
        return this.mQName.getPrefix();
    }

    @Override
    public String getTextContent() throws DOMException {
        NodeImpl child;
        this.fillAllChildren();
        if (child == null) {
            return null;
        }
        StringBuilder resultBuilder = new StringBuilder();
        for (child = this.getFirstChildImpl(); child != null; child = child.getNext()) {
            String childContent;
            if (child instanceof Comment || child instanceof ProcessingInstruction || (childContent = child.getTextContent()) == null) continue;
            resultBuilder.append(childContent);
        }
        return resultBuilder.toString();
    }

    @Override
    public Object getUserData(String key) {
        return null;
    }

    @Override
    public boolean hasAttributes() {
        return this.getXFAElement().getNumAttrs() > 0;
    }

    @Override
    public boolean isDefaultNamespace(String namespaceURI) {
        return this.testDefaultNamespace(namespaceURI);
    }

    @Override
    public boolean isEqualNode(Node other) {
        int otherAttrCount;
        if (this == other) {
            return true;
        }
        if (!super.isEqualNode(other)) {
            return false;
        }
        if (!(other instanceof ElementImpl)) {
            return false;
        }
        ElementImpl otherElement = (ElementImpl)other;
        NamedNodeMap thisAttrs = this.getAttributes();
        NamedNodeMap otherAttrs = otherElement.getAttributes();
        int thisAttrCount = thisAttrs.getLength();
        if (thisAttrCount != (otherAttrCount = otherAttrs.getLength())) {
            return false;
        }
        for (int i = 0; i < thisAttrCount; ++i) {
            Node thisAttr = thisAttrs.item(i);
            Node otherAttr = null;
            String thisNS = thisAttr.getNamespaceURI();
            otherAttr = StringUtils.isEmpty(thisNS) ? otherAttrs.getNamedItem(thisAttr.getNodeName()) : otherAttrs.getNamedItemNS(thisNS, thisAttr.getLocalName());
            if (otherAttr == null) {
                return false;
            }
            if (thisAttr.isEqualNode(otherAttr)) continue;
            return false;
        }
        return true;
    }

    @Override
    public String lookupNamespaceURI(String prefix) {
        String result = this.doLookupNamespaceURI(prefix);
        return result;
    }

    @Override
    public String lookupPrefix(String namespaceURI) {
        String result = this.doLookupPrefix(namespaceURI, this);
        return result;
    }

    @Override
    public void normalize() {
    }

    @Override
    public void setNodeValue(String newValue) throws DOMException {
        throw new DOMException(7, "");
    }

    @Override
    public void setPrefix(String prefix) throws DOMException {
        throw new DOMException(7, "");
    }

    @Override
    public void setTextContent(String textContent) throws DOMException {
        throw new DOMException(7, "");
    }

    @Override
    public Object setUserData(String key, Object data, UserDataHandler handler) {
        return null;
    }

    String doLookupNamespaceURI(String prefix) {
        String result = null;
        if (prefix != null && (prefix = prefix.intern()) == this.mQName.getPrefix()) {
            result = this.mQName.getNamespace();
        }
        if (result == null) {
            ParentNode parent;
            this.fillAllAttrs();
            for (NodeImpl attrNode = this.mFirstAttr; attrNode != null; attrNode = attrNode.getNext()) {
                assert (attrNode instanceof AttrImpl);
                AttrImpl attr = attrNode;
                if ((!attr.isNamespaceAttr(2) || attr.getLocalName() != prefix) && (prefix != null || !attr.isNamespaceAttr(1))) continue;
                String value = attrNode.getNodeValue();
                result = StringUtils.isEmpty(value) ? null : value;
                break;
            }
            if (result == null && (parent = this.getParent()) instanceof ElementImpl) {
                result = ((ElementImpl)parent).doLookupNamespaceURI(prefix);
            }
        }
        return result;
    }

    String doLookupPrefix(String namespaceURI, ElementImpl originalElement) {
        namespaceURI = namespaceURI.intern();
        String thisNS = this.mQName.getNamespace();
        String thisPrefix = this.mQName.getPrefix();
        if (!StringUtils.isEmpty(thisNS) && thisNS == namespaceURI && namespaceURI == originalElement.lookupNamespaceURI(thisPrefix)) {
            return thisPrefix;
        }
        this.fillAllAttrs();
        for (NodeImpl attrNode = this.mFirstAttr; attrNode != null; attrNode = attrNode.getNext()) {
            assert (attrNode instanceof AttrImpl);
            AttrImpl attr = attrNode;
            String attrLocalName = attr.getLocalName();
            if (!attr.isPopulated() || attr.getPrefix() != "xmlns" || !attr.getNodeValue().equals(namespaceURI) || this.lookupNamespaceURI(attrLocalName) != namespaceURI) continue;
            return attrLocalName;
        }
        ParentNode parent = this.getParent();
        if (parent instanceof ElementImpl) {
            return ((ElementImpl)parent).doLookupPrefix(namespaceURI, originalElement);
        }
        return null;
    }

    Iterator<AttrImpl> getNamespacesInScopeIterator() {
        return new NamespaceIterator(this);
    }

    @Override
    boolean testDefaultNamespace(String namespaceURI) {
        if (StringUtils.isEmpty(this.mQName.getPrefix())) {
            return namespaceURI == this.mQName.getNamespace();
        }
        this.fillAllAttrs();
        for (NodeImpl attrNode = this.mFirstAttr; attrNode != null; attrNode = attrNode.getNext()) {
            assert (attrNode instanceof AttrImpl);
            AttrImpl attr = attrNode;
            if (!attr.isNamespaceAttr(1)) continue;
            return namespaceURI.equals(attrNode.getNodeValue());
        }
        ParentNode parent = this.getParent();
        if (parent != null) {
            return parent.testDefaultNamespace(namespaceURI);
        }
        return false;
    }

    private final void fillAllAttrs() {
        if (this.mAttrCount >= 0) {
            return;
        }
        this.mAttrCount = this.getXFAElement().getNumAttrs();
        for (int i = 0; i < this.mAttrCount; ++i) {
            Attribute xfaAttr = this.getXFAElement().getAttr(i);
            AttrImpl newAttr = new AttrImpl(this, xfaAttr);
            if (newAttr.isNamespaceAttr(3)) {
                newAttr.populate();
            }
            newAttr.setPrev(this.mLastAttr);
            if (this.mFirstAttr == null) {
                this.mFirstAttr = newAttr;
            } else {
                this.mLastAttr.setNext(newAttr);
            }
            this.mLastAttr = newAttr;
        }
    }

    private void addChildrenByTagName(String name, NodeListImpl nodeList) {
        if (name == "*" || name == this.mQName.getQName()) {
            nodeList.addNode(this);
        }
        this.fillAllChildren();
        for (NodeImpl child = this.getFirstChildImpl(); child != null; child = child.getNext()) {
            if (!(child instanceof ElementImpl)) continue;
            ElementImpl childElement = (ElementImpl)child;
            childElement.addChildrenByTagName(name, nodeList);
        }
    }

    private void addChildrenByTagNameNS(String namespaceURI, String localName, NodeListImpl nodeList) {
        if (!(namespaceURI != "*" && this.mQName.getNamespace() != namespaceURI || localName != "*" && this.mQName.getLocalName() != localName)) {
            nodeList.addNode(this);
        }
        this.fillAllChildren();
        for (NodeImpl child = this.getFirstChildImpl(); child != null; child = child.getNext()) {
            if (!(child instanceof ElementImpl)) continue;
            ElementImpl childElement = (ElementImpl)child;
            childElement.addChildrenByTagNameNS(namespaceURI, localName, nodeList);
        }
    }

    private void populateAttrMap() {
        if (this.mAttrMap != null) {
            return;
        }
        this.mAttrMap = new NamedNodeMapImpl(this.getXFAElement().getNumAttrs());
        this.fillAllAttrs();
        for (NodeImpl attr = this.mFirstAttr; attr != null; attr = attr.getNext()) {
            this.mAttrMap.addNode(attr);
        }
    }

    private static class NamespaceIterator
    implements Iterator<AttrImpl> {
        private ElementImpl mElement;
        private AttrImpl mAttr;

        public NamespaceIterator(ElementImpl element) {
            this.mElement = element;
            this.mElement.fillAllAttrs();
            this.mAttr = this.scanToNextNamespaceAttributeInScope(this.mElement.mFirstAttr);
        }

        @Override
        public boolean hasNext() {
            return this.mAttr != null;
        }

        @Override
        public AttrImpl next() {
            if (this.mAttr == null) {
                throw new NoSuchElementException();
            }
            AttrImpl result = this.mAttr;
            this.mAttr = this.scanToNextNamespaceAttributeInScope(this.mAttr.getNext());
            return result;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }

        private AttrImpl scanToNextNamespaceAttributeInScope(NodeImpl currentChild) {
            do {
                for (NodeImpl attrNode = currentChild; attrNode != null; attrNode = attrNode.getNext()) {
                    AttrImpl attr;
                    if (!(attrNode instanceof AttrImpl) || !(attr = (AttrImpl)attrNode).isNamespaceAttr(3)) continue;
                    return attr;
                }
                ParentNode parent = this.mElement.getParent();
                if (!(parent instanceof ElementImpl)) break;
                this.mElement = (ElementImpl)parent;
                this.mElement.fillAllAttrs();
                currentChild = this.mElement.mFirstAttr;
            } while (true);
            return null;
        }
    }

}