001/*
002 * ============================================================================
003 * Copyright © 2002-2023 by Thomas Thrien.
004 * All Rights Reserved.
005 * ============================================================================
006 * Licensed to the public under the agreements of the GNU Lesser General Public
007 * License, version 3.0 (the "License"). You may obtain a copy of the License at
008 *
009 *      http://www.gnu.org/licenses/lgpl.html
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014 * License for the specific language governing permissions and limitations
015 * under the License.
016 */
017
018package org.tquadrat.foundation.svg;
019
020import static org.apiguardian.api.API.Status.STABLE;
021import static org.tquadrat.foundation.lang.Objects.requireNonNullArgument;
022import static org.tquadrat.foundation.xml.builder.XMLElement.Flags.ALLOWS_CHILDREN;
023
024import org.apiguardian.api.API;
025import org.tquadrat.foundation.annotation.ClassVersion;
026import org.tquadrat.foundation.svg.internal.SVGElementImpl;
027
028/**
029 *  This is a generic implementation for an SVG element. This should only be
030 *  used for elements that are not (yet) implemented with their own
031 *  classes.<br>
032 *  <br>The element allows all possible children and attributes as well as
033 *  text. But if it should be added to an already existing paren element, it
034 *  must be registered to it first, before it could be added.
035 *
036 *  @extauthor Thomas Thrien - thomas.thrien@tquadrat.org
037 *  @version $Id: SVGGenericElement.java 1074 2023-10-02 12:05:06Z tquadrat $
038 *  @since 0.0.5
039 *
040 *  @UMLGraph.link
041 */
042@ClassVersion( sourceVersion = "$Id: SVGGenericElement.java 1074 2023-10-02 12:05:06Z tquadrat $" )
043@API( status = STABLE, since = "0.0.5" )
044public final class SVGGenericElement extends SVGElementAdapter
045{
046        /*--------------*\
047    ====** Constructors **=====================================================
048        \*--------------*/
049    /**
050     *  Creates a new {@code SVGGenericElement} instance.
051     *
052     *  @param  elementName The name of the element.
053     *  @param  flags   The flags that determine the behaviour of the new
054     *      element.
055     */
056    SVGGenericElement( final String elementName, final Flags... flags )
057    {
058        super( elementName, flags );
059    }   //  SVGGenericElement()
060
061        /*---------*\
062    ====** Methods **==========================================================
063        \*---------*/
064
065    /**
066     *  Registers this element as a valid child with the given parent. More
067     *  precisely, it adds the
068     *  {@linkplain #getElementName()}
069     *  of this element to the parent's list of valid children. This means that
070     *  it is not necessary to repeat this call for other elements with the
071     *  same name.
072     *
073     *  @param  parent  The parent element.
074     */
075    public final void registerWithParent( final SVGElement parent)
076    {
077        if( (requireNonNullArgument( parent, "parent" ) instanceof final SVGElementImpl parentElement) )
078        {
079            if( !parentElement.getFlags().contains( ALLOWS_CHILDREN ))
080            {
081                throw new IllegalArgumentException( "Element '%s' does not allow children".formatted( parent.getElementName() ) );
082            }
083            parentElement.registerValidChildren( getElementName() );
084        }
085        else
086        {
087            throw new IllegalArgumentException( "Element class '%2$s' does not extend '%1$s'".formatted( SVGElementImpl.class.getName(), parent.getClass().getName() ) );
088        }
089    }   //  registerWithParent()
090}
091//  class SVGGenericElement
092
093/*
094 *  End of File
095 */