/*
 * $Log: RecordXmlTransformer.java,v $
 * Revision 1.4  2006-05-19 09:28:36  europe\m00i745
 * Restore java files from batch package after unwanted deletion.
 *
 * Revision 1.2  2005/10/31 14:38:02  John Dekker <john.dekker@ibissource.org>
 * Add . in javadoc
 *
 * Revision 1.1  2005/10/11 13:00:20  John Dekker <john.dekker@ibissource.org>
 * New ibis file related elements, such as DirectoryListener, MoveFilePie and 
 * BatchFileTransformerPipe
 *
 */
package nl.nn.adapterframework.batch;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;

import nl.nn.adapterframework.core.PipeLineSession;

import org.apache.commons.lang.StringUtils;

/**
 * Translate a record using XSL.
 * 
 * <p><b>Configuration:</b>
 * <table border="1">
 * <tr><th>attributes</th><th>description</th><th>default</th></tr>
 * <tr><td>classname</td><td>nl.nn.ibis4fundation.transformation.RecordXslTransformer</td><td>&nbsp;</td></tr>
 * <tr><td>{@link #setRootTag(String) rootTag}</td><td>Roottag for the generated XML document</td><td>&nbsp;</td></tr>
 * <tr><td>{@link #setOutputFields(String) outputfields}</td><td>Comma seperated string with tagnames for the individual input fields (related using there positions). If you leave a tagname empty, the field is not xml-ized</td><td>&nbsp;</td></tr>
 * </table>
 * </p>
 * 
 * @author: John Dekker
 */
public class RecordXmlTransformer extends AbstractRecordHandler {
	public static final String version = "$RCSfile: RecordXmlTransformer.java,v $  $Revision: 1.4 $ $Date: 2006-05-19 09:28:36 $";

	private String rootTag;
	private List outputFields; 

	public RecordXmlTransformer() {
		outputFields = new LinkedList();
	}

	public void setOutputFields(String fieldLengths) {
		StringTokenizer st = new StringTokenizer(fieldLengths, ",");
		while (st.hasMoreTokens()) {
			String token = st.nextToken().trim();
			outputFields.add(token);
		}
	}

	public Object handleRecord(PipeLineSession session, ArrayList parsedRecord) throws Exception {
		return getXml(parsedRecord);
	}
	
	protected String getXml(ArrayList parsedRecord) {
		StringBuffer tmpResult = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		tmpResult.append("<").append(rootTag).append(">");
		
		int ndx = 0;
		for (Iterator outputFieldIt = outputFields.iterator(); outputFieldIt.hasNext();) {
			// get tagname
			String tagName = (String) outputFieldIt.next();
			
			// get value
			String value = "";
			if (ndx < parsedRecord.size()) {
				value = (String)parsedRecord.get(ndx++);
			}
			// if tagname is empty, then it is not added to the XML
			if (! StringUtils.isEmpty(tagName)) {
				tmpResult.append("<").append(tagName);
				if (StringUtils.isEmpty(value)) {
					tmpResult.append("/>");
				}
				else {
					tmpResult.append(">").append(value).append("</").append(tagName).append(">");
				}
			}
		}
		tmpResult.append("</").append(rootTag).append(">");
		
		return tmpResult.toString();
	}

	public String getRootTag() {
		return rootTag;
	}

	public void setRootTag(String string) {
		rootTag = string;
	}

}

@@ -53,46 +53,174 @@
 n 1.
-4
+5
   200
-6
+7
 -05-
-19 09:28:36  europe%5Cm00i745
+03 11:36:43  europe%5CL190409%0A * encode characters where required%0A *%0A * Revision 1.4  2006/05/19 09:28:36  Peter Eijgermans %3Cpeter.eijgermans@ibissource.org%3E
 %0A * 
@@ -812,16 +812,61 @@
 Session;
+%0Aimport nl.nn.adapterframework.util.XmlUtils;
 %0A%0Aimport
@@ -1102,37 +1102,30 @@
 .nn.
-ibis4fundation.transformation
+adapterframework.batch
 .Rec
@@ -1159,32 +1159,132 @@
 nbsp;%3C/td%3E%3C/tr%3E%0A
+ * %3Ctr%3E%3Ctd%3E%7B@link #setName(String) name%7D%3C/td%3E%3Ctd%3Ename of the RecordHandler%3C/td%3E%3Ctd%3E&nbsp;%3C/td%3E%3C/tr%3E%0A
  * %3Ctr%3E%3Ctd%3E%7B@lin
@@ -1845,17 +1845,17 @@
 sion: 1.
-4
+5
  $ $Date
@@ -1863,24 +1863,24 @@
  200
-6
+7
 -05-
-19 09:28:36
+03 11:36:43
  $%22;
@@ -2812,16 +2812,18 @@
 ) %7B%0A%09%09%09%09
+//
 value = 
@@ -2855,16 +2855,83 @@
 ndx++);%0A
+%09%09%09%09value = XmlUtils.encodeChars((String)parsedRecord.get(ndx++));%0A
 %09%09%09%7D%0A%09%09%09

encode characters where required