001: /**
002: * $Id: IWAYResponseParser.java,v 1.5 2005/10/19 12:38:55 pg133018 Exp $
003: * Copyright 2004 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.psftportlet.common;
014:
015: import java.util.ResourceBundle;
016: import java.util.List;
017: import java.util.Iterator;
018: import java.util.MissingResourceException;
019: import java.util.Vector;
020: import java.util.Map;
021: import java.util.HashMap;
022: import java.util.Collections;
023: import java.util.logging.Logger;
024:
025: import java.io.StringReader;
026: import java.io.IOException;
027:
028: import org.jdom.Document;
029: import org.jdom.Element;
030: import org.jdom.JDOMException;
031: import org.jdom.input.SAXBuilder;
032: import com.sun.portal.log.common.PortalLogger;
033:
034: /**
035: * This is the parser for the response recieved from
036: * iWay for the PeopleSoft
037: *
038: * @author Pradeep Gond
039: */
040:
041: public class IWAYResponseParser {
042: private static Logger logger = PortalLogger
043: .getLogger(IWAYResponseParser.class);
044:
045: private String responseObject;
046: private Element root;
047: private ResourceBundle ruleResource;
048: private boolean valid = true;
049: private String message = "";
050:
051: public IWAYResponseParser(String responseObject,
052: ResourceBundle ruleResource) {
053: this .responseObject = responseObject;
054: this .ruleResource = ruleResource;
055: init();
056: }
057:
058: private void init() {
059: // initialize the xml document from responseObject
060: try {
061: SAXBuilder builder = new SAXBuilder();
062: Document responseDoc = builder.build(new StringReader(
063: responseObject.trim()));
064: root = responseDoc.getRootElement();
065: } catch (JDOMException je) {
066: logger
067: .severe("Exception during init => "
068: + je.getMessage());
069: } catch (IOException e) {
070: logger.severe("Exception during init => " + e.getMessage());
071: }
072:
073: //check for blank result and errors, set error
074: if (responseObject.indexOf(PSFTPortletConstants.BLANK_RESULT) != -1) {
075: setValidity(false, "Blank result data");
076: }
077:
078: Element errorElement = root
079: .getChild(PSFTPortletConstants.ERROR);
080: if (errorElement != null) {
081: setValidity(false, errorElement.getTextTrim());
082: }
083: }
084:
085: private void setValidity(boolean status, String msg) {
086: valid = status;
087: message = msg;
088: logger.info("Valid => " + status);
089: logger.info("Message => " + msg);
090: }
091:
092: public boolean isValid() {
093: return valid;
094: }
095:
096: public String getErrorMessage() {
097: return message;
098: }
099:
100: public String getValue(String key) {
101: String result = "";
102:
103: try {
104: String value = ruleResource.getString(key);
105: String[] tokens = value.split(PSFTPortletConstants.DELIM);
106:
107: if (tokens.length > 1) {
108: String type = tokens[0];
109: if (type
110: .equalsIgnoreCase(PSFTPortletConstants.TYPE_SIMPLE)) {
111: result = parseSimpleData(tokens[1]);
112: } else if (type
113: .equalsIgnoreCase(PSFTPortletConstants.TYPE_NESTED)) {
114: String keys[] = new String[tokens.length - 1];
115: for (int i = 1; i < tokens.length; i++) {
116: keys[i - 1] = tokens[i];
117: }
118: result = parseNestedData(keys)[0];
119: } else if (type
120: .equalsIgnoreCase(PSFTPortletConstants.TYPE_COMPLEX)) {
121: String keys[] = new String[tokens.length - 1];
122: for (int i = 1; i < tokens.length; i++) {
123: keys[i - 1] = tokens[i];
124: }
125: result = parseComplexDataInternal(keys);
126: }
127: }
128: } catch (MissingResourceException mre) {
129: // do nothing
130: }
131: logger.info("(" + key + ") => " + result);
132:
133: return result;
134: }
135:
136: public String[] getNestedValues(String key) {
137: String[] result = null;
138:
139: try {
140: String value = ruleResource.getString(key);
141: String[] tokens = value.split(PSFTPortletConstants.DELIM);
142:
143: if (tokens.length > 1) {
144: String type = tokens[0];
145: if (type
146: .equalsIgnoreCase(PSFTPortletConstants.TYPE_NESTED)) {
147: String keys[] = new String[tokens.length - 1];
148: for (int i = 1; i < tokens.length; i++) {
149: keys[i - 1] = tokens[i];
150: }
151: result = parseNestedData(keys);
152: }
153: }
154: } catch (MissingResourceException mre) {
155: // do nothing
156: }
157:
158: return result;
159: }
160:
161: public String[] getComplexValues(String key) {
162: String[] result = null;
163:
164: try {
165: String value = ruleResource.getString(key);
166: String[] tokens = value.split(PSFTPortletConstants.DELIM);
167:
168: if (tokens.length > 1) {
169: String type = tokens[0];
170: if (type
171: .equalsIgnoreCase(PSFTPortletConstants.TYPE_COMPLEX)) {
172: String keys[] = new String[tokens.length - 1];
173: for (int i = 1; i < tokens.length; i++) {
174: keys[i - 1] = tokens[i];
175: }
176: result = parseComplexData(keys);
177: }
178: }
179: } catch (MissingResourceException mre) {
180: // do nothing
181: }
182:
183: return result;
184: }
185:
186: private String parseSimpleData(String key) {
187: return parseSimpleDataInternal(root
188: .getChild(PSFTPortletConstants.RESULT), key);
189: }
190:
191: private String parseSimpleDataInternal(Element root, String key) {
192: String result = "";
193:
194: if (root != null) {
195: String value = root.getChild(key).getTextTrim();
196: if (value != null) {
197: result = value;
198: }
199: }
200: logger.finest("(" + key + ") => " + result);
201:
202: return result;
203: }
204:
205: private String[] parseNestedData(String[] keys) {
206: String[] result = { "", "" };
207: String nestedName = "";
208: String nestedType = "";
209: String nestedTypeValue = "";
210: String key = "";
211: String row = "";
212:
213: try {
214: nestedName = keys[0];
215: nestedType = keys[1];
216: nestedTypeValue = keys[2];
217: key = keys[3];
218: } catch (ArrayIndexOutOfBoundsException ex) {
219: logger.warning("Invalid nested data type => " + keys);
220: }
221:
222: Element resultElement = root
223: .getChild(PSFTPortletConstants.RESULT);
224: if (resultElement != null) {
225: if (nestedTypeValue
226: .equalsIgnoreCase(PSFTPortletConstants.NO_NESTED_TYPE_VALUE)) {
227: Element child = resultElement.getChild(nestedName);
228: if (child != null) {
229: result[0] = parseSimpleDataInternal(child, key);
230: row = child
231: .getAttributeValue(PSFTPortletConstants.ROW);
232: }
233: } else {
234: List children = resultElement.getChildren(nestedName);
235: Iterator itr = children.iterator();
236: while (itr.hasNext()) {
237: Element child = (Element) itr.next();
238: if (child.getChild(nestedType).getTextTrim()
239: .equalsIgnoreCase(nestedTypeValue)) {
240: result[0] = parseSimpleDataInternal(child, key);
241: row = child
242: .getAttributeValue(PSFTPortletConstants.ROW);
243: break;
244: }
245: }
246: }
247: result[1] = row;
248: }
249:
250: return result;
251: }
252:
253: private String parseComplexDataInternal(String[] keys) {
254: String result = "";
255:
256: String[] data = parseComplexData(keys);
257: for (int i = 0; i < data.length - 1; i++) {
258: if (i == 0) {
259: result = data[i];
260: } else {
261: result += PSFTPortletConstants.DEFAULT_SEPARATOR
262: + data[i];
263: }
264: }
265:
266: return result;
267: }
268:
269: private String[] parseComplexData(String[] keys) {
270: String[] result;
271: String complexName = "";
272: String complexType = "";
273: String complexTypeValue = "";
274: String newRootName = "";
275: String row = "";
276: int index = 4;
277:
278: result = new String[keys.length - index + 1];
279:
280: try {
281: complexName = keys[0];
282: complexType = keys[1];
283: complexTypeValue = keys[2];
284: newRootName = keys[3];
285: } catch (ArrayIndexOutOfBoundsException ex) {
286: logger.info("Invalid complex data type => " + keys);
287: }
288:
289: Element resultElement = root
290: .getChild(PSFTPortletConstants.RESULT);
291: if (resultElement != null) {
292: List children = resultElement.getChildren(complexName);
293: Iterator itr = children.iterator();
294: while (itr.hasNext()) {
295: Element child = (Element) itr.next();
296: if (child.getChild(complexType).getTextTrim()
297: .equalsIgnoreCase(complexTypeValue)) {
298: row = child
299: .getAttributeValue(PSFTPortletConstants.ROW);
300: int j = 0;
301: if (newRootName
302: .equalsIgnoreCase(PSFTPortletConstants.SAME_NEW_ROOT)) {
303: for (int i = index; i < keys.length; i++, j++) {
304: result[j] = parseSimpleDataInternal(child,
305: keys[i]);
306: }
307: } else {
308: Element newRoot = child.getChild(newRootName);
309: if (newRoot != null) {
310: for (int i = index; i < keys.length; i++, j++) {
311: result[j] = parseSimpleDataInternal(
312: newRoot, keys[i]);
313: }
314: }
315: }
316: result[j] = row;
317: break;
318: }
319: }
320: }
321:
322: return result;
323: }
324:
325: public Vector getElements(String name) {
326: Vector result = new Vector();
327:
328: Element resultElement = root
329: .getChild(PSFTPortletConstants.RESULT);
330: if (resultElement != null) {
331: List children = resultElement.getChildren(name);
332: Iterator itr = children.iterator();
333: while (itr.hasNext()) {
334: Element child = (Element) itr.next();
335: result.add(child);
336: }
337: }
338:
339: return result;
340: }
341:
342: public Map getRecordsAsBDMap(String key, String value) {
343: HashMap result = new HashMap(2);
344: Map forwardMap = new HashMap();
345: Map reverseMap = new HashMap();
346:
347: List records = root.getChildren(PSFTPortletConstants.RECORD);
348: Iterator itr = records.iterator();
349: while (itr.hasNext()) {
350: Element record = (Element) itr.next();
351: String keyString = record.getChild(key).getTextTrim();
352: String valueString = record.getChild(value).getTextTrim();
353:
354: if (keyString != null && valueString != null) {
355: forwardMap.put(keyString, valueString);
356: reverseMap.put(valueString, keyString);
357: }
358: }
359:
360: result.put(PSFTPortletConstants.FORWARD, Collections
361: .unmodifiableMap(forwardMap));
362: result.put(PSFTPortletConstants.REVERSE, Collections
363: .unmodifiableMap(reverseMap));
364:
365: return Collections.unmodifiableMap(result);
366: }
367: }
|