1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.example.loanbroker.transformers; |
12 | |
|
13 | |
import org.mule.api.transformer.TransformerException; |
14 | |
import org.mule.example.loanbroker.messages.CreditProfile; |
15 | |
import org.mule.transformer.AbstractTransformer; |
16 | |
import org.mule.transformer.types.DataTypeFactory; |
17 | |
|
18 | |
import org.dom4j.Document; |
19 | |
import org.dom4j.DocumentException; |
20 | |
import org.dom4j.DocumentHelper; |
21 | |
|
22 | |
public class CreditProfileXmlToCreditProfile extends AbstractTransformer |
23 | |
{ |
24 | |
|
25 | |
public CreditProfileXmlToCreditProfile() |
26 | 0 | { |
27 | 0 | registerSourceType(DataTypeFactory.STRING); |
28 | 0 | registerSourceType(DataTypeFactory.create(Document.class)); |
29 | 0 | setReturnDataType(DataTypeFactory.create(CreditProfile.class)); |
30 | 0 | } |
31 | |
|
32 | |
@Override |
33 | |
public Object doTransform(Object src, String encoding) throws TransformerException |
34 | |
{ |
35 | 0 | Document doc = null; |
36 | |
|
37 | 0 | if (src instanceof Document) |
38 | |
{ |
39 | 0 | doc = (Document)src; |
40 | |
} |
41 | |
else |
42 | |
{ |
43 | |
try |
44 | |
{ |
45 | 0 | doc = DocumentHelper.parseText(src.toString()); |
46 | |
} |
47 | 0 | catch (DocumentException e) |
48 | |
{ |
49 | 0 | throw new TransformerException(this, e); |
50 | 0 | } |
51 | |
} |
52 | |
|
53 | 0 | String history = doc.valueOf("/credit-profile/customer-history"); |
54 | 0 | String score = doc.valueOf("/credit-profile/credit-score"); |
55 | 0 | CreditProfile cp = new CreditProfile(); |
56 | 0 | cp.setCreditHistory(Integer.valueOf(history).intValue()); |
57 | 0 | cp.setCreditScore(Integer.valueOf(score).intValue()); |
58 | 0 | return cp; |
59 | |
} |
60 | |
|
61 | |
} |