]> err.no Git - sope/blob - sopex/SOPEX/SOPEXContentValidator.m
fixed a warning
[sope] / sopex / SOPEX / SOPEXContentValidator.m
1 /*
2  Copyright (C) 2004 Marcus Mueller <znek@mulle-kybernetik.com>
3
4  This file is part of OGo
5
6  OGo 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  OGo 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 OGo; 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 // $Id: SOPEXContentValidator.m 1 2004-08-20 11:17:52Z znek $
22 //  Created by znek on Mon Apr 05 2004.
23
24
25 #import "SOPEXContentValidator.h"
26 #import <NGObjWeb/NGObjWeb.h>
27
28
29 NSString *SOPEXDocumentValidationErrorDomain = @"SOPEXDocumentValidationErrorDomain";
30
31
32 @interface SOPEXContentValidator (PrivateAPI)
33 + (NSError *)validateContent:(id)content usingSelector:(SEL)selector;
34 - (NSError *)validateContent:(id)content ofMIMEType:(NSString *)mimeType;
35 - (NSError *)validateContent:(id)content withParserClass:(Class)parserClass selector:(SEL)selector;
36 - (NSString *)formattedErrorString;
37 @end
38
39
40 @implementation SOPEXContentValidator
41
42
43 #pragma mark -
44 #pragma mark ### INIT & DEALLOC ###
45
46
47 - (id)init
48 {
49     [super init];
50     self->warnings = [[NSMutableArray alloc] init];
51     self->errors = [[NSMutableArray alloc] init];
52     return self;
53 }
54
55 - (void)dealloc
56 {
57     [self->warnings release];
58     [self->errors release];
59     [super dealloc];
60 }
61
62
63 #pragma mark -
64 #pragma mark ### VALIDATION ###
65
66
67 + (NSError *)validateWOXContent:(id)content
68 {
69     return [self validateContent:content usingSelector:@selector(validateWOXContent:)];
70 }
71
72 + (NSError *)validateWOHTMLContent:(id)content
73 {
74     return [self validateContent:content usingSelector:@selector(validateWOHTMLContent:)];
75 }
76
77 + (NSError *)validateWODContent:(id)content
78 {
79     return [self validateContent:content usingSelector:@selector(validateWODContent:)];
80 }
81
82 + (NSError *)validateContent:(id)content usingSelector:(SEL)selector
83 {
84     SOPEXContentValidator *validator;
85     NSError *status;
86     
87     validator = [[self alloc] init];
88     status = [validator performSelector:selector withObject:content];
89     [validator release];
90     return status;
91 }
92
93 - (NSError *)validateWOXContent:(id)content
94 {
95     return [self validateContent:content ofMIMEType:@"text/xml"];
96 }
97
98 - (NSError *)validateContent:(id)content ofMIMEType:(NSString *)mimeType
99 {
100     id <NSObject, SaxXMLReader> xmlReader;
101     
102     xmlReader = [[SaxXMLReaderFactory standardXMLReaderFactory] createXMLReaderForMimeType:mimeType];
103     [xmlReader setErrorHandler:self];
104     [xmlReader parseFromSource:content];
105     return [self status];
106 }
107
108 - (NSError *)validateWOHTMLContent:(id)content
109 {
110     return [self validateContent:content withParserClass:[WOHTMLParser class] selector:@selector(parseHTMLData:)];
111 }
112
113 - (NSError *)validateWODContent:(id)content
114 {
115     return [self validateContent:content withParserClass:[WODParser class] selector:@selector(parseDeclarationData:)];
116 }
117
118 - (NSError *)validateContent:(id)content withParserClass:(Class)parserClass selector:(SEL)selector
119 {
120     NSData *data;
121     id parser;
122
123     if([content isKindOfClass:[NSString class]])
124         data = [content dataUsingEncoding:NSUTF8StringEncoding];
125     else
126         data = content;
127     
128     NS_DURING
129         
130         parser = [[parserClass alloc] initWithHandler:self];
131         [parser performSelector:selector withObject:data];
132         
133     NS_HANDLER
134         
135         [self->errors addObject:[localException reason]];
136         
137     NS_ENDHANDLER;
138     
139     [parser release];
140     return [self status];
141 }
142
143
144 #pragma mark -
145 #pragma mark ### ACCESSORS ###
146
147
148 - (BOOL)hasWarnings
149 {
150     return [self->warnings count] != 0;
151 }
152
153 - (NSArray *)warnings
154 {
155     return self->warnings;
156 }
157
158 - (BOOL)hasErrors
159 {
160     return [self->errors count] != 0;
161 }
162
163 - (NSArray *)errors
164 {
165     return self->errors;
166 }
167
168 - (NSString *)formattedErrorString
169 {
170     return [[self errors] componentsJoinedByString:@"\n"];
171 }
172
173 - (NSError *)status
174 {
175     NSDictionary *info;
176
177     if([self hasErrors] == NO)
178         return nil;
179
180     info = [NSDictionary dictionaryWithObject:[self formattedErrorString] forKey:NSLocalizedDescriptionKey];
181     return [NSError errorWithDomain:SOPEXDocumentValidationErrorDomain code:0 userInfo:info];
182 }
183
184
185 #pragma mark -
186 #pragma mark ### SaxErrorHandler Protocol ###
187
188
189 - (void)warning:(SaxParseException *)_exception
190 {
191     [self->warnings addObject:[_exception reason]];
192 }
193
194 - (void)error:(SaxParseException *)_exception
195 {
196 #if 0
197     NSLog(@"%s reason:%@ ui:%@ line:%@ column:%@", __PRETTY_FUNCTION__, [_exception reason], [_exception userInfo], [[_exception userInfo] objectForKey:@"line"], [[_exception userInfo] objectForKey:@"column"]);
198 #endif
199     [self->errors addObject:[_exception reason]];
200 }
201
202 - (void)fatalError:(SaxParseException *)_exception
203 {
204     [self error:_exception];
205 }
206
207
208 #pragma mark -
209 #pragma mark ### WODParserHandler PROTOCOL ###
210
211
212 - (BOOL)parser:(id)_parser willParseDeclarationData:(NSData *)_data
213 {
214     return YES;
215 }
216
217 - (void)parser:(id)_parser finishedParsingDeclarationData:(NSData *)_data declarations:(NSDictionary *)_decls
218 {
219 }
220
221 - (void)parser:(id)_parser failedParsingDeclarationData:(NSData *)_data exception:(NSException *)_exception
222 {
223     [_exception raise];
224 }
225
226 - (id)parser:(id)_parser makeAssociationWithValue:(id)_value
227 {
228     return nil;
229 }
230
231 - (id)parser:(id)_parser makeAssociationWithKeyPath:(NSString *)_keyPath
232 {
233     return nil;
234 }
235
236 - (id)parser:(id)_parser makeDefinitionForComponentNamed:(NSString *)_cname associations:(id)_entry elementName:(NSString *)_elemName
237 {
238     return nil;
239 }
240
241
242 #pragma mark -
243 #pragma mark ### WOHTMLParserHandler PROTOCOL ###
244
245
246 - (BOOL)parser:(id)_parser willParseHTMLData:(NSData *)_data
247 {
248     return YES;
249 }
250
251 - (void)parser:(id)_parser finishedParsingHTMLData:(NSData *)_data elements:(NSArray *)_elements
252 {
253 }
254
255 - (void)parser:(id)_parser failedParsingHTMLData:(NSData *)_data exception:(NSException *)_exception
256 {
257 #if 0
258     NSLog(@"%s reason:%@ ui:%@ line:%@ column:%@", __PRETTY_FUNCTION__, [_exception reason], [_exception userInfo], [[_exception userInfo] objectForKey:@"line"], [[_exception userInfo] objectForKey:@"column"]);
259 #endif
260     [_exception raise];
261 }
262
263 - (WOElement *)dynamicElementWithName:(NSString *)_element attributes:(NSDictionary *)_attributes contentElements:(NSArray *)_subElements
264 {
265     return (WOElement *)[NSNull null];
266 }
267
268
269 #pragma mark -
270 #pragma mark ### DEBUGGING ###
271
272
273 - (NSString *)description
274 {
275     return [NSString stringWithFormat:@"<%@:0x%x warnings:%d errors:%d>", NSStringFromClass(self->isa), self, [self->warnings count], [self->errors count]];
276 }
277
278 @end