PageRenderTime 45ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/src/android/LibraryProject/src/com/google/zxing/client/result/VCardResultParser.java

https://gitlab.com/taveek/BarcodeScanner
Java | 341 lines | 279 code | 25 blank | 37 comment | 90 complexity | 090ce310b4fc5862ab2f8869d7506e11 MD5 | raw file
  1. /*
  2. * Copyright 2008 ZXing authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.google.zxing.client.result;
  17. import com.google.zxing.Result;
  18. import java.io.ByteArrayOutputStream;
  19. import java.io.UnsupportedEncodingException;
  20. import java.util.ArrayList;
  21. import java.util.Collection;
  22. import java.util.List;
  23. import java.util.regex.Matcher;
  24. import java.util.regex.Pattern;
  25. /**
  26. * Parses contact information formatted according to the VCard (2.1) format. This is not a complete
  27. * implementation but should parse information as commonly encoded in 2D barcodes.
  28. *
  29. * @author Sean Owen
  30. */
  31. public final class VCardResultParser extends ResultParser {
  32. private static final Pattern BEGIN_VCARD = Pattern.compile("BEGIN:VCARD", Pattern.CASE_INSENSITIVE);
  33. private static final Pattern VCARD_LIKE_DATE = Pattern.compile("\\d{4}-?\\d{2}-?\\d{2}");
  34. private static final Pattern CR_LF_SPACE_TAB = Pattern.compile("\r\n[ \t]");
  35. private static final Pattern NEWLINE_ESCAPE = Pattern.compile("\\\\[nN]");
  36. private static final Pattern VCARD_ESCAPES = Pattern.compile("\\\\([,;\\\\])");
  37. private static final Pattern EQUALS = Pattern.compile("=");
  38. private static final Pattern SEMICOLON = Pattern.compile(";");
  39. private static final Pattern UNESCAPED_SEMICOLONS = Pattern.compile("(?<!\\\\);+");
  40. @Override
  41. public AddressBookParsedResult parse(Result result) {
  42. // Although we should insist on the raw text ending with "END:VCARD", there's no reason
  43. // to throw out everything else we parsed just because this was omitted. In fact, Eclair
  44. // is doing just that, and we can't parse its contacts without this leniency.
  45. String rawText = getMassagedText(result);
  46. Matcher m = BEGIN_VCARD.matcher(rawText);
  47. if (!m.find() || m.start() != 0) {
  48. return null;
  49. }
  50. List<List<String>> names = matchVCardPrefixedField("FN", rawText, true, false);
  51. if (names == null) {
  52. // If no display names found, look for regular name fields and format them
  53. names = matchVCardPrefixedField("N", rawText, true, false);
  54. formatNames(names);
  55. }
  56. List<List<String>> phoneNumbers = matchVCardPrefixedField("TEL", rawText, true, false);
  57. List<List<String>> emails = matchVCardPrefixedField("EMAIL", rawText, true, false);
  58. List<String> note = matchSingleVCardPrefixedField("NOTE", rawText, false, false);
  59. List<List<String>> addresses = matchVCardPrefixedField("ADR", rawText, true, true);
  60. List<String> org = matchSingleVCardPrefixedField("ORG", rawText, true, true);
  61. List<String> birthday = matchSingleVCardPrefixedField("BDAY", rawText, true, false);
  62. if (birthday != null && !isLikeVCardDate(birthday.get(0))) {
  63. birthday = null;
  64. }
  65. List<String> title = matchSingleVCardPrefixedField("TITLE", rawText, true, false);
  66. List<String> url = matchSingleVCardPrefixedField("URL", rawText, true, false);
  67. List<String> instantMessenger = matchSingleVCardPrefixedField("IMPP", rawText, true, false);
  68. return new AddressBookParsedResult(toPrimaryValues(names),
  69. null,
  70. toPrimaryValues(phoneNumbers),
  71. toTypes(phoneNumbers),
  72. toPrimaryValues(emails),
  73. toTypes(emails),
  74. toPrimaryValue(instantMessenger),
  75. toPrimaryValue(note),
  76. toPrimaryValues(addresses),
  77. toTypes(addresses),
  78. toPrimaryValue(org),
  79. toPrimaryValue(birthday),
  80. toPrimaryValue(title),
  81. toPrimaryValue(url));
  82. }
  83. static List<List<String>> matchVCardPrefixedField(CharSequence prefix,
  84. String rawText,
  85. boolean trim,
  86. boolean parseFieldDivider) {
  87. List<List<String>> matches = null;
  88. int i = 0;
  89. int max = rawText.length();
  90. while (i < max) {
  91. // At start or after newline, match prefix, followed by optional metadata
  92. // (led by ;) ultimately ending in colon
  93. Matcher matcher = Pattern.compile("(?:^|\n)" + prefix + "(?:;([^:]*))?:",
  94. Pattern.CASE_INSENSITIVE).matcher(rawText);
  95. if (i > 0) {
  96. i--; // Find from i-1 not i since looking at the preceding character
  97. }
  98. if (!matcher.find(i)) {
  99. break;
  100. }
  101. i = matcher.end(0); // group 0 = whole pattern; end(0) is past final colon
  102. String metadataString = matcher.group(1); // group 1 = metadata substring
  103. List<String> metadata = null;
  104. boolean quotedPrintable = false;
  105. String quotedPrintableCharset = null;
  106. if (metadataString != null) {
  107. for (String metadatum : SEMICOLON.split(metadataString)) {
  108. if (metadata == null) {
  109. metadata = new ArrayList<String>(1);
  110. }
  111. metadata.add(metadatum);
  112. String[] metadatumTokens = EQUALS.split(metadatum, 2);
  113. if (metadatumTokens.length > 1) {
  114. String key = metadatumTokens[0];
  115. String value = metadatumTokens[1];
  116. if ("ENCODING".equalsIgnoreCase(key) && "QUOTED-PRINTABLE".equalsIgnoreCase(value)) {
  117. quotedPrintable = true;
  118. } else if ("CHARSET".equalsIgnoreCase(key)) {
  119. quotedPrintableCharset = value;
  120. }
  121. }
  122. }
  123. }
  124. int matchStart = i; // Found the start of a match here
  125. while ((i = rawText.indexOf((int) '\n', i)) >= 0) { // Really, end in \r\n
  126. if (i < rawText.length() - 1 && // But if followed by tab or space,
  127. (rawText.charAt(i+1) == ' ' || // this is only a continuation
  128. rawText.charAt(i+1) == '\t')) {
  129. i += 2; // Skip \n and continutation whitespace
  130. } else if (quotedPrintable && // If preceded by = in quoted printable
  131. ((i >= 1 && rawText.charAt(i-1) == '=') || // this is a continuation
  132. (i >= 2 && rawText.charAt(i-2) == '='))) {
  133. i++; // Skip \n
  134. } else {
  135. break;
  136. }
  137. }
  138. if (i < 0) {
  139. // No terminating end character? uh, done. Set i such that loop terminates and break
  140. i = max;
  141. } else if (i > matchStart) {
  142. // found a match
  143. if (matches == null) {
  144. matches = new ArrayList<List<String>>(1); // lazy init
  145. }
  146. if (i >= 1 && rawText.charAt(i-1) == '\r') {
  147. i--; // Back up over \r, which really should be there
  148. }
  149. String element = rawText.substring(matchStart, i);
  150. if (trim) {
  151. element = element.trim();
  152. }
  153. if (quotedPrintable) {
  154. element = decodeQuotedPrintable(element, quotedPrintableCharset);
  155. if (parseFieldDivider) {
  156. element = UNESCAPED_SEMICOLONS.matcher(element).replaceAll("\n").trim();
  157. }
  158. } else {
  159. if (parseFieldDivider) {
  160. element = UNESCAPED_SEMICOLONS.matcher(element).replaceAll("\n").trim();
  161. }
  162. element = CR_LF_SPACE_TAB.matcher(element).replaceAll("");
  163. element = NEWLINE_ESCAPE.matcher(element).replaceAll("\n");
  164. element = VCARD_ESCAPES.matcher(element).replaceAll("$1");
  165. }
  166. if (metadata == null) {
  167. List<String> match = new ArrayList<String>(1);
  168. match.add(element);
  169. matches.add(match);
  170. } else {
  171. metadata.add(0, element);
  172. matches.add(metadata);
  173. }
  174. i++;
  175. } else {
  176. i++;
  177. }
  178. }
  179. return matches;
  180. }
  181. private static String decodeQuotedPrintable(CharSequence value, String charset) {
  182. int length = value.length();
  183. StringBuilder result = new StringBuilder(length);
  184. ByteArrayOutputStream fragmentBuffer = new ByteArrayOutputStream();
  185. for (int i = 0; i < length; i++) {
  186. char c = value.charAt(i);
  187. switch (c) {
  188. case '\r':
  189. case '\n':
  190. break;
  191. case '=':
  192. if (i < length - 2) {
  193. char nextChar = value.charAt(i+1);
  194. if (nextChar != '\r' && nextChar != '\n') {
  195. char nextNextChar = value.charAt(i+2);
  196. int firstDigit = parseHexDigit(nextChar);
  197. int secondDigit = parseHexDigit(nextNextChar);
  198. if (firstDigit >= 0 && secondDigit >= 0) {
  199. fragmentBuffer.write((firstDigit << 4) + secondDigit);
  200. } // else ignore it, assume it was incorrectly encoded
  201. i += 2;
  202. }
  203. }
  204. break;
  205. default:
  206. maybeAppendFragment(fragmentBuffer, charset, result);
  207. result.append(c);
  208. }
  209. }
  210. maybeAppendFragment(fragmentBuffer, charset, result);
  211. return result.toString();
  212. }
  213. private static void maybeAppendFragment(ByteArrayOutputStream fragmentBuffer,
  214. String charset,
  215. StringBuilder result) {
  216. if (fragmentBuffer.size() > 0) {
  217. byte[] fragmentBytes = fragmentBuffer.toByteArray();
  218. String fragment;
  219. if (charset == null) {
  220. fragment = new String(fragmentBytes);
  221. } else {
  222. try {
  223. fragment = new String(fragmentBytes, charset);
  224. } catch (UnsupportedEncodingException e) {
  225. // Yikes, well try anyway:
  226. fragment = new String(fragmentBytes);
  227. }
  228. }
  229. fragmentBuffer.reset();
  230. result.append(fragment);
  231. }
  232. }
  233. static List<String> matchSingleVCardPrefixedField(CharSequence prefix,
  234. String rawText,
  235. boolean trim,
  236. boolean parseFieldDivider) {
  237. List<List<String>> values = matchVCardPrefixedField(prefix, rawText, trim, parseFieldDivider);
  238. return values == null || values.isEmpty() ? null : values.get(0);
  239. }
  240. private static String toPrimaryValue(List<String> list) {
  241. return list == null || list.isEmpty() ? null : list.get(0);
  242. }
  243. private static String[] toPrimaryValues(Collection<List<String>> lists) {
  244. if (lists == null || lists.isEmpty()) {
  245. return null;
  246. }
  247. List<String> result = new ArrayList<String>(lists.size());
  248. for (List<String> list : lists) {
  249. result.add(list.get(0));
  250. }
  251. return result.toArray(new String[lists.size()]);
  252. }
  253. private static String[] toTypes(Collection<List<String>> lists) {
  254. if (lists == null || lists.isEmpty()) {
  255. return null;
  256. }
  257. List<String> result = new ArrayList<String>(lists.size());
  258. for (List<String> list : lists) {
  259. String type = null;
  260. for (int i = 1; i < list.size(); i++) {
  261. String metadatum = list.get(i);
  262. int equals = metadatum.indexOf('=');
  263. if (equals < 0) {
  264. // take the whole thing as a usable label
  265. type = metadatum;
  266. break;
  267. }
  268. if ("TYPE".equalsIgnoreCase(metadatum.substring(0, equals))) {
  269. type = metadatum.substring(equals + 1);
  270. break;
  271. }
  272. }
  273. result.add(type);
  274. }
  275. return result.toArray(new String[lists.size()]);
  276. }
  277. private static boolean isLikeVCardDate(CharSequence value) {
  278. return value == null || VCARD_LIKE_DATE.matcher(value).matches();
  279. }
  280. /**
  281. * Formats name fields of the form "Public;John;Q.;Reverend;III" into a form like
  282. * "Reverend John Q. Public III".
  283. *
  284. * @param names name values to format, in place
  285. */
  286. private static void formatNames(Iterable<List<String>> names) {
  287. if (names != null) {
  288. for (List<String> list : names) {
  289. String name = list.get(0);
  290. String[] components = new String[5];
  291. int start = 0;
  292. int end;
  293. int componentIndex = 0;
  294. while (componentIndex < components.length - 1 && (end = name.indexOf(';', start)) > 0) {
  295. components[componentIndex] = name.substring(start, end);
  296. componentIndex++;
  297. start = end + 1;
  298. }
  299. components[componentIndex] = name.substring(start);
  300. StringBuilder newName = new StringBuilder(100);
  301. maybeAppendComponent(components, 3, newName);
  302. maybeAppendComponent(components, 1, newName);
  303. maybeAppendComponent(components, 2, newName);
  304. maybeAppendComponent(components, 0, newName);
  305. maybeAppendComponent(components, 4, newName);
  306. list.set(0, newName.toString().trim());
  307. }
  308. }
  309. }
  310. private static void maybeAppendComponent(String[] components, int i, StringBuilder newName) {
  311. if (components[i] != null) {
  312. newName.append(' ');
  313. newName.append(components[i]);
  314. }
  315. }
  316. }