Search in sources :

Example 6 with VCardDataType

use of ezvcard.VCardDataType 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 7 with VCardDataType

use of ezvcard.VCardDataType in project ez-vcard by mangstadt.

the class VCardParameters method validate.

/**
 * <p>
 * Checks the parameters for data consistency problems or deviations from
 * the specification.
 * </p>
 * <p>
 * These problems will not prevent the vCard from being written to a data
 * stream*, but may prevent it from being parsed correctly by the consuming
 * application.
 * </p>
 * <p>
 * *With a few exceptions: One thing this method does is check for illegal
 * characters. There are certain characters that will break the vCard syntax
 * if written (such as a newline character in a parameter name). If one of
 * these characters is present, it WILL prevent the vCard from being
 * written.
 * </p>
 * @param version the vCard version to validate against
 * @return a list of warnings or an empty list if no problems were found
 */
public List<ValidationWarning> validate(VCardVersion version) {
    List<ValidationWarning> warnings = new ArrayList<ValidationWarning>(0);
    /*
		 * Check for invalid characters in names and values.
		 */
    SyntaxStyle syntax = version.getSyntaxStyle();
    for (Map.Entry<String, List<String>> entry : this) {
        String name = entry.getKey();
        /*
			 * Don't check LABEL parameter for 2.1 and 3.0 because this
			 * parameter is converted to a property in those versions.
			 */
        if (version != VCardVersion.V4_0 && LABEL.equalsIgnoreCase(name)) {
            continue;
        }
        // check the parameter name
        if (!VObjectValidator.validateParameterName(name, syntax, true)) {
            if (syntax == SyntaxStyle.OLD) {
                AllowedCharacters notAllowed = VObjectValidator.allowedCharactersParameterName(syntax, true).flip();
                warnings.add(new ValidationWarning(30, name, notAllowed.toString(true)));
            } else {
                warnings.add(new ValidationWarning(26, name));
            }
        }
        // check the parameter value(s)
        List<String> values = entry.getValue();
        for (String value : values) {
            /*
				 * Newlines are allowed in LABEL parameters, but are not allowed
				 * by vobject, so remove them from the value before validating.
				 */
            if (LABEL.equalsIgnoreCase(name)) {
                value = value.replaceAll("\r\n|\r|\n", "");
            }
            if (!VObjectValidator.validateParameterValue(value, syntax, false, true)) {
                AllowedCharacters notAllowed = VObjectValidator.allowedCharactersParameterValue(syntax, false, true).flip();
                int code = (syntax == SyntaxStyle.OLD) ? 31 : 25;
                warnings.add(new ValidationWarning(code, name, value, notAllowed.toString(true)));
            }
        }
    }
    /*
		 * Check for invalid or unsupported values (e.g. "ENCODING=foo").
		 */
    {
        final int nonStandardValueCode = 3;
        final int unsupportedValueCode = 4;
        String value = first(CALSCALE);
        if (value != null && Calscale.find(value) == null) {
            warnings.add(new ValidationWarning(nonStandardValueCode, CALSCALE, value, Calscale.all()));
        }
        value = first(ENCODING);
        if (value != null) {
            Encoding encoding = Encoding.find(value);
            if (encoding == null) {
                warnings.add(new ValidationWarning(nonStandardValueCode, ENCODING, value, Encoding.all()));
            } else if (!encoding.isSupportedBy(version)) {
                warnings.add(new ValidationWarning(unsupportedValueCode, ENCODING, value));
            }
        }
        value = first(VALUE);
        if (value != null) {
            VCardDataType dataType = VCardDataType.find(value);
            if (dataType == null) {
                warnings.add(new ValidationWarning(nonStandardValueCode, VALUE, value, VCardDataType.all()));
            } else if (!dataType.isSupportedBy(version)) {
                warnings.add(new ValidationWarning(unsupportedValueCode, VALUE, value));
            }
        }
    }
    /*
		 * Check for parameters with malformed values.
		 */
    {
        final int malformedValueCode = 5;
        try {
            getGeo();
        } catch (IllegalStateException e) {
            warnings.add(new ValidationWarning(malformedValueCode, GEO, first(GEO)));
        }
        try {
            Integer index = getIndex();
            if (index != null && index <= 0) {
                warnings.add(new ValidationWarning(28, index));
            }
        } catch (IllegalStateException e) {
            warnings.add(new ValidationWarning(malformedValueCode, INDEX, first(INDEX)));
        }
        List<String> pids = get(PID);
        for (String pid : pids) {
            if (!isPidValid(pid)) {
                warnings.add(new ValidationWarning(27, pid));
            }
        }
        try {
            Integer pref = getPref();
            if (pref != null && (pref < 1 || pref > 100)) {
                warnings.add(new ValidationWarning(29, pref));
            }
        } catch (IllegalStateException e) {
            warnings.add(new ValidationWarning(malformedValueCode, PREF, first(PREF)));
        }
    }
    /*
		 * Check that each parameter is supported by the given vCard version.
		 */
    {
        final int paramNotSupportedCode = 6;
        for (Map.Entry<String, Set<VCardVersion>> entry : supportedVersions.entrySet()) {
            String name = entry.getKey();
            String value = first(name);
            if (value == null) {
                continue;
            }
            Set<VCardVersion> versions = entry.getValue();
            if (!versions.contains(version)) {
                warnings.add(new ValidationWarning(paramNotSupportedCode, name));
            }
        }
    }
    /*
		 * Check that the CHARSET parameter has a character set that is
		 * supported by this JVM.
		 */
    {
        final int invalidCharsetCode = 22;
        String charsetStr = getCharset();
        if (charsetStr != null) {
            try {
                Charset.forName(charsetStr);
            } catch (IllegalCharsetNameException e) {
                warnings.add(new ValidationWarning(invalidCharsetCode, charsetStr));
            } catch (UnsupportedCharsetException e) {
                warnings.add(new ValidationWarning(invalidCharsetCode, charsetStr));
            }
        }
    }
    return warnings;
}
Also used : EnumSet(java.util.EnumSet) Set(java.util.Set) ArrayList(java.util.ArrayList) VCardVersion(ezvcard.VCardVersion) ValidationWarning(ezvcard.ValidationWarning) SortString(ezvcard.property.SortString) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) AllowedCharacters(com.github.mangstadt.vinnie.validate.AllowedCharacters) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) SyntaxStyle(com.github.mangstadt.vinnie.SyntaxStyle) AbstractList(java.util.AbstractList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) ClientPidMap(ezvcard.property.ClientPidMap) Map(java.util.Map) VCardDataType(ezvcard.VCardDataType)

