Search in sources :

Example 11 with RawProperty

use of ezvcard.property.RawProperty in project qksms by moezbhatti.

the class ContactOperations method convertGroupedProperties.

private void convertGroupedProperties(List<NonEmptyContentValues> contentValues, VCard vcard) {
    List<RawProperty> extendedProperties = vcard.getExtendedProperties();
    Map<String, List<RawProperty>> orderedByGroup = orderPropertiesByGroup(extendedProperties);
    final int ABDATE = 1, ABRELATEDNAMES = 2;
    for (List<RawProperty> properties : orderedByGroup.values()) {
        if (properties.size() == 1) {
            continue;
        }
        String label = null;
        String val = null;
        int mime = 0;
        for (RawProperty property : properties) {
            String name = property.getPropertyName();
            if (name.equalsIgnoreCase("X-ABDATE")) {
                // date
                label = property.getValue();
                mime = ABDATE;
                continue;
            }
            if (name.equalsIgnoreCase("X-ABRELATEDNAMES")) {
                // name
                label = property.getValue();
                mime = ABRELATEDNAMES;
                continue;
            }
            if (name.equalsIgnoreCase("X-ABLABEL")) {
                // type of value ..Birthday,anniversary
                val = property.getValue();
                continue;
            }
        }
        NonEmptyContentValues cv = null;
        switch(mime) {
            case ABDATE:
                cv = new NonEmptyContentValues(ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE);
                cv.put(ContactsContract.CommonDataKinds.Event.START_DATE, label);
                int type = DataMappings.getDateType(val);
                cv.put(ContactsContract.CommonDataKinds.Event.TYPE, type);
                break;
            case ABRELATEDNAMES:
                if (val != null) {
                    cv = new NonEmptyContentValues(ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE);
                    cv.put(ContactsContract.CommonDataKinds.Nickname.NAME, label);
                    if (!val.equals("Nickname")) {
                        type = DataMappings.getNameType(val);
                        cv.put(ContactsContract.CommonDataKinds.Relation.TYPE, type);
                    }
                }
                break;
            default:
                continue;
        }
        contentValues.add(cv);
    }
}
Also used : RawProperty(ezvcard.property.RawProperty) ArrayList(java.util.ArrayList) List(java.util.List)

Example 12 with RawProperty

use of ezvcard.property.RawProperty in project android by nextcloud.

the class ContactOperations method convertIms.

private void convertIms(List<NonEmptyContentValues> contentValues, VCard vcard) {
    // handle extended properties
    for (Map.Entry<String, Integer> entry : DataMappings.getImPropertyNameMappings().entrySet()) {
        String propertyName = entry.getKey();
        Integer protocolType = entry.getValue();
        List<RawProperty> rawProperties = vcard.getExtendedProperties(propertyName);
        for (RawProperty rawProperty : rawProperties) {
            NonEmptyContentValues cv = new NonEmptyContentValues(ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE);
            String value = rawProperty.getValue();
            cv.put(ContactsContract.CommonDataKinds.Im.DATA, value);
            cv.put(ContactsContract.CommonDataKinds.Im.PROTOCOL, protocolType);
            contentValues.add(cv);
        }
    }
    // handle IMPP properties
    for (Impp impp : vcard.getImpps()) {
        NonEmptyContentValues cv = new NonEmptyContentValues(ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE);
        String immpAddress = impp.getHandle();
        cv.put(ContactsContract.CommonDataKinds.Im.DATA, immpAddress);
        int immpProtocolType = DataMappings.getIMTypeFromProtocol(impp.getProtocol());
        cv.put(ContactsContract.CommonDataKinds.Im.PROTOCOL, immpProtocolType);
        contentValues.add(cv);
    }
}
Also used : RawProperty(ezvcard.property.RawProperty) Impp(ezvcard.property.Impp) HashMap(java.util.HashMap) Map(java.util.Map)

Example 13 with RawProperty

use of ezvcard.property.RawProperty 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 14 with RawProperty

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

the class RawPropertyScribe method _parseXml.

@Override
protected RawProperty _parseXml(XCardElement element, VCardParameters parameters, ParseContext context) {
    XCardValue firstValue = element.firstValue();
    VCardDataType dataType = firstValue.getDataType();
    String value = firstValue.getValue();
    RawProperty property = new RawProperty(propertyName, value);
    property.setDataType(dataType);
    return property;
}
Also used : RawProperty(ezvcard.property.RawProperty) XCardValue(ezvcard.io.xml.XCardElement.XCardValue) VCardDataType(ezvcard.VCardDataType)

Example 15 with RawProperty

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

the class VCardReaderTest method extended_properties.

@Test
public void extended_properties() throws Exception {
    for (VCardVersion version : VCardVersion.values()) {
        // @formatter:off
        String str = "BEGIN:VCARD\r\n" + "VERSION:" + version + "\r\n" + "X-LUCKY-NUM:24\r\n" + "X-GENDER:ma\\,le\r\n" + "X-LUCKY-NUM:22\r\n" + "END:VCARD\r\n";
        // @formatter:on
        VCardReader reader = new VCardReader(str);
        reader.registerScribe(new LuckyNumScribe());
        VCard vcard = reader.readNext();
        assertVersion(version, vcard);
        assertPropertyCount(3, vcard);
        // read a type that has a type class
        List<LuckyNumProperty> luckyNumTypes = vcard.getProperties(LuckyNumProperty.class);
        assertEquals(2, luckyNumTypes.size());
        assertEquals(24, luckyNumTypes.get(0).luckyNum);
        assertEquals(22, luckyNumTypes.get(1).luckyNum);
        assertTrue(vcard.getExtendedProperties("X-LUCKY-NUM").isEmpty());
        // read a type without a type class
        List<RawProperty> genderTypes = vcard.getExtendedProperties("X-GENDER");
        assertEquals(1, genderTypes.size());
        // raw type values are not unescaped
        assertEquals("ma\\,le", genderTypes.get(0).getValue());
        assertParseWarnings(reader);
        assertNoMoreVCards(reader);
    }
}
Also used : RawProperty(ezvcard.property.RawProperty) LuckyNumScribe(ezvcard.io.LuckyNumProperty.LuckyNumScribe) VCardVersion(ezvcard.VCardVersion) LuckyNumProperty(ezvcard.io.LuckyNumProperty) VCard(ezvcard.VCard) Test(org.junit.Test)

Aggregations

RawProperty (ezvcard.property.RawProperty)22 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)4 VCard (ezvcard.VCard)3 VCardVersion (ezvcard.VCardVersion)3 FormattedName (ezvcard.property.FormattedName)3 Impp (ezvcard.property.Impp)3 VCardProperty (ezvcard.property.VCardProperty)3 LuckyNumProperty (ezvcard.io.LuckyNumProperty)2 LuckyNumScribe (ezvcard.io.LuckyNumProperty.LuckyNumScribe)2 Label (ezvcard.property.Label)2 ProductId (ezvcard.property.ProductId)2 StructuredName (ezvcard.property.StructuredName)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 VCardDataType (ezvcard.VCardDataType)1 AgeProperty (ezvcard.io.AgeProperty)1 AgeScribe (ezvcard.io.AgeProperty.AgeScribe)1 CannotParseException (ezvcard.io.CannotParseException)1