SaxHandler.java 16.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
/*
 * Decompiled with CFR 0_118.
 */
package com.adobe.xfa;

import com.adobe.xfa.*;
import com.adobe.xfa.content.ExDataValue;
import com.adobe.xfa.ut.ExFull;
import com.adobe.xfa.ut.ResId;
import org.xml.sax.*;
import org.xml.sax.ext.LexicalHandler;

import java.util.ArrayList;
import java.util.List;

public final class SaxHandler
implements ContentHandler,
LexicalHandler,
ErrorHandler {
    private final Document mDoc;
    private final String mFileName;
    private final AppModel mAppModel;
    private Model mCurrentModel;
    private Element[] mParentStack = new Element[16];
    private int mParentDepth;
    private Node mPreviousSibling;
    private Element mStartingParent;
    private boolean mbIgnoreNextElement;
    private Locator mLocator;
    private Generator mGenerator;
    private char[] mCharBuffer = new char[128];
    private int mCharCount;
    private final List<Element> mLoadedModelsAndPackets = new ArrayList<Element>();
    private static String[] whitespaceCache = new String[32];

    SaxHandler(Document d) {
        this.mDoc = d;
        this.mFileName = d.getParseFileName();
        this.mAppModel = d.getAppModel();
    }

    void setContext(Model startingModel, Element startingParent, boolean bIgnoreAggregating) {
        assert (startingParent != null);
        this.mCurrentModel = startingModel;
        this.mStartingParent = startingParent;
        this.mbIgnoreNextElement = bIgnoreAggregating;
    }

    @Override
    public void setDocumentLocator(Locator locator) {
        this.mLocator = locator;
    }

    @Override
    public void startDocument() {
        this.pushParent(this.mDoc);
        this.mDoc.setWillDirty(false);
    }

    @Override
    public void endDocument() {
        if (this.mAppModel != null) {
            this.mAppModel.postLoad();
        }
        for (int i = 0; i < this.mLoadedModelsAndPackets.size(); ++i) {
            Element element = this.mLoadedModelsAndPackets.get(i);
            if (element instanceof Model) {
                Element model = (Model)element;
                model.disposeSymbolTable();
                boolean stripWhiteSpace = true;
                Model node = model instanceof Model.DualDomModel ? (Element)model.getXmlPeer() : model;
                for (Node child = model.getFirstXMLChild(); child != null; child = child.getNextXMLSibling()) {
                    if (!(child instanceof Chars) || ((Chars)child).isXMLSpace()) continue;
                    stripWhiteSpace = false;
                    break;
                }
                if (stripWhiteSpace) {
                    node.removeWhiteSpace();
                }
                this.mDoc.declareXFAId(model.getNSInternal(), "id");
                this.mDoc.indexSubtree(model, false);
                this.mDoc.setWillDirty(true);
                model.postLoad();
                this.mDoc.setWillDirty(false);
                continue;
            }
            this.mDoc.declareXFAId(element.getNSInternal(), "id");
            this.mDoc.indexSubtree(element, false);
        }
        this.mDoc.setWillDirty(true);
    }

    @Override
    public void startPrefixMapping(String prefix, String uri) {
    }

    @Override
    public void endPrefixMapping(String prefix) {
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) {
        Element e;
        Element parent;
        this.flushCharacters();
        if (this.mStartingParent != null) {
            this.pushParent(this.mStartingParent);
            parent = this.mStartingParent;
            this.mPreviousSibling = this.mStartingParent.getLastXMLChild();
            this.mStartingParent = null;
            if (this.mCurrentModel != null) {
                this.mLoadedModelsAndPackets.add(this.mCurrentModel);
            }
            if (this.mbIgnoreNextElement) {
                this.mbIgnoreNextElement = false;
                return;
            }
        } else {
            parent = this.peekParent();
        }
        int nLineNumber = this.mLocator.getLineNumber();
        this.validateIdUniqueness(uri, localName, attributes);
        if (this.mCurrentModel == null) {
            if (this.mParentDepth == 1) {
                if (this.mAppModel.isXFANode(uri, localName, qName)) {
                    e = new ModelPeer(this.mDoc, this.mPreviousSibling, uri, localName, qName, attributes, this.mAppModel);
                    if (this.mAppModel.getOwnerDocument().isDefaultDocument()) {
                        this.mAppModel.setXmlPeer(e);
                        this.mAppModel.setDocument(this.mDoc);
                    } else {
                        int index = e.findAttr("", "uuid");
                        if (index != -1) {
                            this.mAppModel.setAttribute(new StringAttr("uuid", e.getAttrVal(index)), XFA.UUIDTAG);
                        }
                        if ((index = e.findAttr("", "timeStamp")) != -1) {
                            this.mAppModel.setAttribute(new StringAttr("timeStamp", e.getAttrVal(index)), XFA.TIMESTAMPTAG);
                        }
                    }
                } else {
                    try {
                        this.mAppModel.setAllowThirdPartyXml(true);
                        e = this.createModelOrPacket(parent, uri, localName, qName, attributes, nLineNumber);
                    }
                    finally {
                        this.mAppModel.setAllowThirdPartyXml(false);
                    }
                }
            } else if (this.mParentDepth == 2 && parent instanceof ModelPeer) {
                e = this.createModelOrPacket(parent, uri, localName, qName, attributes, nLineNumber);
            } else {
                assert (parent.getClassTag() == XFA.INVALID_ELEMENT || parent.getClassTag() == XFA.PACKETTAG);
                e = new Element(parent, this.mPreviousSibling, uri, localName, qName, attributes, XFA.INVALID_ELEMENT, "");
            }
            e.setLineNumber(nLineNumber);
        } else if (parent instanceof ExDataValue) {
            e = new Element(parent, this.mPreviousSibling, uri, localName, qName, attributes, XFA.INVALID_ELEMENT, "");
            e.setLineNumber(nLineNumber);
        } else {
            e = this.mCurrentModel.createElement(parent, this.mPreviousSibling, uri, localName, qName, attributes, nLineNumber, this.mFileName);
        }
        e.setDocument(this.mDoc);
        this.pushParent(e);
        this.mPreviousSibling = null;
    }

    private void validateIdUniqueness(String aElementNameSpaceURI, String aElementLocalName, Attributes attributes) {
        if (this.mDoc.uniquifyIDsOnParse()) {
            return;
        }
        for (int i = 0; i < attributes.getLength(); ++i) {
            if (!this.mDoc.isId(aElementNameSpaceURI, aElementLocalName, attributes.getURI(i), attributes.getLocalName(i)) || !this.mDoc.idValueInUse(attributes.getValue(i))) continue;
            throw new ExFull(ResId.DOM_DUPLICATE_ID_ERR);
        }
    }

    private Element createModelOrPacket(Element xmlParent, String uri, String localName, String qName, Attributes attributes, int nLineNumber) {
        Element e = this.mAppModel.createElement(this.mAppModel, null, uri, localName, qName, attributes, nLineNumber, this.mFileName);
        Element xmlPeer = (Element)((Element.DualDomNode)((Object)e)).getXmlPeer();
        e.setDocument(this.mDoc);
        xmlPeer.setDocument(this.mDoc);
        if (e instanceof Model) {
            this.mCurrentModel = (Model)e;
            this.mLoadedModelsAndPackets.add(this.mCurrentModel);
            this.mCurrentModel.isLoading(true);
            this.mCurrentModel.initializeSymbolTable();
            this.mCurrentModel.setGenerator(this.mGenerator);
        } else assert (e instanceof Packet);
        if (e instanceof Model.DualDomModel) {
            xmlParent.appendChild(xmlPeer);
            xmlPeer.setModel((Model)e);
            e = xmlPeer;
        } else if (xmlPeer.getXMLParent() != null && xmlPeer.getXMLParent() != xmlParent) {
            xmlParent.appendChild(xmlPeer);
        }
        return e;
    }

    @Override
    public void endElement(String uri, String localName, String qName) {
        this.flushCharacters();
        this.mPreviousSibling = this.popParent();
        if (this.mPreviousSibling instanceof Model || this.mPreviousSibling instanceof ModelPeer) {
            Model model = this.mPreviousSibling.getModel();
            model.isLoading(false);
            this.mPreviousSibling = model.getXmlPeer();
            this.mCurrentModel = null;
        } else if (this.mPreviousSibling instanceof Packet) {
            this.mPreviousSibling = ((Packet)this.mPreviousSibling).getXmlPeer();
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) {
        if (this.mbIgnoreNextElement) {
            return;
        }
        if (length == 0) {
            return;
        }
        if (length > this.mCharBuffer.length - this.mCharCount) {
            int requiredLength = this.mCharCount + length;
            int newCapacity = this.mCharBuffer.length;
            do {
                if ((newCapacity *= 2) >= 0) continue;
                newCapacity = Integer.MAX_VALUE;
            } while (newCapacity < requiredLength);
            char[] newCharBuffer = new char[newCapacity];
            System.arraycopy(this.mCharBuffer, 0, newCharBuffer, 0, this.mCharCount);
            this.mCharBuffer = newCharBuffer;
        }
        System.arraycopy(ch, start, this.mCharBuffer, this.mCharCount, length);
        this.mCharCount += length;
    }

    private void flushCharacters() {
        TextNode t;
        if (this.mCharCount == 0) {
            return;
        }
        Element parent = this.peekParent();
        if (parent instanceof AppModel) {
            this.mPreviousSibling = new Chars(parent, this.mPreviousSibling, this.mCharBuffer, 0, this.mCharCount);
            this.mCharCount = 0;
            return;
        }
        if (this.mCurrentModel != null && parent != null && parent.processTextChildrenDuringParse()) {
            ChildReln childReln = parent.getChildReln(XFA.TEXTNODETAG);
            if (childReln == null && Chars.isXMLSpace(0, this.mCharCount, this.mCharBuffer)) {
                this.mCharCount = 0;
                return;
            }
            if (childReln != null && childReln.getMax() == 1) {
                for (Node child = parent.getFirstXFAChild(); child != null; child = child.getNextXFASibling()) {
                    if (!(child instanceof TextNode)) continue;
                    TextNode firstTextNode = (TextNode)child;
                    StringBuilder sText = new StringBuilder(firstTextNode.getText());
                    sText.append(this.mCharBuffer, 0, this.mCharCount);
                    firstTextNode.setText(sText.toString());
                    this.mCharCount = 0;
                    return;
                }
            }
            try {
                if (!parent.isValidChild(XFA.TEXTNODETAG, ResId.InvalidChildAppendException, true, false)) {
                    this.mCharCount = 0;
                    return;
                }
            }
            catch (ExFull ex) {
                this.mCurrentModel.addErrorList(ex, 5, parent);
                this.mCharCount = 0;
                return;
            }
            t = this.mCurrentModel.createTextNode(parent, this.mPreviousSibling, this.mCharBuffer, 0, this.mCharCount);
        } else {
            t = new TextNode(parent, this.mPreviousSibling, this.getTextNodeStringFromCharBuffer());
        }
        this.mCharCount = 0;
        this.mPreviousSibling = t;
    }

    private String getTextNodeStringFromCharBuffer() {
        assert (this.mCharCount > 0);
        if (this.isCacheableWhitespace()) {
            String result;
            if (this.mCharCount >= whitespaceCache.length) {
                String[] newCache = new String[whitespaceCache.length * 2];
                System.arraycopy(whitespaceCache, 0, newCache, 0, whitespaceCache.length);
                whitespaceCache = newCache;
            }
            if ((result = whitespaceCache[this.mCharCount]) == null) {
                SaxHandler.whitespaceCache[this.mCharCount] = result = new String(this.mCharBuffer, 0, this.mCharCount);
            }
            return result;
        }
        return new String(this.mCharBuffer, 0, this.mCharCount);
    }

    private boolean isCacheableWhitespace() {
        char[] buffer = this.mCharBuffer;
        if (buffer[0] != '\n') {
            return false;
        }
        for (int i = this.mCharCount - 1; i > 0; --i) {
            if (buffer[i] == ' ') continue;
            return false;
        }
        return true;
    }

    @Override
    public void ignorableWhitespace(char[] ch, int start, int length) {
    }

    @Override
    public void processingInstruction(String target, String data) {
        this.flushCharacters();
        Element parent = this.peekParent();
        ProcessingInstruction pi = new ProcessingInstruction(parent, this.mPreviousSibling, target, data);
        if (pi.getName() == "xfa") {
            this.mGenerator = new Generator(pi);
        }
        if (this.mCurrentModel != null) {
            this.mCurrentModel.preLoadNode(parent, pi, this.mGenerator);
        }
        this.mPreviousSibling = pi;
    }

    @Override
    public void skippedEntity(String name) {
    }

    @Override
    public void startDTD(String name, String publicId, String systemId) {
    }

    @Override
    public void endDTD() {
    }

    @Override
    public void startEntity(String name) {
    }

    @Override
    public void endEntity(String name) {
    }

    @Override
    public void startCDATA() {
        if (this.mCharCount > 0) {
            this.flushCharacters();
            this.mCharCount = 0;
        }
    }

    @Override
    public void endCDATA() {
        Element parent;
        if (this.mStartingParent != null) {
            this.pushParent(this.mStartingParent);
            parent = this.mStartingParent;
            this.mPreviousSibling = this.mStartingParent.getLastXMLChild();
            this.mStartingParent = null;
            if (this.mCurrentModel != null) {
                this.mLoadedModelsAndPackets.add(this.mCurrentModel);
            }
            if (this.mbIgnoreNextElement) {
                this.mbIgnoreNextElement = false;
                return;
            }
        } else {
            parent = this.peekParent();
        }
        CDataNode cdNode = new CDataNode(parent, this.mPreviousSibling, this.mCharBuffer, 0, this.mCharCount);
        this.mPreviousSibling = cdNode;
        this.mCharCount = 0;
    }

    @Override
    public void comment(char[] ch, int start, int length) {
        this.flushCharacters();
        Element parent = this.peekParent();
        String data = new String(ch, start, length);
        Comment c = new Comment(parent, this.mPreviousSibling, data);
        if (this.mCurrentModel != null) {
            this.mCurrentModel.preLoadNode(parent, c, this.mGenerator);
        }
        this.mPreviousSibling = c;
    }

    @Override
    public void warning(SAXParseException exception) {
        throw SaxHandler.createXFAException(exception);
    }

    @Override
    public void error(SAXParseException exception) {
        throw SaxHandler.createXFAException(exception);
    }

    @Override
    public void fatalError(SAXParseException exception) {
        throw SaxHandler.createXFAException(exception);
    }

    private void pushParent(Element e) {
        if (this.mParentDepth == this.mParentStack.length) {
            Element[] newStack = new Element[this.mParentStack.length * 2];
            System.arraycopy(this.mParentStack, 0, newStack, 0, this.mParentDepth);
            this.mParentStack = newStack;
        }
        this.mParentStack[this.mParentDepth++] = e;
    }

    private Element peekParent() {
        if (this.mParentDepth == 0) {
            return null;
        }
        return this.mParentStack[this.mParentDepth - 1];
    }

    private Element popParent() {
        Element e = this.mParentStack[this.mParentDepth - 1];
        this.mParentStack[this.mParentDepth - 1] = null;
        --this.mParentDepth;
        return e;
    }

    private static ExFull createXFAException(SAXParseException inException) {
        return new ExFull(ResId.EXPAT_ERROR, inException.getMessage());
    }
}