Search in sources :

Example 1 with VCardProperty

use of ezvcard.property.VCardProperty in project ez-vcard by mangstadt.

the class JCardWriter method _write.

/**
 * Writes a vCard to the stream.
 * @param vcard the vCard that is being written
 * @param properties the properties to write
 * @throws IOException if there's a problem writing to the output stream
 * @throws IllegalArgumentException if a scribe hasn't been registered for a
 * custom property class (see: {@link #registerScribe registerScribe})
 */
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void _write(VCard vcard, List<VCardProperty> properties) throws IOException {
    Object previousValue = getCurrentValue();
    writer.writeStartVCard();
    writer.writeProperty("version", VCardDataType.TEXT, JCardValue.single(targetVersion.getVersion()));
    for (VCardProperty property : properties) {
        VCardPropertyScribe scribe = index.getPropertyScribe(property);
        // marshal the value
        JCardValue value;
        try {
            value = scribe.writeJson(property);
        } catch (SkipMeException e) {
            // property has requested not to be written
            continue;
        } catch (EmbeddedVCardException e) {
            // don't write because jCard does not support embedded vCards
            continue;
        }
        String group = property.getGroup();
        String name = scribe.getPropertyName().toLowerCase();
        VCardParameters parameters = scribe.prepareParameters(property, targetVersion, vcard);
        VCardDataType dataType = scribe.dataType(property, targetVersion);
        writer.writeProperty(group, name, parameters, dataType, value);
    }
    writer.writeEndVCard();
    setCurrentValue(previousValue);
}
Also used : VCardPropertyScribe(ezvcard.io.scribe.VCardPropertyScribe) VCardParameters(ezvcard.parameter.VCardParameters) EmbeddedVCardException(ezvcard.io.EmbeddedVCardException) VCardProperty(ezvcard.property.VCardProperty) SkipMeException(ezvcard.io.SkipMeException) VCardDataType(ezvcard.VCardDataType)

Example 2 with VCardProperty

use of ezvcard.property.VCardProperty in project ez-vcard by mangstadt.

the class VCard method equals.

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    VCard other = (VCard) obj;
    if (version != other.version)
        return false;
    if (properties.size() != other.properties.size())
        return false;
    for (Map.Entry<Class<? extends VCardProperty>, List<VCardProperty>> entry : properties) {
        Class<? extends VCardProperty> key = entry.getKey();
        List<VCardProperty> value = entry.getValue();
        List<VCardProperty> otherValue = other.properties.get(key);
        if (value.size() != otherValue.size()) {
            return false;
        }
        List<VCardProperty> otherValueCopy = new ArrayList<VCardProperty>(otherValue);
        for (VCardProperty property : value) {
            if (!otherValueCopy.remove(property)) {
                return false;
            }
        }
    }
    return true;
}
Also used : ArrayList(java.util.ArrayList) VCardProperty(ezvcard.property.VCardProperty) AbstractList(java.util.AbstractList) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) ClientPidMap(ezvcard.property.ClientPidMap)

Example 3 with VCardProperty

use of ezvcard.property.VCardProperty in project ez-vcard by mangstadt.

the class ValidationWarnings method getByProperty.

/**
 * Gets all validation warnings that belong to a property of a specific
 * class.
 * @param propertyClass the property class (e.g. {@code Telephone.class}) or
 * null to get the warnings that apply to the vCard as a whole
 * @return the validation warnings
 */
public List<ValidationWarning> getByProperty(Class<? extends VCardProperty> propertyClass) {
    List<ValidationWarning> propWarnings = new ArrayList<ValidationWarning>();
    for (Map.Entry<VCardProperty, List<ValidationWarning>> entry : warnings) {
        VCardProperty property = entry.getKey();
        if ((property == null && propertyClass == null) || (property != null && propertyClass == property.getClass())) {
            List<ValidationWarning> propViolations = entry.getValue();
            propWarnings.addAll(propViolations);
        }
    }
    return propWarnings;
}
Also used : ArrayList(java.util.ArrayList) VCardProperty(ezvcard.property.VCardProperty) List(java.util.List) ArrayList(java.util.ArrayList) IdentityHashMap(java.util.IdentityHashMap) Map(java.util.Map)

Example 4 with VCardProperty

use of ezvcard.property.VCardProperty in project ez-vcard by mangstadt.

the class ValidationWarnings method toString.

/**
 * <p>
 * Outputs all validation warnings as a newline-delimited string. For
 * example:
 * </p>
 *
 * <pre>
 * W01: A FormattedName property is required for vCard versions 3.0 and 4.0.
 * [Gender] | W02: Property is not supported in this vCard version.  Supported versions are: [4.0]
 * </pre>
 */
@Override
public String toString() {
    NumberFormat nf = NumberFormat.getIntegerInstance();
    nf.setMinimumIntegerDigits(2);
    StringBuilder sb = new StringBuilder();
    for (Map.Entry<VCardProperty, List<ValidationWarning>> entry : warnings) {
        VCardProperty property = entry.getKey();
        List<ValidationWarning> propViolations = entry.getValue();
        for (ValidationWarning propViolation : propViolations) {
            if (property != null) {
                sb.append('[');
                sb.append(property.getClass().getSimpleName());
                sb.append("] | ");
            }
            Integer code = propViolation.getCode();
            if (code != null) {
                sb.append('W');
                sb.append(nf.format(code));
                sb.append(": ");
            }
            sb.append(propViolation.getMessage());
            sb.append(StringUtils.NEWLINE);
        }
    }
    return sb.toString();
}
Also used : VCardProperty(ezvcard.property.VCardProperty) List(java.util.List) ArrayList(java.util.ArrayList) IdentityHashMap(java.util.IdentityHashMap) Map(java.util.Map) NumberFormat(java.text.NumberFormat)

Example 5 with VCardProperty

use of ezvcard.property.VCardProperty 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)

Aggregations

VCardProperty (ezvcard.property.VCardProperty)13 VCardVersion (ezvcard.VCardVersion)4 VCardPropertyScribe (ezvcard.io.scribe.VCardPropertyScribe)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Map (java.util.Map)4 VCard (ezvcard.VCard)3 EmbeddedVCardException (ezvcard.io.EmbeddedVCardException)3 SkipMeException (ezvcard.io.SkipMeException)3 VCardParameters (ezvcard.parameter.VCardParameters)3 RawProperty (ezvcard.property.RawProperty)3 VCardDataType (ezvcard.VCardDataType)2 Label (ezvcard.property.Label)2 ProductId (ezvcard.property.ProductId)2 IdentityHashMap (java.util.IdentityHashMap)2 Test (org.junit.Test)2 VObjectParameters (com.github.mangstadt.vinnie.VObjectParameters)1 CannotParseException (ezvcard.io.CannotParseException)1 ParseContext (ezvcard.io.ParseContext)1 RawPropertyScribe (ezvcard.io.scribe.RawPropertyScribe)1