I have a class which encapsulates a StAX Writer for a special business XML writing. You can push into some domain objects and the logic generates the adequate XML.
I don’t want to offer the XMLStreamWriter instance for security reasons, so this class is the only responsible to write into this writer. The XMLStreamWriter will be instanced in this class. To support nearly all output variations, such as Result, OutputStream and Writer I also provide constructos for these variations.
public CustomStreamWriter ([...], Result result) {
this([...], (Object) result);
}
public CustomStreamWriter ([...], OutputStream outputStream) {
this([...], (Object) outputStream);
}
public CustomStreamWriter ([...], Writer writer) {
this([...], (Object) writer);
}
protected CustomStreamWriter ([...], Object outputHandler) {
// Initialize some final fields and do some stuff with [...]
XMLOutputFactory factory = XMLOutputFactory.newInstance();
if(outputHandler instanceof OutputStream) {
this.writer = factory.createXMLStreamWriter((OutputStream) outputHandler);
} else if(outputHandler instanceof Result) {
this.writer = factory.createXMLStreamWriter((Result) outputHandler);
} else if(outputHandler instanceof Writer) {
this.writer = factory.createXMLStreamWriter((Writer) outputHandler);
}
}
When I look at it I think this is very ugly and not a neat way to achieve this. Stackoverflow, do you have any hints for me?
I would write it this way.
If the constructor throws an exception, the constructor cannot handle it and create a valid object, so throwing an exception is a sensible way to handle this.