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 GmbH & Co. KG, 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.main.CmsLog;
032import org.opencms.util.CmsMacroResolver;
033import org.opencms.xml.types.I_CmsXmlContentValue;
034
035import org.apache.commons.logging.Log;
036
037/**
038 * Visitor implementation that provides macro resolving for all visited values.<p>
039 *
040 * This class is used when a new XML content is generated using a default content as model file.<p>
041 *
042 * @since 6.5.5
043 */
044public class CmsXmlContentMacroVisitor implements I_CmsXmlContentValueVisitor {
045
046    /** Static reference to the log. */
047    private static final Log LOG = CmsLog.getLog(CmsXmlContentMacroVisitor.class);
048
049    /** The initialized OpenCms user context (required for VFS access). */
050    CmsObject m_cms;
051
052    /** The macro resolver to use for resolving macros. */
053    CmsMacroResolver m_resolver;
054
055    /**
056     * Creates a new validation node visitor.<p>
057     *
058     * @param cms the initialized OpenCms user context (required for VFS access)
059     * @param resolver the macro resolver to use for resolving macros
060     */
061    public CmsXmlContentMacroVisitor(CmsObject cms, CmsMacroResolver resolver) {
062
063        // store reference to the provided CmsObject
064        m_cms = cms;
065        m_resolver = resolver;
066    }
067
068    /**
069     * @see org.opencms.xml.content.I_CmsXmlContentValueVisitor#visit(org.opencms.xml.types.I_CmsXmlContentValue)
070     */
071    public void visit(I_CmsXmlContentValue value) {
072
073        if (LOG.isDebugEnabled()) {
074            LOG.debug(Messages.get().getBundle().key(Messages.LOG_XMLCONTENT_VISIT_1, value.getPath()));
075        }
076
077        // get original value (for simple types only)
078        if (value.isSimpleType()) {
079            String original = value.getStringValue(m_cms);
080            // resolve the value
081            String resolved = m_resolver.resolveMacros(original);
082            if (!resolved.equals(original)) {
083                // something has been changed, set new value
084                value.setStringValue(m_cms, resolved);
085            }
086        }
087    }
088}