2 Copyright (C) 2004 Marcus Mueller <znek@mulle-kybernetik.com>
4 This file is part of OGo
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
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.
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
21 // $Id: SOPEXContentValidator.m 1 2004-08-20 11:17:52Z znek $
22 // Created by znek on Mon Apr 05 2004.
25 #import "SOPEXContentValidator.h"
26 #import <NGObjWeb/NGObjWeb.h>
29 NSString *SOPEXDocumentValidationErrorDomain = @"SOPEXDocumentValidationErrorDomain";
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;
40 @implementation SOPEXContentValidator
44 #pragma mark ### INIT & DEALLOC ###
50 self->warnings = [[NSMutableArray alloc] init];
51 self->errors = [[NSMutableArray alloc] init];
57 [self->warnings release];
58 [self->errors release];
64 #pragma mark ### VALIDATION ###
67 + (NSError *)validateWOXContent:(id)content
69 return [self validateContent:content usingSelector:@selector(validateWOXContent:)];
72 + (NSError *)validateWOHTMLContent:(id)content
74 return [self validateContent:content usingSelector:@selector(validateWOHTMLContent:)];
77 + (NSError *)validateWODContent:(id)content
79 return [self validateContent:content usingSelector:@selector(validateWODContent:)];
82 + (NSError *)validateContent:(id)content usingSelector:(SEL)selector
84 SOPEXContentValidator *validator;
87 validator = [[self alloc] init];
88 status = [validator performSelector:selector withObject:content];
93 - (NSError *)validateWOXContent:(id)content
95 return [self validateContent:content ofMIMEType:@"text/xml"];
98 - (NSError *)validateContent:(id)content ofMIMEType:(NSString *)mimeType
100 id <NSObject, SaxXMLReader> xmlReader;
102 xmlReader = [[SaxXMLReaderFactory standardXMLReaderFactory] createXMLReaderForMimeType:mimeType];
103 [xmlReader setErrorHandler:self];
104 [xmlReader parseFromSource:content];
105 return [self status];
108 - (NSError *)validateWOHTMLContent:(id)content
110 return [self validateContent:content withParserClass:[WOHTMLParser class] selector:@selector(parseHTMLData:)];
113 - (NSError *)validateWODContent:(id)content
115 return [self validateContent:content withParserClass:[WODParser class] selector:@selector(parseDeclarationData:)];
118 - (NSError *)validateContent:(id)content withParserClass:(Class)parserClass selector:(SEL)selector
123 if([content isKindOfClass:[NSString class]])
124 data = [content dataUsingEncoding:NSUTF8StringEncoding];
130 parser = [[parserClass alloc] initWithHandler:self];
131 [parser performSelector:selector withObject:data];
135 [self->errors addObject:[localException reason]];
140 return [self status];
145 #pragma mark ### ACCESSORS ###
150 return [self->warnings count] != 0;
153 - (NSArray *)warnings
155 return self->warnings;
160 return [self->errors count] != 0;
168 - (NSString *)formattedErrorString
170 return [[self errors] componentsJoinedByString:@"\n"];
177 if([self hasErrors] == NO)
180 info = [NSDictionary dictionaryWithObject:[self formattedErrorString] forKey:NSLocalizedDescriptionKey];
181 return [NSError errorWithDomain:SOPEXDocumentValidationErrorDomain code:0 userInfo:info];
186 #pragma mark ### SaxErrorHandler Protocol ###
189 - (void)warning:(SaxParseException *)_exception
191 [self->warnings addObject:[_exception reason]];
194 - (void)error:(SaxParseException *)_exception
197 NSLog(@"%s reason:%@ ui:%@ line:%@ column:%@", __PRETTY_FUNCTION__, [_exception reason], [_exception userInfo], [[_exception userInfo] objectForKey:@"line"], [[_exception userInfo] objectForKey:@"column"]);
199 [self->errors addObject:[_exception reason]];
202 - (void)fatalError:(SaxParseException *)_exception
204 [self error:_exception];
209 #pragma mark ### WODParserHandler PROTOCOL ###
212 - (BOOL)parser:(id)_parser willParseDeclarationData:(NSData *)_data
217 - (void)parser:(id)_parser finishedParsingDeclarationData:(NSData *)_data declarations:(NSDictionary *)_decls
221 - (void)parser:(id)_parser failedParsingDeclarationData:(NSData *)_data exception:(NSException *)_exception
226 - (id)parser:(id)_parser makeAssociationWithValue:(id)_value
231 - (id)parser:(id)_parser makeAssociationWithKeyPath:(NSString *)_keyPath
236 - (id)parser:(id)_parser makeDefinitionForComponentNamed:(NSString *)_cname associations:(id)_entry elementName:(NSString *)_elemName
243 #pragma mark ### WOHTMLParserHandler PROTOCOL ###
246 - (BOOL)parser:(id)_parser willParseHTMLData:(NSData *)_data
251 - (void)parser:(id)_parser finishedParsingHTMLData:(NSData *)_data elements:(NSArray *)_elements
255 - (void)parser:(id)_parser failedParsingHTMLData:(NSData *)_data exception:(NSException *)_exception
258 NSLog(@"%s reason:%@ ui:%@ line:%@ column:%@", __PRETTY_FUNCTION__, [_exception reason], [_exception userInfo], [[_exception userInfo] objectForKey:@"line"], [[_exception userInfo] objectForKey:@"column"]);
263 - (WOElement *)dynamicElementWithName:(NSString *)_element attributes:(NSDictionary *)_attributes contentElements:(NSArray *)_subElements
265 return (WOElement *)[NSNull null];
270 #pragma mark ### DEBUGGING ###
273 - (NSString *)description
275 return [NSString stringWithFormat:@"<%@:0x%x warnings:%d errors:%d>", NSStringFromClass(self->isa), self, [self->warnings count], [self->errors count]];