class TraxUtils {

	public static void doWithResult(Result result, ResultCallback callback) throws Exception {
		if (result instanceof DOMResult) {
			callback.domResult(((DOMResult) result).getNode());
			return;
		} else if (StaxUtils.isStaxResult(result)) {
			XMLStreamWriter streamWriter = StaxUtils.getXMLStreamWriter(result);
			if (streamWriter != null) {
				callback.staxResult(streamWriter);
				return;
			} else {
				XMLEventWriter eventWriter = StaxUtils.getXMLEventWriter(result);
				if (eventWriter != null) {
					callback.staxResult(eventWriter);
					return;
				}
			}
		} else if (result instanceof SAXResult) {
			SAXResult saxSource = (SAXResult) result;
			callback.saxResult(saxSource.getHandler(), saxSource.getLexicalHandler());
			return;
		} else if (result instanceof StreamResult) {
			StreamResult streamSource = (StreamResult) result;
			if (streamSource.getOutputStream() != null) {
				callback.streamResult(streamSource.getOutputStream());
				return;
			} else if (streamSource.getWriter() != null) {
				callback.streamResult(streamSource.getWriter());
				return;
			}
		}
		if (StringUtils.hasLength(result.getSystemId())) {
			String systemId = result.getSystemId();
			callback.result(systemId);
		} else {
			throw new IllegalArgumentException("Unknown Result type: " + result.getClass());
		}
	}

}