Search in sources :

Example 1 with VCardPropertyScribe

use of ezvcard.io.scribe.VCardPropertyScribe 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 VCardPropertyScribe

use of ezvcard.io.scribe.VCardPropertyScribe 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 3 with VCardPropertyScribe

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

the class VCardWriterTest method date_time_properties_should_not_have_a_VALUE_parameter.

@Test
public void date_time_properties_should_not_have_a_VALUE_parameter() throws Throwable {
    class DateTestScribe<T extends VCardProperty> extends VCardPropertyScribe<T> {

        private final VCardDataType dataType;

        public DateTestScribe(Class<T> clazz, String name, VCardDataType dataType) {
            super(clazz, name);
            this.dataType = dataType;
        }

        @Override
        protected VCardDataType _defaultDataType(VCardVersion version) {
            return VCardDataType.DATE_AND_OR_TIME;
        }

        @Override
        protected VCardDataType _dataType(T property, VCardVersion version) {
            return dataType;
        }

        @Override
        protected String _writeText(T property, WriteContext context) {
            return "value";
        }

        @Override
        protected T _parseText(String value, VCardDataType dataType, VCardParameters parameters, ParseContext context) {
            return null;
        }
    }
    class DateProperty extends VCardProperty {
        // empty
    }
    class DateTimeProperty extends VCardProperty {
        // empty
    }
    class TimeProperty extends VCardProperty {
        // empty
    }
    class DateAndOrTimeProperty extends VCardProperty {
        // empty
    }
    VCard vcard = new VCard();
    vcard.addProperty(new DateProperty());
    vcard.addProperty(new DateTimeProperty());
    vcard.addProperty(new TimeProperty());
    vcard.addProperty(new DateAndOrTimeProperty());
    StringWriter sw = new StringWriter();
    VCardWriter vcw = new VCardWriter(sw, VCardVersion.V4_0);
    vcw.registerScribe(new DateTestScribe<DateProperty>(DateProperty.class, "DATE", VCardDataType.DATE));
    vcw.registerScribe(new DateTestScribe<DateTimeProperty>(DateTimeProperty.class, "DATETIME", VCardDataType.DATE_TIME));
    vcw.registerScribe(new DateTestScribe<TimeProperty>(TimeProperty.class, "TIME", VCardDataType.TIME));
    vcw.registerScribe(new DateTestScribe<DateAndOrTimeProperty>(DateAndOrTimeProperty.class, "DATEANDORTIME", VCardDataType.DATE_AND_OR_TIME));
    vcw.setAddProdId(false);
    vcw.write(vcard);
    String actual = sw.toString();
    // @formatter:off
    String expected = "BEGIN:VCARD\r\n" + "VERSION:4.0\r\n" + "DATE:value\r\n" + "DATETIME:value\r\n" + "TIME:value\r\n" + "DATEANDORTIME:value\r\n" + "END:VCARD\r\n";
    // @formatter:on
    assertEquals(actual, expected);
}
Also used : VCardPropertyScribe(ezvcard.io.scribe.VCardPropertyScribe) VCardVersion(ezvcard.VCardVersion) VCardParameters(ezvcard.parameter.VCardParameters) StringWriter(java.io.StringWriter) ParseContext(ezvcard.io.ParseContext) VCardProperty(ezvcard.property.VCardProperty) VCardDataType(ezvcard.VCardDataType) VCard(ezvcard.VCard) Test(org.junit.Test)

Example 4 with VCardPropertyScribe

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

the class VCardWriter method _write.

