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 <NGObjWeb/WOMessage.h>
23 #include <SaxObjC/SaxObjC.h>
26 @interface WOMessageSaxValidator : SaxDefaultHandler < SaxErrorHandler >
28 id<NSObject,SaxXMLReader> parser;
29 NSMutableArray *issues;
32 + (id)validatorWithXmlReaderName:(NSString *)_name;
34 - (NSArray *)validateContent:(NSData *)_content withType:(NSString *)_ctype;
38 @implementation WOMessageSaxValidator
40 static BOOL valDebugOn = NO;
42 - (id)initWithXmlReaderName:(NSString *)_name {
43 if ((self = [super init])) {
44 SaxXMLReaderFactory *factory;
46 factory = [SaxXMLReaderFactory standardXMLReaderFactory];
47 self->parser = [[factory createXMLReaderWithName:_name] retain];
48 if (self->parser == nil) {
53 /* we are only interested in errors */
54 [self->parser setErrorHandler:self];
58 + (id)validatorWithXmlReaderName:(NSString *)_name {
59 return [[[self alloc] initWithXmlReaderName:_name] autorelease];
63 [self->issues release];
64 [self->parser release];
70 - (void)addIssue:(id)_issue {
71 if (_issue == nil) return;
73 if (self->issues == nil)
74 self->issues = [[NSMutableArray alloc] initWithCapacity:16];
75 [self->issues addObject:_issue];
78 [self->issues removeAllObjects];
83 - (void)warning:(SaxParseException *)_exception {
84 [self addIssue:_exception];
86 - (void)error:(SaxParseException *)_exception {
87 [self addIssue:_exception];
89 - (void)fatalError:(SaxParseException *)_exception {
90 [self addIssue:_exception];
93 - (NSArray *)validateContent:(NSData *)_content withType:(NSString *)_ctype {
97 if (self->parser == nil) return nil;
99 [self debugWithFormat:@"validate %@, content size %d",
100 _ctype, [_content length]];
102 [self->parser parseFromSource:_content
103 systemId:[@"validator://" stringByAppendingString:_ctype]];
105 tmp = [self->issues copy];
109 [self debugWithFormat:@" no issues found :-)"];
111 [self debugWithFormat:@" %d issues found :-|", [tmp count]];
112 return [tmp autorelease];
117 - (BOOL)isDebuggingEnabled {
121 @end /* WOMessageHTMLValidator */
123 @implementation WOMessage(Validation)
125 - (id)validatorForContentType:(NSString *)_ctype {
126 if ([_ctype hasPrefix:@"text/html"]) {
127 // Note: the HTML driver does not report invalid tags
128 return [WOMessageSaxValidator validatorWithXmlReaderName:
129 @"libxmlHTMLSAXDriver"];
132 if ([_ctype hasPrefix:@"text/xml"]) {
133 return [WOMessageSaxValidator validatorWithXmlReaderName:
137 if ([_ctype hasPrefix:@"image/"])
139 if ([_ctype hasPrefix:@"application/octet-stream"])
141 if ([_ctype hasPrefix:@"text/plain"])
144 [self logWithFormat:@"no validator for type: %@", _ctype];
148 - (NSArray *)validateContent {
153 [self logWithFormat:@"should validate output"];
156 if ((ctype = [self headerForKey:@"content-type"]) == nil)
157 return [NSArray arrayWithObject:@"missing content type."];
159 if ((validator = [self validatorForContentType:ctype]) == nil)
162 return [validator validateContent:[self content] withType:ctype];
165 @end /* WOMessage(Validation) */