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; 025import java.util.ArrayList; 026import java.util.Iterator; 027import java.util.List; 028 029/** 030 * Represents a Nexus comment, possibly nested. 031 * 032 * @author Richard Holland 033 * @author Tobias Thierer 034 * @author Jim Balhoff 035 * @since 1.6 036 */ 037public class NexusComment implements NexusObject { 038 039 private List components = new ArrayList(); 040 041 private NexusComment nestedComment = null; 042 043 public void openSubComment() { 044 if (this.hasOpenSubComment()) 045 this.nestedComment.openSubComment(); 046 else { 047 this.nestedComment = new NexusComment(); 048 this.components.add(this.nestedComment); 049 } 050 } 051 052 public boolean hasOpenSubComment() { 053 return this.nestedComment != null; 054 } 055 056 public void closeSubComment() { 057 if (this.hasOpenSubComment() && this.nestedComment.hasOpenSubComment()) 058 this.nestedComment.closeSubComment(); 059 else 060 this.nestedComment = null; 061 } 062 063 public void addCommentText(final String text) { 064 if (this.hasOpenSubComment()) 065 this.nestedComment.addCommentText(text); 066 else 067 this.components.add(text); 068 } 069 070 /** 071 * This iterator iterates over all parts of the comment. Each item returned 072 * is either a String or a NexusComment. 073 * 074 * @return an iterator over the comment components. 075 */ 076 public Iterator commentIterator() { 077 return this.components.iterator(); 078 } 079 080 public void writeObject(final Writer writer) throws IOException { 081 writer.write('['); 082 for (final Iterator i = this.components.iterator(); i.hasNext();) { 083 final Object obj = (Object) i.next(); 084 if (obj instanceof NexusComment) 085 ((NexusComment) obj).writeObject(writer); 086 else { 087 String text = (String) obj; 088 text = text.replaceAll("'", "''"); 089 if (text.indexOf('[') >= 0 || text.indexOf(']') >= 0) 090 text = "'" + text + "'"; 091 writer.write(text); 092 } 093 } 094 writer.write(']'); 095 } 096 097}