| 1 |
package ezvcard.io.html; |
| 2 |
|
| 3 |
import static ezvcard.util.HtmlUtils.isChildOf; |
| 4 |
|
| 5 |
import java.io.IOException; |
| 6 |
import java.io.InputStream; |
| 7 |
import java.io.Reader; |
| 8 |
import java.io.UncheckedIOException; |
| 9 |
import java.net.MalformedURLException; |
| 10 |
import java.net.URL; |
| 11 |
import java.nio.file.Path; |
| 12 |
import java.util.ArrayList; |
| 13 |
import java.util.Iterator; |
| 14 |
import java.util.List; |
| 15 |
import java.util.Set; |
| 16 |
|
| 17 |
import org.jsoup.Jsoup; |
| 18 |
import org.jsoup.nodes.Document; |
| 19 |
import org.jsoup.nodes.Element; |
| 20 |
import org.jsoup.select.Elements; |
| 21 |
|
| 22 |
import ezvcard.VCard; |
| 23 |
import ezvcard.VCardVersion; |
| 24 |
import ezvcard.io.CannotParseException; |
| 25 |
import ezvcard.io.EmbeddedVCardException; |
| 26 |
import ezvcard.io.ParseWarning; |
| 27 |
import ezvcard.io.SkipMeException; |
| 28 |
import ezvcard.io.StreamReader; |
| 29 |
import ezvcard.io.scribe.RawPropertyScribe; |
| 30 |
import ezvcard.io.scribe.VCardPropertyScribe; |
| 31 |
import ezvcard.property.Categories; |
| 32 |
import ezvcard.property.Email; |
| 33 |
import ezvcard.property.Impp; |
| 34 |
import ezvcard.property.Label; |
| 35 |
import ezvcard.property.Nickname; |
| 36 |
import ezvcard.property.RawProperty; |
| 37 |
import ezvcard.property.Telephone; |
| 38 |
import ezvcard.property.Url; |
| 39 |
import ezvcard.property.VCardProperty; |
| 40 |
import ezvcard.util.Gobble; |
| 41 |
import ezvcard.util.IOUtils; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public class HCardParser extends StreamReader { |
| 90 |
private final String pageUrl; |
| 91 |
private final Elements vcardElements; |
| 92 |
private final Iterator<Element> vcardElementsIt; |
| 93 |
private final List<Label> labels = new ArrayList<>(); |
| 94 |
|
| 95 |
private VCard vcard; |
| 96 |
private Elements embeddedVCards = new Elements(); |
| 97 |
private Nickname nickname; |
| 98 |
private Categories categories; |
| 99 |
|
| 100 |
private final String urlPropertyName = index.getPropertyScribe(Url.class).getPropertyName().toLowerCase(); |
| 101 |
private final String categoriesName = index.getPropertyScribe(Categories.class).getPropertyName().toLowerCase(); |
| 102 |
private final String emailName = index.getPropertyScribe(Email.class).getPropertyName().toLowerCase(); |
| 103 |
private final String telName = index.getPropertyScribe(Telephone.class).getPropertyName().toLowerCase(); |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public HCardParser(URL url) throws IOException { |
| 111 |
this(Jsoup.parse(url, 30000), url.toString()); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public HCardParser(InputStream in) throws IOException { |
| 120 |
this(in, null); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public HCardParser(InputStream in, String pageUrl) throws IOException { |
| 131 |
this((pageUrl == null) ? Jsoup.parse(in, null, "") : Jsoup.parse(in, null, pageUrl), pageUrl); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public HCardParser(Path file) throws IOException { |
| 140 |
this(file, null); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public HCardParser(Path file, String pageUrl) throws IOException { |
| 151 |
this((pageUrl == null) ? Jsoup.parse(file.toFile(), null, "") : Jsoup.parse(file.toFile(), null, pageUrl), pageUrl); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public HCardParser(Reader reader) throws IOException { |
| 160 |
this(reader, null); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public HCardParser(Reader reader, String pageUrl) throws IOException { |
| 171 |
this(new Gobble(reader).asString(), pageUrl); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
public HCardParser(String html) { |
| 179 |
this(html, null); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
public HCardParser(String html, String pageUrl) { |
| 189 |
this((pageUrl == null) ? Jsoup.parse(html) : Jsoup.parse(html, pageUrl), pageUrl); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public HCardParser(Document document) { |
| 197 |
this(document, null); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
public HCardParser(Document document, String pageUrl) { |
| 207 |
this.pageUrl = pageUrl; |
| 208 |
|
| 209 |
String anchor = null; |
| 210 |
if (pageUrl != null) { |
| 211 |
try { |
| 212 |
URL url = new URL(pageUrl); |
| 213 |
anchor = url.getRef(); |
| 214 |
} catch (MalformedURLException e) { |
| 215 |
anchor = null; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
Element searchUnder = null; |
| 220 |
if (anchor != null) { |
| 221 |
searchUnder = document.getElementById(anchor); |
| 222 |
} |
| 223 |
if (searchUnder == null) { |
| 224 |
searchUnder = document; |
| 225 |
} |
| 226 |
|
| 227 |
vcardElements = searchUnder.getElementsByClass("vcard"); |
| 228 |
|
| 229 |
|
| 230 |
Iterator<Element> it = vcardElements.iterator(); |
| 231 |
while (it.hasNext()) { |
| 232 |
Element element = it.next(); |
| 233 |
if (isChildOf(element, vcardElements)) { |
| 234 |
it.remove(); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
vcardElementsIt = vcardElements.iterator(); |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
private HCardParser(Element embeddedVCard, String pageUrl) { |
| 247 |
this.pageUrl = pageUrl; |
| 248 |
vcardElements = new Elements(embeddedVCard); |
| 249 |
vcardElementsIt = vcardElements.iterator(); |
| 250 |
} |
| 251 |
|
| 252 |
@Override |
| 253 |
public VCard readNext() { |
| 254 |
try { |
| 255 |
return super.readNext(); |
| 256 |
} catch (IOException e) { |
| 257 |
|
| 258 |
throw new UncheckedIOException(e); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
@Override |
| 263 |
protected VCard _readNext() { |
| 264 |
if (!vcardElementsIt.hasNext()) { |
| 265 |
return null; |
| 266 |
} |
| 267 |
|
| 268 |
context.setVersion(VCardVersion.V3_0); |
| 269 |
parseVCardElement(vcardElementsIt.next()); |
| 270 |
return vcard; |
| 271 |
} |
| 272 |
|
| 273 |
private void parseVCardElement(Element vcardElement) { |
| 274 |
labels.clear(); |
| 275 |
nickname = null; |
| 276 |
categories = null; |
| 277 |
|
| 278 |
vcard = new VCard(); |
| 279 |
vcard.setVersion(VCardVersion.V3_0); |
| 280 |
if (pageUrl != null) { |
| 281 |
vcard.addSource(pageUrl); |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
for (Element child : vcardElement.children()) { |
| 286 |
visit(child); |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
assignLabels(vcard, labels); |
| 291 |
} |
| 292 |
|
| 293 |
private void visit(Element element) { |
| 294 |
boolean visitChildren = true; |
| 295 |
Set<String> classNames = element.classNames(); |
| 296 |
for (String className : classNames) { |
| 297 |
className = className.toLowerCase(); |
| 298 |
|
| 299 |
|
| 300 |
if (urlPropertyName.equals(className)) { |
| 301 |
String href = element.attr("href"); |
| 302 |
if (href.length() > 0) { |
| 303 |
if (!classNames.contains(emailName) && href.matches("(?i)mailto:.*")) { |
| 304 |
className = emailName; |
| 305 |
} else if (!classNames.contains(telName) && href.matches("(?i)tel:.*")) { |
| 306 |
className = telName; |
| 307 |
} else { |
| 308 |
|
| 309 |
VCardPropertyScribe<? extends VCardProperty> scribe = index.getPropertyScribe(Impp.class); |
| 310 |
|
| 311 |
context.getWarnings().clear(); |
| 312 |
context.setPropertyName(scribe.getPropertyName()); |
| 313 |
try { |
| 314 |
VCardProperty property = scribe.parseHtml(new HCardElement(element), context); |
| 315 |
vcard.addProperty(property); |
| 316 |
warnings.addAll(context.getWarnings()); |
| 317 |
continue; |
| 318 |
} catch (SkipMeException e) { |
| 319 |
|
| 320 |
} catch (CannotParseException e) { |
| 321 |
|
| 322 |
} |
| 323 |
} |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
if ("category".equals(className)) { |
| 329 |
className = categoriesName; |
| 330 |
} |
| 331 |
|
| 332 |
VCardPropertyScribe<? extends VCardProperty> scribe = index.getPropertyScribe(className); |
| 333 |
if (scribe == null) { |
| 334 |
|
| 335 |
if (!className.startsWith("x-")) { |
| 336 |
continue; |
| 337 |
} |
| 338 |
scribe = new RawPropertyScribe(className); |
| 339 |
} |
| 340 |
|
| 341 |
context.getWarnings().clear(); |
| 342 |
context.setPropertyName(scribe.getPropertyName()); |
| 343 |
|
| 344 |
VCardProperty property; |
| 345 |
try { |
| 346 |
property = scribe.parseHtml(new HCardElement(element), context); |
| 347 |
warnings.addAll(context.getWarnings()); |
| 348 |
|
| 349 |
|
| 350 |
if (property instanceof Label) { |
| 351 |
labels.add((Label) property); |
| 352 |
continue; |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
if (property instanceof Nickname) { |
| 357 |
Nickname nn = (Nickname) property; |
| 358 |
if (nickname == null) { |
| 359 |
nickname = nn; |
| 360 |
vcard.addProperty(nickname); |
| 361 |
} else { |
| 362 |
nickname.getValues().addAll(nn.getValues()); |
| 363 |
} |
| 364 |
continue; |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
if (property instanceof Categories) { |
| 369 |
Categories c = (Categories) property; |
| 370 |
if (categories == null) { |
| 371 |
categories = c; |
| 372 |
vcard.addProperty(categories); |
| 373 |
} else { |
| 374 |
categories.getValues().addAll(c.getValues()); |
| 375 |
} |
| 376 |
continue; |
| 377 |
} |
| 378 |
} catch (SkipMeException e) { |
| 379 |
|
| 380 |
warnings.add(new ParseWarning.Builder(context) |
| 381 |
.message(22, e.getMessage()) |
| 382 |
.build() |
| 383 |
); |
| 384 |
|
| 385 |
continue; |
| 386 |
} catch (CannotParseException e) { |
| 387 |
|
| 388 |
warnings.add(new ParseWarning.Builder(context) |
| 389 |
.message(e) |
| 390 |
.build() |
| 391 |
); |
| 392 |
|
| 393 |
|
| 394 |
property = new RawProperty(className, element.outerHtml()); |
| 395 |
} catch (EmbeddedVCardException e) { |
| 396 |
if (isChildOf(element, embeddedVCards)) { |
| 397 |
|
| 398 |
continue; |
| 399 |
} |
| 400 |
|
| 401 |
property = e.getProperty(); |
| 402 |
|
| 403 |
embeddedVCards.add(element); |
| 404 |
HCardParser embeddedReader = new HCardParser(element, pageUrl); |
| 405 |
try { |
| 406 |
VCard embeddedVCard = embeddedReader.readNext(); |
| 407 |
e.injectVCard(embeddedVCard); |
| 408 |
} finally { |
| 409 |
warnings.addAll(embeddedReader.getWarnings()); |
| 410 |
IOUtils.closeQuietly(embeddedReader); |
| 411 |
} |
| 412 |
visitChildren = false; |
| 413 |
} |
| 414 |
|
| 415 |
vcard.addProperty(property); |
| 416 |
} |
| 417 |
|
| 418 |
if (visitChildren) { |
| 419 |
for (Element child : element.children()) { |
| 420 |
visit(child); |
| 421 |
} |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
public void close() { |
| 426 |
|
| 427 |
} |
| 428 |
} |