Coverage Report - org.jaudiotagger.logging.XMLTagDisplayFormatter
 
Classes in this File Line Coverage Branch Coverage Complexity
XMLTagDisplayFormatter
81%
39/48
87%
14/16
1.625
 
 1  
 /**
 2  
  *  @author : Paul Taylor
 3  
  *
 4  
  *  Version @version:$Id: XMLTagDisplayFormatter.java 836 2009-11-12 15:44:07Z paultaylor $
 5  
  *
 6  
  *  MusicTag Copyright (C)2003,2004
 7  
  *
 8  
  *  This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser
 9  
  *  General Public  License as published by the Free Software Foundation; either version 2.1 of the License,
 10  
  *  or (at your option) any later version.
 11  
  *
 12  
  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 13  
  *  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 14  
  *  See the GNU Lesser General Public License for more details.
 15  
  *
 16  
  *  You should have received a copy of the GNU Lesser General Public License along with this library; if not,
 17  
  *  you can get a copy from http://www.opensource.org/licenses/lgpl-license.php or write to the Free Software
 18  
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 19  
  *
 20  
  */
 21  
 package org.jaudiotagger.logging;
 22  
 
 23  
 import java.text.CharacterIterator;
 24  
 import java.text.StringCharacterIterator;
 25  
 
 26  
 /*
 27  
  * For Formatting the metadata contents of a file in an XML format
 28  
  *
 29  
  * This could provide the basis of a representation of a files metadata, which can then be manipulated to
 30  
 * to create technical reports.
 31  
 */
 32  
 public class XMLTagDisplayFormatter extends AbstractTagDisplayFormatter
 33  
 {
 34  
     private static XMLTagDisplayFormatter formatter;
 35  
 
 36  
     protected static final String xmlOpenStart = "<";
 37  
     protected static final String xmlOpenEnd = ">";
 38  
     protected static final String xmlCloseStart = "</";
 39  
     protected static final String xmlCloseEnd = ">";
 40  
     protected static final String xmlSingleTagClose = " />";
 41  
     protected static final String xmlCDataTagOpen = "<![CDATA[";
 42  
     protected static final String xmlCDataTagClose = "]]>";
 43  
 
 44  
 
 45  32
     StringBuffer sb = new StringBuffer();
 46  
 
 47  
     public XMLTagDisplayFormatter()
 48  32
     {
 49  
 
 50  32
     }
 51  
 
 52  
     /**
 53  
      * Return xml open tag round a string e.g <tag>
 54  
      * @param xmlName
 55  
      * @return
 56  
      */
 57  
     public static String xmlOpen(String xmlName)
 58  
     {
 59  6748
         return xmlOpenStart + xmlName + xmlOpenEnd;
 60  
     }
 61  
 
 62  
     public static String xmlOpenHeading(String name, String data)
 63  
     {
 64  492
         return (xmlOpen(name + " id=\"" + data + "\""));
 65  
     }
 66  
 
 67  
 
 68  
     /**
 69  
      * Return CDATA tag around xml data e.g <![CDATA[xmlData]]>
 70  
      * We also need to deal with special chars
 71  
      * @param xmlData
 72  
      * @return
 73  
      */
 74  
     public static String xmlCData(String xmlData)
 75  
     {
 76  
         char tempChar;
 77  5004
         StringBuffer replacedString = new StringBuffer();
 78  41368
         for (int i = 0; i < xmlData.length(); i++)
 79  
         {
 80  36364
             tempChar = xmlData.charAt(i);
 81  36364
             if ((Character.isLetterOrDigit(tempChar)) || (Character.isSpaceChar(tempChar)))
 82  
             {
 83  34332
                 replacedString.append(tempChar);
 84  
             }
 85  
             else
 86  
             {
 87  2032
                 replacedString.append("#x").append(Character.digit(tempChar, 16));
 88  
             }
 89  
         }
 90  5004
         return xmlCDataTagOpen + replacedString + xmlCDataTagClose;
 91  
     }
 92  
 
 93  
     /**
 94  
      * Return xml close tag around a string e.g </tag>
 95  
      * @param xmlName
 96  
      * @return
 97  
      */
 98  
     public static String xmlClose(String xmlName)
 99  
     {
 100  6748
         return xmlCloseStart + xmlName + xmlCloseEnd;
 101  
     }
 102  
 
 103  
     public static String xmlSingleTag(String data)
 104  
     {
 105  0
         return xmlOpenStart + data + xmlSingleTagClose;
 106  
     }
 107  
 
 108  
     public static String xmlFullTag(String xmlName, String data)
 109  
     {
 110  5004
         return xmlOpen(xmlName) + xmlCData(data) + xmlClose(xmlName);
 111  
     }
 112  
 
 113  
 
 114  
     public void openHeadingElement(String type, String value)
 115  
     {
 116  1744
         if (value.length() == 0)
 117  
         {
 118  1252
             sb.append(xmlOpen(type));
 119  
         }
 120  
         else
 121  
         {
 122  492
             sb.append(xmlOpenHeading(type, replaceXMLCharacters(value)));
 123  
         }
 124  1744
     }
 125  
 
 126  
     public void openHeadingElement(String type, boolean value)
 127  
     {
 128  0
         openHeadingElement(type, String.valueOf(value));
 129  0
     }
 130  
 
 131  
     public void openHeadingElement(String type, int value)
 132  
     {
 133  0
         openHeadingElement(type, String.valueOf(value));
 134  0
     }
 135  
 
 136  
     public void closeHeadingElement(String type)
 137  
     {
 138  1744
         sb.append(xmlClose(type));
 139  1744
     }
 140  
 
 141  
     public void addElement(String type, String value)
 142  
     {
 143  5004
         sb.append(xmlFullTag(type, replaceXMLCharacters(value)));
 144  5004
     }
 145  
 
 146  
     public void addElement(String type, int value)
 147  
     {
 148  3736
         addElement(type, String.valueOf(value));
 149  3736
     }
 150  
 
 151  
     public void addElement(String type, boolean value)
 152  
     {
 153  144
         addElement(type, String.valueOf(value));
 154  144
     }
 155  
 
 156  
     public String toString()
 157  
     {
 158  32
         return sb.toString();
 159  
     }
 160  
 
 161  
 
 162  
     /**
 163  
      * Replace any special xml characters with the appropiate escape sequences
 164  
      * required to be done for the actual element names
 165  
      * @param xmlData
 166  
      * @return
 167  
      */
 168  
     public static String replaceXMLCharacters(String xmlData)
 169  
     {
 170  5496
         StringBuffer sb = new StringBuffer();
 171  5496
         StringCharacterIterator sCI = new StringCharacterIterator(xmlData);
 172  43896
         for (char c = sCI.first(); c != CharacterIterator.DONE; c = sCI.next())
 173  
         {
 174  38400
             switch (c)
 175  
             {
 176  
                 case'&':
 177  16
                     sb.append("&amp;");
 178  16
                     break;
 179  
                 case'<':
 180  0
                     sb.append("&lt;");
 181  0
                     break;
 182  
                 case'>':
 183  0
                     sb.append("&gt;");
 184  0
                     break;
 185  
                 case'"':
 186  208
                     sb.append("&quot;");
 187  208
                     break;
 188  
                 case'\'':
 189  132
                     sb.append("&apos;");
 190  132
                     break;
 191  
 
 192  
 
 193  
                 default:
 194  38044
                     sb.append(c);
 195  
             }
 196  
         }
 197  5496
         return sb.toString();
 198  
     }
 199  
 }