2 Copyright (C) 2000-2004 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
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
23 #include <NGObjWeb/WOMessage.h>
24 #include <SaxObjC/SaxObjC.h>
27 @interface WOMessageSaxValidator : SaxDefaultHandler < SaxErrorHandler >
29 id<NSObject,SaxXMLReader> parser;
30 NSMutableArray *issues;
33 + (id)validatorWithXmlReaderName:(NSString *)_name;
35 - (NSArray *)validateContent:(NSData *)_content withType:(NSString *)_ctype;
39 @implementation WOMessageSaxValidator
41 static BOOL valDebugOn = NO;
43 - (id)initWithXmlReaderName:(NSString *)_name {
44 if ((self = [super init])) {
45 SaxXMLReaderFactory *factory;
47 factory = [SaxXMLReaderFactory standardXMLReaderFactory];
48 self->parser = [[factory createXMLReaderWithName:_name] retain];
49 if (self->parser == nil) {
54 /* we are only interested in errors */
55 [self->parser setErrorHandler:self];
59 + (id)validatorWithXmlReaderName:(NSString *)_name {
60 return [[[self alloc] initWithXmlReaderName:_name] autorelease];
64 [self->issues release];
65 [self->parser release];
71 - (void)addIssue:(id)_issue {
72 if (_issue == nil) return;
74 if (self->issues == nil)
75 self->issues = [[NSMutableArray alloc] initWithCapacity:16];
76 [self->issues addObject:_issue];
79 [self->issues removeAllObjects];
84 - (void)warning:(SaxParseException *)_exception {
85 [self addIssue:_exception];
87 - (void)error:(SaxParseException *)_exception {
88 [self addIssue:_exception];
90 - (void)fatalError:(SaxParseException *)_exception {
91 [self addIssue:_exception];
94 - (NSArray *)validateContent:(NSData *)_content withType:(NSString *)_ctype {
98 if (self->parser == nil) return nil;
100 [self debugWithFormat:@"validate %@, content size %d",
101 _ctype, [_content length]];
103 [self->parser parseFromSource:_content
104 systemId:[@"validator://" stringByAppendingString:_ctype]];
106 tmp = [self->issues copy];
110 [self debugWithFormat:@" no issues found :-)"];
112 [self debugWithFormat:@" %d issues found :-|", [tmp count]];
113 return [tmp autorelease];
118 - (BOOL)isDebuggingEnabled {
122 @end /* WOMessageHTMLValidator */
124 @implementation WOMessage(Validation)
126 - (id)validatorForContentType:(NSString *)_ctype {
127 if ([_ctype hasPrefix:@"text/html"]) {
128 // Note: the HTML driver does not report invalid tags
129 return [WOMessageSaxValidator validatorWithXmlReaderName:
130 @"libxmlHTMLSAXDriver"];
133 if ([_ctype hasPrefix:@"text/xml"]) {
134 return [WOMessageSaxValidator validatorWithXmlReaderName:
138 if ([_ctype hasPrefix:@"image/"])
140 if ([_ctype hasPrefix:@"application/octet-stream"])
142 if ([_ctype hasPrefix:@"text/plain"])
145 [self logWithFormat:@"no validator for type: %@", _ctype];
149 - (NSArray *)validateContent {
154 [self logWithFormat:@"should validate output"];
157 if ((ctype = [self headerForKey:@"content-type"]) == nil)
158 return [NSArray arrayWithObject:@"missing content type."];
160 if ((validator = [self validatorForContentType:ctype]) == nil)
163 return [validator validateContent:[self content] withType:ctype];
166 @end /* WOMessage(Validation) */