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.biojava.bio.program.xml;
022
023import java.util.Stack;
024
025import org.xml.sax.Attributes;
026
027/**
028 *
029 * Base XMLWriter class for writing XML representations of Java Value
030 * Objects with bespoke architectures.
031 *
032 * Currently reporting QNames for all attribute values as a fix
033 * for needing to defining namespaces by attributes with QNames.
034 * This may not be ideal.
035 *
036 * <p>
037 * Copyright &copy; 2000 Cambridge Antibody Technology.
038 *
039 * <p>
040 * Primary author -<ul>
041 * <li>Simon Brocklehurst (CAT)
042 * </ul>
043 * Other authors  -<ul>
044 * <li>Tim Dilks          (CAT)
045 * <li>Colin Hardman      (CAT)
046 * <li>Stuart Johnston    (CAT)
047 *</ul>
048 *
049 * @author Cambridge Antibody Technology (CAT)
050 * @author Greg Cox
051 * @version 1.01
052 *
053 */
054public class BaseXMLWriter {
055    private Stack oElementStack;
056    private StringBuffer oStr;
057    private StringBuffer oIndent;
058    private String oLineSeparator;
059    private StringBuffer oSpaceStore;
060    private boolean tLastWritePCData;
061    private boolean tLastWriteStartElement;
062    private String oAttName;
063    private String oAttValue;
064    static final String oHeader = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
065
066    public BaseXMLWriter()
067    {
068        oElementStack = new Stack();
069        oStr = new StringBuffer();
070        oIndent = new StringBuffer();
071        oSpaceStore = new StringBuffer();
072        oLineSeparator = System.getProperty("line.separator");
073        oIndent.setLength(0);
074        tLastWritePCData = false;
075        tLastWriteStartElement = false;
076    }
077
078    private void decreaseIndent()
079    {
080        oIndent.setLength(oIndent.length() - 2);
081    }
082
083    public String endElement()
084    {
085        decreaseIndent();
086        oStr.setLength(0);
087        if (!tLastWriteStartElement && !tLastWritePCData)
088        {
089            oStr.append(nl());
090            oStr.append(indent());
091        }
092        oStr.append("</");
093        oStr.append((String)oElementStack.pop());
094        oStr.append(">");
095        tLastWritePCData = false;
096        tLastWriteStartElement = false;
097        return oStr.substring(0);
098    }
099
100    private void increaseIndent()
101    {
102        oIndent.append("  ");
103    }
104
105    public String indent()
106    {
107        return oIndent.substring(0);
108    }
109
110    public String nl()
111    {
112        return oLineSeparator;
113    }
114
115    public String startElement(String string)
116    {
117        oElementStack.push(string);
118        oStr.setLength(0);
119        if (!tLastWritePCData)
120            oStr.append(nl());
121        oStr.append(indent());
122        oStr.append("<");
123        oStr.append(string);
124        oStr.append(">");
125        increaseIndent();
126        tLastWritePCData = false;
127        tLastWriteStartElement = true;
128        return oStr.substring(0);
129    }
130
131    public String startElement(String string, Attributes attributes)
132    {
133        oElementStack.push(string);
134        oStr.setLength(0);
135        if (!tLastWritePCData)
136            oStr.append(nl());
137        oStr.append(indent());
138        oStr.append("<");
139        oStr.append(string);
140        int i = 0;
141        oSpaceStore.setLength(0);
142        oSpaceStore.append(indent());
143        for (i = 0; i <= string.length(); i++)
144            oSpaceStore.append(" ");
145        for (i = 0; i < attributes.getLength() - 1; i++)
146        {
147            //oAttName = attributes.getLocalName(i);
148            oAttName = attributes.getQName(i);
149            oAttValue = attributes.getValue(i);
150            oStr.append(" ");
151            if (i > 0)
152                oStr.append(oSpaceStore.substring(0));
153            oStr.append(oAttName);
154            oStr.append("=\"");
155            oStr.append(oAttValue);
156            oStr.append("\"");
157            oStr.append(nl());
158        }
159
160        //oAttName = attributes.getLocalName(i);
161        oAttName = attributes.getQName(i);
162        oAttValue = attributes.getValue(i);
163        oStr.append(" ");
164        if (attributes.getLength() > 1)
165            oStr.append(oSpaceStore.substring(0));
166        oStr.append(oAttName);
167        oStr.append("=\"");
168        oStr.append(oAttValue);
169        oStr.append("\"");
170        oStr.append(">");
171        increaseIndent();
172        tLastWritePCData = false;
173        tLastWriteStartElement = true;
174        return oStr.substring(0);
175    }
176
177    public String writeEmptyElement(String string)
178    {
179        oStr.setLength(0);
180        if (!tLastWritePCData)
181            oStr.append(nl());
182        oStr.append(indent());
183        oStr.append("<");
184        oStr.append(string);
185        oStr.append("/>");
186        tLastWritePCData = false;
187        tLastWriteStartElement = false;
188        return oStr.substring(0);
189    }
190
191    public String writeEmptyElement(String string, Attributes attributes)
192    {
193        oStr.setLength(0);
194        if (!tLastWritePCData)
195            oStr.append(nl());
196        oStr.append(indent());
197        oStr.append("<");
198        oStr.append(string);
199        for (int i = 0; i < attributes.getLength(); i++)
200        {
201            //oAttName = attributes.getLocalName(i);
202            oAttName = attributes.getQName(i);
203            oAttValue = attributes.getValue(i);
204            oStr.append(" ");
205            oStr.append(oAttName);
206            oStr.append("=\"");
207            oStr.append(oAttValue);
208            oStr.append("\"");
209        }
210        oStr.append("/>");
211        tLastWritePCData = false;
212        tLastWriteStartElement = false;
213        return oStr.substring(0);
214    }
215
216    public String writeHeader()
217    {
218        oStr.setLength(0);
219        oStr.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
220        tLastWritePCData = false;
221        return oStr.substring(0);
222    }
223
224    public String writePCData(String poPCData)
225    {
226        oStr.setLength(0);
227        oStr.append(poPCData);
228        tLastWritePCData = true;
229        tLastWriteStartElement = false;
230        return oStr.substring(0);
231    }
232}