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

com.consol.citrus.xml.schema.RootQNameSchemaMappingStrategy Maven / Gradle / Ivy

/*
 * Copyright 2006-2012 the original author or authors.
 *
 * 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 com.consol.citrus.xml.schema;

import java.util.List;
import java.util.Map;

import javax.xml.namespace.QName;

import org.springframework.util.StringUtils;
import org.springframework.xml.namespace.QNameUtils;
import org.springframework.xml.xsd.XsdSchema;

import com.consol.citrus.exceptions.CitrusRuntimeException;

/**
 * Mapping strategy uses the root element local name to find matching schema
 * instance.
 * 
 * @author Christoph Deppisch
 */
public class RootQNameSchemaMappingStrategy extends AbstractSchemaMappingStrategy {

    /** Root element names mapping to schema instances */
    private Map mappings;
    
    @Override
    public XsdSchema getSchema(List schemas, String namespace, String elementName) {
        XsdSchema schema = null;
        QName rootQName = QNameUtils.createQName(namespace, elementName, "");
        
        if (mappings.containsKey(rootQName.toString())) {
            schema = mappings.get(rootQName.toString());
        } else if (mappings.containsKey(elementName)) {
            schema = mappings.get(elementName);
        }
        
        if (schema!= null && !(StringUtils.hasText(schema.getTargetNamespace()) && 
                schema.getTargetNamespace().equals(namespace))) {
            throw new CitrusRuntimeException("Schema target namespace inconsitency " +
            		"for located XSD schema definition (" + schema.getTargetNamespace() + ")");
        }
        
        return schema;
    }

    /**
     * Sets the mappings.
     * @param mappings the mappings to set
     */
    public void setMappings(Map mappings) {
        this.mappings = mappings;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy