| Code with Finding: |
class Barcode128 {
/** Converts the human readable text to the characters needed to
* create a barcode. Some optimization is done to get the shortest code.
* @param text the text to convert
* @param ucc <CODE>true</CODE> if it is an UCC/EAN-128. In this case
* the character FNC1 is added
* @return the code ready to be fed to getBarsCode128Raw()
*/
public static String getRawText(String text, boolean ucc) {
String out = "";
int tLen = text.length();
if (tLen == 0) {
out += START_B;
if (ucc)
out += FNC1_INDEX;
return out;
}
int c = 0;
for (int k = 0; k < tLen; ++k) {
c = text.charAt(k);
if (c > 127 && c != FNC1)
throw new RuntimeException(MessageLocalization.getComposedMessage("there.are.illegal.characters.for.barcode.128.in.1", text));
}
c = text.charAt(0);
char currentCode = START_B;
int index = 0;
if (isNextDigits(text, index, 2)) {
currentCode = START_C;
out += currentCode;
if (ucc)
out += FNC1_INDEX;
String out2 = getPackedRawDigits(text, index, 2);
index += out2.charAt(0);
out += out2.substring(1);
}
else if (c < ' ') {
currentCode = START_A;
out += currentCode;
if (ucc)
out += FNC1_INDEX;
out += (char)(c + 64);
++index;
}
else {
out += currentCode;
if (ucc)
out += FNC1_INDEX;
if (c == FNC1)
out += FNC1_INDEX;
else
out += (char)(c - ' ');
++index;
}
while (index < tLen) {
switch (currentCode) {
case START_A:
{
if (isNextDigits(text, index, 4)) {
currentCode = START_C;
out += CODE_AB_TO_C;
String out2 = getPackedRawDigits(text, index, 4);
index += out2.charAt(0);
out += out2.substring(1);
}
else {
c = text.charAt(index++);
if (c == FNC1)
out += FNC1_INDEX;
else if (c > '_') {
currentCode = START_B;
out += CODE_AC_TO_B;
out += (char)(c - ' ');
}
else if (c < ' ')
out += (char)(c + 64);
else
out += (char)(c - ' ');
}
}
break;
case START_B:
{
if (isNextDigits(text, index, 4)) {
currentCode = START_C;
out += CODE_AB_TO_C;
String out2 = getPackedRawDigits(text, index, 4);
index += out2.charAt(0);
out += out2.substring(1);
}
else {
c = text.charAt(index++);
if (c == FNC1)
out += FNC1_INDEX;
else if (c < ' ') {
currentCode = START_A;
out += CODE_BC_TO_A;
out += (char)(c + 64);
}
else {
out += (char)(c - ' ');
}
}
}
break;
case START_C:
{
if (isNextDigits(text, index, 2)) {
String out2 = getPackedRawDigits(text, index, 2);
index += out2.charAt(0);
out += out2.substring(1);
}
else {
c = text.charAt(index++);
if (c == FNC1)
out += FNC1_INDEX;
else if (c < ' ') {
currentCode = START_A;
out += CODE_BC_TO_A;
out += (char)(c + 64);
}
else {
currentCode = START_B;
out += CODE_AC_TO_B;
out += (char)(c - ' ');
}
}
}
break;
}
}
return out;
}
}
|