001/*
002 * ============================================================================
003 * Copyright © 2002-2020 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 static java.util.Collections.emptyList;
021import static java.util.Collections.emptyMap;
022import static java.util.Collections.emptySet;
023import static org.apiguardian.api.API.Status.MAINTAINED;
024import static org.tquadrat.foundation.lang.Objects.requireNotEmptyArgument;
025import static org.tquadrat.foundation.xml.builder.spi.SGMLPrinter.composeElementString;
026
027import java.util.Collection;
028import java.util.Map;
029import java.util.Optional;
030
031import org.apiguardian.api.API;
032import org.tquadrat.foundation.annotation.ClassVersion;
033import org.tquadrat.foundation.xml.builder.Namespace;
034
035/**
036 *  The definition for an SGML element.
037 *
038 *  @extauthor Thomas Thrien - thomas.thrien@tquadrat.org
039 *  @version $Id: Element.java 980 2022-01-06 15:29:19Z tquadrat $
040 *  @since 0.0.5
041 *
042 *  @UMLGraph.link
043 */
044@ClassVersion( sourceVersion = "$Id: Element.java 980 2022-01-06 15:29:19Z 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     *  Returns the block flag.<br>
120     *  <br>This flag is used in the conversion of the element into a String;
121     *  it indicates whether the element is 'inline' (like an HTML
122     *  <i>&lt;span&gt;</i>) or 'block' (as an HTML <i>&lt;div&gt;</i>). This is
123     *  important only for elements where whitespace is relevant, like for HTML
124     *  elements, as <i>pretty printing</i> will add additional whitespace
125     *  around inline elements that can become visible on parsing (for HTML: on
126     *  the rendered page).<br>
127     *  <br>XML elements for example will be always <i>block</i> as there
128     *  whitespace is not that important.<br>
129     *  <br>Obviously, {@code true} indicates a block element, while
130     *  {@code false} stands for an inline element.<br>
131     *  <br>The default is {@code true}.
132     *
133     *  @return  The flag.
134     */
135    public default boolean isBlock() { return true; }
136
137    /**
138     *  Sets the parent for this element.
139     *
140     *  @param  <E> The implementation of {@code Element}.
141     *  @param  parent  The parent.
142     */
143    public <E extends Element> void setParent( final E parent );
144
145    /**
146     *  Returns a String representation for this element instance.
147     *
148     *  @param  indentationLevel    The indentation level.
149     *  @param  prettyPrint The pretty print flag.
150     *  @return The String representation.
151     */
152    public default String toString( final int indentationLevel, final boolean prettyPrint )
153    {
154        final var retValue = composeElementString( indentationLevel, prettyPrint, this, true );
155
156        //---* Done *----------------------------------------------------------
157        return retValue;
158    }   //  toString()
159}
160//  interface Element
161
162/*
163 *  End of File
164 */