| 1 | /* Copyright (C) 2008-2009 Peter Speck |
|---|
| 2 | * |
|---|
| 3 | * This program is free software: you can redistribute it and/or modify |
|---|
| 4 | * it under the terms of the GNU General Public License as published by |
|---|
| 5 | * the Free Software Foundation, either version 3 of the License, or |
|---|
| 6 | * (at your option) any later version. |
|---|
| 7 | * |
|---|
| 8 | * This program is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * GNU General Public License for more details. |
|---|
| 12 | * |
|---|
| 13 | * You should have received a copy of the GNU General Public License |
|---|
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 15 | */ |
|---|
| 16 | |
|---|
| 17 | #import "XmlDoc.h" |
|---|
| 18 | #import "Xml.h" |
|---|
| 19 | #import "GBDebug.h" |
|---|
| 20 | |
|---|
| 21 | @implementation XmlDoc |
|---|
| 22 | |
|---|
| 23 | #ifdef DEBUG |
|---|
| 24 | //#define DEBUG_LOG_XML 1 |
|---|
| 25 | #endif |
|---|
| 26 | |
|---|
| 27 | - (id)initWithRootElement:(NSXMLElement*)rootE |
|---|
| 28 | { |
|---|
| 29 | if (!(self = [super init])) |
|---|
| 30 | return NULL; |
|---|
| 31 | [self reloadWithRootElement:rootE]; |
|---|
| 32 | return self; |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | - (id)initWithRootElementTagName:(NSString*)tagName |
|---|
| 36 | { |
|---|
| 37 | if (!(self = [super init])) |
|---|
| 38 | return NULL; |
|---|
| 39 | [self reloadWithRootElement:[Xml createElementWithName:tagName]]; |
|---|
| 40 | return self; |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | - (id)initWithURL:(NSURL*)url withData:(NSData*)data |
|---|
| 44 | { |
|---|
| 45 | if (!(self = [super init])) |
|---|
| 46 | return self; |
|---|
| 47 | _doc = [[NSXMLDocument alloc] initWithData:data |
|---|
| 48 | options:NSXMLNodePreserveCDATA |
|---|
| 49 | error:&_error]; |
|---|
| 50 | [_error retain]; |
|---|
| 51 | #ifdef DEBUG_LOG_XML |
|---|
| 52 | NSLog(@"Got xml: %@", _doc); |
|---|
| 53 | #endif |
|---|
| 54 | if (_error) { |
|---|
| 55 | NSLog(@"GlimmerBlocker got error parsing xml from: %@", _url); |
|---|
| 56 | NSLog(@" error: %@", _error); |
|---|
| 57 | } |
|---|
| 58 | if (_doc && !_error) { |
|---|
| 59 | _rootE = [[_doc rootElement] retain]; |
|---|
| 60 | if (!_rootE) { |
|---|
| 61 | NSLog(@"Invalid xml document (no root element) loaded from %@", _url); |
|---|
| 62 | [_doc release]; |
|---|
| 63 | _doc = NULL; |
|---|
| 64 | } |
|---|
| 65 | } |
|---|
| 66 | return self; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | - (void)releaseDoc |
|---|
| 70 | { |
|---|
| 71 | [_doc release]; |
|---|
| 72 | _doc = NULL; |
|---|
| 73 | [_rootE release]; |
|---|
| 74 | _rootE = NULL; |
|---|
| 75 | [_error release]; |
|---|
| 76 | _error = NULL; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | - (void)dealloc |
|---|
| 80 | { |
|---|
| 81 | [self releaseDoc]; |
|---|
| 82 | [_url release]; |
|---|
| 83 | [super dealloc]; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | - (void)reloadWithRootElement:(NSXMLElement*)rootE |
|---|
| 87 | { |
|---|
| 88 | NSAssert(rootE, @"rootE"); |
|---|
| 89 | [self releaseDoc]; |
|---|
| 90 | _rootE = [rootE retain]; |
|---|
| 91 | NSXMLNode* parent = [rootE parent]; |
|---|
| 92 | if (parent) { |
|---|
| 93 | NSAssert([parent isKindOfClass:[NSXMLDocument class]], @"root-parent is doc"); |
|---|
| 94 | _doc = (NSXMLDocument*)[parent retain]; |
|---|
| 95 | } else { |
|---|
| 96 | _doc = [[NSXMLDocument alloc] initWithRootElement:_rootE]; |
|---|
| 97 | } |
|---|
| 98 | [_doc setCharacterEncoding:@"UTF-8"]; // used when saving document. |
|---|
| 99 | [_doc setDocumentContentKind:NSXMLDocumentXMLKind]; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | - (NSURL*) url |
|---|
| 103 | { |
|---|
| 104 | return _url; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | - (NSError*) error |
|---|
| 108 | { |
|---|
| 109 | return _error; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | - (NSXMLDocument*) doc |
|---|
| 113 | { |
|---|
| 114 | return _doc; |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | - (NSXMLElement*) rootE |
|---|
| 118 | { |
|---|
| 119 | return _rootE; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | - (NSXMLElement*) byXPath:(NSString*)xpath |
|---|
| 123 | { |
|---|
| 124 | NSError* error = NULL; |
|---|
| 125 | NSArray* a = [_rootE nodesForXPath:xpath error:&error]; |
|---|
| 126 | if (error) { |
|---|
| 127 | NSLog(@"XmlDoc.byXPath failed for %@", xpath); |
|---|
| 128 | NSLog(@" %@", error); |
|---|
| 129 | return NULL; |
|---|
| 130 | } |
|---|
| 131 | if ([a count] > 1) |
|---|
| 132 | NSLog(@"Warning: matched %ld elements for: %@", (long)[a count], xpath); |
|---|
| 133 | return [a count] ? [a objectAtIndex:0] : NULL; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | - (NSXMLElement*)createRootChildWithName:(NSString*)name |
|---|
| 137 | { |
|---|
| 138 | NSXMLElement* e = [Xml createElementWithName:name]; |
|---|
| 139 | [_rootE addChild:e]; |
|---|
| 140 | return e; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | - (NSData*)printToData; |
|---|
| 144 | { |
|---|
| 145 | return [Xml documentToData:_doc]; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | - (NSString*)printToString |
|---|
| 149 | { |
|---|
| 150 | return [Xml documentToString:_doc]; |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | #pragma mark -------------------------------- XPath scalars |
|---|
| 154 | |
|---|
| 155 | - (NSString*)stringForXPath:(NSString*)xpath |
|---|
| 156 | { |
|---|
| 157 | return [Xml stringForXPath:xpath withRoot:_rootE]; |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | - (void)setString:(NSString*)value forXPath:(NSString*)xpath |
|---|
| 161 | { |
|---|
| 162 | [Xml setString:value forXPath:xpath withRoot:_rootE]; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | - (double)doubleForXPath:(NSString*)xpath |
|---|
| 166 | { |
|---|
| 167 | return [Xml doubleForXPath:xpath withRoot:_rootE]; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | - (void)setDouble:(double)value forXPath:(NSString*)xpath |
|---|
| 171 | { |
|---|
| 172 | [Xml setDouble:value forXPath:xpath withRoot:_rootE]; |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | - (int)intForXPath:(NSString*)xpath |
|---|
| 176 | { |
|---|
| 177 | return [Xml intForXPath:xpath withRoot:_rootE]; |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | - (void)setInt:(int)value forXPath:(NSString*)xpath |
|---|
| 181 | { |
|---|
| 182 | [Xml setInt:value forXPath:xpath withRoot:_rootE]; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | - (BOOL)boolForXPath:(NSString*)xpath |
|---|
| 186 | { |
|---|
| 187 | return [Xml boolForXPath:xpath withRoot:_rootE]; |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | - (void)setBool:(BOOL)value forXPath:(NSString*)xpath |
|---|
| 191 | { |
|---|
| 192 | [Xml setBool:value forXPath:xpath withRoot:_rootE]; |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | - (NSString*)dateFromMillisecsXPath:(NSString*)xpath |
|---|
| 196 | { |
|---|
| 197 | return [Xml dateFromMillisecsXPath:xpath withRoot:_rootE]; |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | #pragma mark -------------------------------- XPath |
|---|
| 201 | |
|---|
| 202 | - (NSArray *)nodesForXPath:(NSString *)xpath |
|---|
| 203 | { |
|---|
| 204 | return [Xml nodesForXPath:xpath withRoot:_rootE]; |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | - (NSXMLNode *)nodeForXPath:(NSString *)xpath |
|---|
| 208 | { |
|---|
| 209 | return [Xml nodeForXPath:xpath withRoot:_rootE]; |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | - (void)removeElementsForXPath:(NSString*)xpath |
|---|
| 213 | { |
|---|
| 214 | [Xml remove:xpath withRoot:_rootE]; |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | - (NSXMLElement*)singletonElementForXPath:(NSString*)xpath |
|---|
| 218 | { |
|---|
| 219 | return [Xml singletonElementForXPath:xpath forParent:_rootE]; |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | - (NSXMLElement*)singletonElementForXPath:(NSString*)xpath |
|---|
| 223 | asFirstChild:(BOOL)asFirstChild |
|---|
| 224 | { |
|---|
| 225 | return [Xml singletonElementForXPath:xpath forParent:_rootE asFirstChild:asFirstChild]; |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | #pragma mark -------------------------------- unqiue id |
|---|
| 229 | |
|---|
| 230 | - (int)makeUniqueId |
|---|
| 231 | { |
|---|
| 232 | return [Xml makeUniqueIdWithRoot:_rootE]; |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | @end |
|---|