]> err.no Git - sope/blob - sope-appserver/WOXML/WOXMLMappingModel.m
fixed install locations on OSX
[sope] / sope-appserver / WOXML / WOXMLMappingModel.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21
22 #include "WOXMLMappingModel.h"
23 #include "WOXMLMappingEntity.h"
24 #include "WOXMLSaxModelHandler.h"
25 #include <SaxObjC/SaxXMLReader.h>
26 #include <SaxObjC/SaxXMLReaderFactory.h>
27 #include "common.h"
28
29 @implementation WOXMLMappingModel
30
31 + (id)mappingModelByParsingFromURL:(NSString *)_url {
32   id<NSObject,SaxXMLReader> parser;
33   WOXMLSaxModelHandler      *sax;
34   WOXMLMappingModel         *model;
35   
36   if ([_url length] == 0)
37     /* invalid URL */
38     return nil;
39   
40   parser = [[SaxXMLReaderFactory standardXMLReaderFactory] createXMLReader];
41   if (parser == nil)
42     /* couldn't create parser */
43     return nil;
44   
45   sax = [[WOXMLSaxModelHandler alloc] init];
46   [parser setContentHandler:sax];
47   [parser setErrorHandler:sax];
48   
49   [parser parseFromSystemId:_url];
50   
51   model = RETAIN([sax model]);
52   RELEASE(sax);
53   
54   return AUTORELEASE(model);
55 }
56
57 - (void)dealloc {
58   RELEASE(self->tagToEntity);
59   RELEASE(self->entities);
60   [super dealloc];
61 }
62
63 /* entities */
64
65 - (void)addEntity:(WOXMLMappingEntity *)_entity {
66   NSAssert1([_entity isValid], @"tried to add invalid entity %@", _entity);
67   
68   if (self->entities == nil)
69     self->entities = [[NSMutableArray alloc] init];
70   if (self->tagToEntity == nil)
71     self->tagToEntity = [[NSMutableDictionary alloc] init];
72   
73   if ([self->tagToEntity objectForKey:[_entity xmlTag]]) {
74     NSLog(@"WARNING: already defined entity for tag %@", [_entity xmlTag]);
75     return;
76   }
77   
78   [self->entities    addObject:_entity];
79   [self->tagToEntity setObject:_entity forKey:[_entity xmlTag]];
80 }
81
82 - (WOXMLMappingEntity *)entityForXmlTag:(NSString *)_xmlTag {
83   return [self->tagToEntity objectForKey:_xmlTag];
84 }
85
86 - (NSArray *)entities {
87   return self->entities;
88 }
89
90 /* XML representation */
91
92 - (NSString *)xmlStringValue {
93   NSMutableString *s;
94   NSEnumerator *e;
95   id entity;
96
97   s = [NSMutableString stringWithCapacity:4096];
98   [s appendString:@"<model>\n"];
99   
100   e = [[self entities] objectEnumerator];
101   while ((entity = [e nextObject]))
102     [s appendString:[entity xmlStringValue]];
103   
104   [s appendString:@"</model>\n"];
105   return s;
106 }
107
108 @end /* WOXMLMappingModel */