2007-01-31 18:11:29 +01:00
|
|
|
/* CardVersitRenderer.m - this file is part of SOPE
|
|
|
|
*
|
2008-09-27 03:13:41 +02:00
|
|
|
* Copyright (C) 2006 Inverse inc.
|
2007-01-31 18:11:29 +01:00
|
|
|
*
|
|
|
|
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
|
|
|
|
*
|
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import <Foundation/NSArray.h>
|
2008-07-04 18:06:09 +02:00
|
|
|
#import <Foundation/NSEnumerator.h>
|
2007-01-31 18:11:29 +01:00
|
|
|
#import <Foundation/NSDictionary.h>
|
|
|
|
#import <Foundation/NSString.h>
|
|
|
|
|
|
|
|
#import <NGExtensions/NSObject+Logs.h>
|
|
|
|
|
|
|
|
#import "CardElement.h"
|
|
|
|
#import "CardGroup.h"
|
|
|
|
|
|
|
|
#import "NSString+NGCards.h"
|
|
|
|
#import "NSArray+NGCards.h"
|
|
|
|
|
|
|
|
#import "CardVersitRenderer.h"
|
|
|
|
|
|
|
|
@interface CardVersitRenderer (PrivateAPI)
|
|
|
|
|
|
|
|
- (NSString *) renderElement: (CardElement *) anElement;
|
|
|
|
- (NSString *) renderGroup: (CardGroup *) aGroup;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation CardVersitRenderer
|
|
|
|
|
|
|
|
- (NSString *) render: (id) anElement
|
|
|
|
{
|
|
|
|
return (([anElement isKindOfClass: [CardGroup class]])
|
|
|
|
? [self renderGroup: anElement]
|
|
|
|
: [self renderElement: anElement]);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) renderElement: (CardElement *) anElement
|
|
|
|
{
|
|
|
|
NSMutableString *rendering;
|
|
|
|
NSDictionary *attributes;
|
|
|
|
NSEnumerator *keys;
|
|
|
|
NSArray *values, *renderedAttrs;
|
|
|
|
NSString *key, *finalRendering, *tag;
|
|
|
|
|
|
|
|
if (![anElement isVoid])
|
|
|
|
{
|
2009-10-13 16:10:17 +02:00
|
|
|
rendering = [NSMutableString string];
|
2007-01-31 18:11:29 +01:00
|
|
|
if ([anElement group])
|
|
|
|
[rendering appendFormat: @"%@.", [anElement group]];
|
|
|
|
tag = [anElement tag];
|
|
|
|
if (!(tag && [tag length]))
|
|
|
|
{
|
|
|
|
tag = @"<no-tag>";
|
|
|
|
[self warnWithFormat: @"card element of class '%@' has an empty tag",
|
|
|
|
NSStringFromClass([anElement class])];
|
|
|
|
}
|
|
|
|
|
|
|
|
[rendering appendString: [tag uppercaseString]];
|
|
|
|
attributes = [anElement attributes];
|
|
|
|
keys = [[attributes allKeys] objectEnumerator];
|
2009-08-27 18:09:26 +02:00
|
|
|
while ((key = [keys nextObject]))
|
2007-01-31 18:11:29 +01:00
|
|
|
{
|
2008-11-13 17:03:41 +01:00
|
|
|
NSString *s;
|
|
|
|
int i, c;
|
|
|
|
|
2007-01-31 18:11:29 +01:00
|
|
|
renderedAttrs = [[attributes objectForKey: key] renderedForCards];
|
2008-11-13 17:03:41 +01:00
|
|
|
c = [renderedAttrs count];
|
2009-08-27 18:09:26 +02:00
|
|
|
if (c > 0)
|
|
|
|
{
|
|
|
|
[rendering appendFormat: @";%@=", [key uppercaseString]];
|
2008-11-13 17:03:41 +01:00
|
|
|
|
2009-08-27 18:09:26 +02:00
|
|
|
for (i = 0; i < c; i++)
|
|
|
|
{
|
|
|
|
s = [renderedAttrs objectAtIndex: i];
|
2008-11-13 17:03:41 +01:00
|
|
|
|
2009-08-27 18:09:26 +02:00
|
|
|
/* We MUST quote attribute values that have a ":" in them
|
|
|
|
and that not already quoted */
|
|
|
|
if ([s length] > 2 && [s rangeOfString: @":"].length &&
|
|
|
|
[s characterAtIndex: 0] != '"' && ![s hasSuffix: @"\""])
|
|
|
|
s = [NSString stringWithFormat: @"\"%@\"", s];
|
2008-11-13 17:03:41 +01:00
|
|
|
|
2009-08-27 18:09:26 +02:00
|
|
|
[rendering appendFormat: @"%@", s];
|
2008-11-13 17:03:41 +01:00
|
|
|
|
2009-08-27 18:09:26 +02:00
|
|
|
if (i+1 < c)
|
|
|
|
[rendering appendString: @","];
|
|
|
|
}
|
|
|
|
}
|
2007-01-31 18:11:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
values = [anElement values];
|
|
|
|
if ([values count] > 0)
|
|
|
|
[rendering appendFormat: @":%@",
|
|
|
|
[[values renderedForCards] componentsJoinedByString: @";"]];
|
|
|
|
|
|
|
|
if ([rendering length] > 0)
|
|
|
|
[rendering appendString: @"\r\n"];
|
|
|
|
|
|
|
|
finalRendering = [rendering foldedForVersitCards];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
finalRendering = @"";
|
|
|
|
|
|
|
|
return finalRendering;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) renderGroup: (CardGroup *) aGroup
|
|
|
|
{
|
|
|
|
NSEnumerator *children;
|
|
|
|
CardElement *currentChild;
|
|
|
|
NSMutableString *rendering;
|
|
|
|
NSString *groupTag;
|
|
|
|
|
2009-10-13 16:10:17 +02:00
|
|
|
rendering = [NSMutableString string];
|
2007-01-31 18:11:29 +01:00
|
|
|
|
|
|
|
groupTag = [aGroup tag];
|
|
|
|
if (!(groupTag && [groupTag length]))
|
|
|
|
{
|
|
|
|
groupTag = @"<no-tag>";
|
|
|
|
[self warnWithFormat: @"card group of class '%@' has an empty tag",
|
|
|
|
NSStringFromClass([aGroup class])];
|
|
|
|
}
|
|
|
|
|
|
|
|
groupTag = [groupTag uppercaseString];
|
|
|
|
[rendering appendFormat: @"BEGIN:%@\r\n", groupTag];
|
|
|
|
children = [[aGroup children] objectEnumerator];
|
|
|
|
currentChild = [children nextObject];
|
|
|
|
while (currentChild)
|
|
|
|
{
|
|
|
|
[rendering appendString: [self render: currentChild]];
|
|
|
|
currentChild = [children nextObject];
|
|
|
|
}
|
|
|
|
[rendering appendFormat: @"END:%@\r\n", groupTag];
|
|
|
|
|
|
|
|
return rendering;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|