01: //=============================================================================
02: //=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
03: //=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
04: //=== and United Nations Environment Programme (UNEP)
05: //===
06: //=== This program is free software; you can redistribute it and/or modify
07: //=== it under the terms of the GNU General Public License as published by
08: //=== the Free Software Foundation; either version 2 of the License, or (at
09: //=== your option) any later version.
10: //===
11: //=== This program is distributed in the hope that it will be useful, but
12: //=== WITHOUT ANY WARRANTY; without even the implied warranty of
13: //=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: //=== General Public License for more details.
15: //===
16: //=== You should have received a copy of the GNU General Public License
17: //=== along with this program; if not, write to the Free Software
18: //=== Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19: //===
20: //=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
21: //=== Rome - Italy. email: geonetwork@osgeo.org
22: //==============================================================================
23:
24: package org.fao.geonet.csw.common.util;
25:
26: import java.io.IOException;
27: import java.io.InputStream;
28: import java.io.StringReader;
29: import org.jdom.Document;
30: import org.jdom.Element;
31: import org.jdom.JDOMException;
32: import org.jdom.input.SAXBuilder;
33: import org.jdom.output.Format;
34: import org.jdom.output.XMLOutputter;
35:
36: //=============================================================================
37:
38: /** This is a portion of the jeeves.utils.Xml class and is replicated here just
39: * to avoid the jeeves jar
40: */
41:
42: public class Xml {
43: //---------------------------------------------------------------------------
44: //---
45: //--- API methods
46: //---
47: //---------------------------------------------------------------------------
48:
49: /** Loads an xml file and returns its root node (validates the xml with a dtd) */
50:
51: public static Element loadString(String data, boolean validate)
52: throws IOException, JDOMException {
53: SAXBuilder builder = new SAXBuilder(validate);
54: Document jdoc = builder.build(new StringReader(data));
55:
56: return (Element) jdoc.getRootElement().detach();
57: }
58:
59: //--------------------------------------------------------------------------
60: /** Loads an xml stream and returns its root node (validates the xml with a dtd) */
61:
62: public static Element loadStream(InputStream input)
63: throws IOException, JDOMException {
64: SAXBuilder builder = new SAXBuilder();
65: Document jdoc = builder.build(input);
66:
67: return (Element) jdoc.getRootElement().detach();
68: }
69:
70: //---------------------------------------------------------------------------
71: /** Converts an xml element to a string */
72:
73: public static String getString(Element data) {
74: XMLOutputter outputter = new XMLOutputter(Format
75: .getPrettyFormat());
76:
77: return outputter.outputString(data);
78: }
79:
80: //---------------------------------------------------------------------------
81:
82: public static String getString(Document data) {
83: XMLOutputter outputter = new XMLOutputter(Format
84: .getPrettyFormat());
85:
86: return outputter.outputString(data);
87: }
88: }
89:
90: //=============================================================================
|