File: Validate.hs

package info (click to toggle)
haskell-feed 0.3.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 408 kB
  • sloc: haskell: 3,403; xml: 1,096; makefile: 2
file content (295 lines) | stat: -rw-r--r-- 9,177 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
--------------------------------------------------------------------
-- |
-- Module    : Text.Atom.Feed.Validate
-- Copyright : (c) Galois, Inc. 2008,
--             (c) Sigbjorn Finne 2009-
-- License   : BSD3
--
-- Maintainer: Sigbjorn Finne <sof@forkIO.com>
-- Stability : provisional
-- Portability: portable
--
--------------------------------------------------------------------
module Text.Atom.Feed.Validate where

import Text.Atom.Feed.Import
import Text.XML.Light.Types
import Text.XML.Light.Proc


import Data.List
import Data.Maybe

data VTree a = VNode [a] [VTree a] | VLeaf [a]
     deriving (Eq, Show)

type ValidatorResult = VTree (Bool,String)

advice :: String -> ValidatorResult
advice s = VLeaf [(False,s)]

demand :: String -> ValidatorResult
demand s = VLeaf [(True,s)]

valid :: ValidatorResult
valid = VLeaf []

mkTree :: [(Bool,String)] -> [ValidatorResult] -> ValidatorResult
mkTree as bs = VNode as bs

flattenT :: VTree a -> [a]
flattenT (VLeaf xs) = xs
flattenT (VNode as bs) = as ++ concatMap flattenT bs

validateEntry :: Element -> ValidatorResult
validateEntry e =
  mkTree []
     [ checkEntryAuthor e
     , checkCats e
     , checkContents e
     , checkContributor e
     , checkId e
     , checkContentLink e
     , checkLinks e
     , checkPublished e
     , checkRights e
     , checkSource e
     , checkSummary e
     , checkTitle e
     , checkUpdated e
     ]

-- Sec 4.1.2, check #1
checkEntryAuthor :: Element -> ValidatorResult
checkEntryAuthor e =
  case pNodes "author" (elChildren e) of
    [] -> -- required
      case pNode "summary" (elChildren e) of
        Nothing -> demand "Required 'author' element missing (no 'summary' either)"
	Just e1 ->
	  case pNode "author" (elChildren e1) of
	    Just a -> checkAuthor a
	    _ -> demand "Required 'author' element missing"
    xs -> mkTree [] $ map checkAuthor xs


-- Sec 4.1.2, check #2
checkCats :: Element -> ValidatorResult
checkCats e = mkTree [] $ map checkCat (pNodes "category" (elChildren e))

checkContents :: Element -> ValidatorResult
checkContents e =
  case pNodes "content" (elChildren e) of
    []  -> valid
    [c] -> mkTree [] $ [checkContent c]
    cs  -> mkTree (flattenT (demand ("at most one 'content' element expected inside 'entry', found: " ++ show (length cs))))
                  (map checkContent cs)


checkContributor :: Element -> ValidatorResult
checkContributor _e = valid

checkContentLink :: Element -> ValidatorResult
checkContentLink e =
  case pNodes "content" (elChildren e) of
    [] ->
      case pNodes "link" (elChildren e) of
        [] -> demand ("An 'entry' element with no 'content' element must have at least one 'link-rel' element")
	xs ->
	  case filter (=="alternate") $ mapMaybe (pAttr "rel") xs of
	    [] -> demand ("An 'entry' element with no 'content' element must have at least one 'link-rel' element")
	    _  -> valid
    _ -> valid

checkLinks :: Element -> ValidatorResult
checkLinks e =
  case pNodes "link" (elChildren e) of
    xs ->
      case map fst $ filter (\ (_,n) -> n =="alternate") $
            mapMaybe (\ ex -> fmap (\x -> (ex,x)) $ pAttr "rel" ex) xs of
       xs1 ->
         let
	  jmb (Just x) (Just y) = Just (x,y)
	  jmb _ _ = Nothing
	 in
         case mapMaybe (\ ex -> pAttr "type" ex `jmb` pAttr "hreflang" ex) xs1 of
	   xs2 ->
	     case any (\ x -> length x > 1) (group xs2) of
	       True -> demand ("An 'entry' element cannot have duplicate 'link-rel-alternate-type-hreflang' elements")
	       _ -> valid

checkId :: Element -> ValidatorResult
checkId e =
  case pNodes "id" (elChildren e) of
    []  -> demand "required field 'id' missing from 'entry' element"
    [_] -> valid
    xs  -> demand ("only one 'id' field expected in 'entry' element, found: " ++ show (length xs))

checkPublished :: Element -> ValidatorResult
checkPublished e =
  case pNodes "published" (elChildren e) of
    []  -> valid
    [_] -> valid
    xs  -> demand ("expected at most one 'published' field in 'entry' element, found: " ++ show (length xs))

checkRights :: Element -> ValidatorResult
checkRights e =
  case pNodes "rights" (elChildren e) of
    []  -> valid
    [_] -> valid
    xs  -> demand ("expected at most one 'rights' field in 'entry' element, found: " ++ show (length xs))

checkSource :: Element -> ValidatorResult
checkSource e =
  case pNodes "source" (elChildren e) of
    []  -> valid
    [_] -> valid
    xs  -> demand ("expected at most one 'source' field in 'entry' element, found: " ++ show (length xs))

