cancel
Showing results for 
Search instead for 
Did you mean: 

How to test Module Processor javabean as a standalone ?

Former Member
0 Kudos

Hi All,

I've a javabean using as a moduleprocessor to convert PDF to XML.

I would like to know How to test this javabean prog as a standalone?

Pls need step by step process..?

Thanks and Regards,

Govindu.

Accepted Solutions (1)

Accepted Solutions (1)

former_member206604
Active Contributor
0 Kudos

Hi,

I am not aware if there is a better way to do it. But what I used to do is... whenever I do a module processing I used to write a function which accepts a stream and returns a stream. All the processing would be done inside the function. In ur case I will pass the PDF stream and will get XML stream as a result.

When you test this you can write a simple test java program where u can use the same function. In the test program you can read the PDF and pass it as a stream to the function and it will return you the XML and now save the XML and check if it is as expected.

There might be many ways to do this but I do this way. Hope some of our friends may post some better way to do it.

Thanks,

Prakash

Former Member
0 Kudos

Hi Prakash,

Thanks for your reply.

Can i get one Example Function( any one)? then i will go thru that after that i will write Funtion for my Scenario. BCZ i am not from a java background...

Thanks,

Govindu.

former_member206604
Active Contributor
0 Kudos

Hi,

This is a sample Module program for parsing Excel into XML. Check the function parseExcel. You can copy this parseExcel function and its associated function into a test java program and test it.


package com.dpost.AF_Module.File;
import java.io.ByteArrayInputStream;

import javax.ejb.CreateException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

// Excel Import
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

import com.sap.aii.af.mp.module.Module;
import com.sap.aii.af.mp.module.ModuleContext;
import com.sap.aii.af.mp.module.ModuleData;
import com.sap.aii.af.mp.module.ModuleException;
import com.sap.aii.af.ra.ms.api.Message;
import com.sap.aii.af.ra.ms.api.XMLPayload;
import com.sap.aii.af.service.trace.Trace;
//import jxl.read.biff.BiffException;


