com.hfg.xml.parser.XMLRepeatContentHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.xml.parser;
import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import com.hfg.util.StringUtil;
import com.hfg.xml.XMLAttribute;
import com.hfg.xml.XMLUtil;
//------------------------------------------------------------------------------
/**
* XML SAX ContentHandler that reproduces the XML. Useful in conjunction with
* pseudo-SAX parsers that operate on non-XML files (the NcbiASN1BinarySAXParser
* for example.)
*
* @author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------
public class XMLRepeatContentHandler extends AbstractContentHandler
{
private BufferedWriter mWriter;
private String mIndent = " ";
private int mIndentCount;
private METHOD mLastMethod;
private String mCurrentStartTag;
private Collection mCurrentAttributes;
private static enum METHOD { startElement, endElement, characters }
//-----------------------------------------------------------------------
public XMLRepeatContentHandler(OutputStream stream)
{
mWriter = new BufferedWriter(new OutputStreamWriter(stream));
}
//-----------------------------------------------------------------------
public XMLRepeatContentHandler setIndentSize(int inValue)
{
mIndent = StringUtil.polyChar(' ', inValue);
return this;
}
//-----------------------------------------------------------------------
@Override
public void startDocument()
throws SAXException
{
try
{
mWriter.write("");
mWriter.newLine();
}
catch (IOException e)
{
throw new SAXException(e);
}
}
//-----------------------------------------------------------------------
@Override
public void endDocument()
throws SAXException
{
try
{
mWriter.close();
}
catch (IOException e)
{
throw new SAXException(e);
}
}
//-----------------------------------------------------------------------
@Override
public void startElement(String inURI, String inLocalName, String inName, Attributes inAttributes)
{
try
{
if (mLastMethod == METHOD.startElement)
{
mWriter.write(XMLUtil.composeStartTag(mCurrentStartTag, mCurrentAttributes));
mWriter.newLine();
mIndentCount++;
}
mWriter.write(StringUtil.polyString(mIndent, mIndentCount));
mCurrentStartTag = inName;
mCurrentAttributes = null;
if (inAttributes.getLength() > 0)
{
mCurrentAttributes = new ArrayList(inAttributes.getLength());
for (int i = 0; i < inAttributes.getLength(); i++)
{
mCurrentAttributes.add(new XMLAttribute(inAttributes.getQName(i), inAttributes.getValue(i)));
}
}
// mWriter.write(XMLUtil.composeStartTag(inName, inAttributes));
}
catch (IOException e)
{
throw new RuntimeException(e);
}
mLastMethod = METHOD.startElement;
}
//-----------------------------------------------------------------------
@Override
public void endElement(String inURI, String inLocalName, String inName)
{
try
{
if (mLastMethod == METHOD.endElement)
{
mWriter.write(StringUtil.polyString(mIndent, (mIndentCount--) - 1));
}
if (mLastMethod == METHOD.startElement)
{
mWriter.write(XMLUtil.composeStartTag(mCurrentStartTag, mCurrentAttributes, XMLUtil.EMPTY_TAG));
}
else
{
mWriter.write(XMLUtil.composeEndTag(inName));
}
mWriter.newLine();
}
catch (IOException e)
{
throw new RuntimeException(e);
}
mLastMethod = METHOD.endElement;
}
//-----------------------------------------------------------------------
@Override
public void characters(char[] inChars, int offset, int length)
{
try
{
mWriter.write(XMLUtil.composeStartTag(mCurrentStartTag, mCurrentAttributes, XMLUtil.NOT_EMPTY_TAG));
mWriter.write(XMLUtil.escapeContentIfNecessary(new String(inChars, offset, length)));
}
catch (IOException e)
{
throw new RuntimeException(e);
}
mLastMethod = METHOD.characters;
}
}