| | 1 | | #pragma rtGlobals=3 |
| | 2 | | #pragma TextEncoding="UTF-8" |
| | 3 | | #pragma rtFunctionErrors=1 |
| | 4 | | #pragma version=1.10 |
| | 5 | | #pragma ModuleName=IUTF_Utils_XML |
| | 6 | |
|
| | 7 | | // Helper functions to create XML files. |
| | 8 | |
|
| | 9 | | /// XML properties |
| | 10 | | /// New Line is \n |
| | 11 | | /// The xs:int signed 32 bit Integer |
| | 12 | | /// The xs:decimal generic fp with max. 18 digits, exponential or scientific notation not supported. |
| | 13 | | /// The xs:string data type can contain characters, line feeds, carriage returns, and tab characters, needs also entity |
| | 14 | | /// The xs:token data type also contains characters, but the XML processor will remove |
| | 15 | | /// line feeds, carriage returns, tabs, leading and trailing spaces, and multiple spaces. |
| | 16 | | /// it is a subtype of xs:string, entity escapes apply here |
| | 17 | | /// XML: Reduces a string to a xs:token |
| 405 | 18 | | static Function/S ToXMLToken(str) |
| | 19 | | string str |
| 405 | 20 | | variable i |
| | 21 | |
|
| 405 | 22 | | str = ReplaceString("\n", str, "") |
| 405 | 23 | | str = ReplaceString("\r", str, "") |
| 405 | 24 | | str = ReplaceString("\t", str, "") |
| | 25 | | #if (IgorVersion() >= 7.0) |
| 405 | 26 | | return (TrimString(str, 1)) |
| | 27 | | #else |
| 0 | 28 | | for(i = 0; strsearch(str, " ", 0) >= 0;) |
| 0 | 29 | | str = ReplaceString(" ", str, " ") |
| 0 | 30 | | endfor |
| 0 | 31 | | return (TrimString(str)) |
| | 32 | | #endif |
| 405 | 33 | | End |
| | 34 | |
|
| | 35 | | /// entity references |
| | 36 | | /// < < less than |
| | 37 | | /// > > greater than |
| | 38 | | /// & & ampersand |
| | 39 | | /// ' ' apostrophe |
| | 40 | | /// " " quotation mark |
| | 41 | | /// XML: Escape Entity Replacer for strings |
| 570 | 42 | | static Function/S ToXMLCharacters(str) |
| | 43 | | string str |
| | 44 | |
|
| 570 | 45 | | str = ReplaceString("&", str, "&") |
| 570 | 46 | | str = ReplaceString("<", str, "<") |
| 570 | 47 | | str = ReplaceString(">", str, ">") |
| 570 | 48 | | str = ReplaceString("'", str, "'") |
| 570 | 49 | | str = ReplaceString("\"", str, """) |
| | 50 | |
|
| 570 | 51 | | return str |
| 570 | 52 | | End |
| | 53 | |
|
| | 54 | | /// Writes the report xml to a local file. |
| | 55 | | /// |
| | 56 | | /// @param prefix The name of the prefix which is prepended to the file name |
| | 57 | | /// @param content The XML content that should be written |
| | 58 | | /// @param outDir (optional, default: home directory) The output directory. It will use the home |
| | 59 | | /// directory if this string is empty. |
| 5 | 60 | | static Function WriteXML(prefix, content, [outDir]) |
| | 61 | | string prefix, content |
| | 62 | | string outDir |
| | 63 | |
|
| 5 | 64 | | string fileName, msg |
| 5 | 65 | | variable fnum |
| | 66 | |
|
| 5 | 67 | | if(ParamIsDefault(outDir) || IUTF_Utils#IsEmpty(outDir)) |
| 5 | 68 | | fileName = IUTF_Utils_Paths#AtHome(prefix + GetBaseFilename() + ".xml", unusedName = 1) |
| 0 | 69 | | else |
| 0 | 70 | | fileName = outDir + prefix + GetBaseFilename() + ".xml" |
| 0 | 71 | | fileName = IUTF_Utils_Paths#getUnusedFileName(fileName) |
| 0 | 72 | | if(IUTF_Utils#IsEmpty(fileName)) |
| 0 | 73 | | sprintf msg, "Cannot determine unused file for %s at %s", fileName, outDir |
| 0 | 74 | | IUTF_Reporting#ReportErrorAndAbort(msg) |
| 0 | 75 | | endif |
| 5 | 76 | | endif |
| | 77 | |
|
| 5 | 78 | | Open/Z fnum as fileName |
| 5 | 79 | | if(!V_flag) |
| 5 | 80 | | FBinWrite fnum, content |
| 5 | 81 | | Close fnum |
| 0 | 82 | | else |
| 0 | 83 | | sprintf msg, "Error: Could not create XML output file at %s", fileName |
| 0 | 84 | | IUTF_Reporting#IUTF_PrintStatusMessage(msg) |
| 5 | 85 | | endif |
| 5 | 86 | | End |