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

org.xwiki.rendering.internal.renderer.xhtml.XHTMLMetaDataRenderer Maven / Gradle / Ivy

There is a newer version: 8.4-rc-1
Show newest version
/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.rendering.internal.renderer.xhtml;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.inject.Inject;
import javax.inject.Singleton;

import org.xwiki.component.annotation.Component;
import org.xwiki.properties.ConverterManager;
import org.xwiki.rendering.internal.parser.xhtml.wikimodel.XHTMLXWikiGeneratorListener;
import org.xwiki.rendering.listener.MetaData;
import org.xwiki.rendering.renderer.printer.XHTMLWikiPrinter;

/**
 * Renders metadata into Annotated XHTML, i.e., a div or span with the metadata as attributes.
 *
 * @version $Id: 3aa4ab8c1880b3a9d835808b540c4112e2463a9c $
 * @since 14.0RC1
 */
@Component(roles = XHTMLMetaDataRenderer.class)
@Singleton
public class XHTMLMetaDataRenderer
{
    @Inject
    private ConverterManager converterManager;

    /**
     * @return a span element if we are inside an inline macro. Else a div.
     */
    private String getMetadataContainerElement(boolean inline)
    {
        if (inline) {
            return "span";
        } else {
            return "div";
        }
    }

    /**
     * Render the open tag of the metadata.
     *
     * @param printer The output printer.
     * @param inline If the current context is an inline context, i.e., only phrasing content is allowed.
     * @param metaData The metadata to render.
     */
    public void beginRender(XHTMLWikiPrinter printer, boolean inline, MetaData metaData)
    {
        Map attributes = new LinkedHashMap<>();

        for (Map.Entry metadataPair : metaData.getMetaData().entrySet()) {
            String value = this.converterManager.convert(String.class, metadataPair.getValue());
            attributes.put(XHTMLXWikiGeneratorListener.METADATA_ATTRIBUTE_PREFIX + metadataPair.getKey(), value);
        }

        attributes.put("class", XHTMLXWikiGeneratorListener.METADATA_CONTAINER_CLASS);

        printer.printXMLStartElement(getMetadataContainerElement(inline), attributes);
    }

    /**
     * Render the closing tag of the metadata.
     *
     * @param printer The output print.
     * @param inline If the current context is an inline context, i.e., only phrasing content is allowed.
     */
    public void endRender(XHTMLWikiPrinter printer, boolean inline)
    {
        printer.printXMLEndElement(getMetadataContainerElement(inline));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy