-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathExample.java
More file actions
159 lines (131 loc) · 6.22 KB
/
Example.java
File metadata and controls
159 lines (131 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* GS1 Barcode Syntax Engine example user of the Java binding
*
* @author Copyright (c) 2022-2026 GS1 AISBL.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import org.gs1.gs1encoders.*;
import java.util.Scanner;
public class Example {
static {
System.loadLibrary("gs1encodersjni");
}
public static void main(final String args[]) {
GS1Encoder gs1encoder = null;
try {
gs1encoder = new GS1Encoder();
} catch (GS1EncoderGeneralException e) {
System.err.println(e.getMessage());
System.exit(1);
}
boolean exit = false;
if (args.length == 1 && args[0].equals("--version")) {
System.out.format("Library version: %s\n", gs1encoder.getVersion());
exit = true;
}
Scanner scanner = new Scanner(System.in);
while (!exit) {
String dataStr = gs1encoder.getDataStr();
String aiDataStr = "";
String dlURI = "";
String[] hri = {};
if (!dataStr.isEmpty()) {
aiDataStr = gs1encoder.getAIdataStr();
if (aiDataStr == null) aiDataStr = "⧚ Not AI-based data ⧚";
try { dlURI = gs1encoder.getDLuri(null); } catch (GS1EncoderDigitalLinkException e) { dlURI = "⧚ " + e.getMessage() + " ⧚"; }
hri = gs1encoder.getHRI();
}
System.out.println("\n\n\nCurrent state:");
System.out.format("\n Barcode message: %s", dataStr);
System.out.format("\n AI element string: %s", aiDataStr);
System.out.format("\n GS1 Digital Link URI: %s", dlURI);
System.out.format("\n HRI: %s\n", !dataStr.isEmpty() && hri.length == 0 ? "⧚ Not AI-based data ⧚": "");
for (String h : hri) {
System.out.format(" %s\n", h);
}
System.out.println("\n\nMENU:");
System.out.println("\n 1) Process raw barcode message data, either:");
System.out.println(" * Plain data");
System.out.println(" * Unbracketed AI element string with FNC1 in first position");
System.out.println(" * GS1 Digital Link URI");
System.out.println(" 2) Process a bracketed AI element string");
System.out.println(" 3) Process barcode scan data (prefixed by AIM Symbology Identifier)");
System.out.format("\n 4) Toggle 'include data titles in HRI' flag. Current value = %s\n",
gs1encoder.getIncludeDataTitlesInHRI() ? "ON" : "OFF");
System.out.format(" 5) Toggle 'permit unknown AIs' flag. Current value = %s\n",
gs1encoder.getPermitUnknownAIs() ? "ON" : "OFF");
System.out.format(" 6) Toggle 'validate AI requisites'. Current value = %s\n",
gs1encoder.getValidationEnabled(GS1Encoder.Validation.RequisiteAIs) ? "ON" : "OFF");
System.out.println("\n 0) Exit program");
System.out.print("\nMenu selection: ");
String menuVal = scanner.nextLine();
String inpStr;
switch (menuVal) {
case "1":
case "2":
case "3":
System.out.print("\nEnter data: ");
inpStr = scanner.nextLine();
if (inpStr.isEmpty())
continue;
try {
if (menuVal.equals("1"))
gs1encoder.setDataStr(inpStr);
else if (menuVal.equals("2"))
gs1encoder.setAIdataStr(inpStr);
else // "3"
gs1encoder.setScanData(inpStr);
} catch (GS1EncoderParameterException | GS1EncoderScanDataException e) {
System.out.format("\n\nERROR message: %s\n\n", e.getMessage());
String markup = gs1encoder.getErrMarkup();
if (!markup.isEmpty())
System.out.format("ERROR markup: %s\n", markup.replace("|", "⧚"));
continue;
}
break;
case "4":
case "5":
case "6":
System.out.print("\nEnter 0 for OFF or 1 for ON: ");
inpStr = scanner.nextLine();
if (inpStr.isEmpty())
continue;
if (!inpStr.equals("0") && !inpStr.equals("1")) {
System.out.println("\n\nOUT OF RANGE. PLEASE ENTER 0 or 1");
continue;
}
try {
if (menuVal.equals("4"))
gs1encoder.setIncludeDataTitlesInHRI(inpStr.equals("1"));
else if (menuVal.equals("5"))
gs1encoder.setPermitUnknownAIs(inpStr.equals("1"));
else // "6"
gs1encoder.setValidationEnabled(GS1Encoder.Validation.RequisiteAIs, inpStr.equals("1"));
} catch (GS1EncoderParameterException e) {
System.out.format("\n\nERROR: %s\n", e.getMessage());
continue;
}
break;
case "0":
exit = true;
break;
default:
System.out.println("\n\nOUT OF RANGE. PLEASE ENTER 1-6, 9 or 0.");
}
}
gs1encoder.free();
}
}