001: package org.mandarax.xkb;
002:
003: /*
004: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: import java.io.File;
022: import java.io.FileWriter;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.io.OutputStream;
026: import java.io.Writer;
027: import java.net.URL;
028: import org.apache.log4j.Category;
029: import org.jdom.Document;
030: import org.jdom.JDOMException;
031: import org.jdom.input.SAXBuilder;
032: import org.jdom.output.Format;
033: import org.jdom.output.XMLOutputter;
034: import org.mandarax.kernel.KnowledgeBase;
035:
036: /**
037: * Manager for managing drivers and adding support like xml data source access
038: * via writers and readers, streams, urls and files.
039: * <br>
040: * Changed in version 1.9: output uses "pretty printing".
041: * <br>
042: * <strong>Important Note:</strong>In version 2.2 the similar ZKB framework has been added. In the long term
043: * ZKB will replace XKB! For details, see the <code>org.mandarax.zkb</code> package.
044: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
045: * @version 3.4 <7 March 05>
046: * @since 1.4
047: */
048: public class XKBManager {
049:
050: private SAXBuilder saxBuilder = null;
051: private boolean validate = false;
052: private Category LOG = null;
053: private XKBDriver driver = null;
054:
055: /**
056: * Constructor.
057: */
058: public XKBManager() {
059: super ();
060: }
061:
062: /**
063: * Exports a knowledge base to a file.
064: * @param kb a knowledge base
065: * @param target a file
066: * @throws an XKBException is thrown if export fails
067: */
068: public void exportKnowledgeBase(File target, KnowledgeBase kb)
069: throws XKBException {
070: try {
071: Writer writer = new FileWriter(target);
072: exportKnowledgeBase(writer, kb);
073: writer.close();
074: } catch (IOException x) {
075: throw new XKBException(x.getMessage());
076: }
077: }
078:
079: /**
080: * Exports a knowledge base to an output stream.
081: * @param kb a knowledge base
082: * @param target an output stream
083: * @throws an XKBException is thrown if export fails
084: */
085: public void exportKnowledgeBase(OutputStream target,
086: KnowledgeBase kb) throws XKBException {
087: if (driver == null)
088: throw new XKBException(
089: "Cannot export knowledge base without driver");
090:
091: try {
092: Document doc = driver.exportKnowledgeBase(kb);
093:
094: // com.mdcs.joi.Inspector.inspect(doc);
095: XMLOutputter xmlOut = new XMLOutputter(Format
096: .getPrettyFormat());
097: String stringifiedXML = xmlOut.outputString(doc);
098: // xmlOut.output (stringifiedXML, System.out);
099: xmlOut.output(doc, target);
100: } catch (IOException x) {
101: throw new XKBException(x.getMessage(), x);
102: }
103: }
104:
105: /**
106: * Exports a knowledge base to a writer.
107: * @param kb a knowledge base
108: * @param target a writer
109: * @throws an XKBException is thrown if export fails
110: */
111: public void exportKnowledgeBase(Writer target, KnowledgeBase kb)
112: throws XKBException {
113: if (driver == null) {
114: throw new XKBException(
115: "Cannot export knowledge base without driver");
116: }
117: try {
118: Document doc = driver.exportKnowledgeBase(kb);
119: (new XMLOutputter(Format.getPrettyFormat())).output(doc,
120: target);
121: } catch (IOException x) {
122: throw new XKBException(x.getMessage(), x);
123: }
124: }
125:
126: /**
127: * Get the document from the data sorce.
128: * @return a document
129: * @param File the data source
130: */
131: protected Document getDocument(File data) throws XKBException {
132: if (data == null)
133: throw new XKBException("Cannot build document from null");
134: try {
135: return getSAXBuilder().build(data);
136: } catch (JDOMException x) {
137: throw new XKBException(x.getMessage(), x);
138: } catch (IOException x) {
139: throw new XKBException(x.getMessage(), x);
140: }
141: }
142:
143: /**
144: * Get the document from the data sorce.
145: * @return a document
146: * @param InputStream the data source
147: */
148: protected Document getDocument(InputStream data)
149: throws XKBException {
150: if (data == null) {
151: throw new XKBException("Cannot build document from null");
152: }
153:
154: try {
155: return getSAXBuilder().build(data);
156: } catch (JDOMException x) {
157: throw new XKBException(x.getMessage(), x);
158: } catch (IOException x) {
159: throw new XKBException(x.getMessage(), x);
160: }
161: }
162:
163: /**
164: * Get the document from the data sorce.
165: * @return a document
166: * @param URL the data source
167: */
168: protected Document getDocument(URL data) throws XKBException {
169: if (data == null) {
170: throw new XKBException("Cannot build document from null");
171: }
172:
173: try {
174: return getSAXBuilder().build(data);
175: } catch (JDOMException x) {
176: throw new XKBException(x.getMessage(), x);
177: } catch (IOException x) {
178: throw new XKBException(x.getMessage(), x);
179: }
180: }
181:
182: /**
183: * Get the SAX builder.
184: * @return the SAX builder
185: */
186: private SAXBuilder getSAXBuilder() {
187: if (saxBuilder == null) {
188: saxBuilder = new SAXBuilder(validate);
189: }
190:
191: return saxBuilder;
192: }
193:
194: /**
195: * Import a knowledge base from a file source.
196: * @return a knowledge base
197: * @param source a file
198: * @throws an XKBException is thrown if import fails
199: */
200: public KnowledgeBase importKnowledgeBase(File source)
201: throws XKBException {
202: if (driver == null) {
203: throw new XKBException(
204: "Cannot import knowledge base without driver");
205: } else {
206: return driver.importKnowledgeBase(getDocument(source));
207: }
208: }
209:
210: /**
211: * Import a knowledge base from an input stream source.
212: * @return a knowledge base
213: * @param source an input stream
214: * @throws an XKBException is thrown if import fails
215: */
216: public KnowledgeBase importKnowledgeBase(InputStream source)
217: throws XKBException {
218: if (driver == null) {
219: throw new XKBException(
220: "Cannot import knowledge base without driver");
221: } else {
222: return driver.importKnowledgeBase(getDocument(source));
223: }
224: }
225:
226: /**
227: * Import a knowledge base from a url source.
228: * @return a knowledge base
229: * @param source a url
230: * @throws an XKBException is thrown if import fails
231: */
232: public KnowledgeBase importKnowledgeBase(URL source)
233: throws XKBException {
234: if (driver == null) {
235: throw new XKBException(
236: "Cannot import knowledge base without driver");
237: } else {
238: return driver.importKnowledgeBase(getDocument(source));
239: }
240: }
241:
242: /**
243: * Set a new driver.
244: * @param aDriver a driver
245: */
246: public void setDriver(XKBDriver aDriver) {
247: driver = aDriver;
248: }
249:
250: /**
251: * Set the SAX builder. E.g., here we can set a special XML parser.
252: * Note that validation of the builder and validation set here could become inconsistent!
253: * @param builder a SAX builder
254: */
255: public void setSAXBuilder(SAXBuilder builder) {
256: saxBuilder = builder;
257: }
258: }
|