View Javadoc

1   /*
2    * $Id: BaseToOutputHandler.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.module.atom.transformers;
12  
13  import org.mule.api.MuleEvent;
14  import org.mule.api.transformer.TransformerException;
15  import org.mule.api.transport.OutputHandler;
16  import org.mule.transformer.AbstractDiscoverableTransformer;
17  import org.mule.transformer.types.DataTypeFactory;
18  
19  import java.io.IOException;
20  import java.io.OutputStream;
21  
22  import org.apache.abdera.model.Base;
23  import org.apache.abdera.parser.stax.FOMWriterOptions;
24  
25  /**
26   * Converts Abdera model elements which extend {@link Base} to OutputHandlers.
27   */
28  public class BaseToOutputHandler extends AbstractDiscoverableTransformer
29  {
30      public BaseToOutputHandler()
31      {
32          this.registerSourceType(DataTypeFactory.create(Base.class));
33          setReturnDataType(DataTypeFactory.create(OutputHandler.class));
34      }
35  
36      @Override
37      public Object doTransform(Object src, String outputEncoding) throws TransformerException
38      {
39          try
40          {
41              final Base e = (Base) src;
42  
43              return new OutputHandler()
44              {
45                  public void write(MuleEvent event, OutputStream out) throws IOException
46                  {
47                      FOMWriterOptions opts = new FOMWriterOptions();
48                      opts.setCharset(event.getEncoding());
49                      e.writeTo(out, opts);
50                  }
51              };
52          }
53          catch (Exception e)
54          {
55              throw new TransformerException(this, e);
56          }
57      }
58  }