View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.common.content;
20  
21  import java.io.IOException;
22  import java.io.OutputStream;
23  
24  import javax.xml.transform.Source;
25  import javax.xml.transform.dom.DOMSource;
26  
27  import org.jdom2.JDOMException;
28  import org.jdom2.input.DOMBuilder;
29  import org.jdom2.output.XMLOutputter;
30  import org.w3c.dom.Document;
31  
32  /**
33   * Reads MCRContent from a W3C DOM XML document.
34   * 
35   * @author Frank L\u00FCtzenkichen
36   */
37  public class MCRDOMContent extends MCRXMLContent {
38  
39      private Document dom;
40  
41      /**
42       * @param dom the W3C DOM XML document to read from 
43       */
44      public MCRDOMContent(Document dom) {
45          super();
46          this.dom = dom;
47          super.docType = dom.getDoctype() == null ? dom.getDocumentElement().getLocalName() : dom.getDoctype().getName();
48      }
49  
50      @Override
51      public Source getSource() {
52          DOMSource source = new DOMSource(dom);
53          source.setSystemId(systemId);
54          return source;
55      }
56  
57      @Override
58      public void sendTo(OutputStream out) throws IOException {
59          org.jdom2.Document jdom;
60          try {
61              jdom = asXML();
62          } catch (JDOMException ex) {
63              throw new IOException(ex);
64          }
65          new XMLOutputter(format).output(jdom, out);
66      }
67  
68      @Override
69      public org.jdom2.Document asXML() throws JDOMException {
70          return new DOMBuilder().build(dom);
71      }
72  }