qzind / qz-print

Archive for legacy qz-print versions (1.8, 1.9). See https://github.com/qzind/tray for modern versions.
Other
141 stars 101 forks source link

Printers with \E in their name break findPrinter() #43

Closed tresf closed 8 years ago

tresf commented 9 years ago

This bug is for qz-print versions 1.9.x and older (1.8.0 included) and breaks printer searching for search parameters containing backslash E (\E or JavaScript "\\E" also "\\\\E").


Example

If a printer shared on a Windows or Samba network with "E" as part of the host name, for example:

\\Elephant\printer

And you perform a search like this:

findPrinter("\\\\Elephant\\printer");

You will receive the following exception:

Exception in thread "Thread-26" java.util.regex.PatternSyntaxException: Illegal/unsupported escape sequence near index x ...
\b\Q\\Elephant\printer\E\b
      ^

This is caused by the special RegEx character \E being used within a Pattern match.


Work-around 1 Replace uppercase \E with lowercase \e:

   findPrinter(printerName.toLowerCase());

Reason

\e is not a reserved character, which allows printer searching to succeed

Work-around 2 Perform JavaRegex pattern escaping:

if (printerName.indexOf("\E") != -1) {
   printerName = printerName.replace(/\\E/, "\\E\\\\E\\Q");
}

Reason

This complicated combination of E's and Q's is the proper escaping technique and is a work-around until the applet is written to do this automatically

More information on these E's and Q's here:

http://stackoverflow.com/questions/11927452/what-does-these-special-characters-mean-in-java


Permanent Fix

The long term fix to this is to use Pattern.quote(printerName); internal to the applet, which will be corrected with 2.0.0.

-Tres

tresf commented 8 years ago

@bberenz can we revisit this bug for 2.0? There's really no advantage in using RegEx in my opinion as programmers can do that client-side if they wish.

https://github.com/qzind/qz-print/blob/2.0/src/qz/printer/PrintServiceMatcher.java#L69:L129

akberenz commented 8 years ago

Can you clarify how you want findPrinter to work in that case? It seems like you want the method to return a PrintService for exact matches on the name string, and partial matches will be handled client side only from searching the printer list.

tresf commented 8 years ago

FYI, closed via https://github.com/qzind/qz-print/commit/5fc8e2a69666caad502bdb54bfff024da75e1313.