on 04-15-2007 2:07 PM
Hi all,
I am using the following link to implement mapping and I get the follwoing error in Sap Netweaver Developer Studio. Can anayone tell me what is wrong.
/people/thorsten.nordholmsbirk/blog/2006/08/10/using-jaxp-to-both-parse-and-emit-xml-in-xi-java-mapping-programs
Here is my program
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import java.util.HashMap;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
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;
/*
Created on Apr 14, 2007
*
To change the template for this generated file go to
Window>Preferences>Java>Code Generation>Code and Comments
*/
/**
@author ishani
*
To change the template for this generated type comment go to
Window>Preferences>Java>Code Generation>Code and Comments
*/
public class FileToFile implements StreamTransformation {
/* (non-Javadoc)
@see com.sap.aii.mapping.api.StreamTransformation#setParameter(java.util.Map)
*/
private Map param = null;
private MappingTrace trace = null;
public void setParameter(Map param) {
// TODO Auto-generated method stub
this.param = param;
if (param == null) {
this.param = new HashMap();
}
}
/* (non-Javadoc)
@see com.sap.aii.mapping.api.StreamTransformation#execute(java.io.InputStream, java.io.OutputStream)
*/
public void execute(InputStream instr, OutputStream outstr)
throws StreamTransformationException {
// TODO Auto-generated method stub
trace = (MappingTrace)param.get(StreamTransformationConstants.MAPPING_TRACE);
trace.addInfo("Start of Trace");
String receivername =
(String)param.get(StreamTransformationConstants.RECEIVER_NAME);
trace.addInfo("Receiver is " + receivername);
String contenttype =
(String)param.get(StreamTransformationConstants.CONTENT_TYPE);
trace.addInfo("ContentType is " + contenttype);
DocumentBuilderFactory tmpDOMFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder tmpBuilder = tmpDOMFactory.newDocumentBuilder();
Document doc = tmpBuilder.parse(instr);
Node tmpFirstChild = doc.getFirstChild();
}
}
It says for these two lines
DocumentBuilder tmpBuilder = tmpDOMFactory.newDocumentBuilder();
Document doc = tmpBuilder.parse(instr);
it gives the following errors
1. Add throws declaration
2. Surround with try and catch.
Is it an error or a warning which I can ignore. Please help
Yeah I figured that one out but now it is giving that StreamTransformationException is unreachable exception. Why the heck is that?
public void execute(InputStream instr, OutputStream outstr)
throws StreamTransformationException {
// TODO Auto-generated method stub
try {
trace = (MappingTrace)param.get(StreamTransformationConstants.MAPPING_TRACE);
trace.addInfo("Start of Trace");
String receivername =
(String)param.get(StreamTransformationConstants.RECEIVER_NAME);
trace.addInfo("Receiver is " + receivername);
String contenttype =
(String)param.get(StreamTransformationConstants.CONTENT_TYPE);
trace.addInfo("ContentType is " + contenttype);
DocumentBuilderFactory tmpDOMFactory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder tmpBuilder = tmpDOMFactory.newDocumentBuilder();
Document doc = tmpBuilder.parse(instr);
}
catch ( SAXException tmpSAXException){
}
catch ( IOException tmpIOException){
}
catch ( ParserConfigurationException tmpParserException){
}
//Node tmpFirstChild = doc.getFirstChild();
}
catch(StreamTransformationException tmpStreamException) {
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Sonia-
You are getting that error because that catch block cannot be reached. Any exception thrown will be caught by one of the upper catch blocks before it reaches the StreamTransformationException catch block. In general, the catch blocks should be arranged in the following hierarchical(top down) manner:
specific exception to the more generic exception.
This way if the more specific catch block cannot catch the exception, it percolates down to the more generic one. However, if the generic exception block is placed above the specific one, it will always catch all the exceptions and thus the specific catch block will never get executed. And the java compiler certainly doesn't like that.
Since your method is throwing a StreamTransformationException a better way of handling this error would be something like this:
try{
// your custom code here
} catch (ParserConfigurationException e) {
//some logging code here
throw new StreamTransformationException("Can not create DocumentBuilder.", e);
} catch (SAXException e) {
//some logging code here
throw new StreamTransformationException("Can not read XML.", e);
} catch (IOException e) {
//some logging code here
throw new StreamTransformationException("Can not read XML.", e);
} catch (TransformerConfigurationException e) {
//some logging code here
throw new StreamTransformationException("Can not create Transformer.", e);
} catch (TransformerException e) {
//some logging code here
throw new StreamTransformationException("Can not write XML.", e);
}
Hope this helps.
In java, if your methos throws some exception (as defined in its prototype), then you will need to either:
1. if you call methods in your code that throw the exception that your custom method throws, then in this case you won't need try/catch (since the caller of your method will need to handle this exception);
2. if your code calls methods that throw some other exceptions that your method doesn't throw, you'll have to treat them with try/catch block and then throw a new instance of any of the exceptions that your custom method throws.
If it is saying that "StreamTransformationException is unreachable exception" it is because no called method throws it (or if any does, you are already catching it).
In particular for Java mappings, since the execute method is allowed to have only StreamTransformationException, all exceptions that you catch will have to throw it (in order to be treated by caller, in this case, XI's mapping engine).
To solve it, for example, you could treat all exceptions like this:
...
try {
...
}
catch (SAXException e){
throw new StreamTransformationException(e.getMessage());
}
...
Regards,
Henrique.
Hi,
You need the surround those lines with try...catch, e.g.
try {
DocumentBuilderFactory tmpDOMFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder tmpBuilder = tmpDOMFactory.newDocumentBuilder();
Document doc = tmpBuilder.parse(instr);
Node tmpFirstChild = doc.getFirstChild();
}
catch (Exception e) {
// do something here in case of error
}
Regards,
Bill
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
84 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.