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