Section.java

  1. /*
  2.  *   Copyright (C) Christian Schulte <cs@schulte.it>, 2005-206
  3.  *   All rights reserved.
  4.  *
  5.  *   Redistribution and use in source and binary forms, with or without
  6.  *   modification, are permitted provided that the following conditions
  7.  *   are met:
  8.  *
  9.  *     o Redistributions of source code must retain the above copyright
  10.  *       notice, this list of conditions and the following disclaimer.
  11.  *
  12.  *     o Redistributions in binary form must reproduce the above copyright
  13.  *       notice, this list of conditions and the following disclaimer in
  14.  *       the documentation and/or other materials provided with the
  15.  *       distribution.
  16.  *
  17.  *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  18.  *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  19.  *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  20.  *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  *
  28.  *   $JOMC: Section.java 5043 2015-05-27 07:03:39Z schulte $
  29.  *
  30.  */
  31. package org.jomc.util;

  32. import java.util.ArrayList;
  33. import java.util.List;

  34. /**
  35.  * Section of text.
  36.  *
  37.  * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
  38.  * @version $JOMC: Section.java 5043 2015-05-27 07:03:39Z schulte $
  39.  */
  40. public class Section
  41. {

  42.     /**
  43.      * Constant for the mode during parsing the head content of a section.
  44.      */
  45.     static final int MODE_HEAD = 1;

  46.     /**
  47.      * Constant for the mode during parsing the tail content of a section.
  48.      */
  49.     static final int MODE_TAIL = 2;

  50.     /**
  51.      * The current parsing mode.
  52.      */
  53.     private int mode = MODE_HEAD;

  54.     /**
  55.      * The name of this section.
  56.      */
  57.     private String name;

  58.     /**
  59.      * The parsed head content of this section.
  60.      */
  61.     private StringBuilder headContent;

  62.     /**
  63.      * The parsed tail content of this section.
  64.      */
  65.     private StringBuilder tailContent;

  66.     /**
  67.      * Line marking the start of this section.
  68.      */
  69.     private String startingLine;

  70.     /**
  71.      * Line marking the end of this section.
  72.      */
  73.     private String endingLine;

  74.     /**
  75.      * The child sections of this section.
  76.      */
  77.     private List<Section> sections;

  78.     /**
  79.      * Creates a new {@code Section} instance.
  80.      */
  81.     public Section()
  82.     {
  83.         super();
  84.     }

  85.     /**
  86.      * Gets the name of this section.
  87.      *
  88.      * @return The name of this section or {@code null}.
  89.      */
  90.     public String getName()
  91.     {
  92.         return this.name;
  93.     }

  94.     /**
  95.      * Sets the name of this section.
  96.      *
  97.      * @param value The new name of this section or {@code null}.
  98.      */
  99.     public void setName( final String value )
  100.     {
  101.         this.name = value;
  102.     }

  103.     /**
  104.      * Gets the line marking the start of this section.
  105.      *
  106.      * @return The line marking the start of this section.
  107.      */
  108.     public String getStartingLine()
  109.     {
  110.         return this.startingLine;
  111.     }

  112.     /**
  113.      * Sets the line marking the start of this section.
  114.      *
  115.      * @param value The new line marking the start of this section.
  116.      */
  117.     public void setStartingLine( final String value )
  118.     {
  119.         this.startingLine = value;
  120.     }

  121.     /**
  122.      * Gets the line marking the end of this section.
  123.      *
  124.      * @return The line marking the end of this section.
  125.      */
  126.     public String getEndingLine()
  127.     {
  128.         return this.endingLine;
  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.      * Gets the content of this section preceding any child section content.
  141.      *
  142.      * @return The content of this section preceding any child section content.
  143.      */
  144.     public StringBuilder getHeadContent()
  145.     {
  146.         if ( this.headContent == null )
  147.         {
  148.             this.headContent = new StringBuilder( 512 );
  149.         }

  150.         return this.headContent;
  151.     }

  152.     /**
  153.      * Gets the content of this section succeeding any child section content.
  154.      *
  155.      * @return The content of this section succeeding any child section content.
  156.      */
  157.     public StringBuilder getTailContent()
  158.     {
  159.         if ( this.tailContent == null )
  160.         {
  161.             this.tailContent = new StringBuilder( 512 );
  162.         }

  163.         return this.tailContent;
  164.     }

  165.     /**
  166.      * Gets the child sections of this section.
  167.      * <p>
  168.      * This accessor method returns a reference to the live list, not a snapshot. Therefore any modification you make
  169.      * to the returned list will be present inside the object. This is why there is no {@code set} method for the
  170.      * sections property.
  171.      * </p>
  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.         return this.sections;
  182.     }

  183.     /**
  184.      * Gets a child section matching a given name.
  185.      *
  186.      * @param sectionName The name of the section to return.
  187.      *
  188.      * @return The first child section matching {@code sectionName} or {@code null}, if no such section is found.
  189.      *
  190.      * @throws NullPointerException if {@code sectionName} is {@code null}.
  191.      */
  192.     public Section getSection( final String sectionName )
  193.     {
  194.         if ( sectionName == null )
  195.         {
  196.             throw new NullPointerException( "sectionName" );
  197.         }

  198.         return this.getSection( this, sectionName );
  199.     }

  200.     private Section getSection( final Section current, final String sectionName )
  201.     {
  202.         if ( sectionName.equals( current.getName() ) )
  203.         {
  204.             return current;
  205.         }

  206.         for ( final Section child : current.getSections() )
  207.         {
  208.             if ( sectionName.equals( child.getName() ) )
  209.             {
  210.                 return child;
  211.             }

  212.             if ( child.getName() == null )
  213.             {
  214.                 final Section section = child.getSection( sectionName );

  215.                 if ( section != null )
  216.                 {
  217.                     return section;
  218.                 }
  219.             }
  220.         }

  221.         return null;
  222.     }

  223.     /**
  224.      * Gets the parsing mode of the instance.
  225.      *
  226.      * @return The parsing mode of the instance.
  227.      *
  228.      * @see #MODE_HEAD
  229.      * @see #MODE_TAIL
  230.      */
  231.     int getMode()
  232.     {
  233.         return this.mode;
  234.     }

  235.     /**
  236.      * Sets the parsing mode of the instance.
  237.      *
  238.      * @param value The new parsing mode of the instance.
  239.      *
  240.      * @see #MODE_HEAD
  241.      * @see #MODE_TAIL
  242.      */
  243.     void setMode( final int value )
  244.     {
  245.         this.mode = value;
  246.     }

  247. }