checkSummary :: Element -> ValidatorResult
checkSummary e =
  case pNodes "summary" (elChildren e) of
    []  -> valid
    [_] -> valid
    xs  -> demand ("expected at most one 'summary' field in 'entry' element, found: " ++ show (length xs))

checkTitle :: Element -> ValidatorResult
checkTitle e =
  case pNodes "title" (elChildren e) of
    []  -> demand "required field 'title' missing from 'entry' element"
    [_] -> valid
    xs  -> demand ("only one 'title' field expected in 'entry' element, found: " ++ show (length xs))

checkUpdated :: Element -> ValidatorResult
checkUpdated e =
  case pNodes "updated" (elChildren e) of
    []  -> demand "required field 'updated' missing from 'entry' element"
    [_] -> valid
    xs  -> demand ("only one 'updated' field expected in 'entry' element, found: " ++ show (length xs))

checkCat :: Element -> ValidatorResult
checkCat e = mkTree []
  [ checkTerm e
  , checkScheme e
  , checkLabel e
  ]
 where
  checkScheme e' =
    case pAttrs "scheme" e' of
      [] -> valid
      (_:xs)
        | null xs   -> valid
	| otherwise -> demand ("Expected at most one 'scheme' attribute, found: " ++ show (1+length xs))

  checkLabel e' =
    case pAttrs "label" e' of
      [] -> valid
      (_:xs)
        | null xs   -> valid
	| otherwise -> demand ("Expected at most one 'label' attribute, found: " ++ show (1+length xs))

checkContent :: Element -> ValidatorResult
checkContent e = mkTree (flattenT (mkTree [] [type_valid, src_valid]))
  [case ty of
    "text" ->
      case onlyElems (elContent e) of
        [] -> valid
	_  -> demand ("content with type 'text' cannot have child elements, text only.")
    "html" ->
      case onlyElems (elContent e) of
        [] -> valid
	_  -> demand ("content with type 'html' cannot have child elements, text only.")

    "xhtml" ->
      case onlyElems (elContent e) of
        []  -> valid
	[_] -> valid -- ToDo: check that it is a 'div'.
	_ds -> demand ("content with type 'xhtml' should only contain one 'div' child.")
    _ -> valid]
{-
      case parseMIMEType ty of
        Nothing -> valid
	Just mt
	  | isXmlType mt -> valid
          | otherwise ->
            case onlyElems (elContent e) of
              [] -> valid -- check
	      _  -> demand ("content with MIME type '" ++ ty ++ "' must only contain base64 data")]
-}
 where
  types = pAttrs "type" e
  (ty, type_valid) =
    case types of
      []  -> ("text", valid)
      [t] -> checkTypeA t
      (t:ts) -> (t, demand ("Expected at most one 'type' attribute, found: " ++ show (1+length ts)))

  src_valid =
    case pAttrs "src" e of
      []     -> valid
      [_]   ->
        case types of
	  []    -> advice "It is advisable to provide a 'type' along with a 'src' attribute"
	  (_:_) -> valid
{-
	    case parseMIMEType t of
	      Just{} -> valid
	      _      -> demand "The 'type' attribute must be a valid MIME type"
-}
      ss -> demand ("Expected at most one 'src' attribute, found: " ++ show (length ss))


  checkTypeA v
    | v `elem` std_types = (v, valid)
    | otherwise = (v,valid)
{-
        case parseMIMEType v of
	  Nothing -> ("text", demand ("Invalid/unknown type value " ++ v))
	  Just mt ->
	    case mimeType mt of
	      Multipart{} -> ("text", demand "Multipart MIME types not a legal 'type'")
	      _ -> (v, valid)
-}
   where
    std_types = [ "text", "xhtml", "html"]

checkTerm :: Element -> ValidatorResult
checkTerm e =
  case pNodes "term" (elChildren e) of
    []  -> demand "required field 'term' missing from 'category' element"
    [_] -> valid
    xs  -> demand ("only one 'term' field expected in 'category' element, found: " ++ show (length xs))

checkAuthor :: Element -> ValidatorResult
checkAuthor e = checkPerson e

checkPerson :: Element -> ValidatorResult
checkPerson e =
   mkTree (flattenT $ checkName e)
          [ checkEmail e
          , checkUri e
          ]

checkName :: Element -> ValidatorResult
checkName e =
  case pNodes "name" (elChildren e) of
    []  -> demand "required field 'name' missing from 'author' element"
    [_] -> valid
    xs  -> demand ("only one 'name' expected in 'author' element, found: " ++ show (length xs))

checkEmail :: Element -> ValidatorResult
checkEmail e =
  case pNodes "email" (elChildren e) of
    [] -> valid
    (_:xs)
     | null xs   -> valid
     | otherwise -> demand ("at most one 'email' expected in 'author' element, found: " ++ show (1+length xs))

checkUri :: Element -> ValidatorResult
checkUri e =
  case pNodes "email" (elChildren e) of
    [] -> valid
    (_:xs)
     | null xs   -> valid
     | otherwise -> demand ("at most one 'uri' expected in 'author' element, found: " ++ show (1+length xs))