diff --git a/zxing.appspot.com/generator/src/com/google/zxing/web/generator/client/ContactInfoGenerator.java b/zxing.appspot.com/generator/src/com/google/zxing/web/generator/client/ContactInfoGenerator.java index fd4029bdb..2eeca40e4 100644 --- a/zxing.appspot.com/generator/src/com/google/zxing/web/generator/client/ContactInfoGenerator.java +++ b/zxing.appspot.com/generator/src/com/google/zxing/web/generator/client/ContactInfoGenerator.java @@ -21,6 +21,8 @@ import com.google.gwt.user.client.ui.Grid; import com.google.gwt.user.client.ui.TextBox; import com.google.gwt.user.client.ui.Widget; +import java.net.URI; + /** * A Generator for contact informations, output is in MeCard format. * @@ -34,8 +36,9 @@ public class ContactInfoGenerator implements GeneratorSource { TextBox url = new TextBox(); TextBox email = new TextBox(); TextBox address = new TextBox(); + TextBox address2 = new TextBox(); TextBox memo = new TextBox(); - TextBox[] widgets = {name, company, tel, url, email, address, memo}; + TextBox[] widgets = {name, company, tel, url, email, address, address2, memo}; public ContactInfoGenerator(ChangeListener changeListener) { for (TextBox w: widgets) { @@ -54,26 +57,47 @@ public class ContactInfoGenerator implements GeneratorSource { String url = getUrlField(); String email = getEmailField(); String address = getAddressField(); + String address2 = getAddress2Field(); String memo = getMemoField(); // Build the output with obtained data. // note that some informations may just be "" if they were not specified. //return getVCard(name, company, tel, url, email, address, memo); - return getMeCard(name, company, tel, url, email, address, memo); + return getMeCard(name, company, tel, url, email, address, address2, memo); } private String getMeCard(String name, String company, String tel, String url, - String email, String address, String memo) { - String output = "MECARD:"; - output += "N:" + name + ";"; - output += company.length() > 0 ? "ORG:" + company + ";" : ""; - output += tel.length() > 0 ? "TEL:" + tel + ";" : ""; - output += url.length() > 0 ? "URL:" + url + ";" : ""; - output += email.length() > 0 ? "EMAIL:" + email + ";" : ""; - output += address.length() > 0 ? "ADR:" + address + ";" : ""; - output += memo.length() > 0 ? "NOTE:" + memo + ";" : ""; - output += ";"; - return output; + String email, String address, String address2, String memo) { + StringBuilder output = new StringBuilder(); + output.append("MECARD:"); + output.append("N:").append(name).append(';'); + maybeAppend(output, "ORG:", company); + maybeAppend(output, "TEL:", tel); + maybeAppend(output, "URL:", url); + maybeAppend(output, "EMAIL:", email); + maybeAppend(output, "ADR:", address); + if (address.length() > 0 || address2.length() > 0) { + output.append("ADR:"); + if (address.length() > 0) { + output.append(address); + } + if (address2.length() > 0) { + if (address.length() > 0) { + output.append(' '); + } + output.append(address2); + } + output.append(';'); + } + maybeAppend(output, "NOTE:", memo); + output.append(';'); + return output.toString(); + } + + private static void maybeAppend(StringBuilder output, String prefix, String value) { + if (value.length() > 0) { + output.append(prefix).append(value).append(';'); + } } /*// VCARD GENERATION. Keep this in case we want to go back to vcard format @@ -93,35 +117,28 @@ public class ContactInfoGenerator implements GeneratorSource { return output; } */ - - private String getNameField() throws GeneratorException { - String inputName = name.getText(); - if (inputName.length() < 1) { - throw new GeneratorException("Name must be at least 1 character."); - } - if (inputName.contains("\n")) { - throw new GeneratorException("Name should not contanains \\n characters."); - } - if (inputName.contains(";")) { - throw new GeneratorException("Name must not contains ; characters"); - } - return inputName; - } - - private String getCompanyField() throws GeneratorException { - String input = company.getText(); + + private static String parseTextField(TextBox textBox) throws GeneratorException { + String input = textBox.getText(); if (input.length() < 1) { return ""; } if (input.contains("\n")) { - throw new GeneratorException("Company should not contanains \\n characters."); + throw new GeneratorException("Field must not contain \\n characters."); } if (input.contains(";")) { - throw new GeneratorException("Company must not contains ; characters"); + throw new GeneratorException("Field must not contains ; characters"); } - // the input contains some informations. return input; } + + private String getNameField() throws GeneratorException { + return parseTextField(name); + } + + private String getCompanyField() throws GeneratorException { + return parseTextField(company); + } private String getTelField() throws GeneratorException { String input = Validators.filterNumber(tel.getText()); @@ -137,13 +154,7 @@ public class ContactInfoGenerator implements GeneratorSource { private String getUrlField() throws GeneratorException { String input = url.getText(); - if (input.length() < 1) { - return ""; - } Validators.validateUrl(input); - if (input.contains(";")) { - throw new GeneratorException("URL must not contains ; characters"); - } return input; } @@ -160,31 +171,15 @@ public class ContactInfoGenerator implements GeneratorSource { } private String getAddressField() throws GeneratorException { - String input = address.getText(); - if (input.length() < 1) { - return ""; - } - if (input.contains("\n")) { - throw new GeneratorException("Address must not contain \\n characters."); - } - if (input.contains(";")) { - throw new GeneratorException("Address must not contains ; characters"); - } - return input; + return parseTextField(address); + } + + private String getAddress2Field() throws GeneratorException { + return parseTextField(address2); } private String getMemoField() throws GeneratorException { - String input = memo.getText(); - if (input.length() < 1) { - return ""; - } - if (input.contains("\n")) { - throw new GeneratorException("Memo must not contain \\n characters."); - } - if (input.contains(";")) { - throw new GeneratorException("Memo must not contains ; characters"); - } - return input; + return parseTextField(memo); } public Grid getWidget() { @@ -192,7 +187,7 @@ public class ContactInfoGenerator implements GeneratorSource { // early termination if the table has already been constructed return table; } - table = new Grid(7, 2); + table = new Grid(8, 2); table.setText(0, 0, "Name"); table.setWidget(0, 1, name); @@ -204,10 +199,12 @@ public class ContactInfoGenerator implements GeneratorSource { table.setWidget(3, 1, email); table.setText(4, 0, "Address"); table.setWidget(4, 1, address); - table.setText(5, 0, "Website"); - table.setWidget(5, 1, url); - table.setText(6, 0, "Memo"); - table.setWidget(6, 1, memo); + table.setText(5, 0, "Address 2"); + table.setWidget(5, 1, address2); + table.setText(6, 0, "Website"); + table.setWidget(6, 1, url); + table.setText(7, 0, "Memo"); + table.setWidget(7, 1, memo); name.addStyleName(StylesDefs.INPUT_FIELD_REQUIRED); return table; @@ -219,6 +216,7 @@ public class ContactInfoGenerator implements GeneratorSource { if (widget == tel) getTelField(); if (widget == email) getEmailField(); if (widget == address) getAddressField(); + if (widget == address2) getAddress2Field(); if (widget == url) getUrlField(); if (widget == memo) getMemoField(); } diff --git a/zxing.appspot.com/generator/src/com/google/zxing/web/generator/client/Generator.java b/zxing.appspot.com/generator/src/com/google/zxing/web/generator/client/Generator.java index 05cb0c034..86947e988 100644 --- a/zxing.appspot.com/generator/src/com/google/zxing/web/generator/client/Generator.java +++ b/zxing.appspot.com/generator/src/com/google/zxing/web/generator/client/Generator.java @@ -176,13 +176,12 @@ public class Generator implements EntryPoint { } protected String getUrl(int sizeX, int sizeY, String content) { - String result = "http://chart.apis.google.com/chart?cht=qr&chs="; - result += sizeX; - result += "x"; - result += sizeY; - result += "&chl="; - result += URL.encodeComponent(content); - return result; + StringBuilder result = new StringBuilder(); + result.append("http://chart.apis.google.com/chart?cht=qr&chs="); + result.append(sizeX).append('x').append(sizeY); + result.append("&chl="); + result.append(URL.encodeComponent(content)); + return result.toString(); } private void generate() { diff --git a/zxing.appspot.com/generator/src/com/google/zxing/web/generator/client/GeneratorSource.java b/zxing.appspot.com/generator/src/com/google/zxing/web/generator/client/GeneratorSource.java index 00a277d11..23b5c4377 100644 --- a/zxing.appspot.com/generator/src/com/google/zxing/web/generator/client/GeneratorSource.java +++ b/zxing.appspot.com/generator/src/com/google/zxing/web/generator/client/GeneratorSource.java @@ -28,25 +28,25 @@ public interface GeneratorSource { /** * @return a GWT Grid object, containing the GUI. */ - public Grid getWidget(); + Grid getWidget(); /** * @return the name of the generator to be used in the GUI. */ - public String getName(); + String getName(); /** * @return the text to be encoded into the QR code. * @throws GeneratorException if the input data contains errors. */ - public String getText() throws GeneratorException; + String getText() throws GeneratorException; /** * @param widget the widget that was last modified, and that we want to * validate the content. * @throws GeneratorException if the widget contains errors. */ - public void validate(Widget widget) throws GeneratorException; + void validate(Widget widget) throws GeneratorException; /** * Called when the generator is selected in the list. Using this method, * the generator should set the focus to the first widget it defines. */ - public void setFocus(); + void setFocus(); } diff --git a/zxing.appspot.com/generator/src/com/google/zxing/web/generator/client/GeoLocationGenerator.java b/zxing.appspot.com/generator/src/com/google/zxing/web/generator/client/GeoLocationGenerator.java index f391c60b1..d02c578c8 100644 --- a/zxing.appspot.com/generator/src/com/google/zxing/web/generator/client/GeoLocationGenerator.java +++ b/zxing.appspot.com/generator/src/com/google/zxing/web/generator/client/GeoLocationGenerator.java @@ -204,17 +204,19 @@ public class GeoLocationGenerator implements GeneratorSource, ChangeListener { } String q = ""; if (link.matches(".*&q=[^&]*&.*")) { + StringBuilder qBuilder = new StringBuilder(); for (int i = link.indexOf("&q=") + 3; i < link.length() && link.charAt(i) != '&'; ++i) { - q += link.charAt(i); + qBuilder.append(link.charAt(i)); } + q = qBuilder.toString(); // special cases: q = q.replace("+", " "); q = q.replace("%26", "&"); } - String lat = ""; - String lon = ""; + StringBuilder lat = new StringBuilder(); + StringBuilder lon = new StringBuilder(); if (link.matches(".*&s?ll=[^&]*&.*")) { int start; if (link.indexOf("&sll=") == -1) { @@ -224,21 +226,22 @@ public class GeoLocationGenerator implements GeneratorSource, ChangeListener { } boolean beforeComma = true; for (int i = start; i < link.length() && link.charAt(i) != '&'; ++i) { + char c = link.charAt(i); if (beforeComma) { - if (link.charAt(i) == ',') { + if (c == ',') { beforeComma = false; } else { - lat += link.charAt(i); + lat.append(c); } } else { - lon += link.charAt(i); + lon.append(c); } } } query.setText(URL.decode(q)); - latitude.setText(lat); - longitude.setText(lon); + latitude.setText(lat.toString()); + longitude.setText(lon.toString()); changeListener.onChange(latitude); changeListener.onChange(longitude); this.onChange(latitude); diff --git a/zxing.appspot.com/generator/src/com/google/zxing/web/generator/client/StringConstants.java b/zxing.appspot.com/generator/src/com/google/zxing/web/generator/client/StringConstants.java index e690b9fe4..354c2db20 100644 --- a/zxing.appspot.com/generator/src/com/google/zxing/web/generator/client/StringConstants.java +++ b/zxing.appspot.com/generator/src/com/google/zxing/web/generator/client/StringConstants.java @@ -20,6 +20,6 @@ import com.google.gwt.i18n.client.Messages; // Not used yet. A first atempt to localization. public interface StringConstants extends Messages { - public String codeType(); - public String generateButton(); + String codeType(); + String generateButton(); } diff --git a/zxing.appspot.com/generator/src/com/google/zxing/web/generator/client/Validators.java b/zxing.appspot.com/generator/src/com/google/zxing/web/generator/client/Validators.java index 99458e4f7..4f1432408 100644 --- a/zxing.appspot.com/generator/src/com/google/zxing/web/generator/client/Validators.java +++ b/zxing.appspot.com/generator/src/com/google/zxing/web/generator/client/Validators.java @@ -16,6 +16,9 @@ package com.google.zxing.web.generator.client; +import java.net.URL; +import java.net.MalformedURLException; + /** * Helpers methods to check for phone numbers, email addresses, and URL. Other * general purpose check methods should go here as well. @@ -34,11 +37,10 @@ public final class Validators { } public static void validateUrl(String url) throws GeneratorException { - //FIXME: url specification is a bit more complex than just that. - if (!((url.startsWith("http://") && url.length() > 7) - || (url.startsWith("https://") && url.length() > 8))) { - throw new GeneratorException("URL: http:// or https://," + - "plus at least 1 character."); + try { + new URL(url); + } catch (MalformedURLException mue) { + throw new GeneratorException("URL is not valid."); } }