Example 8 with VCardDataType

use of ezvcard.VCardDataType in project ez-vcard by mangstadt.

the class XCardElement method firstValue.

/**
 * Finds the first child element that has the xCard namespace and returns
 * its data type and value. If no such element is found, the parent
 * {@link XCardElement}'s text content, along with a null data type, is
 * returned.
 * @return the value and data type
 */
public XCardValue firstValue() {
    String elementNamespace = version.getXmlNamespace();
    for (Element child : children()) {
        String childNamespace = child.getNamespaceURI();
        if (elementNamespace.equals(childNamespace)) {
            VCardDataType dataType = toDataType(child.getLocalName());
            String value = child.getTextContent();
            return new XCardValue(dataType, value);
        }
    }
    return new XCardValue(null, element.getTextContent());
}
Also used : Element(org.w3c.dom.Element) VCardDataType(ezvcard.VCardDataType)

Example 9 with VCardDataType

use of ezvcard.VCardDataType in project ez-vcard by mangstadt.

the class XCardWriter method write.

private void write(VCardParameters parameters) throws SAXException {
    if (parameters.isEmpty()) {
        return;
    }
    start(PARAMETERS);
    for (Map.Entry<String, List<String>> parameter : parameters) {
        String parameterName = parameter.getKey().toLowerCase();
        start(parameterName);
        for (String parameterValue : parameter.getValue()) {
            VCardDataType dataType = parameterDataTypes.get(parameterName);
            String dataTypeElementName = (dataType == null) ? "unknown" : dataType.getName().toLowerCase();
            start(dataTypeElementName);
            text(parameterValue);
            end(dataTypeElementName);
        }
        end(parameterName);
    }
    end(PARAMETERS);
}
Also used : NodeList(org.w3c.dom.NodeList) List(java.util.List) Map(java.util.Map) NamedNodeMap(org.w3c.dom.NamedNodeMap) VCardDataType(ezvcard.VCardDataType)

Example 10 with VCardDataType

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

Aggregations

VCardDataType (ezvcard.VCardDataType)14 VCardParameters (ezvcard.parameter.VCardParameters)3 Map (java.util.Map)3 VCard (ezvcard.VCard)2 VCardVersion (ezvcard.VCardVersion)2 VCardPropertyScribe (ezvcard.io.scribe.VCardPropertyScribe)2 XCardValue (ezvcard.io.xml.XCardElement.XCardValue)2 VCardProperty (ezvcard.property.VCardProperty)2 HashMap (java.util.HashMap)2 List (java.util.List)2 SyntaxStyle (com.github.mangstadt.vinnie.SyntaxStyle)1 AllowedCharacters (com.github.mangstadt.vinnie.validate.AllowedCharacters)1 ValidationWarning (ezvcard.ValidationWarning)1 EmbeddedVCardException (ezvcard.io.EmbeddedVCardException)1 ParseContext (ezvcard.io.ParseContext)1 SkipMeException (ezvcard.io.SkipMeException)1 WriteContext (ezvcard.io.text.WriteContext)1 XCardDocument (ezvcard.io.xml.XCardDocument)1 XCardDocumentStreamWriter (ezvcard.io.xml.XCardDocument.XCardDocumentStreamWriter)1 ClientPidMap (ezvcard.property.ClientPidMap)1