001/*
002 *  Copyright 2019 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.runtime.model.type;
017
018import java.util.Optional;
019
020import org.xml.sax.ContentHandler;
021import org.xml.sax.SAXException;
022
023import org.ametys.runtime.model.ViewItem;
024import org.ametys.runtime.plugin.component.PluginAware;
025
026/**
027 * Interface for model item types
028 */
029public interface ModelItemType extends PluginAware
030{
031    /**
032     * Retrieves the identifier of the parameter type
033     * @return the identifier of the parameter type
034     */
035    public String getId();
036
037    /**
038     * Generates SAX events for the given value
039     * @param contentHandler the {@link ContentHandler} that will receive the SAX events
040     * @param tagName the tag name of the SAX event to generate.
041     * @param value the value to SAX
042     * @param context The context of the data to SAX
043     * @throws SAXException if an error occurs during the SAX events generation
044     */
045    public default void valueToSAX(ContentHandler contentHandler, String tagName, Object value, DataContext context) throws SAXException
046    {
047        valueToSAX(contentHandler, tagName, value, Optional.empty(), context);
048    }
049    
050    /**
051     * Generates SAX events for the given value in edition mode.
052     * This method exist for legacy purposes because some types have to be saxed as JSON
053     * @param contentHandler the {@link ContentHandler} that will receive the SAX events
054     * @param tagName the tag name of the SAX event to generate.
055     * @param value the value to SAX
056     * @param viewItem The optional view item corresponding item that is currently saxed.
057     *  This view item gives context for the SAX event that will be generated here.
058     * @param context The context of the data to SAX
059     * @throws SAXException if an error occurs during the SAX events generation
060     */
061    public void valueToSAXForEdition(ContentHandler contentHandler, String tagName, Object value, Optional<ViewItem> viewItem, DataContext context) throws SAXException;
062    
063    /**
064     * Generates SAX events for the given value
065     * @param contentHandler the {@link ContentHandler} that will receive the SAX events
066     * @param tagName the tag name of the SAX event to generate.
067     * @param value the value to SAX
068     * @param viewItem The optional view item corresponding item that is currently saxed.
069     *  This view item gives context for the SAX event that will be generated here.
070     * @param context The context of the data to SAX
071     * @throws SAXException if an error occurs during the SAX events generation
072     */
073    public void valueToSAX(ContentHandler contentHandler, String tagName, Object value, Optional<ViewItem> viewItem, DataContext context) throws SAXException;
074}