cancel
Showing results for 
Search instead for 
Did you mean: 

Add schema in file payload

siddhesh_pathak4
Contributor
0 Kudos

Hello all,

I have one scenario in which i have to add header (schema definitaion) and footer in addition to file payload. Is there any way to get this done??? XSLT mapiing?? Is there anyone who came across such scenario.

Thanks,

Siddhesh Pathak

Accepted Solutions (1)

Accepted Solutions (1)

former_member190389
Active Contributor
0 Kudos

Hi,

You can use the string operations in java to append the header and footer to the payload by searching the appropriate index.

You can achieve this in a java mapping.

siddhesh_pathak4
Contributor
0 Kudos

It would be great if you could elaborate on this.. And provide if some sample code is available..Thanks

former_member190389
Active Contributor
0 Kudos

Do you also want to validate the file against the schema defination?

siddhesh_pathak4
Contributor
0 Kudos

Yes...and schema definitaion values are constant fyi..

Answers (2)

Answers (2)

siddhesh_pathak4
Contributor
0 Kudos

I have used XSLT Mapping for this. Thanks for your help.

former_member190389
Active Contributor
0 Kudos

For schema validation my scenario was

1. In sender file adapter CC I created a ValidationBean module.

2. This module used to validate the xml file against the xsd placed at server location /usr/sap/<SID>/DVEB<>/j2ee/cluster/server0

3. In the adapter module, i combined two structures into one i.e. one the file after successful validation and the other was log if any error occured in validation. For this to happen , I kept the occurences of both the structure to be 0..1 i.e. optional

e.g

<MT>

<FileValid>

<Name/>

</FileValid>

<Log>

<error>Not Valid </error>

</Log>

</MT>

The occurences of FileValid & Log tags are 0..1

Now this structure I am creating it in the adapter module and then it comes to the Message Mapping.

After Message Mapping , I used the string functionalities into a Java mapping to add the defination

 //Test 

package com.ibis.mapping.java.addXSI;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import com.sap.aii.mapping.api.AbstractTrace;
import com.sap.aii.mapping.api.MappingTrace;
import com.sap.aii.mapping.api.StreamTransformation;
import com.sap.aii.mapping.api.StreamTransformationConstants;
import com.sap.aii.mapping.api.StreamTransformationException;

public class AddXSI implements StreamTransformation {
	public Map param = null;

	public static void main(String[] args) {
		try {
			InputStream in = new FileInputStream(new File("op.xml"));
			OutputStream out = new FileOutputStream(new File("Test.xml"));
			System.out.println("Hello");
			AddXSI rfcLookup = new AddXSI();
			rfcLookup.execute(in, out);

		} catch (Exception e) {
			System.out.println("ERROR IS :" + e.getCause());
		}

	}

	public void execute(InputStream inputStream, OutputStream outputStream)
		throws StreamTransformationException {

		String sb = null;
		//MappingTrace importanttrace;
		//importanttrace = (AbstractTrace)param.get(StreamTransformationConstants.MAPPING_TRACE );
		byte[] b = new byte[80000];
		try {
			int count = 0;
			for (int n;(n = inputStream.read(b)) != -1;) {
				sb = new String(b, 0, n);

				//System.out.println(sb);
				sb =
					sb.replaceAll(
						"<StockReport xmlns=\"http://www.w3.org/2001/XMLSchema-instance\" noNamespaceSchemaLocation=\"EDItX_TradeStockReport_V1.1.xsd\" version=\"1.1\">",
						"<StockReport xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"EDItX_TradeStockReport_V1.1.xsd\" version=\"1.1\">");
				int i = sb.indexOf("<ResponseStatus>") + 16;
				int j = sb.indexOf("</ResponseStatus>");

				//				importanttrace.addInfo(""+i+" "+j);
				System.out.println(count++ +" " + i + " " + j);
				String temp = null;
				try {
					temp = sb.substring(i, j);
				} catch (StringIndexOutOfBoundsException se) {
					temp = "";
				}
				if (temp.indexOf(":") != -1) {
					String[] split = temp.split(" : ");

					split[1] = split[1].replaceAll("&lt;", "<");
					split[1] = split[1].replaceAll("&gt;", ">");
					sb =
						sb.replaceAll(
							temp + "</ResponseStatus>",
							split[0]
								+ "</ResponseStatus><!-- "
								+ split[1]
								+ "-->");
				}
				//System.out.println(i + " " +j+" "+ temp+" "+split[0]+" "+split[1]);
				outputStream.write(sb.getBytes());

			}

			sb = null;

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void setParameter(Map param) {
		this.param = param;
		if (param == null) {
			this.param = new HashMap();
		}

	}

}

Edited by: Fariha Ali on Jan 28, 2010 5:56 PM