@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void _write(VCard vcard, List<VCardProperty> propertiesToAdd) throws IOException {
    VCardVersion targetVersion = getTargetVersion();
    TargetApplication targetApplication = getTargetApplication();
    Boolean includeTrailingSemicolons = this.includeTrailingSemicolons;
    if (includeTrailingSemicolons == null) {
        includeTrailingSemicolons = (targetVersion == VCardVersion.V4_0);
    }
    WriteContext context = new WriteContext(targetVersion, targetApplication, includeTrailingSemicolons);
    writer.writeBeginComponent("VCARD");
    writer.writeVersion(targetVersion.getVersion());
    for (VCardProperty property : propertiesToAdd) {
        VCardPropertyScribe scribe = index.getPropertyScribe(property);
        String value = null;
        VCard nestedVCard = null;
        try {
            value = scribe.writeText(property, context);
        } catch (SkipMeException e) {
            continue;
        } catch (EmbeddedVCardException e) {
            nestedVCard = e.getVCard();
        }
        VCardParameters parameters = scribe.prepareParameters(property, targetVersion, vcard);
        if (nestedVCard != null) {
            writeNestedVCard(nestedVCard, property, scribe, parameters, value);
            continue;
        }
        handleValueParameter(property, scribe, parameters);
        handleLabelParameter(property, parameters);
        writer.writeProperty(property.getGroup(), scribe.getPropertyName(), new VObjectParameters(parameters.getMap()), value);
        fixBinaryPropertyForOutlook(property);
    }
    writer.writeEndComponent("VCARD");
}
Also used : VCardPropertyScribe(ezvcard.io.scribe.VCardPropertyScribe) VCardParameters(ezvcard.parameter.VCardParameters) EmbeddedVCardException(ezvcard.io.EmbeddedVCardException) VObjectParameters(com.github.mangstadt.vinnie.VObjectParameters) VCardProperty(ezvcard.property.VCardProperty) VCardVersion(ezvcard.VCardVersion) SkipMeException(ezvcard.io.SkipMeException) VCard(ezvcard.VCard)

Example 5 with VCardPropertyScribe

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

the class XCardWriter method write.

@SuppressWarnings({ "rawtypes", "unchecked" })
private void write(VCardProperty property, VCard vcard) throws SAXException {
    VCardPropertyScribe scribe = index.getPropertyScribe(property);
    VCardParameters parameters = scribe.prepareParameters(property, targetVersion, vcard);
    // get the property element to write
    Element propertyElement;
    if (property instanceof Xml) {
        Xml xml = (Xml) property;
        Document value = xml.getValue();
        if (value == null) {
            return;
        }
        propertyElement = value.getDocumentElement();
    } else {
        QName qname = scribe.getQName();
        propertyElement = DOC.createElementNS(qname.getNamespaceURI(), qname.getLocalPart());
        try {
            scribe.writeXml(property, propertyElement);
        } catch (SkipMeException e) {
            return;
        } catch (EmbeddedVCardException e) {
            return;
        }
    }
    start(propertyElement);
    write(parameters);
    write(propertyElement);
    end(propertyElement);
}
Also used : VCardPropertyScribe(ezvcard.io.scribe.VCardPropertyScribe) VCardParameters(ezvcard.parameter.VCardParameters) EmbeddedVCardException(ezvcard.io.EmbeddedVCardException) Xml(ezvcard.property.Xml) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) SkipMeException(ezvcard.io.SkipMeException) Document(org.w3c.dom.Document)

Aggregations

VCardPropertyScribe (ezvcard.io.scribe.VCardPropertyScribe)5 EmbeddedVCardException (ezvcard.io.EmbeddedVCardException)4 SkipMeException (ezvcard.io.SkipMeException)4 VCardParameters (ezvcard.parameter.VCardParameters)4 VCardProperty (ezvcard.property.VCardProperty)4 VCard (ezvcard.VCard)3 VCardDataType (ezvcard.VCardDataType)2 VCardVersion (ezvcard.VCardVersion)2 VObjectParameters (com.github.mangstadt.vinnie.VObjectParameters)1 CannotParseException (ezvcard.io.CannotParseException)1 ParseContext (ezvcard.io.ParseContext)1 RawPropertyScribe (ezvcard.io.scribe.RawPropertyScribe)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 Xml (ezvcard.property.Xml)1 StringWriter (java.io.StringWriter)1 QName (javax.xml.namespace.QName)1