* @return the stream object of the given object value
*/
public static InputStream toInputStream(Object value, Map<String, Object> properties) {
if (value == null) {
throw new LoggedRuntimeException("Cannot convert null object to a stream", log);
}
if (value instanceof InputStream) {
return (InputStream) value;
}
if (log.isDebugEnabled()) {
log.debug("Value to be converted to stream : " + value);
}
if (value instanceof OMElement) {
if (properties != null) {
String sourceFormat = (String) properties.get(CommonsConstants.SOURCE);
if (!CommonsConstants.FORMAT_XML.equals(sourceFormat)) {
// if the rule format is native , it have to be wrapped by CDATA
String source = ((OMElement) (value)).getText();
if (source != null && !"".equals(source)) {
return new ByteArrayInputStream(source.trim().getBytes());
}
}
}
OMElement omElement = (OMElement) value;
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
try {
omElement.serialize(arrayOutputStream);
return new ByteArrayInputStream(arrayOutputStream.toByteArray());
} catch (XMLStreamException e) {
throw new LoggedRuntimeException("Error converting to a Stream from OMElement : " +
value, e, log);
}
} else if (value instanceof OMText) {
DataHandler dataHandler = (DataHandler) ((OMText) value).getDataHandler();
if (dataHandler != null) {
try {
return dataHandler.getInputStream();
} catch (IOException e) {
throw new LoggedRuntimeException("Error in reading content as a stream " +
"from DataHandler", log);
}
}
} else if (value instanceof URI) {
try {
return ((URI) (value)).toURL().openStream();
} catch (IOException e) {
throw new LoggedRuntimeException("Error opening stream form URI " + value, e, log);
}
} else if (value instanceof String) {
return new ByteArrayInputStream(((String) value).trim().getBytes());
} else {
throw new LoggedRuntimeException("Cannot convert object to a Stream : " + value, log);
}
return null;
}