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

import com.adobe.xfa.*;
import com.adobe.xfa.content.ExDataValue;
import com.adobe.xfa.template.containers.Variables;
import com.adobe.xfa.ut.*;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

public class ElementScript
extends NodeScript {
    protected static final ScriptTable moScriptTable = new ScriptTable(NodeScript.moScriptTable, "element", new ScriptPropObj[]{new ScriptPropObj(ElementScript.class, "isNull", "getIsNull", null, 3, 10, 9, 0), new ScriptPropObj(ElementScript.class, "oneOfChild", "getOneOfChild", "setOneOfChild", 7, 10, 9, 0), new ScriptPropObj(ElementScript.class, "id", "getId", "setId", 6, 10, 9, 0), new ScriptPropObj(ElementScript.class, "ns", "getNs", null, 6, 10, 9, 0)}, new ScriptFuncObj[]{new ScriptFuncObj(ElementScript.class, "clone", "clone", 7, new int[]{3}, 1, 10, 9, 0), new ScriptFuncObj(ElementScript.class, "isPropertySpecified", "isPropertySpecified", 3, new int[]{6, 3, 4}, 1, 10, 9, 0), new ScriptFuncObj(ElementScript.class, "getElement", "getElement", 7, new int[]{6, 4}, 1, 10, 9, 0), new ScriptFuncObj(ElementScript.class, "setElement", "setElement", 1, new int[]{7, 6}, 1, 10, 9, "setElementPermsCheck", 0), new ScriptFuncObj(ElementScript.class, "getAttribute", "getAttribute", 6, new int[]{6}, 1, 10, 9, 0), new ScriptFuncObj(ElementScript.class, "setAttribute", "setAttribute", 1, new int[]{6, 6}, 2, 10, 9, "setAttributePermsCheck", 0), new ScriptFuncObj(ElementScript.class, "loadXML", "loadXML", 1, new int[]{6, 3, 3}, 1, 10, 9, "loadXMLPermsCheck", 0), new ScriptFuncObj(ElementScript.class, "saveXML", "saveXML", 6, new int[]{6}, 0, 10, 9, 0), new ScriptFuncObj(ElementScript.class, "saveFilteredXML", "saveFilteredXML", 6, new int[]{7, 6}, 1, 24, 9, 0), new ScriptFuncObj(ElementScript.class, "applyXSL", "applyXSL", 6, new int[]{6}, 1, 10, 9, 0), new ScriptFuncObj(ElementScript.class, "assignNode", "assignNode", 7, new int[]{6, 6, 4}, 1, 10, 9, "assignNodePermsCheck", 0), new ScriptFuncObj(ElementScript.class, "resolveNode", "resolveNode", 7, new int[]{6}, 1, 10, 63, 0), new ScriptFuncObj(ElementScript.class, "resolveNodes", "resolveNodes", 7, new int[]{6}, 1, 10, 63, 0)});

    public static ScriptTable getScriptTable() {
        return moScriptTable;
    }

    public static void getIsNull(Obj obj, Arg retVal) {
        if (obj instanceof Element) {
            retVal.setBool(((Element)obj).getIsNull());
        } else if (obj instanceof TextNode) {
            retVal.setBool(Boolean.FALSE);
        }
    }

    public static void getOneOfChild(Obj obj, Arg retVal) {
        if (obj instanceof Element) {
            retVal.setObject(((Element)obj).getOneOfChild());
        } else {
            retVal.setObject(null);
        }
    }

    public static void setOneOfChild(Obj obj, Arg oArg) {
        Obj child = oArg.getObject();
        if (!(child instanceof Node)) {
            throw new ExFull(ResId.ArgumentMismatchException);
        }
        if (!(obj instanceof Element)) {
            MsgFormatPos message = new MsgFormatPos(ResId.InvalidSetOneOfException);
            message.format(child.getClassAtom());
            throw new ExFull(message);
        }
        ((Element)obj).setOneOfChild((Node)child);
    }

    public static void getId(Obj obj, Arg retVal) {
        if (obj instanceof Element) {
            retVal.setString(((Element)obj).getID());
        } else {
            retVal.setString("");
        }
    }

    public static void setId(Obj obj, Arg oArg) {
        if (!(obj instanceof Element)) {
            MsgFormatPos message = new MsgFormatPos(ResId.InvalidSetPropertyException);
            message.format(obj.getClassAtom());
            message.format("id");
            throw new ExFull(message);
        }
        ((Element)obj).setID(oArg.getString());
    }

    public static void getNs(Obj obj, Arg retVal) {
        if (obj instanceof Element) {
            retVal.setString(((Element)obj).getNSInternal());
        } else if (obj instanceof TextNode) {
            retVal.setString("");
        }
    }

    public static void clone(Obj obj, Arg retVal, Arg[] args) {
        boolean bRecursive = args[0].getBool();
        if (obj instanceof Element) {
            retVal.setObject(((Element)obj).clone(null, bRecursive));
        } else {
            retVal.setObject(((Node)obj).clone(null));
        }
    }

    public static void isPropertySpecified(Obj obj, Arg retVal, Arg[] args) {
        if (obj instanceof Element) {
            Element e = (Element)obj;
            String propertyName = args[0].getString();
            boolean bCheckProtos = args.length < 2 ? true : args[1].getBool();
            int occurrence = args.length < 3 ? 0 : args[2].getInteger();
            retVal.setBool(e.isPropertySpecified(propertyName, bCheckProtos, occurrence));
        } else {
            retVal.setBool(Boolean.FALSE);
        }
    }

    public static void getElement(Obj obj, Arg retVal, Arg[] args) {
        int occurrence;
        if (!(obj instanceof Element)) {
            return;
        }
        Element e = (Element)obj;
        String propName = args[0].getString().intern();
        Object property = e.getProperty(propName, occurrence = args.length == 1 ? 0 : args[1].getInteger());
        if (property == null) {
            return;
        }
        if (!(property instanceof Element)) {
            throw new ExFull(new MsgFormat(ResId.InvalidAttributeException, " element[" + propName + ")"));
        }
        retVal.setObject((Element)property);
    }

    public static void setElement(Obj obj, Arg retVal, Arg[] args) {
        String propertyName;
        Obj property = args[0].getObject();
        if (!(property instanceof Element)) {
            throw new ExFull(ResId.ArgumentMismatchException);
        }
        String string = propertyName = args.length < 2 ? "" : args[1].getString();
        if (!(obj instanceof Element)) {
            MsgFormatPos message = new MsgFormatPos(ResId.InvalidSetPropertyException);
            message.format(obj.getClassAtom());
            message.format(propertyName);
            throw new ExFull(message);
        }
        ((Element)obj).setProperty((Object)property, propertyName);
    }

    public static void getAttribute(Obj obj, Arg retVal, Arg[] args) {
        if (!(obj instanceof Element)) {
            return;
        }
        String propName = args[0].getString();
        Object property = ((Element)obj).getProperty(propName, 0);
        if (property == null) {
            return;
        }
        if (!(property instanceof Attribute)) {
            throw new ExFull(ResId.InvalidAttributeException, " attribute[" + propName + ")");
        }
        retVal.setString(property.toString());
    }

    public static void setAttribute(Obj obj, Arg retVal, Arg[] args) {
        String aAttrName = args[1].getString();
        if (aAttrName != null) {
            aAttrName = aAttrName.intern();
        }
        int eTag = -1;
        if (aAttrName != null) {
            eTag = XFA.getTag(aAttrName);
        }
        if (!(obj instanceof Element) || eTag == -1) {
            MsgFormatPos oMessage = new MsgFormatPos(ResId.InvalidSetPropertyException);
            oMessage.format(((Node)obj).getClassAtom());
            oMessage.format(aAttrName);
            throw new ExFull(oMessage);
        }
        ((Element)obj).setAttribute(((Element)obj).newAttribute(eTag, args[0].getString()), eTag);
    }

    public static void loadXML(Obj obj, Arg retVal, Arg[] args) {
        if (!(obj instanceof Element)) {
            MsgFormatPos message = new MsgFormatPos(ResId.ParentChildViolationException);
            throw new ExFull(message);
        }
        try {
            ByteArrayInputStream streamFile = new ByteArrayInputStream(args[0].getString().getBytes("UTF-8"));
            boolean bIgnoreAggregatingTag = true;
            boolean bReplaceContent = false;
            if (args.length == 1 && obj instanceof ExDataValue) {
                bIgnoreAggregatingTag = false;
            }
            if (args.length >= 2) {
                bIgnoreAggregatingTag = args[1].getBool();
            }
            if (args.length == 3) {
                bReplaceContent = args[2].getBool();
            }
            Element.ReplaceContent eReplace = Element.ReplaceContent.None;
            if (bReplaceContent) {
                eReplace = obj instanceof Packet && ((Packet)obj).getAppModel().getLegacySetting(AppModel.XFA_LEGACY_V32_SCRIPTING) ? Element.ReplaceContent.AllContent : Element.ReplaceContent.XFAContent;
            }
            ((Element)obj).loadXML((InputStream)streamFile, bIgnoreAggregatingTag, eReplace);
        }
        catch (UnsupportedEncodingException e) {
            // empty catch block
        }
    }

    public static void saveXML(Obj obj, Arg retVal, Arg[] args, DependencyTracker dependencyTracker) {
        ElementScript.addDependency((Element)obj, dependencyTracker);
        DOMSaveOptions options = new DOMSaveOptions();
        options.setSaveTransient(((Element)obj).getSaveXMLSaveTransient());
        ByteArrayOutputStream memStream = new ByteArrayOutputStream();
        if (args.length == 1) {
            if (args[0].getString().equals("pretty")) {
                options.setDisplayFormat(2);
            } else {
                MsgFormatPos message = new MsgFormatPos(ResId.ArgumentMismatchException);
                throw new ExFull(message);
            }
        }
        if (obj instanceof Element) {
            ((Element)obj).saveXML(memStream, options, true);
        } else if (obj instanceof Node) {
            Node node = (Node)obj;
            node.getOwnerDocument().saveAs(memStream, node, options);
        }
        String result = "";
        try {
            result = memStream.toString("UTF-8");
        }
        catch (UnsupportedEncodingException ex) {
            // empty catch block
        }
        if (result.endsWith("\n")) {
            result = result.substring(0, result.length() - 1);
        }
        retVal.setString(result);
    }

    public static void saveFilteredXML(Obj obj, Arg retVal, Arg[] args, DependencyTracker dependencyTracker) {
        Obj object = args[0].getObject();
        if (!(object instanceof NodeList)) {
            throw new ExFull(ResId.ArgumentMismatchException);
        }
        ByteArrayOutputStream memStreamFile = new ByteArrayOutputStream();
        ElementScript.addDependency((Element)obj, dependencyTracker);
        DOMSaveOptions options = new DOMSaveOptions();
        options.setSaveTransient(false);
        if (args.length == 2) {
            if (args[1].getString().equals("pretty")) {
                options.setSaveTransient(true);
                options.setDisplayFormat(2);
            } else {
                MsgFormatPos oMessage = new MsgFormatPos(ResId.ArgumentMismatchException);
                throw new ExFull(oMessage);
            }
        }
        if (obj instanceof Element) {
            ((Element)obj).saveFilteredXML((NodeList)object, memStreamFile, options);
        } else if (obj instanceof Node) {
            Node node = (Node)obj;
            node.getOwnerDocument().saveAs(memStreamFile, node, options);
        }
        try {
            retVal.setString(memStreamFile.toString("UTF-8"));
        }
        catch (UnsupportedEncodingException ex) {
            // empty catch block
        }
    }

    public static void applyXSL(Obj obj, Arg retVal, Arg[] args) {
        if (!(obj instanceof Element)) {
            MsgFormatPos message = new MsgFormatPos(ResId.InvalidMethodException);
            message.format(obj.getClassAtom());
            message.format("applyXSL");
            throw new ExFull(message);
        }
        try {
            ByteArrayInputStream xslTransformStream = new ByteArrayInputStream(args[0].getString().getBytes("UTF-8"));
            ByteArrayOutputStream resultStream = new ByteArrayOutputStream();
            ((Element)obj).applyXSL(xslTransformStream, resultStream);
            retVal.setString(resultStream.toString("UTF-8"));
        }
        catch (UnsupportedEncodingException ex) {
            // empty catch block
        }
    }

    public static void assignNode(Obj obj, Arg retVal, Arg[] args) {
        if (!(obj instanceof Element)) {
            MsgFormatPos message = new MsgFormatPos(ResId.InvalidMethodException);
            message.format(obj.getClassAtom());
            message.format("assignNode");
            throw new ExFull(message);
        }
        retVal.setObject(((Element)obj).assignNode(args[0].getString(), args[1].getString(), args[2].getInteger()));
    }

    public static void resolveNode(Obj obj, Arg retVal, Arg[] args, DependencyTracker dependencyTracker) {
        AppModel oAppModel = ((Element)obj).getAppModel();
        DependencyTracker oDependencyTracker = null;
        if (oAppModel != null && !oAppModel.getLegacySetting(AppModel.XFA_LEGACY_V29_SCRIPTING)) {
            oDependencyTracker = dependencyTracker;
        }
        retVal.setObject(((Element)obj).resolveNode(args[0].getString(), false, false, false, oDependencyTracker, null));
    }

    public static void resolveNodes(Obj obj, Arg retVal, Arg[] args, int nParamCount, DependencyTracker dependencyTracker) {
        AppModel oAppModel = ((Element)obj).getAppModel();
        DependencyTracker oDependencyTracker = null;
        if (oAppModel != null && !oAppModel.getLegacySetting(AppModel.XFA_LEGACY_V29_SCRIPTING)) {
            oDependencyTracker = dependencyTracker;
        }
        retVal.setObject(((Element)obj).resolveNodes(args[0].getString(), false, false, false, oDependencyTracker, null));
    }

    public static boolean locateOneOf(Obj obj, Arg retVal, String sProp) {
        Node pOneOf;
        if (StringUtils.isEmpty(sProp)) {
            return false;
        }
        int nTag = XFA.getTag(sProp);
        if (nTag == -1) {
            return false;
        }
        int eTag = nTag;
        int eType = ((Element)obj).getSchemaType(eTag);
        if (eType == 3 && (pOneOf = ((Element)obj).getOneOfChild(true, false)) != null && pOneOf.getClassTag() == eTag) {
            pOneOf = ((Element)obj).getOneOfChild(false, false);
            retVal.setObject(pOneOf);
            return true;
        }
        return false;
    }

    public static boolean locateProp(Obj obj, Arg retVal, String sProp) {
        if (StringUtils.isEmpty(sProp)) {
            return false;
        }
        int nTag = XFA.getTag(sProp);
        if (nTag == -1) {
            return false;
        }
        int eTag = nTag;
        int eType = ((Element)obj).getSchemaType(eTag);
        if (eType != 1 && eType != 2) {
            return false;
        }
        Element oNode = (Element)obj;
        if (eType == 1) {
            Attribute oProp = oNode.getAttribute(eTag, false, false);
            if (oProp != null) {
                retVal.setString(oProp.toString());
                return true;
            }
        } else {
            Element oProp = oNode.getElement(eTag, 0);
            if (oProp != null) {
                retVal.setObject(oProp);
                return true;
            }
        }
        return false;
    }

    public static boolean locatePropPeek(Obj obj, Arg retVal, String sProp) {
        if (StringUtils.isEmpty(sProp)) {
            return false;
        }
        int nTag = XFA.getTag(sProp);
        if (nTag == -1) {
            return false;
        }
        int eTag = nTag;
        int eType = ((Element)obj).getSchemaType(eTag);
        if (eType != 1 && eType != 2) {
            return false;
        }
        if (eType == 1) {
            Attribute prop = ((Element)obj).getAttribute(eTag, true, false);
            if (prop != null) {
                retVal.setString(prop.toString());
                return true;
            }
        } else {
            Element prop = ((Element)obj).getElement(eTag, true, 0, false, false);
            if (prop != null) {
                retVal.setObject(prop);
                return true;
            }
        }
        return false;
    }

    public static boolean setProp(Obj obj, Arg arg, String sProp) {
        if (StringUtils.isEmpty(sProp)) {
            return false;
        }
        int eTag = XFA.getTag(sProp);
        if (eTag == -1) {
            return false;
        }
        if (!(obj instanceof Element)) {
            return false;
        }
        Element elementThis = (Element)obj;
        int eType = elementThis.getSchemaType(eTag);
        if (eType != 1) {
            return false;
        }
        StringAttr prop = new StringAttr(sProp, arg.getString());
        elementThis.setAttribute(prop, eTag);
        return true;
    }

    private static void addDependency(Node node, DependencyTracker dependencyTracker) {
        if (dependencyTracker != null && node != null && !(node instanceof Variables)) {
            dependencyTracker.addDependency(node);
            for (Node child = node.getFirstXFAChild(); child != null; child = child.getNextXFASibling()) {
                ElementScript.addDependency(child, dependencyTracker);
            }
        }
    }

    public static boolean setElementPermsCheck(Obj obj, Arg[] args) {
        if (obj instanceof Element) {
            Element element = (Element)obj;
            if (!element.checkAncestorPerms()) {
                return false;
            }
            Obj object = args[0].getObject();
            if (!(object instanceof Node)) {
                throw new ExFull(ResId.ArgumentMismatchException);
            }
            Node node = (Node)object;
            Element oldChild = element.getElement(node.getClassTag(), 0);
            if (oldChild != null && !oldChild.checkDescendentPerms()) {
                return false;
            }
        }
        return true;
    }

    public static boolean setAttributePermsCheck(Obj obj, Arg[] args) {
        Element element;
        if (obj instanceof Element && !(element = (Element)obj).checkAncestorPerms()) {
            return false;
        }
        return true;
    }

    public static boolean loadXMLPermsCheck(Obj obj, Arg[] args) {
        if (obj instanceof Element) {
            boolean bReplace;
            Element element = (Element)obj;
            if (!element.checkAncestorPerms()) {
                return false;
            }
            if (args.length == 3 && (bReplace = args[2].getBool().booleanValue()) && !element.checkDescendentPerms()) {
                return false;
            }
        }
        return true;
    }

    public static boolean assignNodePermsCheck(Obj obj, Arg[] args) {
        if (obj instanceof Element && args.length >= 1) {
            String sCopy;
            Element element = (Element)obj;
            Node resolvedNode = element.resolveNode(args[0].getString(), true, false, false);
            if (resolvedNode != null) {
                if (args.length < 3 || args[2].getInteger() == 0) {
                    if (!resolvedNode.checkAncestorPerms()) {
                        return false;
                    }
                    if (!resolvedNode.checkDescendentPerms()) {
                        return false;
                    }
                }
            } else if (args[2].getInteger() != 0 && (sCopy = args[0].getString()).length() != 0) {
                Node parentNode;
                Node parent = element.resolveNode(sCopy, true, false, false);
                while (parent == null && sCopy.length() != 0) {
                    int nFoundAt;
                    boolean bTrimmed = false;
                    int nOffset = 0;
                    while ((nFoundAt = sCopy.indexOf(46, nOffset)) != -1) {
                        bTrimmed = true;
                        nOffset = nFoundAt + 1;
                    }
                    if (!bTrimmed) break;
                    sCopy = sCopy.substring(0, nOffset - 1);
                    parent = element.resolveNode(sCopy, true, false, false);
                }
                if (parent instanceof Node && !(parentNode = parent).checkAncestorPerms()) {
                    return false;
                }
            }
        }
        return true;
    }
}