How to handle Writer, Result, OutputStream in the constructor?

I have a class that encapsulates StAX Writer for a special XML business record. You can click on some domain objects, and the logic generates adequate XML.

I do not want to offer an instance XMLStreamWriterfor security reasons, so this class is only responsible for the record of this author. XMLStreamWriterwill be installed in this class. To support almost all output changes, such as Result, OutputStream, and Writer, I also provide constructs for these changes.

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 it is very ugly, not a neat way to achieve this. Stackoverflow, do you have any hints for me?

+3
1

.

public CustomStreamWriter ([...], Writer writer) throws XMLStreamException {
    this([...], XMLOutputFactory.newInstance().createXMLStreamWriter(writer);
}

protected CustomStreamWriter ([...], XMLStreamWriter outputHandler) {
    this.writer = writer;
}

, , - .

+1

Source: https://habr.com/ru/post/1781834/


All Articles