001: /* ====================================================================
002: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
003: *
004: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by Jcorporate Ltd.
021: * (http://www.jcorporate.com/)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. "Jcorporate" and product names such as "Expresso" must
026: * not be used to endorse or promote products derived from this
027: * software without prior written permission. For written permission,
028: * please contact info@jcorporate.com.
029: *
030: * 5. Products derived from this software may not be called "Expresso",
031: * or other Jcorporate product names; nor may "Expresso" or other
032: * Jcorporate product names appear in their name, without prior
033: * written permission of Jcorporate Ltd.
034: *
035: * 6. No product derived from this software may compete in the same
036: * market space, i.e. framework, without prior written permission
037: * of Jcorporate Ltd. For written permission, please contact
038: * partners@jcorporate.com.
039: *
040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
043: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
044: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
045: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
046: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
051: * SUCH DAMAGE.
052: * ====================================================================
053: *
054: * This software consists of voluntary contributions made by many
055: * individuals on behalf of the Jcorporate Ltd. Contributions back
056: * to the project(s) are encouraged when you make modifications.
057: * Please send them to support@jcorporate.com. For more information
058: * on Jcorporate Ltd. and its products, please see
059: * <http://www.jcorporate.com/>.
060: *
061: * Portions of this software are based upon other open source
062: * products and are subject to their respective licenses.
063: */
064:
065: package com.jcorporate.expresso.ext.xml.dbobj;
066:
067: import com.jcorporate.expresso.core.dataobjects.DataObject;
068: import com.jcorporate.expresso.core.dataobjects.Defineable;
069: import com.jcorporate.expresso.core.db.DBException;
070: import com.jcorporate.expresso.core.dbobj.DBObject;
071: import com.jcorporate.expresso.core.misc.URLUTF8Encoder;
072: import com.jcorporate.expresso.kernel.util.FastStringBuffer;
073: import org.apache.log4j.Logger;
074: import org.xml.sax.Attributes;
075: import org.xml.sax.SAXException;
076: import org.xml.sax.helpers.DefaultHandler;
077:
078: import java.util.Iterator;
079: import java.util.Vector;
080:
081: /**
082: * Insert the type's description here.
083: * Creation date: (4/6/00 8:12:40 PM)
084: *
085: * @author Michael Nash
086: */
087: public class XMLImportDocument extends DefaultHandler {
088: private DataObject myDBObject = null;
089: private String currentItem = ("None");
090: private FastStringBuffer theData = new FastStringBuffer();
091: private static final String this Class = XMLImportDocument.class
092: .getName()
093: + ".";
094: private String currentFieldName = ("None");
095: private Vector errorList = new Vector(3);
096: int recordCount = 0;
097: private String dbName = "default";
098: private static Logger log = Logger
099: .getLogger(XMLImportDocument.class);
100:
101: /**
102: * XMLImportDocument constructor.
103: */
104: public XMLImportDocument() {
105: super ();
106: } /* XMLImportDocument() */
107:
108: /**
109: * XMLImportDocument constructor.
110: *
111: * @param newDBObject The DataObject being set.
112: */
113: public XMLImportDocument(DataObject newDBObject) {
114: super ();
115: myDBObject = newDBObject;
116: } /* XMLImportDocument(DataObject) */
117:
118: public void setDBName(String newName) {
119: dbName = newName;
120: }
121:
122: public String getDBName() {
123: return dbName;
124: }
125:
126: /**
127: * DefaultHandler implementation for SAX parsing.
128: *
129: * @param ch
130: * @param start
131: * @param length
132: */
133: public void characters(char[] ch, int start, int length)
134: throws SAXException {
135: for (int i = start; i < start + length; i++) {
136: theData.append(ch[i]);
137: }
138:
139: if (log.isDebugEnabled()) {
140: log.debug("Data for " + currentItem + ":"
141: + theData.toString());
142: }
143: } /* characters(char[], int, int) */
144:
145: /**
146: * DefaultHandler implementation for SAX parsing.
147: * Creation date: (4/6/00 8:27:22 PM)
148: */
149: public void endDocument() throws SAXException {
150: log.debug("End document");
151:
152: if (myDBObject != null) {
153: saveDBObject();
154: } else {
155: throw new SAXException(
156: "Database object is null - Unable to save object");
157: }
158: } /* endDocument() */
159:
160: /**
161: * DefaultHandler implementation for SAX parsing.
162: *
163: * @param uri
164: * @param localpart
165: * @param rawname
166: */
167: public void endElement(String uri, String localpart, String rawname)
168: throws SAXException {
169: String myName = (this Class + "endElement(String, String, String)");
170: log.debug("End:" + rawname);
171:
172: if (rawname.equals("ObjectName")) {
173: log.debug("Record count was " + recordCount);
174:
175: /* New object */
176: recordCount = 0;
177:
178: try {
179: Class c = Class.forName(theData.toString());
180: myDBObject = (DataObject) c.newInstance();
181: myDBObject.setDataContext(getDBName());
182: } catch (ClassNotFoundException cn) {
183: throw new SAXException(myName
184: + ":Database object not found:"
185: + cn.getMessage());
186: } catch (InstantiationException ie) {
187: throw new SAXException(myName
188: + ":Database object cannot be instantiated:"
189: + ie.getMessage());
190: } catch (IllegalAccessException iae) {
191: throw new SAXException(myName
192: + ":Illegal access loading Database object:"
193: + iae.getMessage());
194: } catch (Exception eeother) {
195: throw new SAXException(eeother.getMessage());
196: }
197: } else if (rawname.equals("FieldName")) {
198: currentFieldName = theData.toString();
199: } else if (rawname.equals("FieldValue")) {
200: if (myDBObject != null) {
201: try {
202: if (myDBObject.getFieldMetaData(currentFieldName) == null) {
203: throw new SAXException(myName
204: + ":Error setting field "
205: + currentFieldName + ":"
206: + "Field does not exist");
207: } else if (myDBObject.getFieldMetaData(
208: currentFieldName).isVirtual()) {
209: log.debug("Skipping virtual field "
210: + currentFieldName);
211: } else {
212: myDBObject.set(currentFieldName, URLUTF8Encoder
213: .decode(theData.toString()));
214: }
215: } catch (DBException de) {
216: throw new SAXException(myName
217: + ":Error setting field "
218: + currentFieldName + ":" + de.getMessage());
219: }
220: }
221: }
222:
223: if (rawname.equals("DataRecord")) {
224: log.debug("--> Saving datarecord");
225:
226: if (myDBObject != null) {
227: saveDBObject();
228: } else {
229: throw new SAXException(
230: "DataObject was null - Error saving object");
231: }
232: }
233:
234: theData.clear();
235: } /* endElement(String) */
236:
237: /**
238: * The set of exceptions that have occurred during processing.
239: * Creation date: (4/6/00 8:27:22 PM)
240: *
241: * @return
242: */
243: public Vector getErrorList() {
244: return errorList;
245: } /* getErrorList() */
246:
247: /**
248: * Insert the method's description here.
249: * Creation date: (4/6/00 8:27:22 PM)
250: */
251: public void saveDBObject() {
252: try {
253: log.debug("Saving database object");
254:
255: if (myDBObject == null) {
256: throw new DBException("DataObject is null, cannot save");
257: }
258:
259: /* If an object with this key exists, delete the old one first */
260: DataObject checkObject = null;
261:
262: if (myDBObject instanceof DBObject) {
263: checkObject = ((DBObject) myDBObject).newInstance();
264: } else {
265: try {
266: checkObject = (DataObject) myDBObject.getClass()
267: .newInstance();
268: if (checkObject instanceof Defineable) {
269: ((Defineable) checkObject)
270: .setDefinitionName(((Defineable) myDBObject)
271: .getDefinitionName());
272: }
273: java.util.Map attrMap = myDBObject
274: .getAllAttributes();
275: java.util.Iterator keys = attrMap.keySet()
276: .iterator();
277: while (keys.hasNext()) {
278: String key = keys.next().toString();
279: checkObject.setAttribute(key, attrMap.get(key));
280: }
281: } catch (Exception e) {
282: throw new DBException(
283: "Error creating checkObject: "
284: + e.getMessage(), e);
285: }
286: }
287: checkObject.setDataContext(getDBName());
288:
289: String oneKey = null;
290:
291: for (Iterator e = myDBObject.getMetaData().getAllKeysMap()
292: .keySet().iterator(); /*myDBObject.getKeyFieldListIterator();*/
293: e.hasNext();) {
294: oneKey = (String) e.next();
295: checkObject.set(oneKey, myDBObject.getField(oneKey));
296: }
297: if (checkObject.find()) {
298: log.debug("Record is there, updating");
299:
300: /* Don't throw an exception if the record doesn't need updating */
301: myDBObject.getMetaData().setCheckZeroUpdate(false);
302: myDBObject.setDataContext(getDBName());
303: myDBObject.update();
304: } else {
305: log.debug("Record is not there, adding");
306: myDBObject.setDataContext(getDBName());
307: myDBObject.add();
308: log.debug("Added");
309: }
310:
311: recordCount++;
312: } catch (DBException de) {
313: de.printStackTrace();
314: errorList.addElement(de);
315: log.error(de);
316: }
317: } /* saveDBObject() */
318:
319: /**
320: * @param name
321: * @param attributes
322: */
323: /* public void startElement(String name, AttributeList attributes)
324:
325: throws SAXException {
326:
327:
328:
329: System.out.println("Start:" + name);
330:
331: //for (int i = 0; i < attributes.getLength(); i++) {
332:
333: // System.out.println("Attribute " + i + " Name: " + attributes.getName(i)
334:
335: // + " Value:" + attributes.getValue(i));
336:
337: //}
338:
339: currentItem = name;
340:
341: //getType(int);
342:
343: //getValue(int);
344:
345: //getValue(String);
346:
347: } /* startElement(String, AttributeList) */
348: ///Idit
349: public void startElement(String uri, String localpart,
350: String rawname, Attributes attributes) throws SAXException {
351: log.debug("Start:" + rawname);
352: currentItem = rawname;
353: }
354:
355: // Idit Mike
356: /**
357: * @return
358: */
359: public DataObject getDBObject() throws DBException {
360: return myDBObject;
361: } /* getDBObject() */
362:
363: } /* XMLImportDocument */
|