1   /*
2    * This file is part of Domingo
3    * an Open Source Java-API to Lotus Notes/Domino
4    * hosted at http://domingo.sourceforge.net
5    *
6    * Copyright (c) 2003-2007 Beck et al. projects GmbH München (http://www.bea.de)
7    *
8    * This library is free software; you can redistribute it and/or
9    * modify it under the terms of the GNU Lesser General Public
10   * License as published by the Free Software Foundation; either
11   * version 2.1 of the License, or (at your option) any later version.
12   *
13   * This library is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   * Lesser General Public License for more details.
17   *
18   * You should have received a copy of the GNU Lesser General Public
19   * License along with this library; if not, write to the Free Software
20   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21   */
22  
23  package de.bea.domingo.samples;
24  
25  import java.io.ByteArrayInputStream;
26  import java.io.InputStream;
27  
28  import javax.xml.parsers.DocumentBuilder;
29  import javax.xml.parsers.DocumentBuilderFactory;
30  
31  import org.w3c.dom.Document;
32  
33  import de.bea.domingo.DDatabase;
34  import de.bea.domingo.DDocument;
35  import de.bea.domingo.DDxlExporter;
36  import de.bea.domingo.DNotesFactory;
37  import de.bea.domingo.DSession;
38  import de.bea.domingo.DView;
39  
40  /***
41   * A simple example accessing the local NAB.
42   *
43   * <p>The file NCSO.jar from the Lotus Domino server must be in the classpath to
44   * run this test.</p>
45   *
46   * @author <a href=mailto:kriede@users.sourceforge.net>Kurt Riede</a>
47   */
48  public final class DocumentToDOM {
49  
50      /***
51       * Main method of sample.
52       * 
53       * @param args arguments
54       * @throws Exception If any error occurs
55       */
56      public static void main(final String[] args) throws Exception {
57  
58          // get an instance of the domingo factory
59          DNotesFactory factory = DNotesFactory.getInstance();
60  
61          // create a session to the local Lotus Notes client
62          DSession session = factory.getSession();
63  
64          // get the mail database
65          DDatabase database = session.getMailDatabase();
66  
67          // get the first email
68          DView view = database.getView("$Inbox");
69          DDocument document = (DDocument) view.getAllDocuments().next();
70  
71          // export the email as XML and extract the body as HTML
72          DDxlExporter exporter = session.createDxlExporter();
73          exporter.setOutputDoctype(false);
74          String xmlString = exporter.exportDxl(document);
75          
76          // parse document XML-string into a DOM document
77          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
78          DocumentBuilder builder = dbf.newDocumentBuilder();
79          InputStream inputStream = new ByteArrayInputStream(xmlString.getBytes());
80          Document dom = builder.parse(inputStream);
81  
82          // do something with the document
83          System.out.println("Document created: " + dom.getDocumentElement().getNodeName());
84      }
85  }