use of ezvcard.VCardVersion in project ez-vcard by mangstadt.
the class StreamWriterTest method productId.
@Test
public void productId() throws IOException {
assertNull(vcard.getProductId());
// default value
{
vcard.setProductId((String) null);
{
writer.write(vcard, V2_1);
assertEquals(1, writer.count());
assertEquals(1, writer.count(RawProperty.class));
for (VCardVersion version : each(V3_0, V4_0)) {
writer.write(vcard, version);
assertEquals(1, writer.count());
assertEquals(1, writer.count(ProductId.class));
}
}
// should be ignored
vcard.setProductId("value");
{
writer.write(vcard, V2_1);
assertEquals(1, writer.count());
assertEquals(1, writer.count(RawProperty.class));
for (VCardVersion version : each(V3_0, V4_0)) {
writer.write(vcard, version);
assertEquals(1, writer.count());
ProductId prodid = writer.first(ProductId.class);
assertNotEquals("value", prodid.getValue());
}
}
}
writer.setAddProdId(false);
{
vcard.setProductId((String) null);
{
for (VCardVersion version : VCardVersion.values()) {
writer.write(vcard, version);
assertEquals(0, writer.count());
}
}
vcard.setProductId("value");
{
writer.write(vcard, V2_1);
assertEquals(0, writer.count());
for (VCardVersion version : each(V3_0, V4_0)) {
writer.write(vcard, version);
ProductId prodId = writer.first(ProductId.class);
assertEquals("value", prodId.getValue());
}
}
}
writer.setAddProdId(true);
{
vcard.setProductId((String) null);
{
writer.write(vcard, V2_1);
assertEquals(1, writer.count());
assertEquals(1, writer.count(RawProperty.class));
for (VCardVersion version : each(V3_0, V4_0)) {
writer.write(vcard, version);
assertEquals(1, writer.count());
assertEquals(1, writer.count(ProductId.class));
}
}
// should be ignored
vcard.setProductId("value");
{
writer.write(vcard, V2_1);
assertEquals(1, writer.count());
assertEquals(1, writer.count(RawProperty.class));
for (VCardVersion version : each(V3_0, V4_0)) {
writer.write(vcard, version);
assertEquals(1, writer.count());
ProductId prodid = writer.first(ProductId.class);
assertNotEquals("value", prodid.getValue());
}
}
}
}
use of ezvcard.VCardVersion 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.VCardVersion in project ez-vcard by mangstadt.
the class ChainingTextWriter method go.
private void go(VCardWriter writer) throws IOException {
writer.setAddProdId(prodId);
writer.setCaretEncodingEnabled(caretEncoding);
writer.setVersionStrict(versionStrict);
writer.setIncludeTrailingSemicolons(includeTrailingSemicolons);
if (!foldLines) {
writer.getVObjectWriter().getFoldedLineWriter().setLineLength(null);
}
writer.setTargetApplication(targetApplication);
if (index != null) {
writer.setScribeIndex(index);
}
for (VCard vcard : vcards) {
if (version == null) {
// use the version that's assigned to each individual vCard
VCardVersion vcardVersion = vcard.getVersion();
if (vcardVersion == null) {
vcardVersion = VCardVersion.V3_0;
}
writer.setTargetVersion(vcardVersion);
}
writer.write(vcard);
writer.flush();
}
}
use of ezvcard.VCardVersion in project ez-vcard by mangstadt.
the class VCardPropertyScribeTest method prepareParameters.
@Test
public void prepareParameters() {
VCardPropertyScribeImpl m = new VCardPropertyScribeImpl() {
@Override
protected void _prepareParameters(TestProperty property, VCardParameters copy, VCardVersion version, VCard vcard) {
copy.put("PARAM", "value");
}
};
TestProperty property = new TestProperty("value");
VCardParameters copy = m.prepareParameters(property, V4_0, new VCard());
assertNotSame(property.getParameters(), copy);
assertEquals("value", copy.first("PARAM"));
}
use of ezvcard.VCardVersion in project ez-vcard by mangstadt.
the class VCardReaderTest method quoted_printable_encoding_invalid_value.
@Test
public void quoted_printable_encoding_invalid_value() throws Exception {
for (VCardVersion version : VCardVersion.values()) {
// @formatter:off
VCardAsserter asserter = read("BEGIN:VCARD\r\n" + "VERSION:" + version + "\r\n" + "PROP;ENCODING=QUOTED-PRINTABLE:=nnnn\r\n" + "END:VCARD\r\n");
asserter.next(version);
asserter.rawProperty("PROP").param("ENCODING", "QUOTED-PRINTABLE").value("=nnnn").noMore();
asserter.warnings(27);
asserter.done();
// @formatter:on
}
}
Aggregations