2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #include "WOXMLMappingModel.h"
23 #include "WOXMLMappingEntity.h"
24 #include "WOXMLSaxModelHandler.h"
25 #include <SaxObjC/SaxXMLReader.h>
26 #include <SaxObjC/SaxXMLReaderFactory.h>
29 @implementation WOXMLMappingModel
31 + (id)mappingModelByParsingFromURL:(NSString *)_url {
32 id<NSObject,SaxXMLReader> parser;
33 WOXMLSaxModelHandler *sax;
34 WOXMLMappingModel *model;
36 if ([_url length] == 0)
40 parser = [[SaxXMLReaderFactory standardXMLReaderFactory] createXMLReader];
42 /* couldn't create parser */
45 sax = [[WOXMLSaxModelHandler alloc] init];
46 [parser setContentHandler:sax];
47 [parser setErrorHandler:sax];
49 [parser parseFromSystemId:_url];
51 model = RETAIN([sax model]);
54 return AUTORELEASE(model);
58 RELEASE(self->tagToEntity);
59 RELEASE(self->entities);
65 - (void)addEntity:(WOXMLMappingEntity *)_entity {
66 NSAssert1([_entity isValid], @"tried to add invalid entity %@", _entity);
68 if (self->entities == nil)
69 self->entities = [[NSMutableArray alloc] init];
70 if (self->tagToEntity == nil)
71 self->tagToEntity = [[NSMutableDictionary alloc] init];
73 if ([self->tagToEntity objectForKey:[_entity xmlTag]]) {
74 NSLog(@"WARNING: already defined entity for tag %@", [_entity xmlTag]);
78 [self->entities addObject:_entity];
79 [self->tagToEntity setObject:_entity forKey:[_entity xmlTag]];
82 - (WOXMLMappingEntity *)entityForXmlTag:(NSString *)_xmlTag {
83 return [self->tagToEntity objectForKey:_xmlTag];
86 - (NSArray *)entities {
87 return self->entities;
90 /* XML representation */
92 - (NSString *)xmlStringValue {
97 s = [NSMutableString stringWithCapacity:4096];
98 [s appendString:@"<model>\n"];
100 e = [[self entities] objectEnumerator];
101 while ((entity = [e nextObject]))
102 [s appendString:[entity xmlStringValue]];
104 [s appendString:@"</model>\n"];
108 @end /* WOXMLMappingModel */