/* Copyright 2003 Elliotte Rusty Harold
This library is free software; you can redistribute it and/or modify
it under the terms of version 2.1 of the GNU Lesser General Public
License as published by the Free Software Foundation.
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, see
* This Serializer
subclass outputs raw, unescaped text
* from the text nodes, but no markup of any kind. In essence, it
* converts XML into plain text.
*
* Create a new text serializer that uses the UTF-8 encoding. *
* * @param out the output stream to write the document on * * @throws NullPointerException if out is null */ public TextSerializer(OutputStream out) { super(out); } /** ** Create a new serializer that uses a specified encoding. * The encoding must be recognized by the Java virtual machine. *
* * @param out the output stream to write the document on * @param encoding the character encoding for the serialization * * @throws NullPointerException ifout
* or encoding
is null
* @throws UnsupportedEncodingException if the VM does not
* support the requested encoding
*
*/
public TextSerializer(OutputStream out, String encoding)
throws UnsupportedEncodingException {
super(out, encoding);
}
protected void writeStartTag(Element element) {}
protected void writeEmptyElementTag(Element element) {}
protected void writeEndTag(Element element) {}
protected void writeXMLDeclaration() {}
protected void write(Comment comment) {}
protected void write(ProcessingInstruction instruction) {}
protected void write(DocType doctype) {}
// Here we use writeRaw because we don't want characters like &
// and < to be escaped. If they can't be written in the specified
// encoding, an exception is thrown.
protected void writeText(Text text) throws IOException {
writeRaw(text.getValue());
}
public static void main(String[] args) {
if (args.length <= 0) {
System.out.println(
"Usage: java nu.xom.samples.TextSerializer URL");
return;
}
try {
Builder parser = new Builder();
Document doc = parser.build(args[0]);
Serializer serializer = new TextSerializer(System.out);
serializer.write(doc);
}
catch (ParsingException ex) {
System.out.println(args[0] + " is not well-formed.");
System.out.println(" at line " + ex.getLineNumber()
+ ", column " + ex.getColumnNumber());
System.out.println(ex.getMessage());
}
catch (IOException ex) {
System.out.println(
"Due to an IOException, the serialization of "
+ args[0] + " could not be completed."
);
}
}
}