]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/save/xml/XmlUtils.java
Version 13, August 2011
[GpsPrune.git] / tim / prune / save / xml / XmlUtils.java
1 package tim.prune.save.xml;
2
3 /**
4  * Collection of utility functions for handling XML
5  */
6 public abstract class XmlUtils
7 {
8         /** Start of Cdata sequence */
9         private static final String CDATA_START = "<![CDATA[";
10         /** End of Cdata sequence */
11         private static final String CDATA_END = "]]>";
12
13         /**
14          * Fix the CDATA blocks in the given String to give valid xml
15          * @param inString String to modify
16          * @return fixed String
17          */
18         public static String fixCdata(String inString)
19         {
20                 if (inString == null) return "";
21                 if (inString.indexOf('<') < 0 && inString.indexOf('>') < 0) {
22                         return inString;
23                 }
24                 String result = inString;
25                 // Remove cdata block at start if present
26                 if (result.startsWith(CDATA_START)) {
27                         result = result.substring(CDATA_START.length());
28                 }
29                 // Remove all instances of end block
30                 result = result.replaceAll(CDATA_END, "");
31                 // Now check whether cdata block is required
32                 if (result.indexOf('<') < 0 && result.indexOf('>') < 0) {
33                         return result;
34                 }
35                 return CDATA_START + result + CDATA_END;
36         }
37 }