001: /*
002: * Created on Dec 10, 2003
003: *
004: * To change the template for this generated file go to
005: * Window>Preferences>Java>Code Generation>Code and Comments
006: */
007: package org.xdev.base.xssl.collection;
008:
009: import java.io.BufferedReader;
010: import java.io.InputStream;
011: import java.io.InputStreamReader;
012: import java.io.Reader;
013: import java.io.StringReader;
014: import java.sql.ResultSet;
015: import java.util.ArrayList;
016: import java.util.Enumeration;
017: import java.util.HashMap;
018: import java.util.Collection;
019: import java.util.Iterator;
020: import java.util.Map;
021: import java.util.StringTokenizer;
022:
023: import org.jaxen.jdom.JDOMXPath;
024: import org.jdom.Document;
025: import org.jdom.Element;
026: import org.jdom.input.SAXBuilder;
027:
028: import org.xdev.base.xssl.XSSLAction;
029: import org.xdev.base.xssl.XSSLReturn;
030:
031: /**
032: * @author AYegorov
033: *
034: * To change the template for this generated type comment go to
035: * Window>Preferences>Java>Code Generation>Code and Comments
036: */
037: public abstract class XSSLCollectionIterator extends XSSLReturn {
038: public static final String MAP_CHOICE = "part";
039:
040: public static final int MAP_KEYS = 0;
041: public static final int MAP_VALUES = 1;
042:
043: public static final int COLLECTION_RECEIVED_EVENT = 36;
044: public static final int COLLECTION_ACTION_EVENT = 37;
045: public static final int COLLECTION_ITERATOR_START_EVENT = 38;
046: public static final int COLLECTION_ITERATOR_NEXT_EVENT = 39;
047: public static final int COLLECTION_PROCESS_START_EVENT = 40;
048: public static final int COLLECTION_PROCESS_END_EVENT = 41;
049: public static final int COLLECTION_ITERATOR_END_EVENT = 42;
050:
051: private Collection collection = null;
052:
053: private ArrayList actions = new ArrayList();
054:
055: private XSSLAction tx = null;
056:
057: private boolean hadValues = false;
058:
059: /**
060: * @param id
061: */
062: public XSSLCollectionIterator(String id) {
063: super (id);
064: }
065:
066: /**
067: * @param id
068: * @param properties
069: */
070: public XSSLCollectionIterator(String id, HashMap properties) {
071: super (id, properties);
072: }
073:
074: public Object getObjectValue() {
075: return this .actions;
076: }
077:
078: /* (non-Javadoc)
079: * @see org.xdev.base.transaction.AbstractTransaction#set()
080: */
081: protected void set() throws Exception {
082: Object coll = this .getEvaluatedReference();
083:
084: int count = 0;
085:
086: if (coll == null && !this .hasReference() && this .getCount() > 0) {
087: coll = ((XSSLReturn) this .getElement(count++))
088: .getObjectValue();
089: }
090:
091: this .tx = (XSSLAction) this .getElement(count);
092:
093: this
094: .issueTrigger(
095: XSSLCollectionIterator.COLLECTION_ACTION_EVENT,
096: this .tx);
097:
098: this .evaluateCollection(coll);
099: }
100:
101: protected void evaluateCollection(Object coll) throws Exception{
102: try {
103: this .logDebug("Collection received: "+coll);
104:
105: boolean pass = true;
106:
107: if (coll == null) {
108: this .collection = new ArrayList();
109: }
110: else if (coll instanceof Collection) {
111: this .collection = (Collection)coll;
112: }
113: else if (coll instanceof Enumeration) {
114: ArrayList list = new ArrayList();
115:
116: this .logDebug("Processing enumeration...");
117:
118: Enumeration enum = (Enumeration)coll;
119:
120: Object o = null;
121:
122: while(enum.hasMoreElements()) {
123: list.add((o = enum.nextElement()));
124:
125: this .logDebug("Enumerating object: "+o);
126: }
127:
128: this .collection = list;
129: }
130: else if (coll instanceof Map) {
131: switch(this .getIntegerProperty(XSSLCollectionIterator.MAP_CHOICE)) {
132: case XSSLCollectionIterator.MAP_KEYS:
133: this .collection = ((Map)coll).keySet();
134: break;
135: case XSSLCollectionIterator.MAP_VALUES:
136: this .collection = ((Map)coll).values();
137: break;
138: }
139: }
140: else if (coll instanceof Object[]) {
141: Object[] obj = (Object[])coll;
142: ArrayList list = new ArrayList();
143:
144: for (int i=0; i<obj.length; i++) {
145: list.add(obj[i]);
146: }
147:
148: this .collection = list;
149:
150: list = null;
151: obj = null;
152: }
153: else if (coll instanceof StringBuffer) {
154: this .evaluateCollection(coll.toString());
155:
156: pass = false;
157: }
158: else if (coll instanceof String) {
159: if (this .hasProperty("xpath")) {
160: SAXBuilder builder = new SAXBuilder();
161:
162: Document doc = builder.build(new StringReader(coll.toString()));
163:
164: this .evaluateCollection(doc);
165: }
166: else if (this .hasProperty("delim")) {
167: this .evaluateCollection(coll.toString().split(this .getProperty("delim")));
168: }
169:
170: pass = false;
171: }
172: else if (coll instanceof Document) {
173: this .evaluateCollection(((Document)coll).getRootElement());
174:
175: pass = false;
176: }
177: else if (coll instanceof Element) {
178: Element root = (Element)coll;
179:
180: if (this .hasProperty("xpath")) {
181: JDOMXPath xpath = new JDOMXPath(this .getProperty("xpath"));
182:
183: this .collection = xpath.selectNodes(root);
184: }
185: else {
186: this .collection = root.getAttributes();
187: }
188: }
189:
190: if (this .collection != null && pass) {
191:
192: this .issueTrigger(XSSLCollectionIterator.COLLECTION_RECEIVED_EVENT, this .collection);
193:
194: Iterator iter = collection.iterator();
195:
196: this .issueTrigger(XSSLCollectionIterator.COLLECTION_ITERATOR_START_EVENT, iter);
197:
198: Object next = null;
199: while(iter.hasNext()) {
200: next = iter.next();
201:
202: this .processCollection(this .tx, next);
203:
204: next = null;
205: }
206: }
207: else if (coll instanceof InputStream) {
208: Reader reader = new InputStreamReader((InputStream)coll);
209:
210: this .logDebug("Collection came in as a stream: "+coll);
211:
212: this .evaluateCollection(reader);
213: }
214: else if (coll instanceof Reader) {
215: BufferedReader reader = new BufferedReader((Reader)coll);
216:
217: this .logDebug("Collection came in as a stream reader: "+reader);
218:
219: String line = null;
220:
221: int c = -1;
222:
223: boolean perCharacter = this .getBooleanProperty("character");
224:
225: while((perCharacter && (c = reader.read()) != -1) || (line = reader.readLine()) != null) {
226: this .processCollection(this .tx, line);
227: }
228: }
229: else if (coll instanceof ResultSet) {
230: ResultSet rs = (ResultSet)coll;
231:
232: this .logDebug("Collection came in as a jdbc result: "+coll);
233:
234: while(rs.next()) {
235:
236: this .logDebug("Processing iterative action with each jdbc result row.");
237:
238: this .processCollection(this .tx, rs);
239: }
240:
241: if (this .getBooleanProperty("close-result")) {
242: rs.close();
243: }
244: }
245: }
246: finally {
247: if (this .hadValues) {
248: this .issueTrigger(XSSLCollectionIterator.COLLECTION_ITERATOR_END_EVENT);
249: }
250: }
251: }
252:
253: protected void processCollection(XSSLAction item, Object next)
254: throws Exception {
255: this .hadValues = true;
256:
257: this .issueTrigger(
258: XSSLCollectionIterator.COLLECTION_ITERATOR_NEXT_EVENT,
259: next);
260:
261: this .setReference(next);
262:
263: this
264: .issueTrigger(XSSLCollectionIterator.COLLECTION_PROCESS_START_EVENT);
265:
266: this .actions.add(this .processCollectionItem(this .tx, next));
267:
268: this
269: .issueTrigger(XSSLCollectionIterator.COLLECTION_PROCESS_END_EVENT);
270:
271: next = null;
272: }
273:
274: protected abstract Object processCollectionItem(XSSLAction item,
275: Object next) throws Exception;
276:
277: /**
278: * @return Returns the mAP_KEYS.
279: */
280: public static int getMAP_KEYS() {
281: return MAP_KEYS;
282: }
283:
284: /**
285: * @return Returns the mAP_VALUES.
286: */
287: public static int getMAP_VALUES() {
288: return MAP_VALUES;
289: }
290:
291: /**
292: * @return Returns the cOLLECTION_ACTION_EVENT.
293: */
294: public static int getCOLLECTION_ACTION_EVENT() {
295: return COLLECTION_ACTION_EVENT;
296: }
297:
298: /**
299: * @return Returns the cOLLECTION_ITERATOR_END_EVENT.
300: */
301: public static int getCOLLECTION_ITERATOR_END_EVENT() {
302: return COLLECTION_ITERATOR_END_EVENT;
303: }
304:
305: /**
306: * @return Returns the cOLLECTION_ITERATOR_NEXT_EVENT.
307: */
308: public static int getCOLLECTION_ITERATOR_NEXT_EVENT() {
309: return COLLECTION_ITERATOR_NEXT_EVENT;
310: }
311:
312: /**
313: * @return Returns the cOLLECTION_ITERATOR_START_EVENT.
314: */
315: public static int getCOLLECTION_ITERATOR_START_EVENT() {
316: return COLLECTION_ITERATOR_START_EVENT;
317: }
318:
319: /**
320: * @return Returns the cOLLECTION_PROCESS_END_EVENT.
321: */
322: public static int getCOLLECTION_PROCESS_END_EVENT() {
323: return COLLECTION_PROCESS_END_EVENT;
324: }
325:
326: /**
327: * @return Returns the cOLLECTION_PROCESS_START_EVENT.
328: */
329: public static int getCOLLECTION_PROCESS_START_EVENT() {
330: return COLLECTION_PROCESS_START_EVENT;
331: }
332:
333: /**
334: * @return Returns the cOLLECTION_RECEIVED_EVENT.
335: */
336: public static int getCOLLECTION_RECEIVED_EVENT() {
337: return COLLECTION_RECEIVED_EVENT;
338: }
339:
340: /**
341: * @return Returns the mAP_CHOICE.
342: */
343: public static String getMAP_CHOICE() {
344: return MAP_CHOICE;
345: }
346:
347: /**
348: * @return Returns the collection.
349: */
350: public Collection getCollection() {
351: return collection;
352: }
353:
354: /**
355: * @return Returns the tx.
356: */
357: public XSSLAction getTx() {
358: return tx;
359: }
360:
361: public void reset() {
362: super .reset();
363:
364: this .hadValues = false;
365:
366: this .actions = new ArrayList();
367: }
368: }
|