Decode/Encode xml characters in the Plist Editor

This commit is contained in:
vectorsigma72 2020-06-03 16:41:01 +02:00
parent 7dec627fb7
commit c0fba39133
3 changed files with 16 additions and 15 deletions

View File

@ -174,9 +174,9 @@ extension String {
return UnsafePointer<UInt8>(buffer) return UnsafePointer<UInt8>(buffer)
} }
/// Escape XML special characthers such: /// Encode XML special characthers such:
/// & as &amp, \ as &quot, ' as &apos, < as &lt, and > as &gt /// & as &amp, \ as &quot, ' as &apos, < as &lt, and > as &gt
var escapingXMLCharacters: String { var encodingXMLCharacters: String {
get { get {
/* /*
" &quot; " &quot;
@ -192,13 +192,14 @@ extension String {
s = s.replacingOccurrences(of: "'", with: "&apos;") s = s.replacingOccurrences(of: "'", with: "&apos;")
s = s.replacingOccurrences(of: "<", with: "&lt;") s = s.replacingOccurrences(of: "<", with: "&lt;")
s = s.replacingOccurrences(of: ">", with: "&gt;") s = s.replacingOccurrences(of: ">", with: "&gt;")
print(s)
return s return s
} }
} }
/// Convert XML characters such: /// Decode XML characters such:
/// &amp to &, &quot to \ , ' to &apos, &lt to <, and &gt to > /// &amp to &, &quot to \ , ' to &apos, &lt to <, and &gt to >
var convertingXMLCharacters: String { var decodingXMLCharacters: String {
get { get {
/* /*
" &quot; " &quot;

View File

@ -50,7 +50,7 @@ func gConvertPENodeToPlist(node: PENode?) -> String {
plist = xml1Header + "\n" + gSerializeNodeToPlist(node: node!, file: "", indentation: 0) + xml1Footer plist = xml1Header + "\n" + gSerializeNodeToPlist(node: node!, file: "", indentation: 0) + xml1Footer
} }
} else if type == .String { } else if type == .String {
plist = xml1Header + "\n" + "<string>" + (ro.value as! String) + "</string>" + "\n" + xml1Footer plist = xml1Header + "\n" + "<string>" + (ro.value as! String).encodingXMLCharacters + "</string>" + "\n" + xml1Footer
} else if type == .Data { } else if type == .Data {
let data = ro.value as! NSData let data = ro.value as! NSData
let strBase64 : String = (data as Data).base64EncodedString(options: .endLineWithLineFeed) let strBase64 : String = (data as Data).base64EncodedString(options: .endLineWithLineFeed)
@ -89,7 +89,7 @@ func gSerializeNodeToPlist(node: PENode, file: String, indentation: Int) -> Stri
if type == .Dictionary { if type == .Dictionary {
if !isRoot { if !isRoot {
if node.peparent != nil && node.peparent!.tagdata!.type != .Array { if node.peparent != nil && node.peparent!.tagdata!.type != .Array {
str += indent + "<key>" + node.tagdata!.key + "</key>" + "\n" str += indent + "<key>" + node.tagdata!.key.encodingXMLCharacters + "</key>" + "\n"
} }
} else { } else {
str = "<dict>\n" str = "<dict>\n"
@ -120,7 +120,7 @@ func gSerializeNodeToPlist(node: PENode, file: String, indentation: Int) -> Stri
} }
} else if type == .Array { } else if type == .Array {
if !isRoot { if !isRoot {
str += indent + "<key>" + node.tagdata!.key + "</key>" + "\n" str += indent + "<key>" + node.tagdata!.key.encodingXMLCharacters + "</key>" + "\n"
} else { } else {
str = "<array>\n" str = "<array>\n"
} }
@ -153,14 +153,14 @@ func gSerializeNodeToPlist(node: PENode, file: String, indentation: Int) -> Stri
<string>hi</string> <string>hi</string>
*/ */
if node.peparent!.tagdata!.type != .Array { if node.peparent!.tagdata!.type != .Array {
str = str + indent + "<key>" + node.tagdata!.key + "</key>" + "\n" str = str + indent + "<key>" + node.tagdata!.key.encodingXMLCharacters + "</key>" + "\n"
} else if isRoot { } else if isRoot {
str = str + indent + "<key>" + localizedNewItem + "</key>" + "\n" str = str + indent + "<key>" + localizedNewItem + "</key>" + "\n"
} }
str = str + indent + "<string>" + (node.tagdata!.value as! String) + "</string>" + "\n" str = str + indent + "<string>" + (node.tagdata!.value as! String).encodingXMLCharacters + "</string>" + "\n"
} else if type == .Data { } else if type == .Data {
if node.peparent!.tagdata!.type != .Array { if node.peparent!.tagdata!.type != .Array {
str = str + indent + "<key>" + node.tagdata!.key + "</key>" + "\n" str = str + indent + "<key>" + node.tagdata!.key.encodingXMLCharacters + "</key>" + "\n"
} else if isRoot { } else if isRoot {
str = str + indent + "<key>" + localizedNewItem + "</key>" + "\n" str = str + indent + "<key>" + localizedNewItem + "</key>" + "\n"
} }
@ -169,14 +169,14 @@ func gSerializeNodeToPlist(node: PENode, file: String, indentation: Int) -> Stri
str = str + indent + "<data>" + strBase64 + "</data>" + "\n" str = str + indent + "<data>" + strBase64 + "</data>" + "\n"
} else if type == .Date { } else if type == .Date {
if node.peparent!.tagdata!.type != .Array { if node.peparent!.tagdata!.type != .Array {
str = str + indent + "<key>" + node.tagdata!.key + "</key>" + "\n" str = str + indent + "<key>" + node.tagdata!.key.encodingXMLCharacters + "</key>" + "\n"
} else if isRoot { } else if isRoot {
str = str + indent + "<key>" + localizedNewItem + "</key>" + "\n" str = str + indent + "<key>" + localizedNewItem + "</key>" + "\n"
} }
str = str + indent + "<date>" + utcDateToString(node.tagdata!.value as! Date) + "</date>" + "\n" str = str + indent + "<date>" + utcDateToString(node.tagdata!.value as! Date) + "</date>" + "\n"
} else if type == .Number { } else if type == .Number {
if node.peparent!.tagdata!.type != .Array { if node.peparent!.tagdata!.type != .Array {
str = str + indent + "<key>" + node.tagdata!.key + "</key>" + "\n" str = str + indent + "<key>" + node.tagdata!.key.encodingXMLCharacters + "</key>" + "\n"
} else if isRoot { } else if isRoot {
str = str + indent + "<key>" + localizedNewItem + "</key>" + "\n" str = str + indent + "<key>" + localizedNewItem + "</key>" + "\n"
} }
@ -190,7 +190,7 @@ func gSerializeNodeToPlist(node: PENode, file: String, indentation: Int) -> Stri
} }
} else if type == .Bool { } else if type == .Bool {
if node.peparent!.tagdata!.type != .Array { if node.peparent!.tagdata!.type != .Array {
str = str + indent + "<key>" + node.tagdata!.key + "</key>" + "\n" str = str + indent + "<key>" + node.tagdata!.key.encodingXMLCharacters + "</key>" + "\n"
} else if isRoot { } else if isRoot {
str = str + indent + "<key>" + localizedNewItem + "</key>" + "\n" str = str + indent + "<key>" + localizedNewItem + "</key>" + "\n"
} }

View File

@ -331,10 +331,10 @@ final class PlistParser: NSObject, XMLParserDelegate {
self.goback() self.goback()
break break
case "key": case "key":
self.currentNode?.tagdata?.key = value?.escapingXMLCharacters ?? "" self.currentNode?.tagdata?.key = value?.decodingXMLCharacters ?? ""
break break
case "string": case "string":
self.currentNode?.tagdata?.value = value!.escapingXMLCharacters self.currentNode?.tagdata?.value = value!.decodingXMLCharacters
self.currentNode?.tagdata?.type = .String self.currentNode?.tagdata?.type = .String
self.goback() self.goback()
break break