| SevenBitXMLEncoder.java |
/*
* $Id: SevenBitXMLEncoder.java,v 1.18 2003/07/07 07:59:12 znerd Exp $
*/
package org.znerd.xmlenc;
import java.io.IOException;
import java.io.Writer;
/**
* XML encoder that outputs 7-bit characters only.
*
* <p />This encoder makes at least one call to either
* {@link Writer#write(int) Writer.write(int)} or
* {@link Writer#write(int) Writer.write(String)} for each character written
* (in {@link XMLEncoder#text(Writer,String,boolean)}). This is why it is advised to
* use buffered {@link Writer} implementations, especially if a lot of
* non-7-bit characters are written.
*
* @version $Revision: 1.18 $ $Date: 2003/07/07 07:59:12 $
* @author Ernst de Haan (<a href="mailto:znerd@FreeBSD.org">znerd@FreeBSD.org</a>)
*/
final class SevenBitXMLEncoder extends XMLEncoder {
//-------------------------------------------------------------------------
// Class functions
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
// Class fields
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
// Constructor
//-------------------------------------------------------------------------
/**
* Constructs a new <code>SevenBitXMLEncoder</code> for the specified
* encoding.
*
* @param encoding
* the encoding, not <code>null</code>.
*
* @throws IllegalArgumentException
* if <code>encoding == null</code>.
*/
SevenBitXMLEncoder(String encoding)
throws IllegalArgumentException {
if (encoding == null) {
throw new IllegalArgumentException("encoding == null");
}
_encoding = encoding;
}
//-------------------------------------------------------------------------
// Fields
//-------------------------------------------------------------------------
/**
* The encoding. Cannot be <code>null</code>.
*/
private final String _encoding;
//-------------------------------------------------------------------------
// Methods
//-------------------------------------------------------------------------
public String getEncoding() {
return _encoding;
}
public void declaration(Writer out)
throws NullPointerException, IOException {
out.write("<?xml version=\"1.0\" encoding=\"");
out.write(_encoding);
out.write("\"?>");
}
public void text(Writer out, char c) throws IOException {
final char ceil = (char) 128;
if (c < ceil) {
out.write(c);
} else {
out.write("&#");
out.write(Integer.toString(c));
out.write(";");
}
}
}