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