1616
1717package za .co .absa .cobrix .cobol .processor
1818
19+ import za .co .absa .cobrix .cobol .parser .Copybook
1920import za .co .absa .cobrix .cobol .processor .impl .CobolProcessorImpl
2021import za .co .absa .cobrix .cobol .reader .parameters .{CobolParametersParser , Parameters , ReaderParameters }
2122import za .co .absa .cobrix .cobol .reader .schema .CobolSchema
22- import za .co .absa .cobrix .cobol .reader .stream .SimpleStream
23+ import za .co .absa .cobrix .cobol .reader .stream .{ FSStream , SimpleStream }
2324
24- import java .io .OutputStream
25+ import java .io .{ BufferedInputStream , BufferedOutputStream , FileOutputStream , OutputStream }
2526import scala .collection .mutable
2627
2728
@@ -45,14 +46,58 @@ trait CobolProcessor {
4546}
4647
4748object CobolProcessor {
48- class CobolProcessorBuilder ( copybookContents : String ) {
49+ class CobolProcessorBuilder {
4950 private val caseInsensitiveOptions = new mutable.HashMap [String , String ]()
51+ private var copybookContentsOpt : Option [String ] = None
52+ private var rawRecordProcessorOpt : Option [RawRecordProcessor ] = None
5053
5154 def build (): CobolProcessor = {
55+ if (copybookContentsOpt.isEmpty) {
56+ throw new IllegalArgumentException (" Copybook contents must be provided." )
57+ }
58+
59+ val readerParameters = getReaderParameters
60+ val cobolSchema = getCobolSchema(readerParameters)
61+
62+ new CobolProcessorImpl (readerParameters, cobolSchema.copybook, copybookContentsOpt.get, caseInsensitiveOptions.toMap)
63+ }
64+
65+ def load (path : String ): CobolProcessorLoader = {
66+ val file = new java.io.File (path)
67+ if (! file.exists) {
68+ throw new IllegalArgumentException (s " Path $path does not exist. " )
69+ }
70+
71+ if (file.isDirectory) {
72+ throw new IllegalArgumentException (s " Path $path should be a file, not a directory. " )
73+ }
74+
75+ if (copybookContentsOpt.isEmpty) {
76+ throw new IllegalArgumentException (" Copybook contents must be provided." )
77+ }
78+
79+ if (rawRecordProcessorOpt.isEmpty) {
80+ throw new IllegalArgumentException (" A RawRecordProcessor must be provided." )
81+ }
82+
83+ if (rawRecordProcessorOpt.isEmpty) {
84+ throw new IllegalArgumentException (" A RawRecordProcessor must be provided." )
85+ }
86+
5287 val readerParameters = getReaderParameters
5388 val cobolSchema = getCobolSchema(readerParameters)
5489
55- new CobolProcessorImpl (readerParameters, cobolSchema.copybook, copybookContents, caseInsensitiveOptions.toMap)
90+ new CobolProcessorLoader (path, copybookContentsOpt.get, cobolSchema.copybook, rawRecordProcessorOpt.get, readerParameters, caseInsensitiveOptions.toMap)
91+ }
92+
93+ def withCopybookContents (copybookContents : String ): CobolProcessorBuilder = {
94+ copybookContentsOpt = Option (copybookContents)
95+ this
96+ }
97+
98+ def withRecordProcessor (processor : RawRecordProcessor ): CobolProcessorBuilder = {
99+ rawRecordProcessorOpt = Option (processor)
100+ this
56101 }
57102
58103 /**
@@ -80,7 +125,7 @@ object CobolProcessor {
80125 }
81126
82127 private [processor] def getCobolSchema (readerParameters : ReaderParameters ): CobolSchema = {
83- CobolSchema .fromReaderParameters(Seq (copybookContents ), readerParameters)
128+ CobolSchema .fromReaderParameters(Seq (copybookContentsOpt.get ), readerParameters)
84129 }
85130
86131 private [processor] def getReaderParameters : ReaderParameters = {
@@ -92,7 +137,57 @@ object CobolProcessor {
92137 private [processor] def getOptions : Map [String , String ] = caseInsensitiveOptions.toMap
93138 }
94139
95- def builder (copybookContent : String ): CobolProcessorBuilder = {
96- new CobolProcessorBuilder (copybookContent)
140+ class CobolProcessorLoader (fileToProcess : String ,
141+ copybookContents : String ,
142+ copybook : Copybook ,
143+ rawRecordProcessor : RawRecordProcessor ,
144+ readerParameters : ReaderParameters ,
145+ options : Map [String , String ]) {
146+ def save (outputFile : String ): Long = {
147+ val processor = new CobolProcessorImpl (readerParameters, copybook, copybookContents, options)
148+
149+ val ifs = new FSStream (fileToProcess)
150+ val ofs = new BufferedOutputStream (new FileOutputStream (outputFile))
151+
152+ var originalException : Throwable = null
153+
154+ val recordCount = try {
155+ processor.process(ifs, ofs)(rawRecordProcessor)
156+ } catch {
157+ case ex : Throwable =>
158+ originalException = ex
159+ 0L
160+ } finally {
161+ try {
162+ ifs.close()
163+ } catch {
164+ case e : Throwable =>
165+ if (originalException != null ) {
166+ originalException.addSuppressed(e)
167+ } else {
168+ originalException = e
169+ }
170+ }
171+
172+ try {
173+ ofs.close()
174+ } catch {
175+ case e : Throwable =>
176+ if (originalException != null ) {
177+ originalException.addSuppressed(e)
178+ } else {
179+ originalException = e
180+ }
181+ }
182+ }
183+
184+ if (originalException != null ) throw originalException
185+
186+ recordCount
187+ }
188+ }
189+
190+ def builder : CobolProcessorBuilder = {
191+ new CobolProcessorBuilder
97192 }
98193}
0 commit comments