View Javadoc

1   /*
2    * $Id: CreditProfileXmlToCreditProfile.java 19250 2010-08-30 16:53:14Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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      {
27          registerSourceType(DataTypeFactory.STRING);
28          registerSourceType(DataTypeFactory.create(Document.class));
29          setReturnDataType(DataTypeFactory.create(CreditProfile.class));
30      }
31  
32      @Override
33      public Object doTransform(Object src, String encoding) throws TransformerException
34      {
35          Document doc = null;
36  
37          if (src instanceof Document)
38          {
39              doc = (Document)src;
40          }
41          else
42          {
43              try
44              {
45                  doc = DocumentHelper.parseText(src.toString());
46              }
47              catch (DocumentException e)
48              {
49                  throw new TransformerException(this, e);
50              }
51          }
52  
53          String history = doc.valueOf("/credit-profile/customer-history");
54          String score = doc.valueOf("/credit-profile/credit-score");
55          CreditProfile cp = new CreditProfile();
56          cp.setCreditHistory(Integer.valueOf(history).intValue());
57          cp.setCreditScore(Integer.valueOf(score).intValue());
58          return cp;
59      }
60  
61  }