001: /*
002: Copyright (C) 2003 Know Gate S.L. All rights reserved.
003: C/Oņa, 107 1š2 28050 Madrid (Spain)
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions
007: are met:
008:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011:
012: 2. The end-user documentation included with the redistribution,
013: if any, must include the following acknowledgment:
014: "This product includes software parts from hipergate
015: (http://www.hipergate.org/)."
016: Alternately, this acknowledgment may appear in the software itself,
017: if and wherever such third-party acknowledgments normally appear.
018:
019: 3. The name hipergate must not be used to endorse or promote products
020: derived from this software without prior written permission.
021: Products derived from this software may not be called hipergate,
022: nor may hipergate appear in their name, without prior written
023: permission.
024:
025: This library is distributed in the hope that it will be useful,
026: but WITHOUT ANY WARRANTY; without even the implied warranty of
027: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
028:
029: You should have received a copy of hipergate License with this code;
030: if not, visit http://www.hipergate.org or mail to info@hipergate.org
031: */
032:
033: package com.knowgate.dataobjs;
034:
035: import java.io.FileNotFoundException;
036:
037: import java.util.LinkedList;
038: import java.util.ListIterator;
039: import java.io.IOException;
040:
041: import java.sql.SQLException;
042:
043: import java.lang.ClassNotFoundException;
044:
045: import org.xml.sax.Attributes;
046: import org.xml.sax.Parser;
047: import org.xml.sax.SAXException;
048: import org.xml.sax.SAXNotRecognizedException;
049: import org.xml.sax.SAXNotSupportedException;
050: import org.xml.sax.SAXParseException;
051: import org.xml.sax.XMLReader;
052: import org.xml.sax.helpers.XMLReaderFactory;
053: import org.xml.sax.helpers.DefaultHandler;
054: import org.xml.sax.helpers.ParserAdapter;
055: import org.xml.sax.helpers.ParserFactory;
056:
057: import com.knowgate.debug.DebugFile;
058: import com.knowgate.jdc.JDCConnection;
059:
060: /**
061: * <p>SAX Parser Event Handler for loading XML formated data into a DBPersist object.</p>
062: * @author Sergio Montoro Ten
063: * @version 1.0
064: */
065:
066: public class DBSaxHandler extends DefaultHandler {
067:
068: /**
069: * Construct and set reference to DBPersist object that will hold the loaded data.
070: * @deprecated Use DBSaxHandler(DBPersist oPersist, JDCConnection) instead
071: */
072:
073: public DBSaxHandler(DBPersist oPersist) {
074: oTarget = oPersist;
075: oTable = oPersist.getTable();
076: oColList = oTable.getColumns();
077: oColIter = oColList.listIterator();
078: }
079:
080: public DBSaxHandler(DBPersist oPersist, JDCConnection oConn)
081: throws SQLException {
082: oTarget = oPersist;
083: oTable = oPersist.getTable(oConn);
084: oColList = oTable.getColumns();
085: oColIter = oColList.listIterator();
086: }
087:
088: // Data
089:
090: /** Number of elements. */
091: protected long fElements;
092: /** Number of characters. */
093: protected long fCharacters;
094:
095: protected LinkedList oColList;
096: protected ListIterator oColIter;
097: protected DBColumn oColumn;
098: protected DBTable oTable;
099: protected DBPersist oTarget;
100:
101: // -------------------------------------------------------------------------
102:
103: // **********************
104: // ContentHandler methods
105: //
106:
107: /**
108: * Start document.
109: */
110: public void startDocument() throws SAXException {
111:
112: fElements = 0;
113: fCharacters = 0;
114: } // startDocument()
115:
116: // -------------------------------------------------------------------------
117:
118: /**
119: * Start element.
120: */
121:
122: public void startElement(String uri, String local, String raw,
123: Attributes attrs) throws SAXException {
124: fElements++;
125:
126: if (null == oColumn) {
127: oColumn = oTable.getColumnByName(local);
128: }
129:
130: } // startElement(String,String,StringAttributes)
131:
132: // -------------------------------------------------------------------------
133:
134: /**
135: * Characters.
136: */
137:
138: public void characters(char ch[], int start, int length)
139: throws SAXException {
140:
141: fCharacters += length;
142:
143: if (null != oColumn) {
144:
145: if (DebugFile.trace)
146: DebugFile.writeln("DBSaxHendler.characters() parsing "
147: + new String(ch, start, length) + " into "
148: + oColumn.getName() + " as type "
149: + oColumn.getSqlTypeName());
150:
151: try {
152: oTarget.put(oColumn.getName(), new String(ch, start,
153: length), oColumn.getSqlType());
154: } catch (FileNotFoundException fnfe) { /* never thrown */
155: }
156:
157: oColumn = null;
158: } // fi (oColumn)
159: } // characters(char[],int,int);
160:
161: // -------------------------------------------------------------------------
162:
163: // ********************
164: // ErrorHandler methods
165: //
166:
167: /**
168: * Warning.
169: */
170: public void warning(SAXParseException ex) throws SAXException {
171: if (DebugFile.trace)
172: DebugFile.write(composeError("Warning", ex));
173: } // warning(SAXParseException)
174:
175: /**
176: * Error.
177: */
178: public void error(SAXParseException ex) throws SAXException {
179: if (DebugFile.trace)
180: DebugFile.write(composeError("Error", ex));
181: throw ex;
182: } // error(SAXParseException)
183:
184: /**
185: * Fatal error.
186: * */
187: public void fatalError(SAXParseException ex) throws SAXException {
188: if (DebugFile.trace)
189: DebugFile.write(composeError("Fatal Error", ex));
190: throw ex;
191: } // fatalError(SAXParseException)
192:
193: // -------------------------------------------------------------------------
194:
195: // *****************
196: // Protected methods
197: //
198:
199: /**
200: * Compose the error message.
201: */
202: protected String composeError(String type, SAXParseException ex) {
203: String sErrDesc = "";
204: String systemId = null;
205: int index;
206:
207: sErrDesc += "[SAX " + type + "] ";
208:
209: if (ex == null)
210: sErrDesc += "!!!";
211: else
212: systemId = ex.getSystemId();
213:
214: if (systemId != null) {
215: index = systemId.lastIndexOf('/');
216: if (index != -1)
217: systemId = systemId.substring(index + 1);
218: sErrDesc += systemId;
219: }
220:
221: sErrDesc += " Line:" + ex.getLineNumber();
222: sErrDesc += " Column:" + ex.getColumnNumber();
223: sErrDesc += " Cause: " + ex.getMessage();
224: sErrDesc += "\n";
225:
226: return sErrDesc;
227: } // composeError(String,SAXParseException)
228:
229: // -------------------------------------------------------------------------
230:
231: // **************
232: // Public methods
233: //
234:
235: /**
236: * Parses an XML document into a DBPersist instace
237: */
238:
239: public void parse(String sXMLSource) throws InstantiationException,
240: IllegalAccessException, ClassNotFoundException,
241: IOException, SAXException {
242:
243: // local variables
244: XMLReader parser;
245: Parser sax1Parser;
246: long time, timeBefore = 0, timeAfter = 0, memory, memoryBefore = 0, memoryAfter = 0;
247:
248: if (DebugFile.trace) {
249: timeBefore = System.currentTimeMillis();
250: memoryBefore = Runtime.getRuntime().freeMemory();
251: }
252:
253: try {
254: if (DebugFile.trace)
255: DebugFile
256: .writeln("XMLReaderFactory.createXMLReader(DEFAULT_PARSER_NAME)");
257:
258: parser = XMLReaderFactory
259: .createXMLReader(DEFAULT_PARSER_NAME);
260: } catch (Exception e) {
261: sax1Parser = ParserFactory.makeParser(DEFAULT_PARSER_NAME);
262: parser = new ParserAdapter(sax1Parser);
263: if (DebugFile.trace)
264: DebugFile
265: .writeln("warning: Features and properties not supported on SAX1 parsers.");
266: }
267:
268: // parse file
269: parser.setContentHandler(this );
270: parser.setErrorHandler(this );
271:
272: if (DebugFile.trace)
273: DebugFile.writeln("XMLReader.parse(" + sXMLSource + ")");
274:
275: parser.parse(sXMLSource);
276:
277: if (DebugFile.trace) {
278: memoryAfter = Runtime.getRuntime().freeMemory();
279: timeAfter = System.currentTimeMillis();
280:
281: time = timeAfter - timeBefore;
282: memory = memoryBefore - memoryAfter;
283: }
284: } // parse
285:
286: // -------------------------------------------------------------------------
287:
288: // feature ids
289:
290: protected static final String NAMESPACES_FEATURE_ID = "http://xml.org/sax/features/namespaces";
291: protected static final String NAMESPACE_PREFIXES_FEATURE_ID = "http://xml.org/sax/features/namespace-prefixes";
292: protected static final String VALIDATION_FEATURE_ID = "http://xml.org/sax/features/validation";
293: protected static final String SCHEMA_VALIDATION_FEATURE_ID = "http://apache.org/xml/features/validation/schema";
294: protected static final String SCHEMA_FULL_CHECKING_FEATURE_ID = "http://apache.org/xml/features/validation/schema-full-checking";
295: protected static final String DYNAMIC_VALIDATION_FEATURE_ID = "http://apache.org/xml/features/validation/dynamic";
296:
297: // default settings
298:
299: protected static final String DEFAULT_PARSER_NAME = "org.apache.xerces.parsers.SAXParser";
300: protected static final int DEFAULT_REPETITION = 1;
301: protected static final boolean DEFAULT_NAMESPACES = true;
302: protected static final boolean DEFAULT_NAMESPACE_PREFIXES = false;
303: protected static final boolean DEFAULT_VALIDATION = false;
304: protected static final boolean DEFAULT_SCHEMA_VALIDATION = false;
305: protected static final boolean DEFAULT_SCHEMA_FULL_CHECKING = false;
306: protected static final boolean DEFAULT_DYNAMIC_VALIDATION = false;
307: protected static final boolean DEFAULT_MEMORY_USAGE = false;
308: protected static final boolean DEFAULT_TAGGINESS = false;
309: }
|