1 32 33 package com.knowgate.dataobjs; 34 35 import java.io.FileNotFoundException ; 36 37 import java.util.LinkedList ; 38 import java.util.ListIterator ; 39 import java.io.IOException ; 40 41 import java.sql.SQLException ; 42 43 import java.lang.ClassNotFoundException ; 44 45 import org.xml.sax.Attributes ; 46 import org.xml.sax.Parser ; 47 import org.xml.sax.SAXException ; 48 import org.xml.sax.SAXNotRecognizedException ; 49 import org.xml.sax.SAXNotSupportedException ; 50 import org.xml.sax.SAXParseException ; 51 import org.xml.sax.XMLReader ; 52 import org.xml.sax.helpers.XMLReaderFactory ; 53 import org.xml.sax.helpers.DefaultHandler ; 54 import org.xml.sax.helpers.ParserAdapter ; 55 import org.xml.sax.helpers.ParserFactory ; 56 57 import com.knowgate.debug.DebugFile; 58 import com.knowgate.jdc.JDCConnection; 59 60 65 66 public class DBSaxHandler extends DefaultHandler { 67 68 72 73 public DBSaxHandler(DBPersist oPersist) { 74 oTarget = oPersist; 75 oTable = oPersist.getTable(); 76 oColList = oTable.getColumns(); 77 oColIter = oColList.listIterator(); 78 } 79 80 public DBSaxHandler(DBPersist oPersist, JDCConnection oConn) 81 throws SQLException { 82 oTarget = oPersist; 83 oTable = oPersist.getTable(oConn); 84 oColList = oTable.getColumns(); 85 oColIter = oColList.listIterator(); 86 } 87 88 90 91 protected long fElements; 92 93 protected long fCharacters; 94 95 protected LinkedList oColList; 96 protected ListIterator oColIter; 97 protected DBColumn oColumn; 98 protected DBTable oTable; 99 protected DBPersist oTarget; 100 101 103 107 110 public void startDocument() throws SAXException { 111 112 fElements = 0; 113 fCharacters = 0; 114 } 116 118 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 } 132 134 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 " + new String (ch,start,length) + " into " + oColumn.getName() + " as type " + oColumn.getSqlTypeName() ); 147 148 try { 149 oTarget.put(oColumn.getName(), new String (ch,start,length), oColumn.getSqlType()); 150 } 151 catch (FileNotFoundException fnfe) { } 152 153 oColumn = null; 154 } } 157 159 163 166 public void warning(SAXParseException ex) throws SAXException { 167 if (DebugFile.trace) DebugFile.write(composeError("Warning", ex)); 168 } 170 173 public void error(SAXParseException ex) throws SAXException { 174 if (DebugFile.trace) DebugFile.write(composeError("Error", ex)); 175 throw ex; 176 } 178 181 public void fatalError(SAXParseException ex) throws SAXException { 182 if (DebugFile.trace) DebugFile.write(composeError("Fatal Error", ex)); 183 throw ex; 184 } 186 188 192 195 protected String composeError(String type, SAXParseException ex) { 196 String sErrDesc = ""; 197 String systemId = null; 198 int index; 199 200 sErrDesc += "[SAX " + type + "] "; 201 202 if (ex==null) 203 sErrDesc += "!!!"; 204 else 205 systemId = ex.getSystemId(); 206 207 if (systemId != null) { 208 index = systemId.lastIndexOf('/'); 209 if (index != -1) systemId = systemId.substring(index + 1); 210 sErrDesc += systemId; 211 } 212 213 sErrDesc += " Line:" + ex.getLineNumber(); 214 sErrDesc += " Column:" + ex.getColumnNumber(); 215 sErrDesc += " Cause: " + ex.getMessage(); 216 sErrDesc += "\n"; 217 218 return sErrDesc; 219 } 221 223 227 230 231 public void parse(String sXMLSource) 232 throws InstantiationException ,IllegalAccessException ,ClassNotFoundException ,IOException ,SAXException { 233 234 XMLReader parser; 236 Parser sax1Parser; 237 long time, timeBefore=0, timeAfter=0, memory, memoryBefore=0, memoryAfter=0; 238 239 if (DebugFile.trace) { 240 timeBefore = System.currentTimeMillis(); 241 memoryBefore = Runtime.getRuntime().freeMemory(); 242 } 243 244 try { 245 if (DebugFile.trace) 246 DebugFile.writeln("XMLReaderFactory.createXMLReader(DEFAULT_PARSER_NAME)"); 247 248 parser = XMLReaderFactory.createXMLReader(DEFAULT_PARSER_NAME); 249 } 250 catch (Exception e) { 251 sax1Parser = ParserFactory.makeParser(DEFAULT_PARSER_NAME); 252 parser = new ParserAdapter (sax1Parser); 253 if (DebugFile.trace) 254 DebugFile.writeln("warning: Features and properties not supported on SAX1 parsers."); 255 } 256 257 parser.setContentHandler(this); 259 parser.setErrorHandler(this); 260 261 if (DebugFile.trace) 262 DebugFile.writeln("XMLReader.parse(" + sXMLSource + ")"); 263 264 parser.parse(sXMLSource); 265 266 if (DebugFile.trace) { 267 memoryAfter = Runtime.getRuntime().freeMemory(); 268 timeAfter = System.currentTimeMillis(); 269 270 time = timeAfter - timeBefore; 271 memory = memoryBefore - memoryAfter; 272 } 273 } 275 277 279 protected static final String NAMESPACES_FEATURE_ID = "http://xml.org/sax/features/namespaces"; 280 protected static final String NAMESPACE_PREFIXES_FEATURE_ID = "http://xml.org/sax/features/namespace-prefixes"; 281 protected static final String VALIDATION_FEATURE_ID = "http://xml.org/sax/features/validation"; 282 protected static final String SCHEMA_VALIDATION_FEATURE_ID = "http://apache.org/xml/features/validation/schema"; 283 protected static final String SCHEMA_FULL_CHECKING_FEATURE_ID = "http://apache.org/xml/features/validation/schema-full-checking"; 284 protected static final String DYNAMIC_VALIDATION_FEATURE_ID = "http://apache.org/xml/features/validation/dynamic"; 285 286 288 protected static final String DEFAULT_PARSER_NAME = "org.apache.xerces.parsers.SAXParser"; 289 protected static final int DEFAULT_REPETITION = 1; 290 protected static final boolean DEFAULT_NAMESPACES = true; 291 protected static final boolean DEFAULT_NAMESPACE_PREFIXES = false; 292 protected static final boolean DEFAULT_VALIDATION = false; 293 protected static final boolean DEFAULT_SCHEMA_VALIDATION = false; 294 protected static final boolean DEFAULT_SCHEMA_FULL_CHECKING = false; 295 protected static final boolean DEFAULT_DYNAMIC_VALIDATION = false; 296 protected static final boolean DEFAULT_MEMORY_USAGE = false; 297 protected static final boolean DEFAULT_TAGGINESS = false; 298 }
| Popular Tags
|