X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=src%2Fde%2Fj32%2Fpimstuff%2Fconduit%2FFritzAddressbookImporter.java;h=82c61d78f5887a7a478e0ccf667ddafadf608ee3;hb=5ac05364dc652686046f01849b810da6ffef1192;hp=6a65a882c6fd20bf3ed2d3ada1797058bf7765a6;hpb=047cf507ebfb3b790d824845741f169ace317b5f;p=jpim.git diff --git a/src/de/j32/pimstuff/conduit/FritzAddressbookImporter.java b/src/de/j32/pimstuff/conduit/FritzAddressbookImporter.java index 6a65a88..82c61d7 100644 --- a/src/de/j32/pimstuff/conduit/FritzAddressbookImporter.java +++ b/src/de/j32/pimstuff/conduit/FritzAddressbookImporter.java @@ -11,57 +11,66 @@ import de.j32.pimstuff.data.Entry; import de.j32.pimstuff.data.EntryConsumer; import de.j32.util.XmlUtil; -public class FritzAddressbookImporter - implements Importer +public class FritzAddressbookImporter implements Importer { - Document xml_; - - public FritzAddressbookImporter(InputStream is) - throws SAXException, IOException - { - xml_ = XmlUtil.parse(is); - } - - @Override - public void sendTo(EntryConsumer consumer) - { - for (Element node : XmlUtil.iterateElements(xml_.getElementsByTagName("contact"))) { - /* subnodes: - * category (unused, always 0) - * person/realName - * person/imageURL - * telephony/number (@prio, @type, @vanity) - * services/email - * mod_time - */ - Entry entry = new Entry(); + Document xml_; - try { - entry.name(node.getElementsByTagName("realName").item(0).getTextContent()); + /** + * Importer reading Addressbook from Fritzbox XML file + * + * @param is InputStream providing the XML data. After successful + * construction, the class takes responsibility for closing the + * stream. + * @throws SAXException + * @throws IOException + */ + public FritzAddressbookImporter(InputStream is) throws SAXException, IOException + { + // It does not make sens to try / finally here, at least conceptually: + // Since the base-class constructor might throw we would never get a + // chance to // properly close is. Thus I deem it safer to only take + // responsibility for is when the constructor does NOT throw and place + // the try finally into the callers code. + xml_ = XmlUtil.parse(is); + is.close(); + } - for (Element phone : XmlUtil.iterateElements(node.getElementsByTagName("number"))) { - entry.attribute("phone", phone.getAttribute("type"), phone.getTextContent()); - } - - try { - entry.attribute("email", "", - node.getElementsByTagName("email").item(0).getTextContent()); - } - catch (NullPointerException e) {} // ignore missing optional email - } - catch (NullPointerException e) { - // Ignore incomplete entries - entry = null; - } + @Override + public void sendTo(EntryConsumer consumer) + { + for (Element node : XmlUtil.iterate(xml_.getElementsByTagName("contact"), Element.class)) { - if (entry != null) - consumer.consume(entry); - } - } + // subnodes: category (unused, always 0) person/realName + // person/imageURL telephony/number (@prio, @type, @vanity) + // services/email mod_time + Entry entry = new Entry(); + + try { + entry.name(node.getElementsByTagName("realName").item(0).getTextContent()); + + for (Element phone : XmlUtil.iterate(node.getElementsByTagName("number"), + Element.class)) { + entry.attribute("phone", phone.getAttribute("type"), phone.getTextContent()); + } + + try { + entry.attribute("email", "", node.getElementsByTagName("email").item(0) + .getTextContent()); + } + catch (NullPointerException e) {} // ignore missing optional + // email + } + catch (NullPointerException e) { + // Ignore incomplete entries + entry = null; + } + + if (entry != null) consumer.consume(entry); + } + } + + @Override + public void close() throws IOException + {} - @Override - public void close() - throws IOException - {} - }