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
023import java.io.IOException;
024import java.io.Writer;
025
026/**
027 * Represents a Nexus block.
028 * 
029 * @author Richard Holland
030 * @author Tobias Thierer
031 * @author Jim Balhoff
032 * @since 1.6
033 */
034public interface NexusBlock extends NexusObject {
035
036        /**
037         * Get the block name.
038         * 
039         * @return the block name.
040         */
041        public String getBlockName();
042
043        public abstract class Abstract implements NexusBlock {
044                private String blockName;
045
046                /**
047                 * Construct a block with a given name.
048                 * 
049                 * @param blockName
050                 *            the name to give the block.
051                 */
052                public Abstract(final String blockName) {
053                        this.blockName = blockName;
054                }
055
056                public String getBlockName() {
057                        return this.blockName;
058                }
059
060                public void writeObject(final Writer writer) throws IOException {
061                        writer.write("BEGIN " + this.blockName + ";");
062                        writer.write(NexusFileFormat.NEW_LINE);
063                        this.writeBlockContents(writer);
064                        writer.write("END;");
065                        writer.write(NexusFileFormat.NEW_LINE);
066                }
067
068                /**
069                 * Writes a token and correctly substitutes all symbols in it.
070                 * 
071                 * @param writer
072                 *            the writer to write to.
073                 * @param token
074                 *            the token to write.
075                 * @throws IOException
076                 *             if writing failed.
077                 */
078                protected void writeToken(final Writer writer, String token)
079                                throws IOException {
080                        token = token.replaceAll("'", "''");
081                        token = token.replaceAll("_", "'_'");
082                        if (token.trim().length() > 0)
083                                token = token.replaceAll(" ", "_");
084                        if (token.indexOf('[') >= 0 || token.indexOf(']') >= 0)
085                                token = "'" + token + "'";
086                        writer.write(token);
087                }
088
089                /**
090                 * Implement this to write out block contents, not including the BEGIN
091                 * and END tags.
092                 * 
093                 * @param writer
094                 *            the writer to write to.
095                 * @throws IOException
096                 *             if writing failed.
097                 */
098                protected abstract void writeBlockContents(Writer writer)
099                                throws IOException;
100        }
101}