source: trunk/gsdl3/src/java/org/greenstone/gsdl3/util/XMLConverter.java@ 3501

Last change on this file since 3501 was 3501, checked in by kjdon, 22 years ago

small change

  • Property svn:keywords set to Author Date Id Revision
File size: 3.3 KB
Line 
1/*
2 * XMLConverter.java
3 * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program 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 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program 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 this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19package org.greenstone.gsdl3.util;
20
21// XML classes
22import org.w3c.dom.Document;
23import org.w3c.dom.Node;
24import org.xml.sax.InputSource;
25import javax.xml.parsers.*;
26import org.apache.xindice.xml.TextWriter;
27import org.apache.xerces.dom.TextImpl;
28import org.apache.xerces.parsers.DOMParser;
29
30// other java classes
31import java.io.Reader;
32import java.io.StringReader;
33import java.io.File;
34import java.io.FileReader;
35
36/** XMLConverter - utitlity class for greenstone
37 *
38 * parses XML Strings into Documents, converts Nodes to Strings
39 * different parsers have different behaviour - can experiment in here
40 *
41 * @author <a href="mailto:[email protected]">Katherine Don</a>
42 * @version $Revision: 3501 $
43 *
44 */
45public class XMLConverter {
46
47 /** JAXP parser factory */
48 protected DocumentBuilderFactory doc_build_fact_=null;
49 /** JAXP parser */
50 protected DocumentBuilder doc_builder_=null;
51
52 /** xerces parser */
53 protected DOMParser parser_ = null;
54
55 /** the no-args constructor */
56 public XMLConverter() {
57 try {
58 doc_build_fact_ = DocumentBuilderFactory.newInstance();
59
60 doc_builder_ = doc_build_fact_.newDocumentBuilder();
61 parser_ = new DOMParser();
62 } catch (Exception e) {
63 System.out.println("XMLConverter:exception "+e.getMessage());
64 }
65 }
66
67 /** returns a DOM Document */
68 public Document getDOM(String in) {
69
70 try {
71 Reader reader = new StringReader(in);
72 InputSource xml_source = new InputSource(reader);
73
74 // Document doc = doc_builder_.parse(xml_source);
75 parser_.parse(xml_source);
76 Document doc = parser_.getDocument();
77 return doc;
78
79 } catch (Exception e) {
80 System.out.println("XMLConverter.getDOM(String): exception "+e.getMessage());
81 }
82 return null;
83 }
84
85 /** returns a DOM Document */
86 public Document getDOM(File in) {
87 try {
88
89 Reader reader = new FileReader(in);
90 InputSource xml_source = new InputSource(reader);
91
92 //Document doc = doc_builder_.parse(xml_source);
93 parser_.parse(xml_source);
94 Document doc = parser_.getDocument();
95
96 return doc;
97
98 } catch (Exception e) {
99 System.out.println("XMLConverter.getDOM(File): exception "+e.getMessage());
100 }
101 return null;
102 }
103
104 /** creates a new empty DOM Document */
105 public Document newDOM() {
106 Document doc = doc_builder_.newDocument();
107 return doc;
108 }
109
110 /** returns the Node as a String */
111 public String getString(Node doc) {
112 if (doc==null) {
113 return "";
114 }
115 TextWriter t = new TextWriter(doc);
116 return t.toString();
117 }
118}
Note: See TracBrowser for help on using the repository browser.