]> err.no Git - sope/blob - sope-appserver/NGObjWeb/WOMessage+Validation.m
fixed WOContext superclass version
[sope] / sope-appserver / NGObjWeb / WOMessage+Validation.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21
22 #include <NGObjWeb/WOMessage.h>
23 #include <SaxObjC/SaxObjC.h>
24 #include "common.h"
25
26 @interface WOMessageSaxValidator : SaxDefaultHandler < SaxErrorHandler >
27 {
28   id<NSObject,SaxXMLReader> parser;
29   NSMutableArray *issues;
30 }
31
32 + (id)validatorWithXmlReaderName:(NSString *)_name;
33
34 - (NSArray *)validateContent:(NSData *)_content withType:(NSString *)_ctype;
35
36 @end
37
38 @implementation WOMessageSaxValidator
39
40 static BOOL valDebugOn = NO;
41
42 - (id)initWithXmlReaderName:(NSString *)_name {
43   if ((self = [super init])) {
44     SaxXMLReaderFactory *factory;
45     
46     factory = [SaxXMLReaderFactory standardXMLReaderFactory];
47     self->parser = [[factory createXMLReaderWithName:_name] retain];
48     if (self->parser == nil) {
49       [self release];
50       return nil;
51     }
52     
53     /* we are only interested in errors */
54     [self->parser setErrorHandler:self];
55   }
56   return self;
57 }
58 + (id)validatorWithXmlReaderName:(NSString *)_name {
59   return [[[self alloc] initWithXmlReaderName:_name] autorelease];
60 }
61
62 - (void)dealloc {
63   [self->issues release];
64   [self->parser release];
65   [super dealloc];
66 }
67
68 /* issues */
69
70 - (void)addIssue:(id)_issue {
71   if (_issue == nil) return;
72   
73   if (self->issues == nil) 
74     self->issues = [[NSMutableArray alloc] initWithCapacity:16];
75   [self->issues addObject:_issue];
76 }
77 - (void)reset {
78   [self->issues removeAllObjects];
79 }
80
81 /* validation */
82
83 - (void)warning:(SaxParseException *)_exception {
84   [self addIssue:_exception];
85 }
86 - (void)error:(SaxParseException *)_exception {
87   [self addIssue:_exception];
88 }
89 - (void)fatalError:(SaxParseException *)_exception {
90   [self addIssue:_exception];
91 }
92
93 - (NSArray *)validateContent:(NSData *)_content withType:(NSString *)_ctype {
94   NSArray *tmp;
95   
96   [self reset];
97   if (self->parser == nil) return nil;
98   
99   [self debugWithFormat:@"validate %@, content size %d",
100           _ctype, [_content length]];
101   
102   [self->parser parseFromSource:_content 
103                 systemId:[@"validator://" stringByAppendingString:_ctype]];
104
105   tmp = [self->issues copy];
106   [self reset];
107
108   if (tmp == nil)
109     [self debugWithFormat:@"  no issues found :-)"];
110   else
111     [self debugWithFormat:@"  %d issues found :-|", [tmp count]];
112   return [tmp autorelease];
113 }
114
115 /* debugging */
116
117 - (BOOL)isDebuggingEnabled {
118   return valDebugOn;
119 }
120
121 @end /* WOMessageHTMLValidator */
122
123 @implementation WOMessage(Validation)
124
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"];
130   }
131   
132   if ([_ctype hasPrefix:@"text/xml"]) {
133     return [WOMessageSaxValidator validatorWithXmlReaderName:
134                                     @"libxmlSAXDriver"];
135   }
136   
137   if ([_ctype hasPrefix:@"image/"])
138     return nil;
139   if ([_ctype hasPrefix:@"application/octet-stream"])
140     return nil;
141   if ([_ctype hasPrefix:@"text/plain"])
142     return nil;
143   
144   [self logWithFormat:@"no validator for type: %@", _ctype];
145   return nil;
146 }
147
148 - (NSArray *)validateContent {
149   NSString *ctype;
150   id validator;
151   
152 #if 0
153   [self logWithFormat:@"should validate output"];
154 #endif
155   
156   if ((ctype = [self headerForKey:@"content-type"]) == nil)
157     return [NSArray arrayWithObject:@"missing content type."];
158   
159   if ((validator = [self validatorForContentType:ctype]) == nil)
160     return nil;
161   
162   return [validator validateContent:[self content] withType:ctype];
163 }
164
165 @end /* WOMessage(Validation) */