001    /*
002     *   Copyright (c) 2009 The JOMC Project
003     *   Copyright (c) 2005 Christian Schulte <schulte2005@users.sourceforge.net>
004     *   All rights reserved.
005     *
006     *   Redistribution and use in source and binary forms, with or without
007     *   modification, are permitted provided that the following conditions
008     *   are met:
009     *
010     *     o Redistributions of source code must retain the above copyright
011     *       notice, this list of conditions and the following disclaimer.
012     *
013     *     o Redistributions in binary form must reproduce the above copyright
014     *       notice, this list of conditions and the following disclaimer in
015     *       the documentation and/or other materials provided with the
016     *       distribution.
017     *
018     *   THIS SOFTWARE IS PROVIDED BY THE JOMC PROJECT AND CONTRIBUTORS "AS IS"
019     *   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020     *   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021     *   PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JOMC PROJECT OR
022     *   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023     *   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024     *   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025     *   OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026     *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
027     *   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
028     *   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029     *
030     *   $Id: Section.java 1546 2010-03-03 22:19:28Z schulte2005 $
031     *
032     */
033    package org.jomc.util;
034    
035    import java.util.ArrayList;
036    import java.util.List;
037    
038    /**
039     * Section of text.
040     *
041     * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a>
042     * @version $Id: Section.java 1546 2010-03-03 22:19:28Z schulte2005 $
043     */
044    public class Section
045    {
046    
047        /** Constant for the mode during parsing the head content of a section. */
048        static final int MODE_HEAD = 1;
049    
050        /** Constant for the mode during parsing the tail content of a section. */
051        static final int MODE_TAIL = 2;
052    
053        /** The current parsing mode. */
054        private int mode = MODE_HEAD;
055    
056        /** The name of this section. */
057        private String name;
058    
059        /** The parsed head content of this section. */
060        private StringBuilder headContent;
061    
062        /** The parsed tail content of this section. */
063        private StringBuilder tailContent;
064    
065        /** Line marking the start of this section. */
066        private String startingLine;
067    
068        /** Line marking the end of this section. */
069        private String endingLine;
070    
071        /** The child sections of this section. */
072        private List<Section> sections;
073    
074        /** Creates a new {@code Section} instance. */
075        public Section()
076        {
077            super();
078        }
079    
080        /**
081         * Gets the name of this section.
082         *
083         * @return The name of this section or {@code null}.
084         */
085        public String getName()
086        {
087            return this.name;
088        }
089    
090        /**
091         * Sets the name of this section.
092         *
093         * @param value The new name of this section or {@code null}.
094         */
095        public void setName( final String value )
096        {
097            this.name = value;
098        }
099    
100        /**
101         * Gets the line marking the start of this section.
102         *
103         * @return The line marking the start of this section.
104         */
105        public String getStartingLine()
106        {
107            return this.startingLine;
108        }
109    
110        /**
111         * Sets the line marking the start of this section.
112         *
113         * @param value The new line marking the start of this section.
114         */
115        public void setStartingLine( final String value )
116        {
117            this.startingLine = value;
118        }
119    
120        /**
121         * Gets the line marking the end of this section.
122         *
123         * @return The line marking the end of this section.
124         */
125        public String getEndingLine()
126        {
127            return this.endingLine;
128        }
129    
130        /**
131         * Sets the line marking the end of this section.
132         *
133         * @param value The new line marking the end of this section.
134         */
135        public void setEndingLine( final String value )
136        {
137            this.endingLine = value;
138        }
139    
140        /**
141         * Gets the content of this section preceding any child section content.
142         *
143         * @return The content of this section preceding any child section content.
144         */
145        public StringBuilder getHeadContent()
146        {
147            if ( this.headContent == null )
148            {
149                this.headContent = new StringBuilder();
150            }
151    
152            return this.headContent;
153        }
154    
155        /**
156         * Gets the content of this section succeeding any child section content.
157         *
158         * @return The content of this section succeeding any child section content.
159         */
160        public StringBuilder getTailContent()
161        {
162            if ( this.tailContent == null )
163            {
164                this.tailContent = new StringBuilder();
165            }
166    
167            return this.tailContent;
168        }
169    
170        /**
171         * Gets the child sections of this section.
172         *
173         * @return A list of child sections of this section.
174         */
175        public List<Section> getSections()
176        {
177            if ( this.sections == null )
178            {
179                this.sections = new ArrayList<Section>();
180            }
181    
182            return this.sections;
183        }
184    
185        /**
186         * Gets a child section matching a given name.
187         *
188         * @param sectionName The name of the section to return.
189         *
190         * @return The child section matching {@code sectionName} or {@code null} if no such section is found.
191         *
192         * @throws NullPointerException if {@code sectionName} is {@code null}.
193         */
194        public Section getSection( final String sectionName )
195        {
196            if ( sectionName == null )
197            {
198                throw new NullPointerException( "sectionName" );
199            }
200    
201            return this.getSection( this, sectionName );
202        }
203    
204        private Section getSection( final Section current, final String sectionName )
205        {
206            if ( sectionName.equals( current.getName() ) )
207            {
208                return current;
209            }
210    
211            for ( Section child : current.getSections() )
212            {
213                if ( sectionName.equals( child.getName() ) )
214                {
215                    return child;
216                }
217    
218                if ( child.getName() == null )
219                {
220                    final Section section = child.getSection( sectionName );
221    
222                    if ( section != null )
223                    {
224                        return section;
225                    }
226                }
227            }
228    
229            return null;
230        }
231    
232        /**
233         * Gets the parsing mode of the instance.
234         *
235         * @return The parsing mode of the instance.
236         *
237         * @see #MODE_HEAD
238         * @see #MODE_TAIL
239         */
240        int getMode()
241        {
242            return this.mode;
243        }
244    
245        /**
246         * Sets the parsing mode of the instance.
247         *
248         * @param value The new parsing mode of the instance.
249         *
250         * @see #MODE_HEAD
251         * @see #MODE_TAIL
252         */
253        void setMode( final int value )
254        {
255            this.mode = value;
256        }
257    
258    }