Clover coverage report - Maven Clover report
Coverage timestamp: Sun Aug 20 2006 04:01:04 PDT
file stats: LOC: 145   Methods: 8
NCLOC: 72   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
TextHandler.java 0% 0% 0% 0%
coverage
 1    /*
 2    * Copyright 2004,2005 The Apache Software Foundation.
 3    *
 4    * Licensed under the Apache License, Version 2.0 (the "License");
 5    * you may not use this file except in compliance with the License.
 6    * You may obtain a copy of the License at
 7    *
 8    * http://www.apache.org/licenses/LICENSE-2.0
 9    *
 10    * Unless required by applicable law or agreed to in writing, software
 11    * distributed under the License is distributed on an "AS IS" BASIS,
 12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13    * See the License for the specific language governing permissions and
 14    * limitations under the License.
 15    */
 16    package org.apache.geronimo.mail.handlers;
 17   
 18    import java.awt.datatransfer.DataFlavor;
 19    import java.io.IOException;
 20    import java.io.InputStreamReader;
 21    import java.io.OutputStream;
 22    import java.io.OutputStreamWriter;
 23    import java.io.StringWriter;
 24    import java.io.UnsupportedEncodingException;
 25   
 26    import javax.activation.ActivationDataFlavor;
 27    import javax.activation.DataContentHandler;
 28    import javax.activation.DataSource;
 29    import javax.mail.internet.ContentType;
 30    import javax.mail.internet.MimeUtility;
 31    import javax.mail.internet.ParseException;
 32   
 33    public class TextHandler implements DataContentHandler {
 34    /**
 35    * Field dataFlavor
 36    */
 37    ActivationDataFlavor dataFlavor;
 38   
 39  0 public TextHandler(){
 40  0 dataFlavor = new ActivationDataFlavor(java.lang.String.class, "text/plain", "Text String");
 41    }
 42   
 43    /**
 44    * Constructor TextHandler
 45    *
 46    * @param dataFlavor
 47    */
 48  0 public TextHandler(ActivationDataFlavor dataFlavor) {
 49  0 this.dataFlavor = dataFlavor;
 50    }
 51   
 52    /**
 53    * Method getDF
 54    *
 55    * @return dataflavor
 56    */
 57  0 protected ActivationDataFlavor getDF() {
 58  0 return dataFlavor;
 59    }
 60   
 61    /**
 62    * Method getTransferDataFlavors
 63    *
 64    * @return dataflavors
 65    */
 66  0 public DataFlavor[] getTransferDataFlavors() {
 67  0 return (new DataFlavor[]{dataFlavor});
 68    }
 69   
 70    /**
 71    * Method getTransferData
 72    *
 73    * @param dataflavor
 74    * @param datasource
 75    * @return
 76    * @throws IOException
 77    */
 78  0 public Object getTransferData(DataFlavor dataflavor, DataSource datasource)
 79    throws IOException {
 80  0 if (getDF().equals(dataflavor)) {
 81  0 return getContent(datasource);
 82    }
 83  0 return null;
 84    }
 85   
 86    /**
 87    * Method getContent
 88    *
 89    * @param datasource
 90    * @return
 91    * @throws IOException
 92    */
 93  0 public Object getContent(DataSource datasource) throws IOException {
 94  0 InputStreamReader reader;
 95  0 try {
 96  0 String s = getCharSet(datasource.getContentType());
 97  0 reader = new InputStreamReader(datasource.getInputStream(), s);
 98    } catch (Exception ex) {
 99  0 throw new UnsupportedEncodingException(ex.toString());
 100    }
 101  0 StringWriter writer = new StringWriter();
 102  0 int ch;
 103  0 while ((ch = reader.read()) != -1) {
 104  0 writer.write(ch);
 105    }
 106  0 return writer.getBuffer().toString();
 107    }
 108   
 109    /**
 110    * Method writeTo
 111    *
 112    * @param object
 113    * @param s
 114    * @param outputstream
 115    * @throws IOException
 116    */
 117  0 public void writeTo(Object object, String s, OutputStream outputstream)
 118    throws IOException {
 119  0 OutputStreamWriter os;
 120  0 try {
 121  0 String charset = getCharSet(s);
 122  0 os = new OutputStreamWriter(outputstream, charset);
 123    } catch (Exception ex) {
 124  0 throw new UnsupportedEncodingException(ex.toString());
 125    }
 126  0 String content = (String) object;
 127  0 os.write(content, 0, content.length());
 128  0 os.flush();
 129    }
 130   
 131    /**
 132    * get the character set from content type
 133    * @param contentType
 134    * @return
 135    * @throws ParseException
 136    */
 137  0 protected String getCharSet(String contentType) throws ParseException {
 138  0 ContentType type = new ContentType(contentType);
 139  0 String charset = type.getParameter("charset");
 140  0 if (charset == null) {
 141  0 charset = "us-ascii";
 142    }
 143  0 return MimeUtility.javaCharset(charset);
 144    }
 145    }