1 64 65 package com.jcorporate.expresso.ext.xml.dbobj; 66 67 72 73 import com.jcorporate.expresso.core.dataobjects.DataObject; 74 import com.jcorporate.expresso.core.dataobjects.jdbc.JDBCObjectMetaData; 75 import com.jcorporate.expresso.core.db.DBException; 76 import com.jcorporate.expresso.core.misc.URLUTF8Encoder; 77 import org.xml.sax.SAXException ; 78 import org.xml.sax.SAXParseException ; 79 import org.xml.sax.XMLReader ; 80 import org.xml.sax.helpers.XMLReaderFactory ; 81 82 import java.io.BufferedOutputStream ; 83 import java.io.FileNotFoundException ; 84 import java.io.FileOutputStream ; 85 import java.io.IOException ; 86 import java.io.PrintStream ; 87 import java.io.PrintWriter ; 88 import java.util.Iterator ; 89 import java.util.Vector ; 90 91 92 98 public class XMLDBObject { 99 private DataObject myDBObject = null; 100 private static final String thisClass = XMLDBObject.class.getName() + "."; 101 private Vector errorList = new Vector (3); 102 private String dbName = "default"; 103 104 107 public XMLDBObject() { 108 } 109 110 115 public XMLDBObject(DataObject newDBObject) { 116 myDBObject = newDBObject; 117 } 118 119 120 public void setDBName(String newDBName) { 121 dbName = newDBName; 122 } 123 124 public String getDBName() { 125 return dbName; 126 } 127 128 134 public void exportToFile(String fileName) 135 throws DBException, IOException , FileNotFoundException { 136 exportToFileXML(fileName); 137 } 138 139 140 143 private void exportToFileXML(String fileName) 144 throws FileNotFoundException , IOException , 145 DBException { 146 FileOutputStream fout = new FileOutputStream (fileName + ".xml", true); 147 BufferedOutputStream bout = new BufferedOutputStream (fout); 148 PrintStream XMLFileStream = new PrintStream (bout); 149 exportToStreamXML(new PrintWriter (XMLFileStream)); 150 fout.close(); 151 } 152 153 154 161 public void exportToStreamXML(PrintWriter p) 162 throws DBException { 163 String myName = (thisClass + 164 "exportToStreamXML(PrintWriter)"); 165 String fieldName = null; 166 DataObject oneObj = null; 167 168 if (p == null) { 169 throw new DBException(myName + ":PrintWriter cannot be null"); 170 } 171 if (myDBObject == null) { 172 throw new DBException(myName + 173 ":Database object is not set - cannot export"); 174 } 175 if (myDBObject.count() == 0) { 176 return; 177 } 178 179 p.println("<DBObject>"); 180 p.println(indent(1) + "<ObjectName>" + 181 myDBObject.getClass().getName() + "</ObjectName>"); 182 if (myDBObject.getMetaData() instanceof JDBCObjectMetaData) { 183 p.println(indent(1) + "<TableName>" + ((JDBCObjectMetaData) myDBObject.getMetaData()).getTargetTable() + 184 "</TableName>"); 185 } 186 p.println(indent(1) + "<Data>"); 187 188 for (Iterator rc = myDBObject.searchAndRetrieveList().iterator(); 189 rc.hasNext();) { 190 oneObj = (DataObject) rc.next(); 191 p.println(indent(2) + "<DataRecord>"); 192 193 for (Iterator e = myDBObject.getMetaData().getAllFieldsMap().keySet().iterator(); 194 e.hasNext();) { 195 fieldName = (String ) e.next(); 196 p.println(indent(3) + "<Field>"); 197 p.println(indent(4) + "<FieldName>" + fieldName + 198 "</FieldName>"); 199 200 try { 201 p.println(indent(4) + "<FieldValue>" + 202 URLUTF8Encoder.encode(oneObj.getField(fieldName)) + 203 "</FieldValue>"); 204 } catch (Exception ex) { 205 throw new DBException(ex); 206 } 207 208 p.println(indent(3) + "</Field>"); 209 } 210 211 p.println(indent(2) + "</DataRecord>"); 212 p.flush(); 213 } 214 215 216 p.println(indent(1) + "</Data>"); 217 p.println("</DBObject>"); 218 p.flush(); 219 } 220 221 222 225 public DataObject getDBObject() 226 throws DBException { 227 return myDBObject; 228 } 229 230 231 234 public Vector getErrorList() { 235 return errorList; 236 } 237 238 241 public void importFromFile(String fileName) 242 throws DBException { 243 String myName = (thisClass + "importFromFile(String)"); 244 245 try { 246 XMLImportDocument xi = new XMLImportDocument(myDBObject); 247 xi.setDBName(getDBName()); 248 249 XMLReader xr = XMLReaderFactory.createXMLReader(); 250 xr.setContentHandler(xi); 251 xr.setErrorHandler(xi); 252 xr.parse(fileName + ".xml"); 253 254 errorList = xi.getErrorList(); 256 257 myDBObject = xi.getDBObject(); 259 } catch (SAXParseException pe) { 260 throw new DBException(myName + ":Parser Configuration Exception:" + 261 pe.getMessage()); 262 } catch (SAXException se) { 263 throw new DBException(myName + ":SAX Exception:" + 264 se.getMessage()); 265 } catch (IOException ie) { 266 throw new DBException(myName + ":IO Exception:" + ie.getMessage()); 267 } 268 } 269 270 271 274 public void importFromURL(String importURL) 275 throws DBException { 276 String myName = (thisClass + "importFromURL(String)"); 277 278 try { 279 XMLImportDocument xi = new XMLImportDocument(myDBObject); 280 XMLReader xr = XMLReaderFactory.createXMLReader(); 281 xr.setContentHandler(xi); 282 xr.setErrorHandler(xi); 283 xr.parse(importURL); 284 285 } catch (SAXParseException pe) { 286 throw new DBException(myName + ":Parser Configuration Exception:" + 287 pe.getMessage()); 288 } catch (SAXException se) { 289 throw new DBException(myName + ":SAX Exception:" + 290 se.getMessage()); 291 } catch (IOException ie) { 292 throw new DBException(myName + ":IO Exception:" + ie.getMessage()); 293 } 294 } 295 296 297 304 private String indent(int i) { 305 StringBuffer tabString = new StringBuffer (""); 306 307 for (int j = 0; j < i; j++) { 308 tabString.append(" "); 309 } 310 311 return tabString.toString(); 312 } 313 314 317 public void setDBObject(DataObject newDBObject) 318 throws DBException { 319 myDBObject = newDBObject; 320 } 321 322 323 } 324 325 | Popular Tags |