Coverage Report - org.jaudiotagger.tag.id3.ID3TextEncodingConversion
 
Classes in this File Line Coverage Branch Coverage Complexity
ID3TextEncodingConversion
91%
21/23
94%
17/18
6.333
 
 1  
 package org.jaudiotagger.tag.id3;
 2  
 
 3  
 import org.jaudiotagger.tag.TagOptionSingleton;
 4  
 import org.jaudiotagger.tag.id3.valuepair.TextEncoding;
 5  
 
 6  
 import java.util.logging.Logger;
 7  
 
 8  
 /**
 9  
  * Functions to encode text according to encodingoptions and ID3 version
 10  
  */
 11  0
 public class ID3TextEncodingConversion
 12  
 {
 13  
     //Logger
 14  27
     public static Logger logger = Logger.getLogger("org.jaudiotagger.tag.id3");
 15  
 
 16  
 
 17  
     /**
 18  
      * Check the text encoding is valid for this header type and is appropriate for
 19  
      * user text encoding options.                                             *
 20  
      * <p/>
 21  
      * This is called before writing any frames that use text encoding
 22  
      *
 23  
      * @param header       used to identify the ID3tagtype
 24  
      * @param textEncoding currently set
 25  
      * @return valid encoding according to version type and user options
 26  
      */
 27  
     public static byte getTextEncoding(AbstractTagFrame header, byte textEncoding)
 28  
     {
 29  
 
 30  
         //Should not happen, assume v23 and provide a warning
 31  1001
         if (header == null)
 32  
         {
 33  1
             logger.warning("Header has not yet been set for this framebody");
 34  
 
 35  1
             if (TagOptionSingleton.getInstance().isResetTextEncodingForExistingFrames())
 36  
             {
 37  0
                 return TagOptionSingleton.getInstance().getId3v23DefaultTextEncoding();
 38  
             }
 39  
             else
 40  
             {
 41  1
                 return convertV24textEncodingToV23textEncoding(textEncoding);
 42  
             }
 43  
         }
 44  1000
         else if (header instanceof ID3v24Frame)
 45  
         {
 46  490
             if (TagOptionSingleton.getInstance().isResetTextEncodingForExistingFrames())
 47  
             {
 48  
                 //Replace with default
 49  32
                 return TagOptionSingleton.getInstance().getId3v24DefaultTextEncoding();
 50  
             }
 51  
             else
 52  
             {
 53  
                 //All text encodings supported nothing to do
 54  458
                 return textEncoding;
 55  
             }
 56  
         }
 57  
         else
 58  
         {
 59  510
             if (TagOptionSingleton.getInstance().isResetTextEncodingForExistingFrames())
 60  
             {
 61  
                 //Replace with default
 62  32
                 return TagOptionSingleton.getInstance().getId3v23DefaultTextEncoding();
 63  
             }
 64  
             else
 65  
             {
 66  
                 //If text encoding is an unsupported v24 one we use unicode v23 equivalent
 67  478
                 return convertV24textEncodingToV23textEncoding(textEncoding);
 68  
             }
 69  
         }
 70  
     }
 71  
 
 72  
     /**
 73  
      * Sets the text encoding to best Unicode type for the version
 74  
      *
 75  
      * @param header
 76  
      * @return
 77  
      */
 78  
     public static byte getUnicodeTextEncoding(AbstractTagFrame header)
 79  
     {
 80  15
         if (header == null)
 81  
         {
 82  1
             logger.warning("Header has not yet been set for this framebody");
 83  1
             return TextEncoding.UTF_16;
 84  
         }
 85  14
         else if (header instanceof ID3v24Frame)
 86  
         {
 87  7
             return TagOptionSingleton.getInstance().getId3v24UnicodeTextEncoding();
 88  
         }
 89  
         else
 90  
         {
 91  7
             return TextEncoding.UTF_16;
 92  
         }
 93  
     }
 94  
 
 95  
     /**
 96  
      * Convert v24 text encoding to a valid v23 encoding
 97  
      *
 98  
      * @param textEncoding
 99  
      * @return valid encoding
 100  
      */
 101  
     private static byte convertV24textEncodingToV23textEncoding(byte textEncoding)
 102  
     {
 103  479
         if ((textEncoding == TextEncoding.UTF_16BE) || (textEncoding == TextEncoding.UTF_8))
 104  
         {
 105  37
             return TextEncoding.UTF_16;
 106  
         }
 107  
         else
 108  
         {
 109  442
             return textEncoding;
 110  
         }
 111  
     }
 112  
 
 113  
 
 114  
 }