|
| 1 | +/* |
| 2 | + * Copyright 2018 ABSA Group Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package za.co.absa.cobrix.cobol.processor |
| 18 | + |
| 19 | +import za.co.absa.cobrix.cobol.parser.recordformats.RecordFormat.FixedLength |
| 20 | +import za.co.absa.cobrix.cobol.processor.impl.{ArrayOfAnyHandler, StreamProcessor} |
| 21 | +import za.co.absa.cobrix.cobol.reader.VarLenNestedReader |
| 22 | +import za.co.absa.cobrix.cobol.reader.extractors.raw.{FixedRecordLengthRawRecordExtractor, RawRecordContext, RawRecordExtractor} |
| 23 | +import za.co.absa.cobrix.cobol.reader.parameters.{CobolParametersParser, Parameters, ReaderParameters} |
| 24 | +import za.co.absa.cobrix.cobol.reader.schema.CobolSchema |
| 25 | +import za.co.absa.cobrix.cobol.reader.stream.SimpleStream |
| 26 | + |
| 27 | +import java.io.OutputStream |
| 28 | +import scala.collection.mutable |
| 29 | + |
| 30 | + |
| 31 | +/** |
| 32 | + * A trait that defines a processor for raw COBOL data streams. |
| 33 | + * It provides a method to process a COBOL file or a stream, provided record processor. |
| 34 | + */ |
| 35 | +trait CobolProcessor { |
| 36 | + /** |
| 37 | + * Processes the input stream of COBOL records and writes the output to the specified output stream. |
| 38 | + * |
| 39 | + * @param inputStream the input stream containing raw COBOL records. |
| 40 | + * @param outputStream the output stream where processed records will be written. |
| 41 | + * @param rawRecordProcessor the processor that processes each raw record. |
| 42 | + */ |
| 43 | + def process(inputStream: SimpleStream, |
| 44 | + outputStream: OutputStream) |
| 45 | + (rawRecordProcessor: RawRecordProcessor): Unit |
| 46 | + |
| 47 | +} |
| 48 | + |
| 49 | +object CobolProcessor { |
| 50 | + class CobolProcessorBuilder(copybookContents: String) { |
| 51 | + private val caseInsensitiveOptions = new mutable.HashMap[String, String]() |
| 52 | + |
| 53 | + def build(): CobolProcessor = { |
| 54 | + new CobolProcessor { |
| 55 | + override def process(inputStream: SimpleStream, |
| 56 | + outputStream: OutputStream) |
| 57 | + (rawRecordProcessor: RawRecordProcessor): Unit = { |
| 58 | + val readerParameters = getReaderParameters |
| 59 | + val cobolSchema = getCobolSchema(readerParameters) |
| 60 | + val recordExtractor = getRecordExtractor(readerParameters, inputStream) |
| 61 | + |
| 62 | + val dataStream = inputStream.copyStream() |
| 63 | + try { |
| 64 | + StreamProcessor.processStream(cobolSchema.copybook, |
| 65 | + caseInsensitiveOptions.toMap, |
| 66 | + dataStream, |
| 67 | + recordExtractor, |
| 68 | + rawRecordProcessor, |
| 69 | + outputStream) |
| 70 | + } finally { |
| 71 | + dataStream.close() |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Adds a single option to the builder. |
| 79 | + * |
| 80 | + * @param key the option key. |
| 81 | + * @param value the option value. |
| 82 | + * @return this builder instance for method chaining. |
| 83 | + */ |
| 84 | + def option(key: String, value: String): CobolProcessorBuilder = { |
| 85 | + caseInsensitiveOptions += (key.toLowerCase -> value) |
| 86 | + this |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Adds multiple options to the builder. |
| 91 | + * |
| 92 | + * @param options a map of option key-value pairs. |
| 93 | + * @return this builder instance for method chaining. |
| 94 | + */ |
| 95 | + def options(options: Map[String, String]): CobolProcessorBuilder = { |
| 96 | + caseInsensitiveOptions ++= options.map(kv => (kv._1.toLowerCase(), kv._2)) |
| 97 | + this |
| 98 | + } |
| 99 | + |
| 100 | + private[processor] def getCobolSchema(readerParameters: ReaderParameters): CobolSchema = { |
| 101 | + CobolSchema.fromReaderParameters(Seq(copybookContents), readerParameters) |
| 102 | + } |
| 103 | + |
| 104 | + private[processor] def getReaderParameters: ReaderParameters = { |
| 105 | + val cobolParameters = CobolParametersParser.parse(new Parameters(caseInsensitiveOptions.toMap)) |
| 106 | + |
| 107 | + CobolParametersParser.getReaderProperties(cobolParameters, None) |
| 108 | + } |
| 109 | + |
| 110 | + private[processor] def getRecordExtractor(readerParameters: ReaderParameters, inputStream: SimpleStream): RawRecordExtractor = { |
| 111 | + val dataStream = inputStream.copyStream() |
| 112 | + val headerStream = inputStream.copyStream() |
| 113 | + |
| 114 | + val reader = new VarLenNestedReader[Array[Any]](Seq(copybookContents), readerParameters, new ArrayOfAnyHandler) |
| 115 | + |
| 116 | + reader.recordExtractor(0, dataStream, headerStream) match { |
| 117 | + case Some(extractor) => extractor |
| 118 | + case None if readerParameters.recordFormat == FixedLength => |
| 119 | + val dataStream = inputStream.copyStream() |
| 120 | + val ctx = RawRecordContext.builder(dataStream, getCobolSchema(readerParameters).copybook).build() |
| 121 | + new FixedRecordLengthRawRecordExtractor(ctx, readerParameters.recordLength) |
| 122 | + case None => |
| 123 | + throw new IllegalArgumentException(s"Cannot create a record extractor for the given reader parameters. " + |
| 124 | + "Please check the copybook and the reader parameters." |
| 125 | + ) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private[processor] def getOptions: Map[String, String] = caseInsensitiveOptions.toMap |
| 130 | + } |
| 131 | + |
| 132 | + def builder(copybookContent: String): CobolProcessorBuilder = { |
| 133 | + new CobolProcessorBuilder(copybookContent) |
| 134 | + } |
| 135 | +} |
0 commit comments