private static Channel loadChannel(InputStream in) throws Exception { SAXBuilder builder = new SAXBuilder(false); Document doc = builder.build(in); Element root = doc.getRootElement(); String version = root.getAttributeValue("version"); if (!"1.0".equals(version)) throw new Exception("Invalid file version"); String type = root.getAttributeValue("type"); Class clazz = Class.forName(type); if (!Channel.class.isAssignableFrom(clazz)) throw new Exception("Invalid channel type"); Element elSettings = root.getChild("settings"); return SimpleBeanToXML.xmlToObject(elSettings, clazz, false); }
private static void saveChannel(Channel channel, OutputStream out) throws Exception { Element root = new Element("channel"); root.setAttribute("version", "1.0"); root.setAttribute("type", channel.getClass().getName()); Element elSettings = new Element("settings"); SimpleBeanToXML.objectToXml(elSettings, channel); root.addContent(elSettings); Document doc = new Document(); doc.setRootElement(root); XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat()); xout.output(doc, out); }