@tripley-kit/xfs-form-parser 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,323 @@
1
+ # @tripley-kit/xfs-form-parser
2
+
3
+ A TypeScript library for parsing and validating XFS (eXtensions for Financial Services) Form and Media definition files.
4
+
5
+ ## Features
6
+
7
+ - 🎯 **Full XFS Support**: Parse both XFSFORM and XFSMEDIA definitions
8
+ - 📝 **Type-Safe**: Complete TypeScript type definitions
9
+ - ✅ **Validation**: Built-in validation for XFS documents
10
+ - 🚀 **ANTLR-Based**: Uses ANTLR 4 for robust parsing
11
+ - 🔍 **Error Reporting**: Detailed error messages with line and column information
12
+ - 📦 **Zero Config**: Works out of the box with ESM and CommonJS
13
+
14
+ ## Installation
15
+
16
+ ```bash
17
+ pnpm add @tripley-kit/xfs-form-parser
18
+ ```
19
+
20
+ ## Quick Start
21
+
22
+ ### Parsing a Form Definition
23
+
24
+ ```typescript
25
+ import { parseXFSForm } from "@tripley-kit/xfs-form-parser";
26
+
27
+ const formDef = `
28
+ XFSFORM "Receipt"
29
+ BEGIN
30
+ UNIT MM, 1, 1
31
+ SIZE 75, 85
32
+ ORIENTATION PORTRAIT
33
+
34
+ XFSFIELD "CustomerName"
35
+ BEGIN
36
+ POSITION 10, 10
37
+ SIZE 50, 5
38
+ FONT "Arial"
39
+ POINTSIZE 12
40
+ HORIZONTAL LEFT
41
+ VERTICAL TOP
42
+ END
43
+ END
44
+ `;
45
+
46
+ const result = parseXFSForm(formDef);
47
+
48
+ if (result.success) {
49
+ console.log("Parsed forms:", result.data);
50
+ result.data.forEach((form) => {
51
+ console.log(`Form: ${form.name}`);
52
+ console.log(`Fields: ${form.fields.length}`);
53
+ });
54
+ } else {
55
+ console.error("Parse errors:", result.errors);
56
+ }
57
+ ```
58
+
59
+ ### Parsing a Media Definition
60
+
61
+ ```typescript
62
+ import { parseXFSMedia } from "@tripley-kit/xfs-form-parser";
63
+
64
+ const mediaDef = `
65
+ XFSMEDIA "Receipt1"
66
+ BEGIN
67
+ TYPE GENERIC
68
+ UNIT MM, 1, 1
69
+ SIZE 75, 85
70
+ END
71
+ `;
72
+
73
+ const result = parseXFSMedia(mediaDef);
74
+
75
+ if (result.success) {
76
+ console.log("Parsed media:", result.data);
77
+ }
78
+ ```
79
+
80
+ ### Parsing Mixed Documents
81
+
82
+ ```typescript
83
+ import { parseXFS } from "@tripley-kit/xfs-form-parser";
84
+
85
+ const xfsDef = `
86
+ XFSFORM "MyForm"
87
+ BEGIN
88
+ SIZE 75, 85
89
+ END
90
+
91
+ XFSMEDIA "MyMedia"
92
+ BEGIN
93
+ TYPE GENERIC
94
+ SIZE 75, 85
95
+ END
96
+ `;
97
+
98
+ const result = parseXFS(xfsDef);
99
+
100
+ if (result.success) {
101
+ console.log("Forms:", result.data.forms);
102
+ console.log("Medias:", result.data.medias);
103
+ }
104
+ ```
105
+
106
+ ### Validating Documents
107
+
108
+ ```typescript
109
+ import { parseXFS, validateXFS } from "@tripley-kit/xfs-form-parser";
110
+
111
+ const result = parseXFS(xfsDefinition);
112
+
113
+ if (result.success) {
114
+ const validation = validateXFS(result.data);
115
+
116
+ if (validation.valid) {
117
+ console.log("Document is valid!");
118
+ } else {
119
+ console.error("Validation errors:", validation.errors);
120
+ console.warn("Validation warnings:", validation.warnings);
121
+ }
122
+ }
123
+ ```
124
+
125
+ ## API Reference
126
+
127
+ ### Parser Functions
128
+
129
+ #### `parseXFS(input: string): ParseResult<XFSDocument>`
130
+
131
+ Parse a complete XFS document containing forms and/or medias.
132
+
133
+ #### `parseXFSForm(input: string): ParseResult<Form[]>`
134
+
135
+ Parse only form definitions from the input.
136
+
137
+ #### `parseXFSMedia(input: string): ParseResult<Media[]>`
138
+
139
+ Parse only media definitions from the input.
140
+
141
+ ### Validator Functions
142
+
143
+ #### `validateXFS(document: XFSDocument): ValidationResult`
144
+
145
+ Validate an XFS document and return validation results.
146
+
147
+ ### Classes
148
+
149
+ #### `XFSParser`
150
+
151
+ Main parser class with methods:
152
+
153
+ - `parse(input: string): ParseResult<XFSDocument>`
154
+ - `parseFile(content: string): ParseResult<XFSDocument>`
155
+ - `parseForms(input: string): ParseResult<Form[]>`
156
+ - `parseMedias(input: string): ParseResult<Media[]>`
157
+ - `validateSyntax(input: string): boolean`
158
+ - `getErrors(): ParseError[]`
159
+
160
+ #### `XFSValidator`
161
+
162
+ Validator class with methods:
163
+
164
+ - `validate(document: XFSDocument): ValidationResult`
165
+ - `validateForm(form: Form): ValidationResult`
166
+
167
+ ## Type Definitions
168
+
169
+ ### Form Structure
170
+
171
+ ```typescript
172
+ interface Form {
173
+ name: string;
174
+ properties: FormProperties;
175
+ frames: Frame[];
176
+ fields: Field[];
177
+ }
178
+
179
+ interface FormProperties {
180
+ unit?: { type: Unit; xResolution?: number; yResolution?: number };
181
+ size?: Size;
182
+ alignment?: { type: Alignment; xOffset?: number; yOffset?: number };
183
+ orientation?: Orientation;
184
+ version?: { major: number; minor: number; revision: string; vendor: string };
185
+ language?: number;
186
+ copyright?: string;
187
+ }
188
+ ```
189
+
190
+ ### Field Structure
191
+
192
+ ```typescript
193
+ interface Field {
194
+ name: string;
195
+ properties: FieldProperties;
196
+ }
197
+
198
+ interface FieldProperties {
199
+ position?: Position;
200
+ size?: Size;
201
+ initialValue?: string;
202
+ horizontal?: HorizontalAlign;
203
+ vertical?: VerticalAlign;
204
+ style?: Style;
205
+ pointSize?: number;
206
+ font?: string;
207
+ overflow?: Overflow;
208
+ color?: Color;
209
+ rgbColor?: RGBColor;
210
+ // ... and more
211
+ }
212
+ ```
213
+
214
+ ### Media Structure
215
+
216
+ ```typescript
217
+ interface Media {
218
+ name: string;
219
+ properties: MediaProperties;
220
+ }
221
+
222
+ interface MediaProperties {
223
+ type?: MediaType;
224
+ unit?: { type: Unit; xResolution?: number; yResolution?: number };
225
+ size?: Size;
226
+ printArea?: Rectangle;
227
+ restricted?: Rectangle;
228
+ paperType?: PaperType;
229
+ // ... and more
230
+ }
231
+ ```
232
+
233
+ ## File Encoding
234
+
235
+ The parser automatically handles Unicode files with BOM (Byte Order Mark), as required by the XFS specification:
236
+
237
+ ```typescript
238
+ import { readFileSync } from "fs";
239
+ import { parseXFSForm } from "@tripley-kit/xfs-form-parser";
240
+
241
+ // Read UTF-16LE file (common for XFS files)
242
+ const content = readFileSync("form.def", "utf16le");
243
+ const result = parseXFSForm(content);
244
+ // BOM is automatically removed by the parser
245
+ ```
246
+
247
+ **Note**: According to the XFS specification, Unicode-encoded form files must include a BOM marker. The parser automatically detects and removes the BOM (U+FEFF) before parsing.
248
+
249
+ ## Supported XFS Features
250
+
251
+ ### Form Properties
252
+
253
+ - UNIT (MM, INCH, ROWCOLUMN)
254
+ - SIZE
255
+ - ALIGNMENT (TOPLEFT, TOPRIGHT, BOTTOMLEFT, BOTTOMRIGHT)
256
+ - ORIENTATION (PORTRAIT, LANDSCAPE)
257
+ - VERSION
258
+ - LANGUAGE
259
+ - COPYRIGHT
260
+
261
+ ### Frame/Field Properties
262
+
263
+ - POSITION
264
+ - SIZE
265
+ - CLASS (STATIC, STATICTEXT, STATICGRAPHIC)
266
+ - TYPE (TEXT, MICR, OCR, MSF, BARCODE, GRAPHIC, PAGEMARK, RECTANGLE)
267
+ - STYLE (SINGLE_THIN, DOUBLE_THIN, BOLD, ITALIC, etc.)
268
+ - HORIZONTAL (LEFT, RIGHT, CENTER, JUSTIFY)
269
+ - VERTICAL (TOP, BOTTOM, CENTER)
270
+ - COLOR / RGBCOLOR
271
+ - FONT
272
+ - POINTSIZE
273
+ - INITIALVALUE
274
+ - OVERFLOW (TERMINATE, TRUNCATE, BESTFIT, OVERWRITE, WORDWRAP)
275
+ - And many more...
276
+
277
+ ### Media Properties
278
+
279
+ - TYPE (GENERIC, PASSBOOK, MULTIPART)
280
+ - UNIT
281
+ - SIZE
282
+ - PRINTAREA
283
+ - RESTRICTED
284
+ - PAPERTYPE (PLAIN, THERMAL, SECURITY_PAPER)
285
+ - SECURITY (NONE, LEVEL1, LEVEL2, LEVEL3)
286
+
287
+ ## Examples
288
+
289
+ See the `doc/` directory for real-world XFS definition files:
290
+
291
+ - `TW_Receipt_Balance.def` - Complex receipt form with multiple frames and fields
292
+ - `TW_Receipt_GeneralPay.def` - General payment receipt form
293
+ - `rmedia.def` - Simple media definition
294
+
295
+ ## Development
296
+
297
+ ### Building
298
+
299
+ ```bash
300
+ pnpm build
301
+ ```
302
+
303
+ ### Testing
304
+
305
+ ```bash
306
+ pnpm test
307
+ ```
308
+
309
+ ### Regenerating Parser
310
+
311
+ If you modify the grammar file (`grammar/XFSForm.g4`), regenerate the parser:
312
+
313
+ ```bash
314
+ java -jar doc/antlr-4.13.2-complete.jar -Dlanguage=TypeScript -visitor -no-listener -o src/generated grammar/XFSForm.g4
315
+ ```
316
+
317
+ ## License
318
+
319
+ ISC
320
+
321
+ ## Author
322
+
323
+ orichyy@hotmail.com