ModulePrefs prefs = spec.getModulePrefs();
// detect the encoding declared in the XML source
// note that we do not need to detect the encoding of gadgetXML because
// it is a String and not a stream
String encoding = new XMLDeclarationParser(gadgetXML).parse().get(XMLDeclarationParser.ENCODING);
if (encoding == null || !Charset.isSupported(encoding)) {
throw new UnsupportedEncodingException(encoding);
}
// get the bytes in the declared encoding
byte[] bytes = gadgetXML.getBytes(encoding);