001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021package org.biojavax.bio.phylo.io.nexus;
022
023
024/**
025 * Builds a Nexus block from listening to events.
026 * 
027 * @author Richard Holland
028 * @author Tobias Thierer
029 * @author Jim Balhoff
030 * @since 1.6
031 */
032public interface NexusBlockBuilder extends NexusBlockListener {
033
034        /**
035         * Obtain the constructed block.
036         * 
037         * @return the constructed block.
038         */
039        public NexusBlock getNexusBlock();
040
041        /**
042         * This abstract version knows how to build and add comments.
043         */
044        public abstract class Abstract implements NexusBlockBuilder {
045
046                private String blockName;
047
048                private NexusBlock block;
049
050                private NexusComment comment;
051
052                /**
053                 * Obtains the name of this block.
054                 */
055                protected String getBlockName() {
056                        return this.blockName;
057                }
058
059                public void beginComment() {
060                        if (this.comment != null)
061                                this.comment.openSubComment();
062                        else
063                                this.comment = new NexusComment();
064                }
065
066                public void commentText(String comment) {
067                        this.comment.addCommentText(comment);
068                }
069
070                public void endComment() {
071                        if (this.comment != null && this.comment.hasOpenSubComment())
072                                this.comment.closeSubComment();
073                        else {
074                                this.addComment(this.comment);
075                                this.comment = null;
076                        }
077                }
078
079                /**
080                 * Tell the builder to add the given comment at the current location.
081                 * 
082                 * @param comment
083                 *            the comment to add.
084                 * @throws ParseException
085                 *             if the comment was invalid.
086                 */
087                protected abstract void addComment(NexusComment comment);
088
089                public void startBlock(String blockName) {
090                        this.blockName = blockName;
091                        this.block = this.startBlockObject();
092                }
093
094                /**
095                 * Tell the builder to start a new block object.
096                 */
097                protected abstract NexusBlock startBlockObject();
098
099                public NexusBlock getNexusBlock() {
100                        return this.block;
101                }
102        }
103}