/* Microsoft SQL Server Integration Services JavaScript Task Copyright (c) 2006-2018 CozyRoc LLC Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ task.parameters = { Action: { description: "Select task action.", category: "Settings", order: 1, type: { id: "list", values: ["Find", "FindAndReplace"] }, validate(value) { if (!value) return "Please specify Action."; } }, Find: { description: "The string that will be matched.", category: "Settings", order: 2, validate(value) { if (!value) return "Please specify Find."; } }, Replace: { description: "What will be inserted in the place of the matched string.", category: "Settings", order: 5, visible() { return this.parameters.Action.value === "FindAndReplace"; } }, StartIndex: { description: "Offset from where to start the search.", category: "Settings", order: 3, type: "numeric", visible() { return this.parameters.Action.value === "Find"; } }, ResultVariable: { description: "Package variable where you would like to store find index.", category: "Settings", order: 4, type: { id: "variable", type: "integer" }, visible() { return this.parameters.Action.value === "Find"; }, validate(value) { if (this.parameters.Action.value === "Find" && !value) return "Please specify ResultVariable."; } }, IsSourceVariable: { description: "Defines if Source property references package variable.", category: "Source", type: "boolean", order: 1, validate(value) { if (value) this.parameters.Source.value = null; else this.parameters.SourceVariable.value = null; } }, SourceVariable: { description: "Variable containing source to search.", category: "Source", type: { id: "variable", variableTypes: ["object", "string"] }, order: 2, visible() { return this.parameters.IsSourceVariable.value; }, validate(value) { if (this.parameters.IsSourceVariable.value && !value) return "Please specify SourceVariable."; } }, Source: { description: "Source to search.", category: "Source", type: { id: "connection", connectionTypes: "FILE" }, order: 3, visible() { return !this.parameters.IsSourceVariable.value; }, validate(value) { if (!this.parameters.IsSourceVariable.value && !value) return "Please specify Source."; } }, IsTargetVariable: { description: "Defines if Target property references package variable.", category: "Target", type: "boolean", order: 1, validate(value) { if (value) this.parameters.Target.value = null; else this.parameters.TargetVariable.value = null; }, visible() { return this.parameters.Action.value === "FindAndReplace"; } }, TargetVariable: { description: "Variable containing target to store result.", category: "Target", order: 2, type: { id: "variable", variableTypes: ["object", "string"] }, visible() { return this.parameters.Action.value === "FindAndReplace" && this.parameters.IsTargetVariable.value; }, validate(value) { if (this.parameters.Action.value === "FindAndReplace" && this.parameters.IsTargetVariable.value && !value) return "Please specify TargetVariable."; } }, Target: { description: "Target to store result.", category: "Target", order: 3, type: { id: "connection", connectionTypes: "FILE" }, visible() { return this.parameters.Action.value === "FindAndReplace" && !this.parameters.IsTargetVariable.value; }, validate(value) { if (this.parameters.Action.value === "FindAndReplace" && !this.parameters.IsTargetVariable.value && !value) return "Please specify Target."; } } }; var File = getClrType("System.IO.File"), Byte = getClrType("System.Byte"), CSArray = getClrType("System.Array"), Encoding = getClrType("System.Text.Encoding"), Regex = getClrType("System.Text.RegularExpressions.Regex"); function getStream(input, isVar, create, hasToClose) { var fileName, result; if (isVar) { // User selected variable. var inputVar = this.variables[input]; if (inputVar.dataType == "String") { // Variable references file name. fileName = inputVar.value; } else { // Variable references a stream ? // Wait until input Stream is setup, polling every second. while (inputVar.value["Position"] === undefined) { console.info("Waiting for stream ..."); sleep(1000); } result = inputVar.value; } } else { // User selected file connection. fileName = this.connections[input].acquire(); } if (fileName !== undefined) { if (create) { result = File.Create(fileName); if (hasToClose !== undefined) hasToClose[0] = true; } else { result = File.OpenRead(fileName); } } return result; } function arrayEquals(a, b) { if (a.Length !== b.Length) return false; for (var i = 0; i < a.Length; i++) { if (a[i] !== b[i]) { // Doesn't match return false; } } return true; } function executeFind(sourceStream) { var result = 0, startIndex = this.parameters.StartIndex.value, findBytes = Encoding.UTF8.GetBytes(Regex.Unescape(this.parameters.Find.value)), findLen = findBytes.Length, buf = CSArray.CreateInstance(Byte, findLen); // Advance to start index. while (result < startIndex) { if (sourceStream.Read(buf, 0, 1) === 0) { break; } result += 1; } // Search for match. result -= findLen; while (!arrayEquals(buf, findBytes)) { CSArray.Copy(buf, 1, buf, 0, findLen - 1); var bytesRead = sourceStream.Read(buf, findLen - 1, 1); if (bytesRead === 0) { // Reached end of stream and still no match. result = -1; break; } result += 1; } this.variables[this.parameters.ResultVariable.value].value = result; } function executeFindAndReplace(sourceStream, targetStream) { var findBytes = Encoding.UTF8.GetBytes(Regex.Unescape(this.parameters.Find.value)), findLen = findBytes.Length, repBytes = Encoding.UTF8.GetBytes(Regex.Unescape(this.parameters.Replace.value)), repLen = repBytes.Length, buf = CSArray.CreateInstance(Byte, findLen); var bytesRead = sourceStream.Read(buf, 0, findLen); while (bytesRead === findLen) { if (arrayEquals(buf, findBytes)) { // Found match. Replace with specified combination. targetStream.Write(repBytes, 0, repLen); bytesRead = sourceStream.Read(buf, 0, findLen); } else { targetStream.Write(buf, 0, 1); CSArray.Copy(buf, 1, buf, 0, findLen - 1); bytesRead = bytesRead - 1 + sourceStream.Read(buf, findLen - 1, 1); } } // Write end of stream. targetStream.Write(buf, 0, bytesRead); targetStream.Flush(); } task.run = function () { var sourceName = this.parameters.IsSourceVariable.value ? this.parameters.SourceVariable.value : this.parameters.Source.value, sourceStream = getStream.call(this, sourceName, this.parameters.IsSourceVariable.value, false), closeTarget = [false], targetStream; try { if (this.parameters.Action.value === "Find") { executeFind.call(this, sourceStream); } else { var targetName = this.parameters.IsTargetVariable.value ? this.parameters.TargetVariable.value : this.parameters.Target.value; targetStream = getStream.call(this, targetName, this.parameters.IsTargetVariable.value, true, closeTarget); executeFindAndReplace.call(this, sourceStream, targetStream); } } catch (ex) { console.error(ex.message); return ScriptResults.Failure; } finally { if (sourceStream !== undefined) sourceStream.Dispose(); if (targetStream !== undefined && closeTarget[0]) { //Close target stream only if FileStream targetStream.Dispose(); } } };