001/*
002 *  Copyright 2018 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.core.impl.model.type.xml;
017
018import java.util.ArrayList;
019import java.util.List;
020
021import org.apache.avalon.framework.configuration.Configuration;
022import org.apache.avalon.framework.configuration.ConfigurationException;
023import org.apache.commons.lang3.StringUtils;
024
025import org.ametys.core.model.type.AbstractStringElementType;
026import org.ametys.runtime.model.type.xml.XMLElementType;
027
028/**
029 * Class for string XML element type
030 */
031public class StringXMLElementType extends AbstractStringElementType implements XMLElementType<String>
032{
033    /**
034     * {@inheritDoc}}
035     * The value is a string, so the default value is an empty string if it can not be read
036     */
037    @Override
038    public Object readValueFromNode(Configuration elementConfiguration) throws ConfigurationException
039    {
040        if (elementConfiguration == null)
041        {
042            return null;
043        }
044        
045        // Multiple element management
046        Configuration[] valuesConfiguration = elementConfiguration.getChildren("value");
047        if (valuesConfiguration.length > 0)
048        {
049            List<String> values = new ArrayList<>();
050            for (Configuration valueConfiguration : valuesConfiguration)
051            {
052                String value = StringUtils.defaultString(parseConfiguration(valueConfiguration));
053                values.add(value);
054            }
055            return values.toArray(String[]::new);
056        }
057        else
058        {
059            // Simple element management
060            return StringUtils.defaultString(parseConfiguration(elementConfiguration));
061        }
062    }
063    
064}