XMPNodeCopyReplaceVisitor.java 5.08 KB
/*
 * Decompiled with CFR 0_118.
 */
package com.adobe.xmp.core.impl;

import com.adobe.xmp.core.XMPArray;
import com.adobe.xmp.core.XMPNode;
import com.adobe.xmp.core.XMPNodeVisitor;
import com.adobe.xmp.core.XMPQualifiers;
import com.adobe.xmp.core.XMPSimple;
import com.adobe.xmp.core.XMPStruct;
import java.util.Stack;

class XMPNodeCopyReplaceVisitor
implements XMPNodeVisitor {
    Stack<XMPNode> stack = new Stack();

    public XMPNodeCopyReplaceVisitor(XMPNode root) {
        this.stack.push(root);
    }

    public void visit(XMPSimple sSimple) {
        XMPNode currentNode = this.stack.peek();
        if (!(currentNode instanceof XMPSimple)) {
            throw new IllegalStateException("Expected XMPSimple node type.");
        }
        XMPSimple simpleTarget = (XMPSimple)currentNode;
        simpleTarget.setValue(sSimple.getValue());
        simpleTarget.setURI(sSimple.isURI());
        this.handleQualifiers(sSimple, simpleTarget);
    }

    public void visit(XMPStruct sStruct) {
        XMPNode currentNode = this.stack.peek();
        if (!(currentNode instanceof XMPStruct)) {
            throw new IllegalStateException("Expected XMPStruct node type.");
        }
        XMPStruct tStruct = (XMPStruct)currentNode;
        for (XMPNode sChild : sStruct) {
            XMPStruct struct;
            XMPNode newChild = null;
            if (sChild instanceof XMPSimple) {
                XMPSimple simple = (XMPSimple)sChild;
                newChild = tStruct.setSimple(simple.getNamespace(), simple.getName(), simple.getValue());
            } else if (sChild instanceof XMPArray) {
                XMPArray array = (XMPArray)sChild;
                newChild = tStruct.getArray(array.getNamespace(), array.getName());
                if (newChild == null || ((XMPArray)newChild).getForm() != array.getForm()) {
                    newChild = tStruct.setArray(array.getNamespace(), array.getName(), array.getForm());
                }
            } else if (sChild instanceof XMPStruct && (newChild = tStruct.getStruct((struct = (XMPStruct)sChild).getNamespace(), struct.getName())) == null) {
                newChild = tStruct.setStruct(struct.getNamespace(), struct.getName());
            }
            this.visitChild(sChild, newChild);
        }
        this.handleQualifiers(sStruct, tStruct);
    }

    public void visit(XMPArray sArray) {
        XMPNode root = this.stack.peek();
        if (!(root instanceof XMPArray)) {
            throw new IllegalStateException("Expected XMPArray node type.");
        }
        XMPArray tArray = (XMPArray)root;
        int index = 0;
        for (XMPNode sChild : sArray) {
            XMPNode newChild;
            XMPNode xMPNode = newChild = index >= tArray.size() ? null : tArray.get(index);
            if (sChild instanceof XMPSimple) {
                newChild = tArray.setSimple(index, ((XMPSimple)sChild).getValue());
            } else if (sChild instanceof XMPArray) {
                if (!(newChild instanceof XMPArray) || ((XMPArray)newChild).getForm() != ((XMPArray)sChild).getForm()) {
                    newChild = tArray.setArray(index, ((XMPArray)sChild).getForm());
                }
            } else if (sChild instanceof XMPStruct && !(newChild instanceof XMPStruct)) {
                newChild = tArray.setStruct(index);
            }
            this.visitChild(sChild, newChild);
            ++index;
        }
        this.handleQualifiers(sArray, tArray);
    }

    private void visitChild(XMPNode sourceChild, XMPNode copyTo) {
        this.stack.push(copyTo);
        sourceChild.accept(this);
        this.stack.pop();
    }

    private void handleQualifiers(XMPNode sourceNode, XMPNode targetNode) {
        if (sourceNode.hasQualifiers()) {
            this.populateQualifiers(sourceNode.accessQualifiers(), targetNode.accessQualifiers());
        }
    }

    private void populateQualifiers(XMPQualifiers sQuals, XMPQualifiers tQuals) {
        for (XMPNode sChild : sQuals) {
            XMPNode tChild = tQuals.get(sChild.getNamespace(), sChild.getName());
            if (sChild instanceof XMPSimple) {
                if (tChild instanceof XMPSimple) continue;
                XMPSimple simple = (XMPSimple)sChild;
                XMPSimple tSimple = tQuals.setSimple(simple.getNamespace(), simple.getName(), simple.getValue());
                this.visitChild((XMPSimple)sChild, tSimple);
                continue;
            }
            if (sChild instanceof XMPArray) {
                if (tChild instanceof XMPArray && ((XMPArray)tChild).getForm() == ((XMPArray)sChild).getForm()) continue;
                XMPArray array = (XMPArray)sChild;
                XMPArray tChildArray = tQuals.setArray(array.getNamespace(), array.getName(), array.getForm());
                this.visitChild((XMPArray)sChild, tChildArray);
                continue;
            }
            if (!(sChild instanceof XMPStruct) || tChild instanceof XMPStruct) continue;
            XMPStruct struct = (XMPStruct)sChild;
            XMPStruct tChildStruct = tQuals.setStruct(struct.getNamespace(), struct.getName());
            this.visitChild((XMPStruct)sChild, tChildStruct);
        }
    }
}