View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.example.loanbroker.transformers;
8   
9   import org.mule.api.transformer.TransformerException;
10  import org.mule.example.loanbroker.messages.CreditProfile;
11  import org.mule.transformer.AbstractTransformer;
12  import org.mule.transformer.types.DataTypeFactory;
13  
14  import org.dom4j.Document;
15  import org.dom4j.DocumentException;
16  import org.dom4j.DocumentHelper;
17  
18  public class CreditProfileXmlToCreditProfile extends AbstractTransformer
19  {
20  
21      public CreditProfileXmlToCreditProfile()
22      {
23          registerSourceType(DataTypeFactory.STRING);
24          registerSourceType(DataTypeFactory.create(Document.class));
25          setReturnDataType(DataTypeFactory.create(CreditProfile.class));
26      }
27  
28      @Override
29      public Object doTransform(Object src, String encoding) throws TransformerException
30      {
31          Document doc = null;
32  
33          if (src instanceof Document)
34          {
35              doc = (Document)src;
36          }
37          else
38          {
39              try
40              {
41                  doc = DocumentHelper.parseText(src.toString());
42              }
43              catch (DocumentException e)
44              {
45                  throw new TransformerException(this, e);
46              }
47          }
48  
49          String history = doc.valueOf("/credit-profile/customer-history");
50          String score = doc.valueOf("/credit-profile/credit-score");
51          CreditProfile cp = new CreditProfile();
52          cp.setCreditHistory(Integer.valueOf(history).intValue());
53          cp.setCreditScore(Integer.valueOf(score).intValue());
54          return cp;
55      }
56  
57  }