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

org.xlcloud.service.heat.parsers.commons.IntrinsicFunctionParser Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2012 AMG.lab, a Bull Group Company
 *
 * 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 org.xlcloud.service.heat.parsers.commons;

import java.util.Iterator;

import org.json.JSONException;
import org.json.JSONObject;
import org.xlcloud.service.heat.parsers.JSONParser;
import org.xlcloud.service.heat.template.commons.functions.Base64;
import org.xlcloud.service.heat.template.commons.functions.FindInMap;
import org.xlcloud.service.heat.template.commons.functions.FnJoin;
import org.xlcloud.service.heat.template.commons.functions.GetAtt;
import org.xlcloud.service.heat.template.commons.functions.IntrinsicFunction;
import org.xlcloud.service.heat.template.commons.functions.Ref;
import org.xlcloud.service.heat.template.fields.IntrinsicFunctionField;

/**
 * {@link JSONParser} for {@link IntrinsicFunction}
 *
 * @author Tomek Adamczewski, AMG.net
 */
public class IntrinsicFunctionParser implements JSONParser {

    /** {@inheritDoc} */
    @Override
    public IntrinsicFunction fromJSON(JSONObject json) throws JSONException {
        IntrinsicFunctionField functionField = getFunctionField(json);
        return getFunction(functionField, (JSONObject) json);
    }

    private IntrinsicFunctionField getFunctionField(JSONObject json) throws JSONException {
        @SuppressWarnings( "unchecked" )
        Iterator keyIterator = ((JSONObject) json).keys();
        if (!keyIterator.hasNext()) {
            throw new JSONException("No intrinsic function");
        }

        String key = keyIterator.next();
        if (!IntrinsicFunctionField.isFunction(key)) {
            throw new JSONException("'" + key + "' is not an intrinsic function");
        }
        if (keyIterator.hasNext()) {
            throw new JSONException("No additional keys are allowed after an intrinsic function '" + key + "'");
        }
        return IntrinsicFunctionField.getFunctionField(key);
    }

    private IntrinsicFunction getFunction(IntrinsicFunctionField functionField, JSONObject json) {
        if (IntrinsicFunctionField.REF.equals(functionField)) {
            return new Ref(json);
        }
        if (IntrinsicFunctionField.JOIN.equals(functionField)) {
            return new FnJoin(json);
        }
        if (IntrinsicFunctionField.BASE64.equals(functionField)) {
            return new Base64(json);
        }
        if (IntrinsicFunctionField.GET_ATT.equals(functionField)) {
            return new GetAtt(json);
        }
        if (IntrinsicFunctionField.FIND_IN_MAP.equals(functionField)) {
            return new FindInMap(json);
        }
        return null;
    }

    @Override
    public JSONObject fromPojo(IntrinsicFunction pojo) throws JSONException {
        if (pojo.get() == null) {
            return null;
        }
        return pojo.get();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy