001/*
002 * ============================================================================
003 * Copyright © 2002-2026 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.xml.builder.spi;
019
020import org.apiguardian.api.API;
021import org.tquadrat.foundation.annotation.ClassVersion;
022import org.tquadrat.foundation.xml.builder.Namespace;
023
024import java.util.Collection;
025import java.util.Map;
026import java.util.Optional;
027
028import static java.util.Collections.emptyList;
029import static java.util.Collections.emptyMap;
030import static java.util.Collections.emptySet;
031import static org.apiguardian.api.API.Status.MAINTAINED;
032import static org.tquadrat.foundation.lang.Objects.requireNotEmptyArgument;
033import static org.tquadrat.foundation.xml.builder.spi.SGMLPrinter.composeElementString;
034
035/**
036 *  The definition for an SGML element.
037 *
038 *  @extauthor Thomas Thrien - thomas.thrien@tquadrat.org
039 *  @version $Id: Element.java 1158 2026-03-14 16:23:29Z tquadrat $
040 *  @since 0.0.5
041 *
042 *  @UMLGraph.link
043 */
044@ClassVersion( sourceVersion = "$Id: Element.java 1158 2026-03-14 16:23:29Z tquadrat $" )
045@API( status = MAINTAINED, since = "0.0.5" )
046public interface Element
047{
048        /*---------*\
049    ====** Methods **==========================================================
050        \*---------*/
051    /**
052     *  Returns the value for the attribute with the given name.
053     *
054     *  @param  name    The attribute name.
055     *  @return An instance of
056     *      {@link Optional}
057     *      that holds the value for that attribute.
058     */
059    public default Optional<String> getAttribute( final String name )
060    {
061        requireNotEmptyArgument( name, "name" );
062        final Optional<String> retValue = Optional.empty();
063
064        //---* Done *----------------------------------------------------------
065        return retValue;
066    }   //  getAttribute()
067
068    /**
069     *  Provides read access to the attributes.
070     *
071     *  @return A reference to the attributes.
072     */
073    public default Map<String,String> getAttributes() { return emptyMap(); }
074
075    /**
076     *  Provides access to the children for this element; the returned
077     *  collection is not modifiable.
078     *
079     *  @return A reference to the children of this element; if the element
080     *      does not have children, an empty collection will be returned.
081     */
082    public default Collection<? extends Element> getChildren() { return emptyList(); }
083
084    /**
085     *  Returns the name of the element.
086     *
087     *  @return The name of the element.
088     */
089    public String getElementName();
090
091    /**
092     *  Provides access to the namespaces for this element; the returned
093     *  collection is not modifiable.
094     *
095     *  @return A reference to the namespaces of this element; if the element
096     *      does not have namespaces assigned, an empty collection will be
097     *      returned.
098     */
099    public default Collection<Namespace> getNamespaces() { return emptySet(); }
100
101    /**
102     *  Returns the parent of this element.
103     *
104     *  @return An instance of
105     *      {@link Optional}
106     *      that holds the parent.
107     */
108    public Optional<Element> getParent();
109
110    /**
111     *  Returns {@code true} if the element has children, {@code false}
112     *  otherwise.
113     *
114     *  @return {@code true} if the element has children.
115     */
116    public default boolean hasChildren() { return !getChildren().isEmpty(); }
117
118    /**
119     *  <p>{@summary Returns the block flag.}</p>
120     *  <p>This flag is used in the conversion of the element into a String; it
121     *  indicates whether the element is 'inline'
122     *  (like an HTML <i>&lt;span&gt;</i>) or 'block'
123     *  (as an HTML <i>&lt;div&gt;</i>). This is important only for elements
124     *  where whitespace is relevant, like for HTML elements, as
125     *  <i>pretty printing</i> will add additional whitespace around inline
126     *  elements that can become visible on parsing (for HTML: on the rendered
127     *  page).</p>
128     *  <p>XML elements for example will be always <i>block</i> as there
129     *  whitespace is not that important.</p>
130     *  <p>Obviously, {@code true} indicates a block element, while
131     *  {@code false} stands for an inline element.</p>
132     *  <p>The default is {@code true}.</p>
133     *
134     *  @return  The flag.
135     */
136    public default boolean isBlock() { return true; }
137
138    /**
139     *  Sets the parent for this element.
140     *
141     *  @param  <E> The implementation of {@code Element}.
142     *  @param  parent  The parent.
143     */
144    public <E extends Element> void setParent( final E parent );
145
146    /**
147     *  Returns a String representation for this element instance.
148     *
149     *  @param  indentationLevel    The indentation level.
150     *  @param  prettyPrint The pretty print flag.
151     *  @return The String representation.
152     */
153    public default String toString( final int indentationLevel, final boolean prettyPrint )
154    {
155        final var retValue = composeElementString( indentationLevel, prettyPrint, this, true );
156
157        //---* Done *----------------------------------------------------------
158        return retValue;
159    }   //  toString()
160}
161//  interface Element
162
163/*
164 *  End of File
165 */