XMPNodeCopyVisitor.java 2.66 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 XMPNodeCopyVisitor
implements XMPNodeVisitor {
    Stack<XMPNode> stack = new Stack();

    private XMPNodeCopyVisitor() {
    }

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

    public void visit(XMPSimple simple) {
        XMPNode root = this.stack.peek();
        XMPSimple nextRoot = null;
        if (root instanceof XMPStruct) {
            XMPStruct struct = (XMPStruct)root;
            nextRoot = struct.setSimple(simple.getNamespace(), simple.getName(), simple.getValue());
            this.handleQualifiers(simple, nextRoot);
        } else if (root instanceof XMPArray) {
            XMPArray array = (XMPArray)root;
            nextRoot = array.appendSimple(simple.getValue());
            this.handleQualifiers(simple, nextRoot);
        }
    }

    public void visit(XMPStruct struct) {
        XMPNode root = this.stack.peek();
        XMPStruct nextRoot = null;
        if (root instanceof XMPStruct) {
            XMPStruct rootAsStruct = (XMPStruct)root;
            nextRoot = rootAsStruct.setStruct(struct.getNamespace(), struct.getName());
        } else if (root instanceof XMPArray) {
            XMPArray rootAsArray = (XMPArray)root;
            nextRoot = rootAsArray.appendStruct();
        }
        this.visitChildren(struct, nextRoot);
        this.handleQualifiers(struct, nextRoot);
    }

    public void visit(XMPArray array) {
        XMPNode root = this.stack.peek();
        XMPArray nextRoot = null;
        if (root instanceof XMPStruct) {
            XMPStruct rootAsStruct = (XMPStruct)root;
            nextRoot = rootAsStruct.setArray(array.getNamespace(), array.getName(), array.getForm());
        } else if (root instanceof XMPArray) {
            XMPArray rootAsArray = (XMPArray)root;
            nextRoot = rootAsArray.appendArray(array.getForm());
        }
        this.visitChildren(array, nextRoot);
        this.handleQualifiers(array, nextRoot);
    }

    private void visitChildren(XMPNode node, XMPNode nextRoot) {
        for (XMPNode field : node) {
            this.stack.push(nextRoot);
            field.accept(this);
            this.stack.pop();
        }
    }

    private void handleQualifiers(XMPNode node, XMPNode nextRoot) {
        if (node.hasQualifiers()) {
            this.visitChildren(node.accessQualifiers(), nextRoot.accessQualifiers());
        }
    }
}