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 "WOXMLMapDecoder.h"
23 #include "WOXMLMappingModel.h"
24 #include "WOXMLMappingEntity.h"
25 #include "WOXMLMappingProperty.h"
28 #include <DOM/DOMElement.h>
29 #include <DOM/DOMDocument.h>
31 @implementation WOXMLMapDecoder
33 - (id)initWithModel:(WOXMLMappingModel *)_model {
34 self->model = [_model retain];
39 [self->model release];
45 - (WOXMLMappingEntity *)defaultEntity {
49 - (id)_processDOMElementNode:(id)_node {
50 WOXMLMappingEntity *entity;
53 NSMutableSet *childTags;
55 NSAssert1([_node nodeType] == DOM_ELEMENT_NODE,
56 @"passed invalid element node: %@", _node);
58 if ((entity = [self->model entityForXmlTag:[_node tagName]]) == nil) {
60 entity = [self defaultEntity];
63 return [_node textValue];
65 entityClass = NSClassFromString([entity name]);
66 if (entityClass == Nil) entityClass = [NSMutableDictionary class];
68 object = AUTORELEASE([[entityClass alloc] init]);
70 childTags = [NSMutableSet setWithCapacity:16];
72 /* apply properties */
75 WOXMLMappingProperty *prop;
77 e = [[entity properties] objectEnumerator];
78 while ((prop = [e nextObject])) {
82 if ([prop attribute]) {
83 /* an attribute property */
87 attrName = [prop xmlTag];
89 if ((value = [_node attribute:attrName]) == nil)
90 value = [_node attribute:attrName namespaceURI:@"*"];
93 [object takeValue:value forKey:[prop name]];
96 [childTags addObject:[prop xmlTag]];
104 NSMutableDictionary *d;
106 d = [NSMutableDictionary dictionaryWithCapacity:16];
108 childNodes = [_node childNodes];
109 for (i = 0, count = [childNodes count]; i < count; i++) {
112 child = [childNodes objectAtIndex:i];
114 if ([child nodeType] == DOM_ELEMENT_NODE) {
115 if ([childTags containsObject:[child tagName]]) {
117 WOXMLMappingProperty *prop;
120 prop = [entity propertyForXmlTag:[child tagName]];
122 o = [self _processDOMElementNode:child];
126 if ([prop forceList]) {
129 a = [d objectForKey:[prop name]];
131 a = [NSMutableArray arrayWithCapacity:1];
132 [d setObject:a forKey:[prop name]];
139 if ((old = [d objectForKey:[prop name]])) {
140 if ([old isKindOfClass:[NSMutableArray class]])
145 a = [NSMutableArray arrayWithCapacity:2];
148 [d setObject:a forKey:[prop name]];
153 [d setObject:o forKey:[prop name]];
158 /* plain content tag */
164 [object takeValuesFromDictionary:d];
170 - (id)_processDOMDocument:(id)_dom {
171 return [self _processDOMElementNode:[_dom documentElement]];
174 /* parsing DOM tree */
176 - (id)decodeRootObjectFromString:(NSString *)_str {
179 doc = [NGDOMDocument documentFromString:_str];
181 return [self _processDOMDocument:doc];
184 - (id)decodeRootObjectFromData:(NSData *)_data {
187 doc = [NGDOMDocument documentFromData:_data];
189 return [self _processDOMDocument:doc];
191 - (id)decodeRootObjectFromFileHandle:(NSFileHandle *)_fh {
192 return [self decodeRootObjectFromData:[_fh readDataToEndOfFile]];
195 @end /* WOXMLMapDecoder */