mirror of
https://github.com/zxing/zxing.git
synced 2025-03-05 20:48:51 -08:00
Moved com.google.zxing.client.result from core-ext to core, rewrote it for J2ME, changed javame to use it, and removed core-ext
git-svn-id: https://zxing.googlecode.com/svn/trunk@114 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
39389daad4
commit
0daa06a5e0
|
@ -23,20 +23,17 @@
|
||||||
<target name="build">
|
<target name="build">
|
||||||
<ant dir="core" target="build"/>
|
<ant dir="core" target="build"/>
|
||||||
<ant dir="javame" target="build"/>
|
<ant dir="javame" target="build"/>
|
||||||
<ant dir="core-ext" target="build"/>
|
|
||||||
<ant dir="javase" target="build"/>
|
<ant dir="javase" target="build"/>
|
||||||
</target>
|
</target>
|
||||||
|
|
||||||
<target name="buildwithoutj2me">
|
<target name="buildwithoutj2me">
|
||||||
<ant dir="core" target="build"/>
|
<ant dir="core" target="build"/>
|
||||||
<ant dir="core-ext" target="build"/>
|
|
||||||
<ant dir="javase" target="build"/>
|
<ant dir="javase" target="build"/>
|
||||||
</target>
|
</target>
|
||||||
|
|
||||||
<target name="clean">
|
<target name="clean">
|
||||||
<ant dir="core" target="clean"/>
|
<ant dir="core" target="clean"/>
|
||||||
<ant dir="javame" target="clean"/>
|
<ant dir="javame" target="clean"/>
|
||||||
<ant dir="core-ext" target="clean"/>
|
|
||||||
<ant dir="javase" target="clean"/>
|
<ant dir="javase" target="clean"/>
|
||||||
</target>
|
</target>
|
||||||
|
|
||||||
|
@ -50,7 +47,6 @@
|
||||||
<include name="build.properties"/>
|
<include name="build.properties"/>
|
||||||
<include name="core/**"/>
|
<include name="core/**"/>
|
||||||
<include name="javame/**"/>
|
<include name="javame/**"/>
|
||||||
<include name="core-ext/**"/>
|
|
||||||
<include name="javase/**"/>
|
<include name="javase/**"/>
|
||||||
<include name="docs/**"/>
|
<include name="docs/**"/>
|
||||||
<exclude name="*.iml"/>
|
<exclude name="*.iml"/>
|
||||||
|
@ -63,7 +59,6 @@
|
||||||
<javadoc destdir="docs/javadoc" footer="<script src="http://www.google-analytics.com/urchin.js" type="text/javascript"></script><script type="text/javascript">_uacct = "UA-788492-4"; urchinTracker();</script>">
|
<javadoc destdir="docs/javadoc" footer="<script src="http://www.google-analytics.com/urchin.js" type="text/javascript"></script><script type="text/javascript">_uacct = "UA-788492-4"; urchinTracker();</script>">
|
||||||
<sourcepath>
|
<sourcepath>
|
||||||
<pathelement location="core/src"/>
|
<pathelement location="core/src"/>
|
||||||
<pathelement location="core-ext/src"/>
|
|
||||||
<pathelement location="javame/src"/>
|
<pathelement location="javame/src"/>
|
||||||
<pathelement location="javase/src"/>
|
<pathelement location="javase/src"/>
|
||||||
</sourcepath>
|
</sourcepath>
|
||||||
|
|
|
@ -1,46 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
|
|
||||||
<!--
|
|
||||||
Copyright 2007 Google Inc.
|
|
||||||
|
|
||||||
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.
|
|
||||||
-->
|
|
||||||
|
|
||||||
<project name="core-ext" default="build">
|
|
||||||
|
|
||||||
<target name="init">
|
|
||||||
<tstamp/>
|
|
||||||
</target>
|
|
||||||
|
|
||||||
<target name="build" depends="init">
|
|
||||||
<mkdir dir="build"/>
|
|
||||||
<javac srcdir="src"
|
|
||||||
destdir="build"
|
|
||||||
source="1.5"
|
|
||||||
target="1.5"
|
|
||||||
optimize="true"
|
|
||||||
debug="true"
|
|
||||||
deprecation="true">
|
|
||||||
<classpath>
|
|
||||||
<pathelement location="../core/core.jar"/>
|
|
||||||
</classpath>
|
|
||||||
</javac>
|
|
||||||
<jar jarfile="core-ext.jar" basedir="build" level="9"/>
|
|
||||||
</target>
|
|
||||||
|
|
||||||
<target name="clean">
|
|
||||||
<delete dir="build"/>
|
|
||||||
<delete file="core-ext.jar"/>
|
|
||||||
</target>
|
|
||||||
|
|
||||||
</project>
|
|
|
@ -1,43 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright 2007 Google Inc.
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.google.zxing.client.result;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author srowen@google.com (Sean Owen)
|
|
||||||
*/
|
|
||||||
public enum ParsedReaderResultType {
|
|
||||||
|
|
||||||
// Order is important: if a string could be construed as multiple types,
|
|
||||||
// put the most specific one first
|
|
||||||
BOOKMARK(BookmarkDoCoMoResult.class),
|
|
||||||
ADDRESSBOOK(AddressBookDoCoMoResult.class),
|
|
||||||
EMAIL(EmailDoCoMoResult.class),
|
|
||||||
UPC(UPCParsedResult.class),
|
|
||||||
URI(URIParsedResult.class),
|
|
||||||
TEXT(TextParsedResult.class);
|
|
||||||
|
|
||||||
private Class<? extends ParsedReaderResult> resultClass;
|
|
||||||
|
|
||||||
ParsedReaderResultType(Class<? extends ParsedReaderResult> resultClass) {
|
|
||||||
this.resultClass = resultClass;
|
|
||||||
}
|
|
||||||
|
|
||||||
Class<? extends ParsedReaderResult> getResultClass() {
|
|
||||||
return resultClass;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,51 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright 2007 Google Inc.
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.google.zxing.client.result;
|
|
||||||
|
|
||||||
import java.net.URI;
|
|
||||||
import java.net.URISyntaxException;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author srowen@google.com (Sean Owen)
|
|
||||||
*/
|
|
||||||
public final class URIParsedResult extends ParsedReaderResult {
|
|
||||||
|
|
||||||
private final URI uri;
|
|
||||||
|
|
||||||
public URIParsedResult(String rawText) {
|
|
||||||
super(ParsedReaderResultType.URI);
|
|
||||||
if (rawText.startsWith("URL:")) {
|
|
||||||
// Sometimes a URL is prefixed with "URL:" -- support this
|
|
||||||
rawText = rawText.substring(4);
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
uri = new URI(rawText);
|
|
||||||
} catch (URISyntaxException urise) {
|
|
||||||
throw new IllegalArgumentException(urise.toString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public URI getURI() {
|
|
||||||
return uri;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getDisplayResult() {
|
|
||||||
return uri.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -16,8 +16,7 @@
|
||||||
|
|
||||||
package com.google.zxing.client.result;
|
package com.google.zxing.client.result;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.Vector;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* See
|
* See
|
||||||
|
@ -36,7 +35,7 @@ abstract class AbstractDoCoMoResult extends ParsedReaderResult {
|
||||||
// to run in a J2ME enviroment, where this unavailable.
|
// to run in a J2ME enviroment, where this unavailable.
|
||||||
|
|
||||||
static String[] matchPrefixedField(String prefix, String rawText) {
|
static String[] matchPrefixedField(String prefix, String rawText) {
|
||||||
List<String> matches = null;
|
Vector matches = null;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
int max = rawText.length();
|
int max = rawText.length();
|
||||||
while (i < max) {
|
while (i < max) {
|
||||||
|
@ -59,9 +58,9 @@ abstract class AbstractDoCoMoResult extends ParsedReaderResult {
|
||||||
} else {
|
} else {
|
||||||
// found a match
|
// found a match
|
||||||
if (matches == null) {
|
if (matches == null) {
|
||||||
matches = new ArrayList<String>(3); // lazy init
|
matches = new Vector(3); // lazy init
|
||||||
}
|
}
|
||||||
matches.add(unescape(rawText.substring(start, i)));
|
matches.addElement(unescape(rawText.substring(start, i)));
|
||||||
i++;
|
i++;
|
||||||
done = true;
|
done = true;
|
||||||
}
|
}
|
||||||
|
@ -70,7 +69,12 @@ abstract class AbstractDoCoMoResult extends ParsedReaderResult {
|
||||||
if (matches == null) {
|
if (matches == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return matches.toArray(new String[matches.size()]);
|
int size = matches.size();
|
||||||
|
String[] result = new String[size];
|
||||||
|
for (int j = 0; j < size; j++) {
|
||||||
|
result[j] = (String) matches.elementAt(j);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static String matchSinglePrefixedField(String prefix, String rawText) {
|
static String matchSinglePrefixedField(String prefix, String rawText) {
|
||||||
|
@ -91,7 +95,7 @@ abstract class AbstractDoCoMoResult extends ParsedReaderResult {
|
||||||
int backslash = escaped.indexOf((int) '\\');
|
int backslash = escaped.indexOf((int) '\\');
|
||||||
if (backslash >= 0) {
|
if (backslash >= 0) {
|
||||||
int max = escaped.length();
|
int max = escaped.length();
|
||||||
StringBuilder unescaped = new StringBuilder(max - 1);
|
StringBuffer unescaped = new StringBuffer(max - 1);
|
||||||
unescaped.append(escaped.toCharArray(), 0, backslash);
|
unescaped.append(escaped.toCharArray(), 0, backslash);
|
||||||
boolean nextIsEscaped = false;
|
boolean nextIsEscaped = false;
|
||||||
for (int i = backslash; i < max; i++) {
|
for (int i = backslash; i < max; i++) {
|
||||||
|
@ -109,7 +113,7 @@ abstract class AbstractDoCoMoResult extends ParsedReaderResult {
|
||||||
return escaped;
|
return escaped;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void maybeAppend(String value, StringBuilder result) {
|
static void maybeAppend(String value, StringBuffer result) {
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
result.append('\n');
|
result.append('\n');
|
||||||
result.append(value);
|
result.append(value);
|
|
@ -66,9 +66,8 @@ public final class AddressBookDoCoMoResult extends AbstractDoCoMoResult {
|
||||||
return address;
|
return address;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getDisplayResult() {
|
public String getDisplayResult() {
|
||||||
StringBuilder result = new StringBuilder(name);
|
StringBuffer result = new StringBuffer(name);
|
||||||
maybeAppend(email, result);
|
maybeAppend(email, result);
|
||||||
maybeAppend(address, result);
|
maybeAppend(address, result);
|
||||||
for (int i = 0; i < phoneNumbers.length; i++) {
|
for (int i = 0; i < phoneNumbers.length; i++) {
|
|
@ -16,16 +16,13 @@
|
||||||
|
|
||||||
package com.google.zxing.client.result;
|
package com.google.zxing.client.result;
|
||||||
|
|
||||||
import java.net.URI;
|
|
||||||
import java.net.URISyntaxException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author srowen@google.com (Sean Owen)
|
* @author srowen@google.com (Sean Owen)
|
||||||
*/
|
*/
|
||||||
public final class BookmarkDoCoMoResult extends AbstractDoCoMoResult {
|
public final class BookmarkDoCoMoResult extends AbstractDoCoMoResult {
|
||||||
|
|
||||||
private final String title;
|
private final String title;
|
||||||
private final URI uri;
|
private final String uri;
|
||||||
|
|
||||||
public BookmarkDoCoMoResult(String rawText) {
|
public BookmarkDoCoMoResult(String rawText) {
|
||||||
super(ParsedReaderResultType.BOOKMARK);
|
super(ParsedReaderResultType.BOOKMARK);
|
||||||
|
@ -34,25 +31,23 @@ public final class BookmarkDoCoMoResult extends AbstractDoCoMoResult {
|
||||||
}
|
}
|
||||||
title = matchSinglePrefixedField("TITLE:", rawText);
|
title = matchSinglePrefixedField("TITLE:", rawText);
|
||||||
String uriString = matchRequiredPrefixedField("URL:", rawText)[0];
|
String uriString = matchRequiredPrefixedField("URL:", rawText)[0];
|
||||||
try {
|
if (!URIParsedResult.isBasicallyValidURI(uriString)) {
|
||||||
this.uri = new URI(uriString);
|
throw new IllegalArgumentException("Invalid URI: " + uriString);
|
||||||
} catch (URISyntaxException urise) {
|
|
||||||
throw new IllegalArgumentException(urise.toString());
|
|
||||||
}
|
}
|
||||||
|
uri = uriString;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getTitle() {
|
public String getTitle() {
|
||||||
return title;
|
return title;
|
||||||
}
|
}
|
||||||
|
|
||||||
public URI getURI() {
|
public String getURI() {
|
||||||
return uri;
|
return uri;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getDisplayResult() {
|
public String getDisplayResult() {
|
||||||
if (title == null) {
|
if (title == null) {
|
||||||
return uri.toString();
|
return uri;
|
||||||
} else {
|
} else {
|
||||||
return title + '\n' + uri;
|
return title + '\n' + uri;
|
||||||
}
|
}
|
|
@ -0,0 +1,50 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2007 Google Inc.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.google.zxing.client.result;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a result that encodes an e-mail address, either as a plain address
|
||||||
|
* like "joe@example.org" or a mailto: URL like "mailto:joe@example.org".
|
||||||
|
*
|
||||||
|
* @author srowen@google.com (Sean Owen)
|
||||||
|
*/
|
||||||
|
public final class EmailAddressResult extends AbstractDoCoMoResult {
|
||||||
|
|
||||||
|
private final String emailAddress;
|
||||||
|
|
||||||
|
public EmailAddressResult(String rawText) {
|
||||||
|
super(ParsedReaderResultType.EMAIL_ADDRESS);
|
||||||
|
if (rawText.startsWith("mailto:")) {
|
||||||
|
// If it starts with mailto:, assume it is definitely trying to be an email address
|
||||||
|
emailAddress = rawText.substring(7);
|
||||||
|
} else {
|
||||||
|
if (!EmailDoCoMoResult.isBasicallyValidEmailAddress(rawText)) {
|
||||||
|
throw new IllegalArgumentException("Invalid email address: " + rawText);
|
||||||
|
}
|
||||||
|
emailAddress = rawText;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getEmailAddress() {
|
||||||
|
return emailAddress;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDisplayResult() {
|
||||||
|
return emailAddress;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -16,9 +16,6 @@
|
||||||
|
|
||||||
package com.google.zxing.client.result;
|
package com.google.zxing.client.result;
|
||||||
|
|
||||||
import java.net.URI;
|
|
||||||
import java.net.URISyntaxException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements the "MATMSG" email message entry format.
|
* Implements the "MATMSG" email message entry format.
|
||||||
*
|
*
|
||||||
|
@ -31,7 +28,6 @@ public final class EmailDoCoMoResult extends AbstractDoCoMoResult {
|
||||||
private final String to;
|
private final String to;
|
||||||
private final String subject;
|
private final String subject;
|
||||||
private final String body;
|
private final String body;
|
||||||
private final URI mailtoURI;
|
|
||||||
|
|
||||||
public EmailDoCoMoResult(String rawText) {
|
public EmailDoCoMoResult(String rawText) {
|
||||||
super(ParsedReaderResultType.EMAIL);
|
super(ParsedReaderResultType.EMAIL);
|
||||||
|
@ -39,10 +35,8 @@ public final class EmailDoCoMoResult extends AbstractDoCoMoResult {
|
||||||
throw new IllegalArgumentException("Does not begin with MATMSG");
|
throw new IllegalArgumentException("Does not begin with MATMSG");
|
||||||
}
|
}
|
||||||
to = matchRequiredPrefixedField("TO:", rawText)[0];
|
to = matchRequiredPrefixedField("TO:", rawText)[0];
|
||||||
try {
|
if (!isBasicallyValidEmailAddress(to)) {
|
||||||
mailtoURI = new URI("mailto:" + to);
|
throw new IllegalArgumentException("Invalid email address: " + to);
|
||||||
} catch (URISyntaxException urise) {
|
|
||||||
throw new IllegalArgumentException(urise.toString());
|
|
||||||
}
|
}
|
||||||
subject = matchSinglePrefixedField("SUB:", rawText);
|
subject = matchSinglePrefixedField("SUB:", rawText);
|
||||||
body = matchSinglePrefixedField("BODY:", rawText);
|
body = matchSinglePrefixedField("BODY:", rawText);
|
||||||
|
@ -52,10 +46,6 @@ public final class EmailDoCoMoResult extends AbstractDoCoMoResult {
|
||||||
return to;
|
return to;
|
||||||
}
|
}
|
||||||
|
|
||||||
public URI getMailtoURI() {
|
|
||||||
return mailtoURI;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getSubject() {
|
public String getSubject() {
|
||||||
return subject;
|
return subject;
|
||||||
}
|
}
|
||||||
|
@ -64,12 +54,22 @@ public final class EmailDoCoMoResult extends AbstractDoCoMoResult {
|
||||||
return body;
|
return body;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getDisplayResult() {
|
public String getDisplayResult() {
|
||||||
StringBuilder result = new StringBuilder(to);
|
StringBuffer result = new StringBuffer(to);
|
||||||
maybeAppend(subject, result);
|
maybeAppend(subject, result);
|
||||||
maybeAppend(body, result);
|
maybeAppend(body, result);
|
||||||
return result.toString();
|
return result.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This implements only the most basic checking for an email address's validity -- that it contains
|
||||||
|
* an '@' and a '.' somewhere after that, and that it contains no space.
|
||||||
|
* We want to generally be lenient here since this class is only intended to encapsulate what's
|
||||||
|
* in a barcode, not "judge" it.
|
||||||
|
*/
|
||||||
|
static boolean isBasicallyValidEmailAddress(String email) {
|
||||||
|
int atIndex = email.indexOf('@');
|
||||||
|
return atIndex >= 0 && email.indexOf('.') > atIndex && email.indexOf(' ') < 0;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -16,9 +16,6 @@
|
||||||
|
|
||||||
package com.google.zxing.client.result;
|
package com.google.zxing.client.result;
|
||||||
|
|
||||||
import java.lang.reflect.Constructor;
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author srowen@google.com (Sean Owen)
|
* @author srowen@google.com (Sean Owen)
|
||||||
*/
|
*/
|
||||||
|
@ -37,30 +34,42 @@ public abstract class ParsedReaderResult {
|
||||||
public abstract String getDisplayResult();
|
public abstract String getDisplayResult();
|
||||||
|
|
||||||
public static ParsedReaderResult parseReaderResult(String rawText) {
|
public static ParsedReaderResult parseReaderResult(String rawText) {
|
||||||
for (ParsedReaderResultType type : ParsedReaderResultType.values()) {
|
// This is a bit messy, but given limited options in MIDP / CLDC, this may well be the simplest
|
||||||
Class<? extends ParsedReaderResult> resultClass = type.getResultClass();
|
// way to go about this. For example, we have no reflection available, really.
|
||||||
try {
|
// Order is important here.
|
||||||
Constructor<? extends ParsedReaderResult> constructor =
|
try {
|
||||||
resultClass.getConstructor(String.class);
|
return new BookmarkDoCoMoResult(rawText);
|
||||||
return constructor.newInstance(rawText);
|
} catch (IllegalArgumentException iae) {
|
||||||
} catch (InvocationTargetException ite) {
|
// continue
|
||||||
Throwable cause = ite.getCause();
|
|
||||||
if (cause instanceof IllegalArgumentException) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
throw new RuntimeException(cause);
|
|
||||||
} catch (NoSuchMethodException nsme) {
|
|
||||||
throw new RuntimeException(nsme);
|
|
||||||
} catch (IllegalAccessException iae) {
|
|
||||||
throw new RuntimeException(iae);
|
|
||||||
} catch (InstantiationException ie) {
|
|
||||||
throw new RuntimeException(ie);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
throw new IllegalStateException("TextResult should always work");
|
try {
|
||||||
|
return new AddressBookDoCoMoResult(rawText);
|
||||||
|
} catch (IllegalArgumentException iae) {
|
||||||
|
// continue
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return new EmailDoCoMoResult(rawText);
|
||||||
|
} catch (IllegalArgumentException iae) {
|
||||||
|
// continue
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return new EmailAddressResult(rawText);
|
||||||
|
} catch (IllegalArgumentException iae) {
|
||||||
|
// continue
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return new URIParsedResult(rawText);
|
||||||
|
} catch (IllegalArgumentException iae) {
|
||||||
|
// continue
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return new UPCParsedResult(rawText);
|
||||||
|
} catch (IllegalArgumentException iae) {
|
||||||
|
// continue
|
||||||
|
}
|
||||||
|
return new TextParsedResult(rawText);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return getDisplayResult();
|
return getDisplayResult();
|
||||||
}
|
}
|
|
@ -0,0 +1,39 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2007 Google Inc.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.google.zxing.client.result;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents the type of data encoded by a barcode -- from plain text, to a
|
||||||
|
* URI, to an e-mail address, etc.
|
||||||
|
*
|
||||||
|
* @author srowen@google.com (Sean Owen)
|
||||||
|
*/
|
||||||
|
public final class ParsedReaderResultType {
|
||||||
|
|
||||||
|
public static final ParsedReaderResultType BOOKMARK = new ParsedReaderResultType();
|
||||||
|
public static final ParsedReaderResultType ADDRESSBOOK = new ParsedReaderResultType();
|
||||||
|
public static final ParsedReaderResultType EMAIL = new ParsedReaderResultType();
|
||||||
|
public static final ParsedReaderResultType EMAIL_ADDRESS = new ParsedReaderResultType();
|
||||||
|
public static final ParsedReaderResultType UPC = new ParsedReaderResultType();
|
||||||
|
public static final ParsedReaderResultType URI = new ParsedReaderResultType();
|
||||||
|
public static final ParsedReaderResultType TEXT = new ParsedReaderResultType();
|
||||||
|
|
||||||
|
private ParsedReaderResultType() {
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -32,7 +32,6 @@ public final class TextParsedResult extends ParsedReaderResult {
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getDisplayResult() {
|
public String getDisplayResult() {
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
|
@ -42,7 +42,6 @@ public final class UPCParsedResult extends ParsedReaderResult {
|
||||||
return upc;
|
return upc;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getDisplayResult() {
|
public String getDisplayResult() {
|
||||||
return upc;
|
return upc;
|
||||||
}
|
}
|
73
core/src/com/google/zxing/client/result/URIParsedResult.java
Normal file
73
core/src/com/google/zxing/client/result/URIParsedResult.java
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2007 Google Inc.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.google.zxing.client.result;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author srowen@google.com (Sean Owen)
|
||||||
|
*/
|
||||||
|
public final class URIParsedResult extends ParsedReaderResult {
|
||||||
|
|
||||||
|
private final String uri;
|
||||||
|
|
||||||
|
public URIParsedResult(String rawText) {
|
||||||
|
super(ParsedReaderResultType.URI);
|
||||||
|
if (!isBasicallyValidURI(rawText)) {
|
||||||
|
throw new IllegalArgumentException("Invalid URI: " + rawText);
|
||||||
|
}
|
||||||
|
uri = massagePossibleURI(rawText);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getURI() {
|
||||||
|
return uri;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDisplayResult() {
|
||||||
|
return uri;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transforms a string that possibly represents a URI into something more proper, by adding or canonicalizing
|
||||||
|
* the protocol.
|
||||||
|
*/
|
||||||
|
static String massagePossibleURI(String uri) {
|
||||||
|
// Take off leading "URL:" if present
|
||||||
|
if (uri.startsWith("URL:")) {
|
||||||
|
uri = uri.substring(4);
|
||||||
|
}
|
||||||
|
int protocolEnd = uri.indexOf(':');
|
||||||
|
if (protocolEnd < 0) {
|
||||||
|
// No protocol, assume http
|
||||||
|
uri = "http://" + uri;
|
||||||
|
} else {
|
||||||
|
// Lowercase protocol to avoid problems
|
||||||
|
uri = uri.substring(0, protocolEnd).toLowerCase() + uri.substring(protocolEnd);
|
||||||
|
// TODO this logic isn't quite right for URIs like "example.org:443/foo"
|
||||||
|
}
|
||||||
|
return uri;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determines whether a string is not obviously not a URI. This implements crude checks; this class does not
|
||||||
|
* intend to strictly check URIs as its only function is to represent what is in a barcode, but, it does
|
||||||
|
* need to know when a string is obviously not a URI.
|
||||||
|
*/
|
||||||
|
static boolean isBasicallyValidURI(String uri) {
|
||||||
|
return uri.indexOf(' ') < 0 && (uri.indexOf(':') >= 0 || uri.indexOf('.') >= 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,87 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2007 Google Inc.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.google.zxing.client.result;
|
||||||
|
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests {@link ParsedReaderResult}.
|
||||||
|
*
|
||||||
|
* @author srowen@google.com (Sean Owen)
|
||||||
|
*/
|
||||||
|
public final class ParsedReaderResultTestCase extends TestCase {
|
||||||
|
|
||||||
|
public void testTextType() {
|
||||||
|
doTestResult("foo", ParsedReaderResultType.TEXT);
|
||||||
|
doTestResult("", ParsedReaderResultType.TEXT);
|
||||||
|
doTestResult("This is a test", ParsedReaderResultType.TEXT);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testBookmarkType() {
|
||||||
|
doTestResult("MEBKM:URL:google.com;;", ParsedReaderResultType.BOOKMARK);
|
||||||
|
doTestResult("MEBKM:URL:google.com;TITLE:Google;;", ParsedReaderResultType.BOOKMARK);
|
||||||
|
doTestResult("MEBKM:TITLE:Google;URL:google.com;;", ParsedReaderResultType.BOOKMARK);
|
||||||
|
doTestResult("MEBKM:URL:http://google.com;;", ParsedReaderResultType.BOOKMARK);
|
||||||
|
doTestResult("MEBKM:URL:HTTPS://google.com;;", ParsedReaderResultType.BOOKMARK);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testEmailType() {
|
||||||
|
doTestResult("MATMSG:TO:srowen@example.org;;", ParsedReaderResultType.EMAIL);
|
||||||
|
doTestResult("MATMSG:TO:srowen@example.org;SUB:Stuff;;", ParsedReaderResultType.EMAIL);
|
||||||
|
doTestResult("MATMSG:TO:srowen@example.org;SUB:Stuff;BODY:This is some text;;", ParsedReaderResultType.EMAIL);
|
||||||
|
doTestResult("MATMSG:SUB:Stuff;BODY:This is some text;TO:srowen@example.org;;", ParsedReaderResultType.EMAIL);
|
||||||
|
doTestResult("TO:srowen@example.org;SUB:Stuff;BODY:This is some text;;", ParsedReaderResultType.TEXT);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testEmailAddressType() {
|
||||||
|
doTestResult("srowen@example.org", ParsedReaderResultType.EMAIL_ADDRESS);
|
||||||
|
doTestResult("mailto:srowen@example.org", ParsedReaderResultType.EMAIL_ADDRESS);
|
||||||
|
doTestResult("srowen@example", ParsedReaderResultType.TEXT);
|
||||||
|
doTestResult("srowen", ParsedReaderResultType.TEXT);
|
||||||
|
doTestResult("Let's meet @ 2", ParsedReaderResultType.TEXT);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testAddressBookType() {
|
||||||
|
doTestResult("MECARD:N:Sean Owen;;", ParsedReaderResultType.ADDRESSBOOK);
|
||||||
|
doTestResult("MECARD:TEL:+12125551212;N:Sean Owen;;", ParsedReaderResultType.ADDRESSBOOK);
|
||||||
|
doTestResult("MECARD:TEL:+12125551212;N:Sean Owen;URL:google.com;;", ParsedReaderResultType.ADDRESSBOOK);
|
||||||
|
doTestResult("TEL:+12125551212;N:Sean Owen;;", ParsedReaderResultType.TEXT);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testUPC() {
|
||||||
|
doTestResult("123456789012", ParsedReaderResultType.UPC);
|
||||||
|
doTestResult("1234567890123", ParsedReaderResultType.UPC);
|
||||||
|
doTestResult("12345678901", ParsedReaderResultType.TEXT);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testURI() {
|
||||||
|
doTestResult("http://google.com", ParsedReaderResultType.URI);
|
||||||
|
doTestResult("google.com", ParsedReaderResultType.URI);
|
||||||
|
doTestResult("https://google.com", ParsedReaderResultType.URI);
|
||||||
|
doTestResult("HTTP://google.com", ParsedReaderResultType.URI);
|
||||||
|
doTestResult("http://google.com/foobar", ParsedReaderResultType.URI);
|
||||||
|
doTestResult("https://google.com:443/foobar", ParsedReaderResultType.URI);
|
||||||
|
doTestResult("google.com:443/foobar", ParsedReaderResultType.URI);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void doTestResult(String text, ParsedReaderResultType type) {
|
||||||
|
ParsedReaderResult result = ParsedReaderResult.parseReaderResult(text);
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(type, result.getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -16,6 +16,12 @@
|
||||||
|
|
||||||
package com.google.zxing.client.j2me;
|
package com.google.zxing.client.j2me;
|
||||||
|
|
||||||
|
import com.google.zxing.client.result.BookmarkDoCoMoResult;
|
||||||
|
import com.google.zxing.client.result.EmailDoCoMoResult;
|
||||||
|
import com.google.zxing.client.result.ParsedReaderResult;
|
||||||
|
import com.google.zxing.client.result.ParsedReaderResultType;
|
||||||
|
import com.google.zxing.client.result.URIParsedResult;
|
||||||
|
|
||||||
import javax.microedition.io.ConnectionNotFoundException;
|
import javax.microedition.io.ConnectionNotFoundException;
|
||||||
import javax.microedition.lcdui.Alert;
|
import javax.microedition.lcdui.Alert;
|
||||||
import javax.microedition.lcdui.AlertType;
|
import javax.microedition.lcdui.AlertType;
|
||||||
|
@ -144,52 +150,18 @@ public final class ZXingMIDlet extends MIDlet {
|
||||||
display.setCurrent(alert, canvas);
|
display.setCurrent(alert, canvas);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// TODO this whole bit needs to be merged with the code in core-ext -- this is messy and duplicative
|
|
||||||
|
|
||||||
void handleDecodedText(String text) {
|
void handleDecodedText(String text) {
|
||||||
// This is a crude imitation of the code found in module core-ext, which handles the contents
|
ParsedReaderResult result = ParsedReaderResult.parseReaderResult(text);
|
||||||
// in a more sophisticated way. It can't be accessed from JavaME just yet because it relies
|
ParsedReaderResultType type = result.getType();
|
||||||
// on URL parsing routines in java.net. This should be somehow worked around: TODO
|
if (type.equals(ParsedReaderResultType.URI)) {
|
||||||
// For now, detect URLs in a simple way, and treat everything else as text
|
showOpenURL(((URIParsedResult) result).getURI());
|
||||||
if (text.startsWith("http://") || text.startsWith("https://")) {
|
} else if (type.equals(ParsedReaderResultType.BOOKMARK)) {
|
||||||
showOpenURL(text);
|
showOpenURL(((BookmarkDoCoMoResult) result).getURI());
|
||||||
} else if (text.startsWith("HTTP://") || text.startsWith("HTTPS://")) {
|
} else if (type.equals(ParsedReaderResultType.EMAIL)) {
|
||||||
showOpenURL(decapitalizeProtocol(text));
|
showOpenURL(((EmailDoCoMoResult) result).getMailtoURI());
|
||||||
} else if (text.startsWith("URL:")) {
|
|
||||||
showOpenURL(decapitalizeProtocol(text.substring(4)));
|
|
||||||
} else if (text.startsWith("MEBKM:")) {
|
|
||||||
int urlIndex = text.indexOf("URL:", 6);
|
|
||||||
if (urlIndex >= 6) {
|
|
||||||
String url = text.substring( urlIndex + 4);
|
|
||||||
int semicolon = url.indexOf((int) ';');
|
|
||||||
if (semicolon >= 0) {
|
|
||||||
url = url.substring(0, semicolon);
|
|
||||||
}
|
|
||||||
showOpenURL(decapitalizeProtocol(url));
|
|
||||||
} else {
|
|
||||||
showAlert("Barcode detected", text);
|
|
||||||
}
|
|
||||||
} else if (maybeURLWithoutScheme(text)) {
|
|
||||||
showOpenURL("http://" + text);
|
|
||||||
} else {
|
} else {
|
||||||
showAlert("Barcode detected", text);
|
showAlert("Barcode detected", result.getDisplayResult());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String decapitalizeProtocol(String url) {
|
|
||||||
int protocolEnd = url.indexOf("://");
|
|
||||||
if (protocolEnd >= 0) {
|
|
||||||
return url.substring(0, protocolEnd).toLowerCase() + url.substring(protocolEnd);
|
|
||||||
} else {
|
|
||||||
return url;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Crudely guesses that a string may represent a URL if it has a '.' and no spaces.
|
|
||||||
*/
|
|
||||||
private static boolean maybeURLWithoutScheme(String text) {
|
|
||||||
return text.indexOf((int) '.') >= 0 && text.indexOf((int) ' ') < 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,7 +33,6 @@
|
||||||
deprecation="true">
|
deprecation="true">
|
||||||
<classpath>
|
<classpath>
|
||||||
<pathelement location="../core/core.jar"/>
|
<pathelement location="../core/core.jar"/>
|
||||||
<pathelement location="../core-ext/core-ext.jar"/>
|
|
||||||
</classpath>
|
</classpath>
|
||||||
</javac>
|
</javac>
|
||||||
<jar jarfile="javase.jar" basedir="build" level="9"/>
|
<jar jarfile="javase.jar" basedir="build" level="9"/>
|
||||||
|
|
Loading…
Reference in a new issue