001/*
002 *  Copyright 2017 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 */
016
017package org.ametys.plugins.repository.dom;
018
019import java.util.HashMap;
020import java.util.Iterator;
021import java.util.Locale;
022import java.util.Map;
023
024import org.w3c.dom.Element;
025import org.w3c.dom.Node;
026
027import org.ametys.core.util.dom.AbstractWrappingAmetysElement;
028import org.ametys.core.util.dom.AmetysAttribute;
029import org.ametys.core.util.dom.AmetysText;
030import org.ametys.plugins.repository.metadata.MultilingualString;
031
032/**
033 * DOM layer for a locale value of a {@link MultilingualString} metadata
034 */
035public class LocaleStringElement extends AbstractWrappingAmetysElement<Locale>
036{
037    private MultilingualString _multilingualString;
038    private Locale _locale;
039    
040    /**
041     * Constructor.
042     * @param locale the underlying {@link Locale}.
043     * @param multilingualString the parent {@link MultilingualString} holding the locale values
044     */
045    public LocaleStringElement(Locale locale, MultilingualString multilingualString)
046    {
047        this(locale, multilingualString, null);
048    }
049
050    /**
051     * Constructor.
052     * @param locale the locale of underlying metadata
053     * @param multilingualString the parent {@link MultilingualString} holding the languages values
054     * @param parent the parent {@link Element}.
055     */
056    public LocaleStringElement(Locale locale, MultilingualString multilingualString, MultilingualStringElement parent)
057    {
058        super(locale, parent);
059        _locale = locale;
060        _multilingualString = multilingualString;
061    }
062    
063    @Override
064    public String getTagName()
065    {
066        return _object.getLanguage();
067    }
068    
069    @Override
070    protected Map<String, AmetysAttribute> _lookupAttributes()
071    {
072        return new HashMap<>();
073    }
074    
075    @Override
076    public boolean hasChildNodes()
077    {
078        return _multilingualString.hasLocale(_locale);
079    }
080    
081    @Override
082    public Node getFirstChild()
083    {
084        return new AmetysText(_multilingualString.getValue(_locale), this);
085    }
086    
087    @Override
088    public Node getNextSibling()
089    {
090        if (_parent == null)
091        {
092            return null;
093        }
094        
095        boolean isNext = false;
096        Locale nextSibling = null;
097        
098        Iterator<Locale> brothers = _multilingualString.getLocales().iterator();
099        while (nextSibling == null && brothers.hasNext())
100        {
101            Locale brother = brothers.next();
102            
103            if (isNext)
104            {
105                nextSibling = brother;
106            }
107            else if (_object.getLanguage().equals(brother.getLanguage()))
108            {
109                isNext = true;
110            }
111        }
112        
113        if (nextSibling == null)
114        {
115            return null;
116        }
117        
118        return new LocaleStringElement(nextSibling, _multilingualString, (MultilingualStringElement) getParentNode());
119    }
120}