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);
}
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;
}
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());
}
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);
}
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;
}
Aggregations