X-Git-Url: http://gitweb.fperrin.net/?p=GpsPrune.git;a=blobdiff_plain;f=tim%2Fprune%2Fsave%2Fxml%2FXmlUtils.java;fp=tim%2Fprune%2Fsave%2Fxml%2FXmlUtils.java;h=3943d078de1c1166e708a15345989df0ef7fc428;hp=dc8284c30ea90d4586b0cdec70c72c6102346225;hb=96b13866faa7559233689ce73e9de8041859bc2f;hpb=83fe7ed9d291abe6b082713e1df34e831a03c319 diff --git a/tim/prune/save/xml/XmlUtils.java b/tim/prune/save/xml/XmlUtils.java index dc8284c..3943d07 100644 --- a/tim/prune/save/xml/XmlUtils.java +++ b/tim/prune/save/xml/XmlUtils.java @@ -25,8 +25,8 @@ public abstract class XmlUtils */ public static String fixCdata(String inString) { - if (inString == null) return ""; - if (inString.indexOf('<') < 0 && inString.indexOf('>') < 0) { + if (inString == null || inString.isEmpty()) return ""; + if (!hasIllegalCharacter(inString)) { return inString; } String result = inString; @@ -37,12 +37,32 @@ public abstract class XmlUtils // Remove all instances of end block result = result.replaceAll(CDATA_END, ""); // Now check whether cdata block is required - if (result.indexOf('<') < 0 && result.indexOf('>') < 0) { + if (!XmlUtils.hasIllegalCharacter(result)) { return result; } return CDATA_START + result + CDATA_END; } + /** + * Checks the input string for the three illegal characters, + * but only looping through the string once instead of three times + * @param inValue string to check + * @return true if at least one of the illegal characters is found + */ + public static boolean hasIllegalCharacter(String inValue) + { + if (inValue == null) return false; + final int numChars = inValue.length(); + for (int i=0; i' || c == '&') + { + return true; + } + } + return false; + } /** * @return true if system uses UTF-8 by default