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

io.cloudslang.content.xml.utils.ValidateUtils Maven / Gradle / Ivy

/*
 * (c) Copyright 2019 EntIT Software LLC, a Micro Focus company, L.P.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License v2.0 which accompany this distribution.
 *
 * The Apache License is available 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 io.cloudslang.content.xml.utils;

import io.cloudslang.content.xml.entities.inputs.EditXmlInputs;
import org.apache.commons.lang3.StringUtils;

/**
 * Created by moldovas on 7/6/2016.
 */
public class ValidateUtils {

    public static void validateIsNotEmpty(String action, String message) throws Exception {
        if (action == null || StringUtils.isEmpty(action)) {
            throw new Exception(message);
        }
    }

    public static void validateXmlAndFilePathInputs(String xml, String filePath) throws Exception {
        if ((StringUtils.isBlank(filePath)) && (StringUtils.isBlank(xml))) {
            throw new Exception("Supplied parameters: file path and xml is missing when one is required");
        }
        if ((!StringUtils.isEmpty(filePath)) && (!StringUtils.isEmpty(xml))) {
            throw new Exception("Supplied parameters: file path and xml when only one is required");
        }
    }

    /**
     * Validates the operation inputs.
     *
     * @param inputs@throws Exception for invalid inputs
     */
    public static void validateInputs(EditXmlInputs inputs) throws Exception {
        validateXmlAndFilePathInputs(inputs.getXml(), inputs.getFilePath());

        if (Constants.Inputs.MOVE_ACTION.equals(inputs.getAction())) {
            validateIsNotEmpty(inputs.getXpath2(), "xpath2 input is required for action 'move' ");
        }
        if (!Constants.Inputs.SUBNODE_ACTION.equals(inputs.getAction()) && !Constants.Inputs.MOVE_ACTION.equals(inputs.getAction())) {
            validateIsNotEmpty(inputs.getType(), "type input is required for action '" + inputs.getAction() + "'");
            if (!Constants.Inputs.TYPE_ELEM.equals(inputs.getType()) && !Constants.Inputs.TYPE_ATTR.equals(inputs.getType()) && !Constants.Inputs.TYPE_TEXT.equals(inputs.getType())) {
                throw new Exception("Invalid type. Only supported : " + Constants.Inputs.TYPE_ELEM + ", " + Constants.Inputs.TYPE_ATTR + ", " + Constants.Inputs.TYPE_TEXT);
            }
            if (Constants.Inputs.TYPE_ATTR.equals(inputs.getType())) {
                validateIsNotEmpty(inputs.getName(), "name input is required for type 'attr' ");
            }
        }
    }

    public static void validateInputs(String prettyPrint, String showXmlDeclaration) throws Exception {
        InputUtils.validateBoolean(prettyPrint);
        InputUtils.validateBoolean(showXmlDeclaration);
    }

    public static void validateInputs(String includeRootElement, String includeAttributes, String prettyPrint) throws Exception {
        InputUtils.validateBoolean(includeRootElement);
        InputUtils.validateBoolean(includeAttributes);
        InputUtils.validateBoolean(prettyPrint);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy