Search in sources :

Example 1 with CannotParseException

use of ezvcard.io.CannotParseException in project ez-vcard by mangstadt.

the class GeoScribe method _parseHtml.

@Override
protected Geo _parseHtml(HCardElement element, ParseContext context) {
    String latitudeStr = element.firstValue("latitude");
    if (latitudeStr == null) {
        throw new CannotParseException(7);
    }
    Double latitude;
    try {
        latitude = Double.parseDouble(latitudeStr);
    } catch (NumberFormatException e) {
        throw new CannotParseException(8, latitudeStr);
    }
    String longitudeStr = element.firstValue("longitude");
    if (longitudeStr == null) {
        throw new CannotParseException(9);
    }
    Double longitude;
    try {
        longitude = Double.parseDouble(longitudeStr);
    } catch (NumberFormatException e) {
        throw new CannotParseException(10, longitudeStr);
    }
    return new Geo(latitude, longitude);
}
Also used : Geo(ezvcard.property.Geo) CannotParseException(ezvcard.io.CannotParseException)

Example 2 with CannotParseException

use of ezvcard.io.CannotParseException in project ez-vcard by mangstadt.

the class SoundScribe method _parseHtml.

@Override
protected Sound _parseHtml(HCardElement element, ParseContext context) {
    String elementName = element.tagName();
    if (!"audio".equals(elementName) && !"source".equals(elementName)) {
        return super._parseHtml(element, context);
    }
    if ("audio".equals(elementName)) {
        // parse its child "<source>" element
        org.jsoup.nodes.Element source = element.getElement().getElementsByTag("source").first();
        if (source == null) {
            throw new CannotParseException(16);
        }
        element = new HCardElement(source);
    }
    String src = element.absUrl("src");
    if (src.length() == 0) {
        throw new CannotParseException(17);
    }
    String type = element.attr("type");
    SoundType mediaType = (type.length() == 0) ? null : _mediaTypeFromMediaTypeParameter(type);
    try {
        DataUri uri = DataUri.parse(src);
        mediaType = _mediaTypeFromMediaTypeParameter(uri.getContentType());
        return new Sound(uri.getData(), mediaType);
    } catch (IllegalArgumentException e) {
        // not a data URI
        if (mediaType == null) {
            String extension = getFileExtension(src);
            mediaType = (extension == null) ? null : _mediaTypeFromFileExtension(extension);
        }
        return new Sound(src, mediaType);
    }
}
Also used : SoundType(ezvcard.parameter.SoundType) CannotParseException(ezvcard.io.CannotParseException) Sound(ezvcard.property.Sound) HCardElement(ezvcard.io.html.HCardElement) DataUri(ezvcard.util.DataUri)

Example 3 with CannotParseException

use of ezvcard.io.CannotParseException in project ez-vcard by mangstadt.

the class ImagePropertyScribe method _parseHtml.

@Override
protected T _parseHtml(HCardElement element, ParseContext context) {
    String elementName = element.tagName();
    if (!"img".equals(elementName)) {
        return super._parseHtml(element, context);
    }
    String src = element.absUrl("src");
    if (src.length() == 0) {
        throw new CannotParseException(13);
    }
    try {
        DataUri uri = DataUri.parse(src);
        ImageType mediaType = _mediaTypeFromMediaTypeParameter(uri.getContentType());
        return _newInstance(uri.getData(), mediaType);
    } catch (IllegalArgumentException e) {
        // not a data URI
        String extension = getFileExtension(src);
        ImageType mediaType = (extension == null) ? null : _mediaTypeFromFileExtension(extension);
        return _newInstance(src, mediaType);
    }
}
Also used : CannotParseException(ezvcard.io.CannotParseException) DataUri(ezvcard.util.DataUri) ImageType(ezvcard.parameter.ImageType)

Example 4 with CannotParseException

use of ezvcard.io.CannotParseException in project ez-vcard by mangstadt.

the class HCardParser method visit.

