All Downloads are FREE. Search and download functionalities are using the official Maven repository.

deepdiff.scope.XMLDocumentDiffUnit Maven / Gradle / Ivy

There is a newer version: 1.0.2
Show newest version
/*
 * Copyright 2011 DeepDiff Contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package deepdiff.scope;

import java.io.FileNotFoundException;
import java.io.InputStream;

import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;

import deepdiff.core.DiffUnit;

/**
 * A DiffUnit for an XML document
 */
public class XMLDocumentDiffUnit implements DiffUnit {
    private XMLDiffScope scope;
    private Document d1;
    private Document d2;

    /**
     * Initializes a new XMLDocumentDiffUnit object.
     *
     * @param scope the diff scope that contains this document
     * @param d1 the document on the left
     * @param d2 the document on the right
     */
    public XMLDocumentDiffUnit(XMLDiffScope scope, Document d1, Document d2) {
        this.scope = scope;
        this.d1 = d1;
        this.d2 = d2;
    }

    /**
     * Returns whether the document on the left exists
     *
     * @return whether the document on the left exists
     */
    public boolean leftExists() {
        return d1 != null;
    }

    /**
     * Returns whether the document on the right exists
     *
     * @return whether the document on the right exists
     */
    public boolean rightExists() {
        return d2 != null;
    }

    /**
     * Returns true, since a document can always have children
     *
     * @return true
     */
    public boolean leftIsDir() {
        return true;
    }

    /**
     * Returns true, since a document can always have children
     *
     * @return true
     */
    public boolean rightIsDir() {
        return true;
    }

    /**
     * Always throws an exception, since all content is handled as children
     * units
     *
     * @return never
     *
     * @throws FileNotFoundException always
     */
    public InputStream getLeftInputStream() throws FileNotFoundException {
        throw new FileNotFoundException(
            "Treating XML documents as a stream is not supported");
    }

    /**
     * Always throws an exception, since all content is handled as children
     * units
     *
     * @return never
     *
     * @throws FileNotFoundException always
     */
    public InputStream getRightInputStream() throws FileNotFoundException {
        throw new FileNotFoundException(
            "Treating XML documents as a stream is not supported");
    }

    /**
     * Returns the scoped path of the unit. This includes the path of the scope
     * and the node name.
     *
     * @return the scoped path of the unit
     */
    public String getScopedPath() {
        String path = scope.getPath();
        if (d1 != null) {
            path += d1.getNodeName();
        } else if (d2 != null) {
            path += d2.getNodeName();
        }
        return path;
    }

    /**
     * Returns the doctype of the left document
     *
     * @return the doctype of the left document
     */
    public DocumentType getLeftDoctype() {
        return d1.getDoctype();
    }

    /**
     * Returns the doctype of the right document
     *
     * @return the doctype of the right document
     */
    public DocumentType getRightDoctype() {
        return d2.getDoctype();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy