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"
|
2011-11-14 17:07:04 +01:00
|
|
|
#import "NSDictionary+NGCards.h"
|
2007-01-31 18:11:29 +01:00
|
|
|
|
|
|
|
#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;
|
2011-11-14 17:07:04 +01:00
|
|
|
NSMutableDictionary *values;
|
|
|
|
NSString *finalRendering, *tag;
|
2007-01-31 18:11:29 +01:00
|
|
|
|
|
|
|
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]];
|
2011-11-14 17:26:04 +01:00
|
|
|
|
|
|
|
/* parameters */
|
2007-01-31 18:11:29 +01:00
|
|
|
attributes = [anElement attributes];
|
2011-11-14 17:26:04 +01:00
|
|
|
if ([attributes count])
|
2007-01-31 18:11:29 +01:00
|
|
|
{
|
2011-11-14 17:26:04 +01:00
|
|
|
[rendering appendString: @";"];
|
|
|
|
[attributes versitRenderInString: rendering asAttributes: YES];
|
2007-01-31 18:11:29 +01:00
|
|
|
}
|
|
|
|
|
2011-11-14 17:26:04 +01:00
|
|
|
/* values */
|
2007-01-31 18:11:29 +01:00
|
|
|
values = [anElement values];
|
2011-11-14 17:26:04 +01:00
|
|
|
[rendering appendString: @":"];
|
|
|
|
[values versitRenderInString: rendering asAttributes: NO];
|
2007-01-31 18:11:29 +01:00
|
|
|
|
|
|
|
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];
|
2011-11-14 17:07:04 +01:00
|
|
|
while ((currentChild = [children nextObject]))
|
|
|
|
[rendering appendString: [self render: currentChild]];
|
2007-01-31 18:11:29 +01:00
|
|
|
[rendering appendFormat: @"END:%@\r\n", groupTag];
|
|
|
|
|
|
|
|
return rendering;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|