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 org.xml.sax.Attributes;
024import org.xml.sax.SAXException;
025import org.xml.sax.helpers.DefaultHandler;
026
027/**
028 * A simple XML DocumentHandler that processes SAX2 events
029 * to create a sensibly formatted XML as it parsed
030 * without populating objects with data. Note, need to
031 * tidy up parameter names which are poor due to decompiling
032 * a .class file from an accidently deleted .java file!
033 * 
034 * <p>
035 * Copyright &copy; 2000 Cambridge Antibody Technology.
036 * 
037 * <p>
038 * Primary author -<ul>
039 * <li>Simon Brocklehurst (CAT)
040 * </ul>
041 * Other authors  -<ul>
042 * <li>Tim Dilks          (CAT)
043 * <li>Colin Hardman      (CAT)
044 * <li>Stuart Johnston    (CAT)
045 *</ul>
046 *
047 * @author Cambridge Antibody Technology (CAT)
048 * @version 1.0
049 *
050 * @see BaseXMLWriter
051 */
052public class SimpleXMLEmitter extends DefaultHandler {
053    private BaseXMLWriter oXMLWriter;
054    private boolean tEmitQNames;
055    private String oName;
056
057    public SimpleXMLEmitter()
058    {
059        oXMLWriter = new BaseXMLWriter();
060        tEmitQNames = true;
061        System.out.println("<?xml version=\"1.0\"?>");
062    }
063
064    public SimpleXMLEmitter(boolean flag)
065    {
066        oXMLWriter = new BaseXMLWriter();
067        tEmitQNames = true;
068        setEmitQNames(flag);
069        System.out.println("<?xml version=\"1.0\"?>");
070    }
071
072    public void characters(char ach[], int i, int j)
073        throws SAXException
074    {
075        System.out.print(oXMLWriter.writePCData(new String(ach, i, j)));
076    }
077
078    public void endElement(String string1, String string2, String string3)
079    {
080        System.out.print(oXMLWriter.endElement());
081    }
082
083    private boolean isEmitQNames()
084    {
085        return tEmitQNames;
086    }
087
088    private void setEmitQNames(boolean flag)
089    {
090        tEmitQNames = flag;
091    }
092
093    public void startElement(String string1, String string2, String string3, Attributes attributes)
094    {
095        if (isEmitQNames())
096            oName = string3;
097        else
098            oName = string2;
099        if (attributes.getLength() != 0)
100            System.out.print(oXMLWriter.startElement(oName, attributes));
101        else
102            System.out.print(oXMLWriter.startElement(oName));
103    }
104}