001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.xml.content;
029
030import org.opencms.file.CmsObject;
031import org.opencms.relations.CmsLink;
032import org.opencms.util.CmsUUID;
033import org.opencms.xml.CmsXmlUtils;
034import org.opencms.xml.I_CmsXmlDocument;
035import org.opencms.xml.types.CmsXmlVfsFileValue;
036import org.opencms.xml.types.I_CmsXmlContentValue;
037
038import java.util.ArrayList;
039import java.util.Collections;
040import java.util.Comparator;
041import java.util.List;
042import java.util.Locale;
043
044import com.google.common.collect.ComparisonChain;
045import com.google.common.collect.Lists;
046
047/**
048 * Represents the concrete location of an XML content value.<p>
049 *
050 * @since 8.0.0
051 */
052public class CmsXmlContentValueLocation implements I_CmsXmlContentValueLocation {
053
054    /** The XML content value. */
055    private I_CmsXmlContentValue m_value;
056
057    /**
058     * Constructs a new XML content value location.<p>
059     *
060     * @param value the XML content value
061     */
062    public CmsXmlContentValueLocation(I_CmsXmlContentValue value) {
063
064        if (value == null) {
065            throw new UnsupportedOperationException("Can't create content value location with a null value.");
066        }
067        m_value = value;
068    }
069
070    /**
071     * @see org.opencms.xml.content.I_CmsXmlContentValueLocation#asId(org.opencms.file.CmsObject)
072     */
073    public CmsUUID asId(CmsObject cms) {
074
075        CmsLink link = ((CmsXmlVfsFileValue)m_value).getLink(cms);
076        if (link == null) {
077            return null;
078        }
079        return link.getStructureId();
080    }
081
082    /**
083     * @see org.opencms.xml.content.I_CmsXmlContentValueLocation#asString(org.opencms.file.CmsObject)
084     */
085    public String asString(CmsObject cms) {
086
087        return m_value.getStringValue(cms);
088    }
089
090    /**
091     * @see org.opencms.xml.content.I_CmsXmlContentLocation#getDocument()
092     */
093    public I_CmsXmlDocument getDocument() {
094
095        return m_value.getDocument();
096    }
097
098    /**
099     * @see org.opencms.xml.content.I_CmsXmlContentLocation#getLocale()
100     */
101    public Locale getLocale() {
102
103        return m_value.getLocale();
104    }
105
106    /**
107     * @see org.opencms.xml.content.I_CmsXmlContentLocation#getSubValue(java.lang.String)
108     */
109    public CmsXmlContentValueLocation getSubValue(String subPath) {
110
111        Locale locale = m_value.getLocale();
112
113        I_CmsXmlContentValue subValue = m_value.getDocument().getValue(
114            CmsXmlUtils.concatXpath(m_value.getPath(), subPath),
115            locale);
116        if (subValue != null) {
117            return new CmsXmlContentValueLocation(subValue);
118        }
119        return null;
120    }
121
122    /**
123     * @see org.opencms.xml.content.I_CmsXmlContentLocation#getSubValues(java.lang.String)
124     */
125    public List<I_CmsXmlContentValueLocation> getSubValues(String subPath) {
126
127        List<I_CmsXmlContentValueLocation> result = new ArrayList<I_CmsXmlContentValueLocation>();
128        String requiredLastElement = CmsXmlUtils.getLastXpathElement(subPath);
129        Locale locale = m_value.getLocale();
130        List<I_CmsXmlContentValue> subValues = Lists.newArrayList(
131            m_value.getDocument().getValues(CmsXmlUtils.concatXpath(m_value.getPath(), subPath), locale));
132
133        Collections.sort(subValues, new Comparator<I_CmsXmlContentValue>() {
134
135            public int compare(I_CmsXmlContentValue firstValue, I_CmsXmlContentValue secondValue) {
136
137                String firstPath = CmsXmlUtils.removeXpathIndex(firstValue.getPath());
138                String secondPath = CmsXmlUtils.removeXpathIndex(secondValue.getPath());
139                int firstIndex = CmsXmlUtils.getXpathIndexInt(firstValue.getPath());
140                int secondIndex = CmsXmlUtils.getXpathIndexInt(secondValue.getPath());
141                int comparisonResult = ComparisonChain.start().compare(firstPath, secondPath).compare(
142                    firstIndex,
143                    secondIndex).result();
144                return comparisonResult;
145            }
146        });
147
148        for (I_CmsXmlContentValue subValue : subValues) {
149            if (subValue != null) {
150                // if subPath is the path of one option of a choice element, getValues() will, strangely,
151                // return all values of the choice, regardless of their name, so we need to check
152                // the name by hand
153                String lastElement = CmsXmlUtils.getLastXpathElement(subValue.getPath());
154                if (lastElement.equals(requiredLastElement)) {
155                    result.add(new CmsXmlContentValueLocation(subValue));
156                }
157            }
158        }
159        return result;
160    }
161
162    /**
163     * Returns the content value at the given location.<p>
164     *
165     * @return the content value at the given location
166     */
167    public I_CmsXmlContentValue getValue() {
168
169        return m_value;
170    }
171
172}