|
| 1 | +Include %callout |
| 2 | + |
| 3 | +/// Encapsulates the XSLT String Buffer XDEV device. <br /> |
| 4 | +/// Note that only one <class>SourceControl.Git.Util.Buffer</class> instance may be used at a time per job. |
| 5 | +Class SourceControl.Git.Util.Buffer Extends %RegisteredObject |
| 6 | +{ |
| 7 | + |
| 8 | +/// Size of the XSLT string buffer, in MB. <br /> |
| 9 | +/// Changes will only take effect the next time <method>BeginCaptureOutput</method>() is called. <br /> |
| 10 | +Property StringBufferSize As %Integer [ InitialExpression = 25 ]; |
| 11 | + |
| 12 | +/// Corresponds to "C" mode flag in device open. <br /> |
| 13 | +/// May be changed after <method>BeginCaptureOutput</method>() is called and the XDEV device is open. <br /> |
| 14 | +Property CarriageReturnMode As %Boolean [ InitialExpression = 1 ]; |
| 15 | + |
| 16 | +Method CarriageReturnModeSet(value As %Boolean) As %Status |
| 17 | +{ |
| 18 | + Set tSC = $$$OK |
| 19 | + Set tIO = $io |
| 20 | + Try { |
| 21 | + Set tValue = ''value |
| 22 | + If ..DeviceOpen { |
| 23 | + Set tModeChange = $Select(tValue: "+", 1: "-")_"C" |
| 24 | + Use ..Device:(::tModeChange) |
| 25 | + } |
| 26 | + } Catch e { |
| 27 | + Set tSC = e.AsStatus() |
| 28 | + } |
| 29 | + Use tIO |
| 30 | + If $$$ISOK(tSC) { |
| 31 | + Set i%CarriageReturnMode = tValue |
| 32 | + } |
| 33 | + Quit tSC |
| 34 | +} |
| 35 | + |
| 36 | +/// Translate Table to use - by default, UTF8 for unicode instances, RAW otherwise. <br /> |
| 37 | +/// May be changed after <method>BeginCaptureOutput</method>() is called and the XDEV device is open. <br /> |
| 38 | +Property TranslateTable As %String [ InitialExpression = {$select($$$IsUnicode:"UTF8",1:"RAW")} ]; |
| 39 | + |
| 40 | +Method TranslateTableSet(value As %String) As %Status |
| 41 | +{ |
| 42 | + Set tSC = $$$OK |
| 43 | + Set tIO = $io |
| 44 | + Try { |
| 45 | + // TODO: validate that value is a valid TranslateTable, especially when ..DeviceOpen = 0 |
| 46 | + If ..DeviceOpen { |
| 47 | + Use ..Device:(/IOT=value) |
| 48 | + } |
| 49 | + } Catch e { |
| 50 | + Set tSC = e.AsStatus() |
| 51 | + } |
| 52 | + Use tIO |
| 53 | + If $$$ISOK(tSC) { |
| 54 | + Set i%TranslateTable = value |
| 55 | + } |
| 56 | + Quit tSC |
| 57 | +} |
| 58 | + |
| 59 | +/// Input buffer size, in bytes. <br /> |
| 60 | +/// Changes will only take effect the next time <method>BeginCaptureOutput</method>() is called. <br /> |
| 61 | +Property InputBufferSize As %Integer(MAXVAL = 1048576, MINVAL = 1024) [ InitialExpression = 16384 ]; |
| 62 | + |
| 63 | +/// Output buffer size, in bytes. <br /> |
| 64 | +/// Changes will only take effect the next time <method>BeginCaptureOutput</method>() is called. <br /> |
| 65 | +Property OutputBufferSize As %Integer(MAXVAL = 1048576, MINVAL = 1024) [ InitialExpression = 16384 ]; |
| 66 | + |
| 67 | +/// Name of XDEV device. |
| 68 | +Property Device As %String [ Internal, Private ]; |
| 69 | + |
| 70 | +/// Tracks whether <property>Device</property> is currently open. |
| 71 | +Property DeviceOpen As %Boolean [ InitialExpression = 0, Internal, Private ]; |
| 72 | + |
| 73 | +/// Keeps track of the previously opened device. |
| 74 | +Property PreviousDevice As %String [ Internal, Private ]; |
| 75 | + |
| 76 | +/// Keeps track of the previous state of the I/O redirection flag |
| 77 | +Property PreviousIORedirectFlag As %Boolean [ Internal, Private ]; |
| 78 | + |
| 79 | +/// Keeps track of the previous mnemonic routine |
| 80 | +Property PreviousMnemonicRoutine As %String [ Internal, Private ]; |
| 81 | + |
| 82 | +/// Initializes the device name for this object. |
| 83 | +Method %OnNew() As %Status [ Private, ServerOnly = 1 ] |
| 84 | +{ |
| 85 | + // Multiple processes can use the same XDEV device name without conflict, so we can use the object reference to identify the device. |
| 86 | + Set ..Device = "|XDEV|"_(+$This) |
| 87 | + Quit $$$OK |
| 88 | +} |
| 89 | + |
| 90 | +/// Begins capturing output. <br /> |
| 91 | +Method BeginCaptureOutput() As %Status |
| 92 | +{ |
| 93 | + Set tSC = $$$OK |
| 94 | + Try { |
| 95 | + Set tModeParams = $Select(..CarriageReturnMode: "C", 1: "")_"S" |
| 96 | + Close ..Device |
| 97 | + Open ..Device:($ZF(-6,$$$XSLTLibrary,12):..StringBufferSize:tModeParams:/HOSTNAME="XSLT":/IOT=..TranslateTable:/IBU=..InputBufferSize:/OBU=..OutputBufferSize) |
| 98 | + Set ..DeviceOpen = 1 |
| 99 | + Set ..PreviousDevice = $io |
| 100 | + Set ..PreviousIORedirectFlag = ##class(%Library.Device).ReDirectIO() |
| 101 | + Set ..PreviousMnemonicRoutine = ##class(%Library.Device).GetMnemonicRoutine() |
| 102 | + Use ..Device |
| 103 | + If ..PreviousIORedirectFlag { |
| 104 | + Do ##class(%Library.Device).ReDirectIO(0) |
| 105 | + } |
| 106 | + } Catch e { |
| 107 | + Set tSC = e.AsStatus() |
| 108 | + Close ..Device |
| 109 | + Set ..DeviceOpen = 0 |
| 110 | + } |
| 111 | + Quit tSC |
| 112 | +} |
| 113 | + |
| 114 | +/// Reads all output from the buffer to stream <var>pStream</var>, |
| 115 | +/// which will be initialized as a <class>%Stream.TmpBinary</class> object if not provided. |
| 116 | +Method ReadToStream(ByRef pStream As %Stream.Object) As %Status |
| 117 | +{ |
| 118 | + Set tSC = $$$OK |
| 119 | + Set tOldIO = $io |
| 120 | + Try { |
| 121 | + If '$IsObject($Get(pStream)) { |
| 122 | + Set pStream = ##class(%Stream.TmpBinary).%New() |
| 123 | + } |
| 124 | + |
| 125 | + If '..DeviceOpen { |
| 126 | + // No-op |
| 127 | + Quit |
| 128 | + } |
| 129 | + |
| 130 | + Use ..Device |
| 131 | + |
| 132 | + // Flush any remaining output |
| 133 | + Write *-3 |
| 134 | + |
| 135 | + // Stream |
| 136 | + If pStream.%IsA("%Stream.FileCharacter") { |
| 137 | + // Force stream's file to open |
| 138 | + Set tSC = pStream.Write("") |
| 139 | + If $$$ISERR(tSC) { |
| 140 | + Quit |
| 141 | + } |
| 142 | + |
| 143 | + Set tFile = pStream.Filename |
| 144 | + For { |
| 145 | + Use ..Device |
| 146 | + Set tChunk = "" |
| 147 | + Try { |
| 148 | + Read tChunk:0 |
| 149 | + } Catch {} |
| 150 | + If '$Length(tChunk) { |
| 151 | + Quit |
| 152 | + } |
| 153 | + Use tFile |
| 154 | + Write tChunk |
| 155 | + } |
| 156 | + } |
| 157 | + Else { |
| 158 | + For { |
| 159 | + Use ..Device |
| 160 | + Set tChunk = "" |
| 161 | + Try { |
| 162 | + Read tChunk:0 |
| 163 | + } Catch {} |
| 164 | + If '$Length(tChunk) { |
| 165 | + Quit |
| 166 | + } |
| 167 | + Do pStream.Write(tChunk) |
| 168 | + } |
| 169 | + } |
| 170 | + } Catch e { |
| 171 | + If e.Name="<ENDOFFILE>" { |
| 172 | + Set tSC = $$$OK |
| 173 | + } Else { |
| 174 | + Set tSC = e.AsStatus() |
| 175 | + } |
| 176 | + } |
| 177 | + Use tOldIO |
| 178 | + Quit tSC |
| 179 | +} |
| 180 | + |
| 181 | +/// Reads all output from the buffer to <var>pString</var>. |
| 182 | +Method ReadToString(Output pString As %String) As %Status |
| 183 | +{ |
| 184 | + Set tSC = $$$OK |
| 185 | + Set tOldIO = $io |
| 186 | + Set pString = $Get(pString) |
| 187 | + Try { |
| 188 | + If '..DeviceOpen { |
| 189 | + // No-op |
| 190 | + Quit |
| 191 | + } |
| 192 | + |
| 193 | + Use ..Device |
| 194 | + |
| 195 | + // Flush any remaining output |
| 196 | + Write *-3 |
| 197 | + |
| 198 | + // String |
| 199 | + For { |
| 200 | + Use ..Device |
| 201 | + Set tChunk = "" |
| 202 | + Try { |
| 203 | + Read tChunk:0 |
| 204 | + } Catch {} |
| 205 | + If '$Length(tChunk) { |
| 206 | + Quit |
| 207 | + } |
| 208 | + Set pString = pString _ tChunk |
| 209 | + } |
| 210 | + } Catch e { |
| 211 | + If e.Name="<ENDOFFILE>" { |
| 212 | + Set tSC = $$$OK |
| 213 | + } Else { |
| 214 | + Set tSC = e.AsStatus() |
| 215 | + } |
| 216 | + } |
| 217 | + Use tOldIO |
| 218 | + Quit tSC |
| 219 | +} |
| 220 | + |
| 221 | +/// Ends capturing output <br /> |
| 222 | +/// If <var>pOutput</var> is any sort of stream, output will be written to it. <br /> |
| 223 | +/// Otherwise, it will be returned as a string (initialized to "" before retrieving output from the buffer). <br /> |
| 224 | +Method EndCaptureOutput(ByRef pOutput) As %Status |
| 225 | +{ |
| 226 | + Set tSC = $$$OK |
| 227 | + Set tOldIO = $io |
| 228 | + Try { |
| 229 | + Set pOutput = $Get(pOutput) |
| 230 | + |
| 231 | + If $IsObject(pOutput) && pOutput.%IsA("%Stream.Object") { |
| 232 | + Set tSC = ..ReadToStream(.pOutput) |
| 233 | + } Else { |
| 234 | + Set tSC = ..ReadToString(.pOutput) |
| 235 | + } |
| 236 | + } Catch e { |
| 237 | + Set tSC = e.AsStatus() |
| 238 | + } |
| 239 | + |
| 240 | + // Close the XDEV device, and switch back to the original device |
| 241 | + Try { |
| 242 | + If (tOldIO = ..Device) { |
| 243 | + Do ..UsePreviousDeviceAndSettings() |
| 244 | + } Else { |
| 245 | + Use tOldIO |
| 246 | + } |
| 247 | + Close ..Device |
| 248 | + } Catch e { |
| 249 | + Set tSC = $$$ADDSC(tSC,e.AsStatus()) |
| 250 | + } |
| 251 | + Quit tSC |
| 252 | +} |
| 253 | + |
| 254 | +Method %OnClose() As %Status [ Private, ServerOnly = 1 ] |
| 255 | +{ |
| 256 | + If (..Device '= "") { |
| 257 | + If ($io = ..Device) { |
| 258 | + // Switch back to the previous device if possible; if this fails, the subsequent |
| 259 | + // close of ..Device will switch back to the principal device. |
| 260 | + Try { |
| 261 | + Do ..UsePreviousDeviceAndSettings() |
| 262 | + } Catch e {} |
| 263 | + } |
| 264 | + Close ..Device |
| 265 | + } |
| 266 | + Set ..DeviceOpen = 0 |
| 267 | + |
| 268 | + Quit $$$OK |
| 269 | +} |
| 270 | + |
| 271 | +Method UsePreviousDeviceAndSettings() [ Internal, Private ] |
| 272 | +{ |
| 273 | + Use ..PreviousDevice |
| 274 | + If (..PreviousMnemonicRoutine '= "") { |
| 275 | + Set tOldMnemonic = "^"_..PreviousMnemonicRoutine |
| 276 | + Use ..PreviousDevice::(tOldMnemonic) |
| 277 | + } |
| 278 | + If ..PreviousIORedirectFlag { |
| 279 | + Do ##class(%Library.Device).ReDirectIO(1) |
| 280 | + } |
| 281 | +} |
| 282 | + |
| 283 | +} |
0 commit comments