Document.java 39.4 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 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
/*
 * Decompiled with CFR 0_118.
 */
package com.adobe.xfa;

import com.adobe.xfa.*;
import com.adobe.xfa.protocol.ProtocolUtils;
import com.adobe.xfa.ut.*;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;

public class Document
extends Element {
    private final AppModel mAppModel;
    private SaveNameSpaceChecker mChecker;
    private URL mParseFile;
    private String msParseFileName;
    private Model mStartingModel;
    private Element mStartingParent;
    private boolean mbIgnoreAggregating;
    private boolean mbWillDirty = true;
    private final Document mRealDocument;
    private boolean mbIsDefaultDocument;
    private boolean mbAutoUniquifyIDs = true;
    private boolean mbUniquifyIDsOnParse;
    private boolean mbAllDataRootsEmpty;
    private Element mAddedRootData;
    private final QNameCache mQNameCache = new QNameCache();
    private final IdentityHashMap<QName, String> mElementToIdAttrNameMap = new IdentityHashMap();
    private final IdentityHashMap<String, String> mNameSpaceToIdAttrNameMap = new IdentityHashMap();
    private final List<QName> mGlobalXMLIdAttrList = new ArrayList<QName>();
    private final IdentityHashMap<QName, KeySpec> mElementToPKeySpecMap = new IdentityHashMap();
    private final Map<String, Element> mXMLIdIndex = new HashMap<String, Element>();
    private final IdentityHashMap<String, Map<String, Element>> mXFAIdIndexes = new IdentityHashMap();
    private final Map<Key, Element> mPKeyIndex = new HashMap<Key, Element>();
    private final IdentityHashMap<String, String> mSimpleNameSpaceMap = new IdentityHashMap();
    public static final String Encoding = "UTF-8";
    public static final byte[] MarkupAttrMiddle = new byte[]{61, 34};
    public static final byte[] MarkupAttrMiddleQuote = new byte[]{61, 39};
    static final byte[] MarkupColon = new byte[]{58};
    public static final byte[] MarkupDQuoteString = new byte[]{34};
    public static final byte[] MarkupQuoteString = new byte[]{39};
    public static final byte[] MarkupCDATAStart = new byte[]{60, 33, 91, 67, 68, 65, 84, 65, 91};
    public static final byte[] MarkupCDATAEnd = new byte[]{93, 93, 62};
    static final byte[] MarkupCommentStart = new byte[]{60, 33, 45, 45};
    static final byte[] MarkupCommentEnd = new byte[]{45, 45, 62};
    public static final byte[] MarkupCloseTag = new byte[]{60, 47};
    public static final byte[] MarkupDocType = new byte[]{60, 33, 68, 79, 67, 60, 84, 89, 80, 69};
    public static final byte[] MarkupEndEntity = new byte[]{34, 62};
    public static final byte[] MarkupEndParen2 = new byte[]{93, 62};
    public static final byte[] MarkupEndTag = new byte[]{62};
    static final byte[] MarkupEndTag2 = new byte[]{47, 62};
    public static final byte[] MarkupEntity = new byte[]{60, 33, 69, 78, 84, 73, 84, 89};
    public static final byte[] MarkupPIStart = new byte[]{60, 63};
    public static final byte[] MarkupPIEnd = new byte[]{63, 62};
    static final byte[] MarkupPrefix = new byte[]{60, 63, 120, 109, 108, 32, 118, 101, 114, 115, 105, 111, 110, 61, 34, 49, 46, 48, 34, 32, 101, 110, 99, 111, 100, 105, 110, 103, 61, 34};
    public static final byte[] MarkupReturn = new byte[]{10};
    public static final byte[] MarkupSpace = new byte[]{32};
    public static final byte[] MarkupStartParen = new byte[]{91};
    public static final byte[] MarkupStartTag = new byte[]{60};
    public static final byte[] MarkupSystem = new byte[]{83, 89, 83, 84, 69, 77};
    static final byte[] MarkupXMLns = new byte[]{120, 109, 108, 110, 115};
    private static final int DEFAULT_XMLDECL_BUFFER_SIZE = 64;

    private Document(AppModel appModel, boolean aliasExistingAppModelDocument) {
        super(null, null, "", "");
        if (appModel == null) {
            throw new NullPointerException("appModel");
        }
        this.mAppModel = appModel;
        if (this.mAppModel.getDocument() == null) {
            this.mRealDocument = this;
            this.mAppModel.setDocument(this);
        } else {
            this.mRealDocument = aliasExistingAppModelDocument ? this.mAppModel.getDocument() : this;
        }
        this.setDocument(this);
        this.setModel(this.mAppModel);
        this.mbIsDefaultDocument = true;
        this.declareGlobalXMLId("http://www.w3.org/XML/1998/namespace", "id");
    }

    public Document(AppModel appModel) {
        this(appModel, true);
    }

    public static Document createDocument(AppModel appModel) {
        return new Document(appModel, false);
    }

    @Override
    public void appendChild(Node child) {
        if (child instanceof Element && this.getDocumentElement() != null) {
            throw new ExFull(ResId.HierarchyRequestException);
        }
        super.appendChild(child);
    }

    public final boolean autoUniquifyIDs() {
        return this.mbAutoUniquifyIDs;
    }

    public final void autoUniquifyIDs(boolean bAutoUniquifyIDs) {
        this.mbAutoUniquifyIDs = bAutoUniquifyIDs;
    }

    public final boolean uniquifyIDsOnParse() {
        return this.mbUniquifyIDsOnParse;
    }

    public final void uniquifyIDsOnParse(boolean bUniquifyIDsOnParse) {
        this.mbUniquifyIDsOnParse = bUniquifyIDsOnParse;
    }

    public final void clearIdMap() {
        this.mXMLIdIndex.clear();
        for (Map<String, Element> index : this.mXFAIdIndexes.values()) {
            index.clear();
        }
        this.mPKeyIndex.clear();
    }

    private String stripVersionFromNameSpace(String aNameSpaceURI) {
        String aSimpleNameSpace = this.mSimpleNameSpaceMap.get(aNameSpaceURI);
        if (aSimpleNameSpace == null) {
            String sNameSpaceURI = aNameSpaceURI == null ? "" : aNameSpaceURI;
            int len = sNameSpaceURI.length();
            aSimpleNameSpace = sNameSpaceURI.length() >= 4 && sNameSpaceURI.charAt(len - 1) == '/' && Character.isDigit(sNameSpaceURI.charAt(len - 2)) && sNameSpaceURI.charAt(len - 3) == '.' && Character.isDigit(sNameSpaceURI.charAt(len - 4)) ? sNameSpaceURI.substring(0, len - 4).intern() : aNameSpaceURI;
            this.mSimpleNameSpaceMap.put(aNameSpaceURI, aSimpleNameSpace);
        }
        return aSimpleNameSpace;
    }

    public final Element getElementByXMLId(String idValue) {
        return this.mXMLIdIndex.get(idValue);
    }

    public final Element getElementByXFAId(String aNameSpaceURI, String aIdValue) {
        if (aNameSpaceURI == "") {
            for (Map<String, Element> index : this.mXFAIdIndexes.values()) {
                Element element = index.get(aIdValue);
                if (element == null) continue;
                return element;
            }
            return null;
        }
        String aSimpleNameSpaceURI = this.stripVersionFromNameSpace(aNameSpaceURI);
        Map<String, Element> index = this.mXFAIdIndexes.get(aSimpleNameSpaceURI);
        return index != null ? index.get(aIdValue) : null;
    }

    public final Element getElementByPKey(Key key) {
        return this.mPKeyIndex.get(key);
    }

    private static boolean referencedByHostDocument(Element element) {
        Element parent;
        while ((parent = Element.getXMLParent(element)) != null) {
            element = parent;
        }
        return element instanceof Document || element.getIsDataWindowRoot();
    }

    private static void indexHelper(Map<String, Element> index, Attribute attr, Element element) {
        assert (index != null);
        Element existing = index.get(attr.getAttrValue());
        if (existing == null) {
            index.put(attr.getAttrValue(), element);
        } else if (existing != element) {
            String sOriginalValue = attr.getAttrValue();
            int i = 1;
            do {
                String sTest;
                if (!index.containsKey(sTest = sOriginalValue + "_copy" + i)) {
                    Attribute newValue = attr.newAttribute(attr.getNS(), attr.getLocalName(), attr.getQName(), sTest, false);
                    element.updateAttributeInternal(newValue);
                    index.put(sTest, element);
                    break;
                }
                ++i;
            } while (true);
        }
    }

    public final void declareXMLId(String aElementNameSpaceURI, String aElementName, String aIdAttributeName) {
        QName elementQName = this.mQNameCache.getQName(aElementNameSpaceURI, aElementName);
        String aExisting = this.mElementToIdAttrNameMap.get(elementQName);
        if (aExisting == null) {
            this.mElementToIdAttrNameMap.put(elementQName, aIdAttributeName);
        } else assert (aExisting == aIdAttributeName);
    }

    public final void declareGlobalXMLId(String aIdAttrNameSpaceURI, String aIdAttrName) {
        for (int i = 0; i < this.mGlobalXMLIdAttrList.size(); ++i) {
            QName test = this.mGlobalXMLIdAttrList.get(i);
            if (test.maNameSpaceURI != aIdAttrNameSpaceURI || test.maLocalName != aIdAttrName) continue;
            return;
        }
        QName newQName = this.mQNameCache.getQName(aIdAttrNameSpaceURI, aIdAttrName);
        this.mGlobalXMLIdAttrList.add(newQName);
    }

    public final void declareXFAId(String aNameSpaceURI, String aIdAttributeName) {
        String aSimpleNameSpaceURI = this.stripVersionFromNameSpace(aNameSpaceURI);
        String aExisting = this.mNameSpaceToIdAttrNameMap.get(aSimpleNameSpaceURI);
        if (aExisting == null) {
            this.mNameSpaceToIdAttrNameMap.put(aSimpleNameSpaceURI, aIdAttributeName);
            assert (this.mXFAIdIndexes.get(aSimpleNameSpaceURI) == null);
            this.mXFAIdIndexes.put(aSimpleNameSpaceURI, new HashMap());
        } else assert (aExisting == aIdAttributeName);
    }

    public final void declarePKey(String aElementNameSpaceURI, String aElementName, String aPKeyNodeAddressList, Element namespaceContextNode) {
        QName elementQName = this.mQNameCache.getQName(aElementNameSpaceURI, aElementName);
        KeySpec keySpec = this.mElementToPKeySpecMap.get(elementQName);
        if (keySpec == null) {
            keySpec = new KeySpec(namespaceContextNode);
            StringTokenizer tokenizer = new StringTokenizer(aPKeyNodeAddressList, ",");
            while (tokenizer.hasMoreTokens()) {
                keySpec.mNodeAddresses.add(tokenizer.nextToken().trim());
            }
            this.mElementToPKeySpecMap.put(elementQName, keySpec);
        }
    }

    public final boolean isId(String aElementNameSpaceURI, String aElementName, String aAttrNameSpaceURI, String aAttrLocalName) {
        String aSimpleNameSpaceURI = this.stripVersionFromNameSpace(aElementNameSpaceURI);
        if (this.mNameSpaceToIdAttrNameMap.get(aSimpleNameSpaceURI) == aAttrLocalName) {
            return true;
        }
        if (this.mElementToIdAttrNameMap.size() > 0) {
            QName elementQName = this.mQNameCache.getExistingQName(aElementNameSpaceURI, aElementName);
            if (elementQName != null && this.mElementToIdAttrNameMap.get(elementQName) == aAttrLocalName) {
                return true;
            }
            elementQName = this.mQNameCache.getExistingQName("", aElementName);
            if (elementQName != null && this.mElementToIdAttrNameMap.get(elementQName) == aAttrLocalName) {
                return true;
            }
        }
        for (int i = 0; i < this.mGlobalXMLIdAttrList.size(); ++i) {
            QName qName = this.mGlobalXMLIdAttrList.get(i);
            if (qName.maNameSpaceURI != aAttrNameSpaceURI || qName.maLocalName != aAttrLocalName) continue;
            return true;
        }
        return false;
    }

    @Override
    public final Node clone(Element parent) {
        MsgFormatPos oMessage = new MsgFormatPos(ResId.InvalidMethodException, this.getClassAtom());
        oMessage.format("clone");
        throw new ExFull(oMessage);
    }

    public final Node cloneNode(boolean bDeep) {
        Document newDoc = new Document(this.mAppModel, false);
        if (bDeep) {
            for (Node child = this.getFirstXMLChild(); child != null; child = child.getNextXMLSibling()) {
                newDoc.appendChild(newDoc.importNode(child, true, null, null));
            }
        }
        return newDoc;
    }

    public final Element createElementNS(String sNameSpaceURI, String sQualifiedName, Element parent) {
        Node.doQualifyNodeName(sQualifiedName, false);
        Element newElement = new Element(parent, null, sNameSpaceURI, sQualifiedName);
        newElement.setDocument(this);
        if (parent != null) {
            parent.appendChild(newElement, true);
        }
        Document.checkValidNameSpace(newElement, sQualifiedName);
        return newElement;
    }

    private static String findEncoding(InputStream is) {
        assert (is.markSupported() || is instanceof MarkableInputStream);
        try {
            String sCharset;
            byte[] buf = new byte[64];
            is.mark(buf.length);
            int nBuf = is.read(buf, 0, buf.length);
            is.reset();
            int offset = 0;
            if (nBuf < 3) {
                return null;
            }
            if (buf[0] == -17 && buf[1] == -69 && buf[2] == -65) {
                offset = 3;
            } else if (buf[0] != 60 || buf[1] != 63) {
                return null;
            }
            int end = -1;
            for (int i = offset; i < nBuf; ++i) {
                if (buf[i] != 62) continue;
                end = i;
                break;
            }
            if (end == -1) {
                return null;
            }
            String sXMLDecl = new String(buf, offset, end, "UTF-8");
            offset = sXMLDecl.indexOf("encoding");
            if (offset == -1) {
                return null;
            }
            offset += "encoding".length();
            while (offset < sXMLDecl.length() && Document.isXMLSpace(sXMLDecl.charAt(offset))) {
                ++offset;
            }
            if (offset == sXMLDecl.length()) {
                return null;
            }
            if (sXMLDecl.charAt(offset) != '=') {
                return null;
            }
            ++offset;
            while (offset < sXMLDecl.length() && Document.isXMLSpace(sXMLDecl.charAt(offset))) {
                ++offset;
            }
            if (offset == sXMLDecl.length()) {
                return null;
            }
            char quote = sXMLDecl.charAt(offset);
            if (quote != '\'' && quote != '\"') {
                return null;
            }
            if ((sCharset = sXMLDecl.substring(++offset, end = sXMLDecl.indexOf(quote, offset))).equalsIgnoreCase("SHIFT-JIS")) {
                return "SHIFT_JIS";
            }
        }
        catch (IOException e) {
            throw new ExFull(e);
        }
        return null;
    }

    private static boolean isXMLSpace(char c) {
        return c == ' ' || c == '\t' || c == '\r' || c == '\n';
    }

    @Override
    public Node getFirstXMLChild() {
        return this.mRealDocument.mFirstXMLChild;
    }

    @Override
    public AppModel getAppModel() {
        return this.mAppModel;
    }

    public final Element getDocumentElement() {
        for (Node child = this.getFirstXMLChild(); child != null; child = child.getNextXMLSibling()) {
            if (!(child instanceof Element)) continue;
            return (Element)child;
        }
        return null;
    }

    public Generator getGenerator() {
        return null;
    }

    @Override
    public String getName() {
        return "#document";
    }

    public URL getParseFile() {
        return this.mRealDocument.mParseFile;
    }

    public String getParseFileName() {
        return this.mRealDocument.msParseFileName;
    }

    @Override
    public boolean getWillDirty() {
        return this.mRealDocument.mbWillDirty;
    }

    public final SaveNameSpaceChecker getSaveChecker() {
        return this.mRealDocument.mChecker;
    }

    public final boolean isDefaultDocument() {
        return this.mRealDocument.mbIsDefaultDocument;
    }

    public final void isDefaultDocument(boolean bIsDefault) {
        this.mRealDocument.mbIsDefaultDocument = bIsDefault;
    }

    public boolean isAllDataRootsEmpty() {
        return this.mbAllDataRootsEmpty;
    }

    public void setAllDataRootsEmpty(boolean AllDataRootsEmpty) {
        this.mbAllDataRootsEmpty = AllDataRootsEmpty;
    }

    public Element getAddedRootData() {
        return this.mAddedRootData;
    }

    public void setAddedRootData(Element AddedRootData) {
        this.mAddedRootData = AddedRootData;
    }

    public final Node importNode(Node source, boolean bDeep) {
        return this.importNode(source, bDeep, null, null);
    }

    private final Node importNode(Node source, boolean bDeep, Element parent, Node previousSibling) {
        if (source == null) {
            return null;
        }
        if (source instanceof Element.DualDomNode) {
            source = ((Element.DualDomNode)((Object)source)).getXmlPeer();
        }
        if (source instanceof Document) {
            throw new ExFull(ResId.UNSUPPORTED_OPERATION, "Document#importNode - document child");
        }
        if (source instanceof Element) {
            Element elementSource = (Element)source;
            Element newElement = new Element(parent, previousSibling, elementSource.getNS(), elementSource.getLocalName(), elementSource.getXMLName(), null, XFA.INVALID_ELEMENT, "");
            newElement.setDocument(this);
            newElement.setModel(this.mAppModel);
            int n = elementSource.getNumAttrs();
            for (int i = 0; i < n; ++i) {
                Attribute attr = elementSource.getAttr(i);
                newElement.setAttribute(attr.getNS(), attr.getQName(), attr.getLocalName(), attr.getAttrValue(), false);
            }
            if (source instanceof ModelPeer && !(source instanceof Model.DualDomModel)) {
                source = ((ModelPeer)source).getXfaPeer();
            }
            if (bDeep) {
                Node prevSibling = null;
                for (Node child = source.getFirstXMLChild(); child != null; child = child.getNextXMLSibling()) {
                    prevSibling = this.importNode(child, true, newElement, prevSibling);
                }
            }
            return newElement;
        }
        if (source instanceof TextNode) {
            return new TextNode(parent, previousSibling, ((TextNode)source).getText());
        }
        if (source instanceof Chars) {
            return new Chars(parent, previousSibling, ((Chars)source).getText());
        }
        if (source instanceof ProcessingInstruction) {
            ProcessingInstruction pi = (ProcessingInstruction)source;
            return new ProcessingInstruction(parent, previousSibling, pi.getName(), pi.getData());
        }
        if (source instanceof Comment) {
            return new Comment(parent, previousSibling, ((Comment)source).getData());
        }
        throw new ExFull(ResId.UNSUPPORTED_OPERATION, "Document.importNode - unknown type");
    }

    public final boolean hasChanged() {
        return this.mRealDocument.isDirty();
    }

    final void indexNode(Element element, boolean bInDocumentCheckCompleted) {
        int index;
        Key primaryKey;
        int index2;
        QName elementLocalName;
        if (element instanceof Element.DualDomNode) {
            Node node = ((Element.DualDomNode)((Object)element)).getXmlPeer();
            if (node instanceof TextNode) {
                return;
            }
            element = (Element)node;
        }
        if (!bInDocumentCheckCompleted && !Document.referencedByHostDocument(element)) {
            return;
        }
        Attribute attr = null;
        String aElementNameSpaceURI = this.stripVersionFromNameSpace(element.getNS());
        String aXFAIdAttrName = this.mNameSpaceToIdAttrNameMap.get(aElementNameSpaceURI);
        if (aXFAIdAttrName != null && (index2 = element.findAttr("", aXFAIdAttrName)) != -1) {
            attr = element.getAttr(index2);
        }
        if (attr != null) {
            Document.indexHelper(this.mXFAIdIndexes.get(aElementNameSpaceURI), attr, element);
        }
        attr = null;
        String aXMLIdAttrName = null;
        QName elementQName = this.mQNameCache.getExistingQName(aElementNameSpaceURI, element.getLocalName());
        if (elementQName != null) {
            aXMLIdAttrName = this.mElementToIdAttrNameMap.get(elementQName);
        }
        if (aXMLIdAttrName == null && (elementLocalName = this.mQNameCache.getExistingQName("", element.getLocalName())) != null) {
            aXMLIdAttrName = this.mElementToIdAttrNameMap.get(elementLocalName);
        }
        if (aXMLIdAttrName != null && (index = element.findAttr("", aXMLIdAttrName)) != -1) {
            attr = element.getAttr(index);
        }
        if (attr != null) {
            Document.indexHelper(this.mXMLIdIndex, attr, element);
        }
        for (int i = 0; i < this.mGlobalXMLIdAttrList.size(); ++i) {
            QName xmlIdAttrQName = this.mGlobalXMLIdAttrList.get(i);
            int index3 = element.findAttr(xmlIdAttrQName.maNameSpaceURI, xmlIdAttrQName.maLocalName);
            if (index3 != -1) {
                attr = element.getAttr(index3);
            }
            if (attr == null) continue;
            Document.indexHelper(this.mXMLIdIndex, attr, element);
        }
        KeySpec primaryKeySpec = this.mElementToPKeySpecMap.get(elementQName);
        if (primaryKeySpec != null && (primaryKey = element.constructKey(primaryKeySpec.mNodeAddresses, primaryKeySpec.mNamespaceContextNode)).numValues() > 0) {
            Element existing = this.mPKeyIndex.get(primaryKey);
            if (existing == null) {
                this.mPKeyIndex.put(primaryKey, element);
            } else assert (existing == element);
        }
    }

    public final void indexSubtree(Element source, boolean bInDocumentCheckCompleted) {
        if (!bInDocumentCheckCompleted && !Document.referencedByHostDocument(source)) {
            return;
        }
        this.indexNode(source, true);
        for (Node child = source.getFirstXMLChild(); child != null; child = child.getNextXMLSibling()) {
            if (!(child instanceof Element)) continue;
            this.indexSubtree((Element)child, true);
        }
    }

    final void deindexNode(Element element, boolean bInDocumentCheckCompleted) {
        int index;
        Key primaryKey;
        int index2;
        QName elementLocalName;
        if (element instanceof Element.DualDomNode) {
            Node node = ((Element.DualDomNode)((Object)element)).getXmlPeer();
            if (node instanceof TextNode) {
                return;
            }
            element = (Element)node;
        }
        if (!bInDocumentCheckCompleted && !Document.referencedByHostDocument(element)) {
            return;
        }
        Attribute attr = null;
        String aElementNameSpaceURI = this.stripVersionFromNameSpace(element.getNSInternal());
        String aXFAIdAttrName = this.mNameSpaceToIdAttrNameMap.get(aElementNameSpaceURI);
        if (aXFAIdAttrName != null && (index2 = element.findAttr("", aXFAIdAttrName)) != -1) {
            attr = element.getAttr(index2);
        }
        if (attr != null) {
            this.mXFAIdIndexes.get(aElementNameSpaceURI).remove(attr.getAttrValue());
        }
        attr = null;
        String aXMLIdAttrName = null;
        QName elementQName = this.mQNameCache.getExistingQName(aElementNameSpaceURI, element.getLocalName());
        if (elementQName != null) {
            aXMLIdAttrName = this.mElementToIdAttrNameMap.get(elementQName);
        }
        if (aXMLIdAttrName == null && (elementLocalName = this.mQNameCache.getExistingQName("", element.getLocalName())) != null) {
            aXMLIdAttrName = this.mElementToIdAttrNameMap.get(elementLocalName);
        }
        if (aXMLIdAttrName != null && (index = element.findAttr("", aXMLIdAttrName)) != -1) {
            attr = element.getAttr(index);
        }
        if (attr != null) {
            this.mXMLIdIndex.remove(attr.getAttrValue());
        }
        for (int i = 0; i < this.mGlobalXMLIdAttrList.size(); ++i) {
            QName xmlIdAttrQName = this.mGlobalXMLIdAttrList.get(i);
            int index3 = element.findAttr(xmlIdAttrQName.maNameSpaceURI, xmlIdAttrQName.maLocalName);
            if (index3 != -1) {
                attr = element.getAttr(index3);
            }
            if (attr == null) continue;
            this.mXMLIdIndex.remove(attr.getAttrValue());
        }
        KeySpec primaryKeySpec = this.mElementToPKeySpecMap.get(elementQName);
        if (primaryKeySpec != null && (primaryKey = element.constructKey(primaryKeySpec.mNodeAddresses, primaryKeySpec.mNamespaceContextNode)).numValues() > 0) {
            this.mPKeyIndex.remove(primaryKey);
        }
    }

    public final void deindexSubtree(Element source, boolean bInDocumentCheckCompleted) {
        if (!bInDocumentCheckCompleted && !Document.referencedByHostDocument(source)) {
            return;
        }
        this.deindexNode(source, true);
        for (Node child = source.getFirstXMLChild(); child != null; child = child.getNextXMLSibling()) {
            if (!(child instanceof Element)) continue;
            this.deindexSubtree((Element)child, true);
        }
    }

    public final boolean idValueInUse(String idValue) {
        if (this.mXMLIdIndex.get(idValue) != null) {
            return true;
        }
        for (Map<String, Element> index : this.mXFAIdIndexes.values()) {
            if (index == null || !index.containsKey(idValue)) continue;
            return true;
        }
        return false;
    }

    public final boolean isIncrementalLoad() {
        return false;
    }

    public final void load(InputStream is, String encoding, boolean parseExternalDTD) {
        this.load(is, null, encoding, parseExternalDTD);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public final void load(File file) {
        try {
            FileInputStream is = null;
            try {
                is = new FileInputStream(file);
                BufferedInputStream in = new BufferedInputStream(is);
                this.load(in, file.toString(), null, false);
            }
            finally {
                if (is != null) {
                    is.close();
                }
            }
        }
        catch (IOException e) {
            throw new ExFull(e);
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public final void load(InputStream is, String source, String encoding, boolean parseExternalDTD) {
        this.mRealDocument.setParseFileName(source);
        this.setWillDirty(false);
        try {
            SaxHandler handler = new SaxHandler(this.mRealDocument);
            if (this.mStartingModel != null) {
                handler.setContext(this.mRealDocument.mStartingModel, this.mRealDocument.mStartingParent, this.mRealDocument.mbIgnoreAggregating);
            }
            if (encoding == null) {
                is = Document.makeInputStreamSupportMark(is);
                encoding = Document.findEncoding(is);
            }
            this.launchSAX(handler, is, encoding, parseExternalDTD);
        }
        finally {
            this.setWillDirty(true);
        }
        this.isDefaultDocument(false);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public final Element loadIntoDocument(InputStream is) {
        this.setWillDirty(false);
        try {
            Element root = this.createElementNS("", "dummy", null);
            SaxHandler handler = new SaxHandler(this.mRealDocument);
            handler.setContext(null, root, false);
            is = Document.makeInputStreamSupportMark(is);
            String encoding = Document.findEncoding(is);
            this.launchSAX(handler, is, encoding, false);
            Element element = root;
            return element;
        }
        finally {
            this.setWillDirty(true);
        }
    }

    public final Element loadToNextElement(IntegerHolder depth) {
        return null;
    }

    private void launchSAX(SaxHandler h, InputStream is, String encoding, boolean parseExtrnlDTD) {
        try {
            SAXParser p = SAXParserFactory.newInstance().newSAXParser();
            XMLReader r = p.getXMLReader();
            r.setFeature("http://xml.org/sax/features/namespaces", true);
            r.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", parseExtrnlDTD);
            if (!parseExtrnlDTD) {
                r.setFeature("http://xml.org/sax/features/validation", false);
            }
            r.setContentHandler(h);
            r.setErrorHandler(h);
            r.setProperty("http://xml.org/sax/properties/lexical-handler", h);
            InputSource in = new InputSource(is);
            if (encoding != null) {
                in.setEncoding(encoding);
            }
            r.parse(in);
        }
        catch (SAXException s) {
            MsgFormatPos msg = new MsgFormatPos(ResId.EXPAT_ERROR);
            msg.format(s.getMessage());
            throw new ExFull(msg);
        }
        catch (ParserConfigurationException p) {
            throw new ExFull(p);
        }
        catch (IOException e) {
            throw new ExFull(e);
        }
    }

    public final void savePreamble(OutputStream os, DOMSaveOptions options) {
        try {
            if (!options.getExcludePreamble()) {
                os.write(MarkupPrefix);
                os.write("UTF-8".getBytes("UTF-8"));
                os.write(MarkupDQuoteString);
                os.write(MarkupPIEnd);
                if (options.getDisplayFormat() != 0 || options.getFormatOutside()) {
                    os.write(MarkupReturn);
                }
                options.setExcludePreamble(true);
            }
        }
        catch (IOException e) {
            throw new ExFull(e);
        }
    }

    public void preSave() {
        throw new ExFull(ResId.UNSUPPORTED_OPERATION, "Document#preSave");
    }

    @Deprecated
    @Override
    public final void saveXML(OutputStream os, DOMSaveOptions options) {
        boolean bClearChecker = false;
        if (this.mRealDocument.mChecker == null) {
            this.mRealDocument.mChecker = new SaveNameSpaceChecker(this);
            bClearChecker = true;
        }
        try {
            this.mRealDocument.saveXMLInternal(os, options);
        }
        catch (IOException ex) {
            throw new ExFull(ex);
        }
        finally {
            if (bClearChecker) {
                this.mRealDocument.mChecker = null;
            }
        }
    }

    private void saveXMLInternal(OutputStream os, DOMSaveOptions options) throws IOException {
        Node prevChild = null;
        for (Node child = this.getFirstXMLChild(); child != null; child = child.getNextXMLSibling()) {
            child.serialize(os, options, 0, prevChild);
            prevChild = child;
        }
    }

    public final void saveAs(OutputStream os, Node startNode, DOMSaveOptions options) {
        block8 : {
            assert (startNode == null || startNode.getXMLParent() == null || startNode.getOwnerDocument().mRealDocument == this /* !! */ .mRealDocument);
            options = this /* !! */ .prepareSaveOptions(os, startNode, options);
            try {
                assert (this /* !! */ .mRealDocument.mChecker == null);
                this /* !! */ .mRealDocument.mChecker = new SaveNameSpaceChecker(startNode != null ? startNode : this /* !! */ );
                if (startNode != null && startNode != this /* !! */ ) {
                    Node xmlDomStartNode = startNode instanceof Element.DualDomNode ? ((Element.DualDomNode)((Object)startNode)).getXmlPeer() : startNode;
                    Node previousSibling = xmlDomStartNode.getPreviousXMLSibling();
                    startNode.serialize(os, options, 0, previousSibling);
                    break block8;
                }
                this /* !! */ .mRealDocument.saveXMLInternal(os, options);
            }
            catch (IOException ex) {
                throw new ExFull(ex);
            }
            finally {
                this /* !! */ .mRealDocument.mChecker = null;
            }
        }
    }

    private DOMSaveOptions prepareSaveOptions(OutputStream os, Node startNode, DOMSaveOptions options) {
        DOMSaveOptions tweakedOptions = options == null ? new DOMSaveOptions() : new DOMSaveOptions(options);
        this.savePreamble(os, tweakedOptions);
        if (tweakedOptions.getIncludeDTD()) {
            Node node = startNode != null && startNode != this ? startNode : this.getFirstXMLChild();
            boolean bEmitCR = tweakedOptions.getDisplayFormat() != 0;
            this.saveDTD(os, node, bEmitCR);
        }
        return tweakedOptions;
    }

    private void saveDTD(OutputStream os, Node oNode, boolean bEmitCr) {
        throw new ExFull(ResId.UNSUPPORTED_OPERATION, "Document#saveDTD");
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    @Override
    public void serialize(OutputStream outFile, DOMSaveOptions options, int level, Node prevSibling) throws IOException {
        boolean bClearChecker = false;
        if (this.mRealDocument.mChecker == null) {
            this.mRealDocument.mChecker = new SaveNameSpaceChecker(this);
            bClearChecker = true;
        }
        try {
            options = this.prepareSaveOptions(outFile, this, options);
            this.mRealDocument.saveXMLInternal(outFile, options);
        }
        finally {
            if (bClearChecker) {
                this.mRealDocument.mChecker = null;
            }
        }
    }

    public final void setContext(Model model, Element parent, boolean bIgnoreAggregating) {
        this.mRealDocument.mStartingModel = model;
        this.mRealDocument.mStartingParent = parent;
        this.mRealDocument.mbIgnoreAggregating = bIgnoreAggregating;
    }

    public final void setParseFileName(String source) {
        URL url = null;
        if (source != null) {
            try {
                File file = new File(source);
                if (file.exists()) {
                    url = file.toURI().toURL();
                }
            }
            catch (IOException ignored) {
                // empty catch block
            }
            if (url == null) {
                try {
                    url = new URL(source);
                }
                catch (MalformedURLException ignored) {
                    // empty catch block
                }
            }
            if (url == null) {
                throw new IllegalArgumentException("source");
            }
        }
        this.mRealDocument.msParseFileName = source;
        this.mRealDocument.mParseFile = url;
    }

    @Override
    public void setWillDirty(boolean bWillDirty) {
        this.mRealDocument.mbWillDirty = bWillDirty;
    }

    private static InputStream makeInputStreamSupportMark(InputStream is) {
        return is.markSupported() ? is : new MarkableInputStream(is);
    }

    private static final class MarkableInputStream
    extends InputStream {
        private final InputStream mInputStream;
        private byte[] mBuffer;
        private int mOffset;
        private int mLength;

        public MarkableInputStream(InputStream is) {
            this.mInputStream = is;
        }

        @Override
        public int read() throws IOException {
            this.preLoadBuffer();
            if (this.mBuffer != null) {
                if (this.mOffset == this.mLength) {
                    this.disposeBuffer();
                } else {
                    return this.mBuffer[this.mOffset++] & 255;
                }
            }
            return this.mInputStream.read();
        }

        @Override
        public int read(byte[] b, int off, int len) throws IOException {
            this.preLoadBuffer();
            if (this.mBuffer != null) {
                if (this.mOffset == this.mLength) {
                    this.disposeBuffer();
                } else {
                    int bytesRead = Math.min(this.mLength - this.mOffset, len);
                    System.arraycopy(this.mBuffer, this.mOffset, b, off, bytesRead);
                    this.mOffset += bytesRead;
                    return bytesRead;
                }
            }
            return this.mInputStream.read(b, off, len);
        }

        private void preLoadBuffer() throws IOException {
            if (this.mOffset == 0 && this.mBuffer == null) {
                this.mBuffer = new byte[64];
                int nRead = ProtocolUtils.read(this.mInputStream, this.mBuffer);
                this.mLength = nRead < 0 ? 0 : nRead;
            }
        }

        private void disposeBuffer() {
            this.mBuffer = null;
            this.mLength = -1;
            this.mOffset = -1;
        }

        @Override
        public long skip(long n) throws IOException {
            if (n <= 0) {
                return 0;
            }
            this.preLoadBuffer();
            if (this.mBuffer != null) {
                int bytesLeft = this.mLength - this.mOffset;
                if (n >= (long)bytesLeft) {
                    this.disposeBuffer();
                    return (long)bytesLeft + this.mInputStream.skip(n - (long)bytesLeft);
                }
                this.mOffset = (int)((long)this.mOffset + n);
                return n;
            }
            return this.mInputStream.skip(n);
        }

        @Override
        public int available() throws IOException {
            this.preLoadBuffer();
            if (this.mBuffer != null) {
                int bytesLeft = this.mLength - this.mOffset;
                return bytesLeft == 0 ? this.mInputStream.available() : bytesLeft;
            }
            return this.mInputStream.available();
        }

        @Override
        public void mark(int howMuch) {
            assert (this.mOffset == 0);
            assert (howMuch == 64);
        }

        @Override
        public void reset() {
            assert (this.mBuffer != null);
            this.mOffset = 0;
        }

        @Override
        public boolean markSupported() {
            return false;
        }

        @Override
        public void close() throws IOException {
            this.mInputStream.close();
        }
    }

    private static class KeySpec {
        final List<String> mNodeAddresses = new ArrayList<String>();
        final Element mNamespaceContextNode;

        KeySpec(Element namespaceContextNode) {
            this.mNamespaceContextNode = namespaceContextNode;
        }
    }

    private static class QNameCache {
        private static final List<QName> mCache = new ArrayList<QName>();

        private QNameCache() {
        }

        public final QName getQName(String aNameSpaceURI, String aLocalName) {
            QName qName = this.getExistingQName(aNameSpaceURI, aLocalName);
            if (qName == null) {
                qName = new QName(aNameSpaceURI, aLocalName);
                mCache.add(qName);
            }
            return qName;
        }

        public final QName getExistingQName(String aNameSpaceURI, String aLocalName) {
            for (int i = 0; i < mCache.size(); ++i) {
                QName qName = mCache.get(i);
                if (aNameSpaceURI != qName.maNameSpaceURI || aLocalName != qName.maLocalName) continue;
                return qName;
            }
            return null;
        }
    }

    private static class QName {
        final String maNameSpaceURI;
        final String maLocalName;

        QName(String aNameSpaceURI, String aLocalName) {
            this.maNameSpaceURI = aNameSpaceURI;
            this.maLocalName = aLocalName;
        }
    }

}