private void visit(Element element) {
    boolean visitChildren = true;
    Set<String> classNames = element.classNames();
    for (String className : classNames) {
        className = className.toLowerCase();
        // give special treatment to certain URLs
        if (urlPropertyName.equals(className)) {
            String href = element.attr("href");
            if (href.length() > 0) {
                if (!classNames.contains(emailName) && href.matches("(?i)mailto:.*")) {
                    className = emailName;
                } else if (!classNames.contains(telName) && href.matches("(?i)tel:.*")) {
                    className = telName;
                } else {
                    // try parsing as IMPP
                    VCardPropertyScribe<? extends VCardProperty> scribe = index.getPropertyScribe(Impp.class);
                    context.getWarnings().clear();
                    context.setPropertyName(scribe.getPropertyName());
                    try {
                        VCardProperty property = scribe.parseHtml(new HCardElement(element), context);
                        vcard.addProperty(property);
                        warnings.addAll(context.getWarnings());
                        continue;
                    } catch (SkipMeException e) {
                    // URL is not an instant messenger URL
                    } catch (CannotParseException e) {
                    // URL is not an instant messenger URL
                    }
                }
            }
        }
        // hCard uses a different name for the CATEGORIES property
        if ("category".equals(className)) {
            className = categoriesName;
        }
        VCardPropertyScribe<? extends VCardProperty> scribe = index.getPropertyScribe(className);
        if (scribe == null) {
            // if no scribe is found, and the class name doesn't start with "x-", then it must be an arbitrary CSS class that has nothing to do with vCard
            if (!className.startsWith("x-")) {
                continue;
            }
            scribe = new RawPropertyScribe(className);
        }
        context.getWarnings().clear();
        context.setPropertyName(scribe.getPropertyName());
        VCardProperty property;
        try {
            property = scribe.parseHtml(new HCardElement(element), context);
            warnings.addAll(context.getWarnings());
            // LABELs must be treated specially so they can be matched up with their ADRs
            if (property instanceof Label) {
                labels.add((Label) property);
                continue;
            }
            // add all NICKNAMEs to the same type object
            if (property instanceof Nickname) {
                Nickname nn = (Nickname) property;
                if (nickname == null) {
                    nickname = nn;
                    vcard.addProperty(nickname);
                } else {
                    nickname.getValues().addAll(nn.getValues());
                }
                continue;
            }
            // add all CATEGORIES to the same type object
            if (property instanceof Categories) {
                Categories c = (Categories) property;
                if (categories == null) {
                    categories = c;
                    vcard.addProperty(categories);
                } else {
                    categories.getValues().addAll(c.getValues());
                }
                continue;
            }
        } catch (SkipMeException e) {
            // @formatter:off
            warnings.add(new ParseWarning.Builder(context).message(22, e.getMessage()).build());
            // @formatter:on
            continue;
        } catch (CannotParseException e) {
            // @formatter:off
            warnings.add(new ParseWarning.Builder(context).message(e).build());
            // @formatter:on
            property = new RawProperty(className, element.outerHtml());
        } catch (EmbeddedVCardException e) {
            if (isChildOf(element, embeddedVCards)) {
                // prevents multiple-nested embedded elements from overwriting each other
                continue;
            }
            property = e.getProperty();
            embeddedVCards.add(element);
            HCardParser embeddedReader = new HCardParser(element, pageUrl);
            try {
                VCard embeddedVCard = embeddedReader.readNext();
                e.injectVCard(embeddedVCard);
            } finally {
                warnings.addAll(embeddedReader.getWarnings());
                IOUtils.closeQuietly(embeddedReader);
            }
            visitChildren = false;
        }
        vcard.addProperty(property);
    }
    if (visitChildren) {
        for (Element child : element.children()) {
            visit(child);
        }
    }
}
Also used : VCardPropertyScribe(ezvcard.io.scribe.VCardPropertyScribe) EmbeddedVCardException(ezvcard.io.EmbeddedVCardException) Categories(ezvcard.property.Categories) Impp(ezvcard.property.Impp) RawPropertyScribe(ezvcard.io.scribe.RawPropertyScribe) Element(org.jsoup.nodes.Element) Label(ezvcard.property.Label) SkipMeException(ezvcard.io.SkipMeException) RawProperty(ezvcard.property.RawProperty) CannotParseException(ezvcard.io.CannotParseException) VCardProperty(ezvcard.property.VCardProperty) VCard(ezvcard.VCard) Nickname(ezvcard.property.Nickname)

Example 5 with CannotParseException

use of ezvcard.io.CannotParseException in project ez-vcard by mangstadt.

the class BinaryPropertyScribe method _parseHtml.

@Override
protected T _parseHtml(HCardElement element, ParseContext context) {
    String elementName = element.tagName();
    if (!"object".equals(elementName)) {
        throw new CannotParseException(1, elementName);
    }
    String data = element.absUrl("data");
    if (data.length() == 0) {
        throw new CannotParseException(2);
    }
    try {
        DataUri uri = DataUri.parse(data);
        U mediaType = _mediaTypeFromMediaTypeParameter(uri.getContentType());
        return _newInstance(uri.getData(), mediaType);
    } catch (IllegalArgumentException e) {
        // not a data URI
        U mediaType = null;
        String type = element.attr("type");
        if (type.length() > 0) {
            mediaType = _mediaTypeFromMediaTypeParameter(type);
        } else {
            String extension = getFileExtension(data);
            mediaType = (extension == null) ? null : _mediaTypeFromFileExtension(extension);
        }
        return _newInstance(data, mediaType);
    }
}
Also used : CannotParseException(ezvcard.io.CannotParseException) DataUri(ezvcard.util.DataUri)

Aggregations

CannotParseException (ezvcard.io.CannotParseException)8 DataUri (ezvcard.util.DataUri)3 Geo (ezvcard.property.Geo)2 SemiStructuredValueIterator (com.github.mangstadt.vinnie.io.VObjectPropertyValues.SemiStructuredValueIterator)1 VCard (ezvcard.VCard)1 EmbeddedVCardException (ezvcard.io.EmbeddedVCardException)1 SkipMeException (ezvcard.io.SkipMeException)1 HCardElement (ezvcard.io.html.HCardElement)1 RawPropertyScribe (ezvcard.io.scribe.RawPropertyScribe)1 VCardPropertyScribe (ezvcard.io.scribe.VCardPropertyScribe)1 ImageType (ezvcard.parameter.ImageType)1 SoundType (ezvcard.parameter.SoundType)1 Categories (ezvcard.property.Categories)1 Impp (ezvcard.property.Impp)1 Label (ezvcard.property.Label)1 Nickname (ezvcard.property.Nickname)1 RawProperty (ezvcard.property.RawProperty)1 Sound (ezvcard.property.Sound)1 Timezone (ezvcard.property.Timezone)1 VCardProperty (ezvcard.property.VCardProperty)1