public class ImportExcelBean implements SessionBean, Module {
		

	
private static final Trace TRACE = new Trace(VERSION_ID);
	
private SessionContext myContext;

// String for XML Payload Header
....

// --- End

	
public void ejbRemove() {
}
public void ejbActivate() {
}
public void ejbPassivate() {
}

public void setSessionContext(SessionContext context) 
{
	myContext = context;
}
public void ejbCreate() throws CreateException {
}
/********* Start of Module Processing code *********************
public ModuleData process(ModuleContext moduleContext, ModuleData inputModuleData) throws ModuleException {
	Object obj = null;
	Message msg = null;


	try {
			obj = inputModuleData.getPrincipalData();
			msg = (Message) obj;
			XMLPayload xmlpayload = msg.getDocument();
			if (xmlpayload != null) 
			{
				byte by[] = parseExcel(xmlpayload.getContent());
				xmlpayload.setContent(by);
			}
			inputModuleData.setPrincipalData(msg);

		} catch (Exception e) {
			ModuleException me = new ModuleException(e);
			throw me;
		}
					
		return inputModuleData;
	}

/********* Start of Excel parsing Code *********************
// Parsing Excel
   public byte[] parseExcel(byte src[]) throws Exception 
   {
	String xmldata = "";
	try 
	{
		 String err = "";
		 
		 ByteArrayInputStream objExcelInput = new ByteArrayInputStream(src);
		 Workbook wb = Workbook.getWorkbook(objExcelInput);
		 Sheet[] sheets = wb.getSheets();
		 //xmldata = xmlHead;
		 for (int ns = 0; ns < sheets.length; ns++)
		 {
			 xmldata+= parseSheets(sheets[ns], xmldata);
		 }
		 xmldata = xmlHead + xmldata + xmlTail;		 
		 wb.close();
	}
	catch (Exception e)
	{}
	return xmldata.getBytes();
   }

// Parse Sheets
   private String parseSheets(Sheet sheet, String xmldata) throws Exception
   {
	   try
	   {
		   int numRows = sheet.getRows();
		   Cell[] tags = sheet.getRow(0);
		   for (int y = 0; y < numRows; y++)
		   {
			   Cell[] attr = sheet.getRow(y);
			   if (checkEmpty(attr))
			   {
				   continue;
			   }
			   //os.write(tagHead.getBytes());
			   xmldata = xmldata + tagHead;
			   for (int x = 0; x < attr.length; x++)
			   {
				   String out = "tt<" + tags[x].getContents() + ">" + attr[x].getContents() + "</" + tags[x].getContents() + ">rn";
				   //os.write(out.getBytes());				
				   xmldata = xmldata + out;
			   }
			   //os.write(tagTail.getBytes());
			   xmldata = xmldata + tagTail;
		   }
	   }
	   catch (Exception ioex)
	   {

	   }
	   xmldata = xmldata.replaceAll("&", "&amp;");
	   return xmldata;
   }
   
   private boolean checkEmpty(Cell[] attr)
   {
	   for (int n = 0; n < attr.length; n++)
	   {
		   if (!"".equals(attr[n].getContents()))
		   {
			   return false;
		   }
	   }
	   return true;
   }   

}

Thanks,

Prakash

Former Member
0 Kudos

Hi Prakash,

I have this Javabean for my Scenario (converting PDF to Text )

now i want to test this javabean before installing in XI server.

Pls let me know how to test this javabean? whether it is working correct or not as standalone.

Thanks,

Govindu.

former_member206604
Active Contributor
0 Kudos

Hi Govindu,

For that you need to have java knowledge to test if it is working or not. I would suggest you to get someone from Java background and ask them to test if it is working fine. If that is not possible let me know I will try to test it for you ok.

Thanks,

Prakash

Former Member
0 Kudos

Hi Govindu,

I think you should be able to test your scenario using NWDS altho' i'm not sure how..

Regards,

Sushumna

Former Member
0 Kudos

Hi'

u can test it using stand alone java.

1. First create an object for ur EJB.

InitialContext ctx = new InitialContext();

TestLocalHome home = (TestLocalHome) Class.forName("package.Beanname").newInstance();

TestBean test_ejb = (Beanname) home.create();

2.Now create Streams for Input and output.

File f = new File("F://XI/Demo/File2.pdf");

InputStream in = new FileInputStream(f);

FileOutputStream out = new FileOutputStream("F://XI/Demo/FileXML.xml");

3.Now execute the method.

test_ejb.execute(in,out);

4.now print the stream.

int a = 0;

out.write(a);

hope this helps.

regards,

datta.

Former Member
0 Kudos

Hi All,

Thanks for replys...

Here i am attaching my JavaBean (ModuleProcessor).

pls test this bean is working fine or not as standalone.

after testing this, pls send me that tested program..

help me out BCZ i am not from Javabackground...

Regards,

Govindu.

//Bean starts from here..

package pdfConverter;

import java.io.ByteArrayOutputStream;

import java.io.OutputStreamWriter;

import org.pdfbox.pdmodel.PDDocument;

import org.pdfbox.util.PDFTextStripper;

import com.sap.aii.af.mp.module.ModuleContext;

import com.sap.aii.af.mp.module.ModuleData;

import com.sap.aii.af.mp.module.ModuleException;

import com.sap.aii.af.ra.ms.api.Message;

import com.sap.aii.af.ra.ms.api.XMLPayload;

public class PDFtoXMLConverter {

public ModuleData process(ModuleContext moduleContext,ModuleData inputModuleData) throws ModuleException

{

Object obj = null;

Message msg = null;

String msgType = null;

String nameSpace = null;

try {

obj = inputModuleData.getPrincipalData();

msg = (Message) obj;

msgType=(String)moduleContext.getContextData("msgType");

nameSpace=(String) moduleContext.getContextData("nameSpace");

XMLPayload xmlpayload = (XMLPayload) msg.getDocument();

if (xmlpayload != null){

xmlpayload.setContent(convertToText(xmlpayload,msgType,nameSpace));

inputModuleData.setPrincipalData(msg);

}

}

catch (Exception e)

{

ModuleException me = new ModuleException (e);

throw me;

}

return inputModuleData;

}

private byte[] convertToText(XMLPayload xmlpayload,String msgType,String nameSpace)

throws Exception {

String textData = null;

int startPage = 1;

int endPage = Integer.MAX_VALUE;

PDDocument document = null;

String retXMLData = null;

try {

// Get PDDocument from the file content

document = PDDocument.load (xmlpayload.getInputStream());

ByteArrayOutputStream by = new ByteArrayOutputStream();

OutputStreamWriter out = new OutputStreamWriter (by);

PDFTextStripper stripper = null;

stripper = new PDFTextStripper();

stripper.setStartPage(startPage);

stripper.setEndPage(endPage);

stripper.writeText(document,out);

textData = by.toString();

retXMLData=

"<?xml version=\"1.0\"encoding=\"UTF-8\"?>\n\n"+

"<ns:" +

msgType +

"xmlns:ns=\"" +

nameSpace +

"\">\n" +

"<TextData>" +

textData +

"</TextData>\n" +

"</ns:" +

msgType +

">";

}

catch (Exception e){}

return retXMLData.getBytes();

}

}

former_member206604
Active Contributor
0 Kudos

Hi,

Please check the code below.. You need to add few linse of code in the place of comments. Some corrections might also be required in the code since I cannot test it.


import java.io.ByteArrayOutputStream;
import java.io.OutputStreamWriter;

import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.util.PDFTextStripper;

import com.sap.aii.af.mp.module.ModuleContext;
import com.sap.aii.af.mp.module.ModuleData;
import com.sap.aii.af.mp.module.ModuleException;
import com.sap.aii.af.ra.ms.api.Message;
import com.sap.aii.af.ra.ms.api.XMLPayload;


public class PDFtoXMLConverter {

public static void main(String[] args)
    {
        if (args.length < 2)
        {
            System.out.println("Usage: java " + PDFtoXMLConverter.class.getName() + " InputFile OutputFile");
            System.exit(1);
        }
        PDFtoXMLConverter parser = new PDFtoXMLConverterr();
// TODO : Get the input file and pass it to ConvertToText() function 

// TODO : Get the return parameter of ConvertToText() function and save it in the OutputFile     
        System.exit(0);
    }


private byte[] convertToText(XMLPayload xmlpayload,String msgType,String nameSpace)
throws Exception {
String textData = null;

int startPage = 1;
int endPage = Integer.MAX_VALUE;
PDDocument document = null;
String retXMLData = null;
try {
// Get PDDocument from the file content
document = PDDocument.load (xmlpayload.getInputStream());
ByteArrayOutputStream by = new ByteArrayOutputStream();
OutputStreamWriter out = new OutputStreamWriter (by);
PDFTextStripper stripper = null;
stripper = new PDFTextStripper();
stripper.setStartPage(startPage);
stripper.setEndPage(endPage);
stripper.writeText(document,out);
textData = by.toString();

retXMLData=
"<?xml version="1.0"encoding="UTF-8"?>nn"+
"<ns:" +
msgType +
"xmlns:ns="" +
nameSpace +
"">n" +
"<TextData>" +
textData +
"</TextData>n" +
"</ns:" +
msgType +
">";
}
catch (Exception e){}
return retXMLData.getBytes();
}
}

Answers (0)