Source code: javatools/db/xml/XTable.java
1 /*
2 * XTable.java
3 *
4 * Created on 1 aprile 2003, 18.22
5 Javatools (modified version) - Some useful general classes.
6 Copyright (C) 2002-2003 Chris Bitmead (original) Antonio Petrelli (modified)
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program 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
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 Contact me at: brenmcguire@users.sourceforge.net
23 *
24 * This file contains code from the "XMLGrammarBuilder.java" of "Xerces2-J"
25 * package from Apache. Here is their Copyright notice:
26 *
27 * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
28 * reserved.
29 */
30
31 package javatools.db.xml;
32
33 import java.util.*;
34 import java.io.*;
35 import javax.xml.parsers.*;
36 import org.xml.sax.*;
37 import javatools.db.*;
38 import javatools.xml.*;
39 import javatools.util.Props;
40 import org.apache.xerces.parsers.*;
41 import org.apache.xerces.util.*;
42 import org.apache.xerces.impl.Constants;
43 import org.apache.xerces.xni.grammars.*;
44 import org.apache.xerces.xni.parser.*;
45
46 /**
47 *
48 * @author Antonio Petrelli
49 * @version 0.2.0
50 */
51 public class XTable extends AbstractTable implements DbResult {
52
53 public static final int RESULT_TYPE = 1;
54 public static final int LOG_TYPE = 2;
55
56 /** Creates a new instance of XTable */
57 public XTable() {
58 colNameMap = new HashMap();
59 }
60
61 public void setSource(InputStream stream, boolean doValidation, XsdTypeMap typeMap,
62 SAXParserFactory factory, SymbolTable sym, XMLGrammarPool grammarPool)
63 throws DbException {
64 XTableHandler handler;
65 XMLReader reader;
66 DbIterator rowIt;
67 String localConfigDir;
68 javax.xml.parsers.SAXParser parser;
69
70 handler = new XTableHandler(this, typeMap);
71 localConfigDir = Props.getLocalConfigDir();
72 if (!localConfigDir.endsWith("/"))
73 localConfigDir += "/";
74 try {
75 parser = factory.newSAXParser();
76 if (doValidation) {
77 XMLUtils.configureParser(parser, sym, grammarPool);
78 }
79 parser.parse(stream, handler);
80 }
81 catch (IOException e) {
82 throw new DbException(e.getMessage());
83 }
84 catch (SAXException e) {
85 throw new DbException(e.getMessage());
86 }
87 catch (ParserConfigurationException e) {
88 throw new DbException(e.getMessage());
89 }
90 if (responseType == LOG_TYPE) {
91 rowIt = this.iterator();
92 if (rowIt.hasNextRow())
93 throw new DbException((String) rowIt.nextRow().getValue(0));
94 }
95 }
96
97 public void setSource(InputStream stream) throws DbException {
98 setSource(stream, doValidation, typeMap, factory, sym, grammarPool);
99 }
100 /** Closes this table.
101 * @throws DbException If something goes wrong.
102 */
103 public void close() throws DbException {
104 resultList.clear();
105 }
106
107 /** Return an iterator to iterate over the rows in this table. <P>
108 *
109 * @return The requested iterator.
110 */
111 public DbIterator iterator() {
112 return new XIterator(this);
113 }
114
115 public int getResponseType() {
116 return responseType;
117 }
118
119 public static void setValidating(boolean value) throws DbException {
120 doValidation = value;
121 }
122
123 public static boolean isValidating() {
124 return doValidation;
125 }
126
127 public static void setDefaultComponents(XsdTypeMap pTypeMap,
128 SAXParserFactory pFactory, SymbolTable pSym, XMLGrammarPool pGrammarPool) {
129 typeMap = pTypeMap;
130 factory = pFactory;
131 sym = pSym;
132 grammarPool = pGrammarPool;
133 }
134
135 protected LinkedList resultList;
136 protected int responseType;
137 protected static XsdTypeMap typeMap;
138 protected static SAXParserFactory factory;
139 protected static SymbolTable sym;
140 protected static XMLGrammarPool grammarPool;
141 protected static boolean doValidation = false;
142 }