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