11--------------------------------------------------------------------
22-- |
33-- Module : Text.Atom.Feed.Validate
4- -- Copyright : (c) Galois, Inc. 2008
4+ -- Copyright : (c) Galois, Inc. 2008,
5+ -- (c) Sigbjorn Finne 2009-
56-- License : BSD3
67--
7- -- Maintainer: Sigbjorn Finne <sof@galois .com>
8+ -- Maintainer: Sigbjorn Finne <sof@forkIO .com>
89-- Stability : provisional
9- -- Portability:
10+ -- Portability: portable
1011--
1112--------------------------------------------------------------------
1213module Text.Atom.Feed.Validate where
@@ -41,7 +42,7 @@ flattenT (VLeaf xs) = xs
4142flattenT (VNode as bs) = as ++ concatMap flattenT bs
4243
4344validateEntry :: Element -> ValidatorResult
44- validateEntry e =
45+ validateEntry e =
4546 mkTree []
4647 [ checkEntryAuthor e
4748 , checkCats e
@@ -60,107 +61,107 @@ validateEntry e =
6061
6162-- Sec 4.1.2, check #1
6263checkEntryAuthor :: Element -> ValidatorResult
63- checkEntryAuthor e =
64+ checkEntryAuthor e =
6465 case pNodes " author" (elChildren e) of
65- [] -> -- required
66+ [] -> -- required
6667 case pNode " summary" (elChildren e) of
6768 Nothing -> demand " Required 'author' element missing (no 'summary' either)"
68- Just e1 ->
69+ Just e1 ->
6970 case pNode " author" (elChildren e1) of
7071 Just a -> checkAuthor a
7172 _ -> demand " Required 'author' element missing"
7273 xs -> mkTree [] $ map checkAuthor xs
73-
74+
7475
7576-- Sec 4.1.2, check #2
7677checkCats :: Element -> ValidatorResult
7778checkCats e = mkTree [] $ map checkCat (pNodes " category" (elChildren e))
7879
7980checkContents :: Element -> ValidatorResult
80- checkContents e =
81+ checkContents e =
8182 case pNodes " content" (elChildren e) of
8283 [] -> valid
8384 [c] -> mkTree [] $ [checkContent c]
8485 cs -> mkTree (flattenT (demand (" at most one 'content' element expected inside 'entry', found: " ++ show (length cs))))
8586 (map checkContent cs)
86-
87-
87+
88+
8889checkContributor :: Element -> ValidatorResult
8990checkContributor _e = valid
9091
9192checkContentLink :: Element -> ValidatorResult
92- checkContentLink e =
93+ checkContentLink e =
9394 case pNodes " content" (elChildren e) of
94- [] ->
95+ [] ->
9596 case pNodes " link" (elChildren e) of
9697 [] -> demand (" An 'entry' element with no 'content' element must have at least one 'link-rel' element" )
97- xs ->
98+ xs ->
9899 case filter (== " alternate" ) $ mapMaybe (pAttr " rel" ) xs of
99100 [] -> demand (" An 'entry' element with no 'content' element must have at least one 'link-rel' element" )
100101 _ -> valid
101102 _ -> valid
102103
103104checkLinks :: Element -> ValidatorResult
104- checkLinks e =
105+ checkLinks e =
105106 case pNodes " link" (elChildren e) of
106- xs ->
107- case map fst $ filter (\ (_,n) -> n == " alternate" ) $
107+ xs ->
108+ case map fst $ filter (\ (_,n) -> n == " alternate" ) $
108109 mapMaybe (\ ex -> fmap (\ x -> (ex,x)) $ pAttr " rel" ex) xs of
109- xs1 ->
110- let
110+ xs1 ->
111+ let
111112 jmb (Just x) (Just y) = Just (x,y)
112113 jmb _ _ = Nothing
113114 in
114115 case mapMaybe (\ ex -> pAttr " type" ex `jmb` pAttr " hreflang" ex) xs1 of
115- xs2 ->
116+ xs2 ->
116117 case any (\ x -> length x > 1 ) (group xs2) of
117118 True -> demand (" An 'entry' element cannot have duplicate 'link-rel-alternate-type-hreflang' elements" )
118119 _ -> valid
119120
120121checkId :: Element -> ValidatorResult
121- checkId e =
122+ checkId e =
122123 case pNodes " id" (elChildren e) of
123124 [] -> demand " required field 'id' missing from 'entry' element"
124125 [_] -> valid
125126 xs -> demand (" only one 'id' field expected in 'entry' element, found: " ++ show (length xs))
126127
127128checkPublished :: Element -> ValidatorResult
128- checkPublished e =
129+ checkPublished e =
129130 case pNodes " published" (elChildren e) of
130131 [] -> valid
131132 [_] -> valid
132133 xs -> demand (" expected at most one 'published' field in 'entry' element, found: " ++ show (length xs))
133134
134135checkRights :: Element -> ValidatorResult
135- checkRights e =
136+ checkRights e =
136137 case pNodes " rights" (elChildren e) of
137138 [] -> valid
138139 [_] -> valid
139140 xs -> demand (" expected at most one 'rights' field in 'entry' element, found: " ++ show (length xs))
140141
141142checkSource :: Element -> ValidatorResult
142- checkSource e =
143+ checkSource e =
143144 case pNodes " source" (elChildren e) of
144145 [] -> valid
145146 [_] -> valid
146147 xs -> demand (" expected at most one 'source' field in 'entry' element, found: " ++ show (length xs))
147148
148149checkSummary :: Element -> ValidatorResult
149- checkSummary e =
150+ checkSummary e =
150151 case pNodes " summary" (elChildren e) of
151152 [] -> valid
152153 [_] -> valid
153154 xs -> demand (" expected at most one 'summary' field in 'entry' element, found: " ++ show (length xs))
154155
155156checkTitle :: Element -> ValidatorResult
156- checkTitle e =
157+ checkTitle e =
157158 case pNodes " title" (elChildren e) of
158159 [] -> demand " required field 'title' missing from 'entry' element"
159160 [_] -> valid
160161 xs -> demand (" only one 'title' field expected in 'entry' element, found: " ++ show (length xs))
161162
162163checkUpdated :: Element -> ValidatorResult
163- checkUpdated e =
164+ checkUpdated e =
164165 case pNodes " updated" (elChildren e) of
165166 [] -> demand " required field 'updated' missing from 'entry' element"
166167 [_] -> valid
@@ -173,7 +174,7 @@ checkCat e = mkTree []
173174 , checkLabel e
174175 ]
175176 where
176- checkScheme e' =
177+ checkScheme e' =
177178 case pAttrs " scheme" e' of
178179 [] -> valid
179180 (_: xs)
@@ -190,16 +191,16 @@ checkCat e = mkTree []
190191checkContent :: Element -> ValidatorResult
191192checkContent e = mkTree (flattenT (mkTree [] [type_valid, src_valid]))
192193 [case ty of
193- " text" ->
194+ " text" ->
194195 case onlyElems (elContent e) of
195196 [] -> valid
196197 _ -> demand (" content with type 'text' cannot have child elements, text only." )
197- " html" ->
198+ " html" ->
198199 case onlyElems (elContent e) of
199200 [] -> valid
200201 _ -> demand (" content with type 'html' cannot have child elements, text only." )
201202
202- " xhtml" ->
203+ " xhtml" ->
203204 case onlyElems (elContent e) of
204205 [] -> valid
205206 [_] -> valid -- ToDo: check that it is a 'div'.
@@ -208,22 +209,22 @@ checkContent e = mkTree (flattenT (mkTree [] [type_valid, src_valid]))
208209{-
209210 case parseMIMEType ty of
210211 Nothing -> valid
211- Just mt
212+ Just mt
212213 | isXmlType mt -> valid
213- | otherwise ->
214+ | otherwise ->
214215 case onlyElems (elContent e) of
215- [] -> valid -- check
216+ [] -> valid -- check
216217 _ -> demand ("content with MIME type '" ++ ty ++ "' must only contain base64 data")]
217218-}
218219 where
219- types = pAttrs " type" e
220- (ty, type_valid) =
220+ types = pAttrs " type" e
221+ (ty, type_valid) =
221222 case types of
222223 [] -> (" text" , valid)
223224 [t] -> checkTypeA t
224225 (t: ts) -> (t, demand (" Expected at most one 'type' attribute, found: " ++ show (1 + length ts)))
225226
226- src_valid =
227+ src_valid =
227228 case pAttrs " src" e of
228229 [] -> valid
229230 [_] ->
@@ -244,7 +245,7 @@ checkContent e = mkTree (flattenT (mkTree [] [type_valid, src_valid]))
244245{-
245246 case parseMIMEType v of
246247 Nothing -> ("text", demand ("Invalid/unknown type value " ++ v))
247- Just mt ->
248+ Just mt ->
248249 case mimeType mt of
249250 Multipart{} -> ("text", demand "Multipart MIME types not a legal 'type'")
250251 _ -> (v, valid)
@@ -253,7 +254,7 @@ checkContent e = mkTree (flattenT (mkTree [] [type_valid, src_valid]))
253254 std_types = [ " text" , " xhtml" , " html" ]
254255
255256checkTerm :: Element -> ValidatorResult
256- checkTerm e =
257+ checkTerm e =
257258 case pNodes " term" (elChildren e) of
258259 [] -> demand " required field 'term' missing from 'category' element"
259260 [_] -> valid
@@ -263,32 +264,32 @@ checkAuthor :: Element -> ValidatorResult
263264checkAuthor e = checkPerson e
264265
265266checkPerson :: Element -> ValidatorResult
266- checkPerson e =
267+ checkPerson e =
267268 mkTree (flattenT $ checkName e)
268269 [ checkEmail e
269270 , checkUri e
270271 ]
271-
272+
272273checkName :: Element -> ValidatorResult
273- checkName e =
274+ checkName e =
274275 case pNodes " name" (elChildren e) of
275276 [] -> demand " required field 'name' missing from 'author' element"
276277 [_] -> valid
277278 xs -> demand (" only one 'name' expected in 'author' element, found: " ++ show (length xs))
278-
279+
279280checkEmail :: Element -> ValidatorResult
280- checkEmail e =
281+ checkEmail e =
281282 case pNodes " email" (elChildren e) of
282283 [] -> valid
283- (_: xs)
284+ (_: xs)
284285 | null xs -> valid
285286 | otherwise -> demand (" at most one 'email' expected in 'author' element, found: " ++ show (1 + length xs))
286-
287+
287288checkUri :: Element -> ValidatorResult
288- checkUri e =
289+ checkUri e =
289290 case pNodes " email" (elChildren e) of
290291 [] -> valid
291- (_: xs)
292+ (_: xs)
292293 | null xs -> valid
293294 | otherwise -> demand (" at most one 'uri' expected in 'author' element, found: " ++ show (1 + length xs))
294295
0 commit comments