Coverage Report - org.jaudiotagger.logging.XMLTagDisplayFormatter
 
Classes in this File Line Coverage Branch Coverage Complexity
XMLTagDisplayFormatter
81%
39/48
88%
14/16
1.562
 
 1  
 /**
 2  
  *  @author : Paul Taylor
 3  
  *
 4  
  *  Version @version:$Id: XMLTagDisplayFormatter.java,v 1.9 2008/07/21 10:46:14 paultaylor Exp $
 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  5
     StringBuffer sb = new StringBuffer();
 46  
 
 47  
     public XMLTagDisplayFormatter()
 48  5
     {
 49  
 
 50  5
     }
 51  
 
 52  
     /**
 53  
      * Return xml open tag round a string e.g <tag>
 54  
      */
 55  
     public static String xmlOpen(String xmlName)
 56  
     {
 57  1594
         return xmlOpenStart + xmlName + xmlOpenEnd;
 58  
     }
 59  
 
 60  
     public static String xmlOpenHeading(String name, String data)
 61  
     {
 62  105
         return (xmlOpen(name + " id=\"" + data + "\""));
 63  
     }
 64  
 
 65  
 
 66  
     /**
 67  
      * Return CDATA tag around xml data e.g <![CDATA[xmlData]]>
 68  
      * We also need to deal with special chars
 69  
      */
 70  
     public static String xmlCData(String xmlData)
 71  
     {
 72  
         char tempChar;
 73  1194
         StringBuffer replacedString = new StringBuffer();
 74  10040
         for (int i = 0; i < xmlData.length(); i++)
 75  
         {
 76  8846
             tempChar = xmlData.charAt(i);
 77  8846
             if ((Character.isLetterOrDigit(tempChar) == true) || (Character.isSpaceChar(tempChar) == true))
 78  
             {
 79  8350
                 replacedString.append(tempChar);
 80  
             }
 81  
             else
 82  
             {
 83  496
                 replacedString.append("#x" + Character.digit(tempChar, 16));
 84  
             }
 85  
         }
 86  1194
         return xmlCDataTagOpen + replacedString + xmlCDataTagClose;
 87  
     }
 88  
 
 89  
     /**
 90  
      * Return xml close tag around a string e.g </tag>
 91  
      */
 92  
     public static String xmlClose(String xmlName)
 93  
     {
 94  1594
         return xmlCloseStart + xmlName + xmlCloseEnd;
 95  
     }
 96  
 
 97  
     public static String xmlSingleTag(String data)
 98  
     {
 99  0
         return xmlOpenStart + data + xmlSingleTagClose;
 100  
     }
 101  
 
 102  
     public static String xmlFullTag(String xmlName, String data)
 103  
     {
 104  1194
         return xmlOpen(xmlName) + xmlCData(data) + xmlClose(xmlName);
 105  
     }
 106  
 
 107  
 
 108  
     public void openHeadingElement(String type, String value)
 109  
     {
 110  400
         if (value.length() == 0)
 111  
         {
 112  295
             sb.append(xmlOpen(type));
 113  
         }
 114  
         else
 115  
         {
 116  105
             sb.append(xmlOpenHeading(type, replaceXMLCharacters(value)));
 117  
         }
 118  400
     }
 119  
 
 120  
     public void openHeadingElement(String type, boolean value)
 121  
     {
 122  0
         openHeadingElement(type, String.valueOf(value));
 123  0
     }
 124  
 
 125  
     public void openHeadingElement(String type, int value)
 126  
     {
 127  0
         openHeadingElement(type, String.valueOf(value));
 128  0
     }
 129  
 
 130  
     public void closeHeadingElement(String type)
 131  
     {
 132  400
         sb.append(xmlClose(type));
 133  400
     }
 134  
 
 135  
     public void addElement(String type, String value)
 136  
     {
 137  1194
         sb.append(xmlFullTag(type, replaceXMLCharacters(value)));
 138  1194
     }
 139  
 
 140  
     public void addElement(String type, int value)
 141  
     {
 142  910
         addElement(type, String.valueOf(value));
 143  910
     }
 144  
 
 145  
     public void addElement(String type, boolean value)
 146  
     {
 147  30
         addElement(type, String.valueOf(value));
 148  30
     }
 149  
 
 150  
     public String toString()
 151  
     {
 152  5
         return sb.toString();
 153  
     }
 154  
 
 155  
 
 156  
     /**
 157  
      * Replace any special xml characters with the appropiate escape sequences
 158  
      * required to be done for the actual element names
 159  
      */
 160  
     public static String replaceXMLCharacters(String xmlData)
 161  
     {
 162  1299
         StringBuffer sb = new StringBuffer();
 163  1299
         StringCharacterIterator sCI = new StringCharacterIterator(xmlData);
 164  10429
         for (char c = sCI.first(); c != CharacterIterator.DONE; c = sCI.next())
 165  
         {
 166  9130
             switch (c)
 167  
             {
 168  
                 case'&':
 169  4
                     sb.append("&amp;");
 170  4
                     break;
 171  
                 case'<':
 172  0
                     sb.append("&lt;");
 173  0
                     break;
 174  
                 case'>':
 175  0
                     sb.append("&gt;");
 176  0
                     break;
 177  
                 case'"':
 178  52
                     sb.append("&quot;");
 179  52
                     break;
 180  
                 case'\'':
 181  30
                     sb.append("&apos;");
 182  30
                     break;
 183  
 
 184  
 
 185  
                 default:
 186  9044
                     sb.append(c);
 187  
             }
 188  
         }
 189  1299
         return sb.toString();
 190  
     }
 191  
 }