~singpolyma/xml-types-haskell

ref: 64470ec69de1232f692f5cb9ff9a70cc8c067f83 xml-types-haskell/lib/Data/XML/Types.hs -rw-r--r-- 8.2 KiB
64470ec6 — Yitzchak Gale Add NFData instances to types. 10 years ago
                                                                                
e9387e7a John Millikin
9343c145 John Millikin
7a03b209 John Millikin
9343c145 John Millikin
7a03b209 John Millikin
9343c145 John Millikin
7a03b209 John Millikin
d90c1400 John Millikin
eedf8561 John Millikin
0074f184 John Millikin
d90c1400 John Millikin
0074f184 John Millikin
7a03b209 John Millikin
0074f184 John Millikin
7a03b209 John Millikin
0074f184 John Millikin
7a03b209 John Millikin
d90c1400 John Millikin
0074f184 John Millikin
fe5792a7 John Millikin
eedf8561 John Millikin
d90c1400 John Millikin
0074f184 John Millikin
d90c1400 John Millikin
eedf8561 John Millikin
d90c1400 John Millikin
e7c9e953 John Millikin
b2eb7497 John Millikin
eedf8561 John Millikin
b2eb7497 John Millikin
eedf8561 John Millikin
d90c1400 John Millikin
b2eb7497 John Millikin
eedf8561 John Millikin
b2eb7497 John Millikin
7a03b209 John Millikin
e9387e7a John Millikin
cb3f7338 John Millikin
e9387e7a John Millikin
cb3f7338 John Millikin
e9387e7a John Millikin
adb1d7a5 John Millikin
e9387e7a John Millikin
64470ec6 Yitzchak Gale
7a03b209 John Millikin
0074f184 John Millikin
7a03b209 John Millikin
cb3f7338 John Millikin
bd358272 John Millikin
64470ec6 Yitzchak Gale
0074f184 John Millikin
cb3f7338 John Millikin
bd358272 John Millikin
64470ec6 Yitzchak Gale
7a03b209 John Millikin
cb3f7338 John Millikin
bd358272 John Millikin
64470ec6 Yitzchak Gale
0074f184 John Millikin
cb3f7338 John Millikin
bd358272 John Millikin
64470ec6 Yitzchak Gale
0074f184 John Millikin
cb3f7338 John Millikin
bd358272 John Millikin
64470ec6 Yitzchak Gale
7a03b209 John Millikin
bd7a2801 John Millikin
7a03b209 John Millikin
cb3f7338 John Millikin
bd358272 John Millikin
64470ec6 Yitzchak Gale
0074f184 John Millikin
9343c145 John Millikin
cb3f7338 John Millikin
bd358272 John Millikin
64470ec6 Yitzchak Gale
9343c145 John Millikin
7a03b209 John Millikin
cb3f7338 John Millikin
bd358272 John Millikin
7a03b209 John Millikin
ed4e0ed0 John Millikin
68e5a6ef John Millikin
6d9f573f John Millikin
64470ec6 Yitzchak Gale
ab160642 John Millikin
0074f184 John Millikin
ab160642 John Millikin
0074f184 John Millikin
cb3f7338 John Millikin
bd358272 John Millikin
64470ec6 Yitzchak Gale
0074f184 John Millikin
cb3f7338 John Millikin
bd358272 John Millikin
64470ec6 Yitzchak Gale
fe5792a7 John Millikin
9343c145 John Millikin
e7d58245 John Millikin
fe5792a7 John Millikin
8955be32 John Millikin
fe5792a7 John Millikin
bd7a2801 John Millikin
fe5792a7 John Millikin
39362bee John Millikin
cb3f7338 John Millikin
bd358272 John Millikin
64470ec6 Yitzchak Gale
d90c1400 John Millikin
0074f184 John Millikin
bf051b2d John Millikin
d90c1400 John Millikin
e7d58245 John Millikin
f2ffe348 John Millikin
b2eb7497 John Millikin
d90c1400 John Millikin
eedf8561 John Millikin
f2ffe348 John Millikin
b2eb7497 John Millikin
997fda1c John Millikin
b2eb7497 John Millikin
997fda1c John Millikin
bd7a2801 John Millikin
b2eb7497 John Millikin
f2ffe348 John Millikin
b2eb7497 John Millikin
f2ffe348 John Millikin
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
296
297
298
299
300
301
302
{-# LANGUAGE DeriveDataTypeable #-}

-- |
-- Module: Data.XML.Types
-- Copyright: 2010-2011 John Millikin
-- License: MIT
--
-- Maintainer: jmillikin@gmail.com
-- Portability: portable
--
-- Basic types for representing XML.
--
-- The idea is to have a full set of appropriate types, which various XML
-- libraries can share. Instead of having equivalent-but-incompatible types
-- for every binding, parser, or client, they all share the same types can
-- can thus interoperate easily.
--
-- This library contains complete types for most parts of an XML document,
-- including the prologue, node tree, and doctype. Some basic combinators
-- are included for common tasks, including traversing the node tree and
-- filtering children.
--
module Data.XML.Types
	( -- * Types
	
	  -- ** Document prologue
	  Document (..)
	, Prologue (..)
	, Instruction (..)
	, Miscellaneous (..)
	
	-- ** Document body
	, Node (..)
	, Element (..)
	, Content (..)
	, Name (..)
	
	-- ** Doctypes
	, Doctype (..)
	, ExternalID (..)
	
	-- ** Incremental processing
	, Event (..)
	
	-- * Combinators
	
	-- ** Filters
	, isElement
	, isInstruction
	, isContent
	, isComment
	, isNamed
	
	-- ** Element traversal
	, elementChildren
	, elementContent
	, elementText
	
	-- ** Node traversal
	, nodeChildren
	, nodeContent
	, nodeText
	
	-- ** Attributes
	, hasAttribute
	, hasAttributeText
	, attributeContent
	, attributeText
	) where

import           Control.Monad ((>=>))
import           Data.Data (Data)
import           Data.Function (on)
import           Data.Maybe (isJust)
import           Data.String (IsString, fromString)
import           Data.Text (Text)
import qualified Data.Text as T
import           Data.Typeable (Typeable)
import           Control.DeepSeq (NFData(rnf))

data Document = Document
	{ documentPrologue :: Prologue
	, documentRoot :: Element
	, documentEpilogue :: [Miscellaneous]
	}
	deriving (Data, Eq, Ord, Show, Typeable)

instance NFData Document where
	rnf (Document a b c) = rnf a `seq` rnf b `seq` rnf c `seq` ()

data Prologue = Prologue
	{ prologueBefore :: [Miscellaneous]
	, prologueDoctype :: Maybe Doctype
	, prologueAfter :: [Miscellaneous]
	}
	deriving (Data, Eq, Ord, Show, Typeable)

instance NFData Prologue where
	rnf (Prologue a b c) = rnf a `seq` rnf b `seq` rnf c `seq` ()

data Instruction = Instruction
	{ instructionTarget :: Text
	, instructionData :: Text
	}
	deriving (Data, Eq, Ord, Show, Typeable)

instance NFData Instruction where
	rnf (Instruction a b) = rnf a `seq` rnf b `seq` ()

data Miscellaneous
	= MiscInstruction Instruction
	| MiscComment Text
	deriving (Data, Eq, Ord, Show, Typeable)

instance NFData Miscellaneous where
	rnf (MiscInstruction a) = rnf a `seq` ()
	rnf (MiscComment a)     = rnf a `seq` ()

data Node
	= NodeElement Element
	| NodeInstruction Instruction
	| NodeContent Content
	| NodeComment Text
	deriving (Data, Eq, Ord, Show, Typeable)

instance NFData Node where
	rnf (NodeElement a)     = rnf a `seq` ()
	rnf (NodeInstruction a) = rnf a `seq` ()
	rnf (NodeContent a)     = rnf a `seq` ()
	rnf (NodeComment a)     = rnf a `seq` ()

data Element = Element
	{ elementName :: Name
	, elementAttributes :: [(Name, [Content])]
	, elementNodes :: [Node]
	}
	deriving (Data, Eq, Ord, Show, Typeable)

instance NFData Element where
	rnf (Element a b c) = rnf a `seq` rnf b `seq` rnf c `seq` ()

data Content
	= ContentText Text
	| ContentEntity Text -- ^ For pass-through parsing
	deriving (Data, Eq, Ord, Show, Typeable)

instance NFData Content where
	rnf (ContentText a)   = rnf a `seq` ()
	rnf (ContentEntity a) = rnf a `seq` ()

-- | A fully qualified name.
--
-- Prefixes are not semantically important; they are included only to
-- simplify pass-through parsing. When comparing names with 'Eq' or 'Ord'
-- methods, prefixes are ignored.
--
-- The @IsString@ instance supports Clark notation; see
-- <http://www.jclark.com/xml/xmlns.htm> and
-- <http://infohost.nmt.edu/tcc/help/pubs/pylxml/etree-QName.html>. Use
-- the @OverloadedStrings@ language extension for very simple @Name@
-- construction:
--
-- > myname :: Name
-- > myname = "{http://example.com/ns/my-namespace}my-name"
--
data Name = Name
	{ nameLocalName :: Text
	, nameNamespace :: Maybe Text
	, namePrefix :: Maybe Text
	}
	deriving (Data, Show, Typeable)

instance Eq Name where
	(==) = (==) `on` (\x -> (nameNamespace x, nameLocalName x))

instance Ord Name where
	compare = compare `on` (\x -> (nameNamespace x, nameLocalName x))

instance IsString Name where
	fromString "" = Name T.empty Nothing Nothing
	fromString full@('{':rest) = case break (== '}') rest of
		(_, "") -> error ("Invalid Clark notation: " ++ show full)
		(ns, local) -> Name (T.pack (drop 1 local)) (Just (T.pack ns)) Nothing
	fromString local = Name (T.pack local) Nothing Nothing

instance NFData Name where
	rnf (Name a b c) = rnf a `seq` rnf b `seq` rnf c `seq` ()

-- | Note: due to the incredible complexity of DTDs, this type only supports
-- external subsets. I've tried adding internal subset types, but they
-- quickly gain more code than the rest of this module put together.
--
-- It is possible that some future version of this library might support
-- internal subsets, but I am no longer actively working on adding them.
data Doctype = Doctype
	{ doctypeName :: Text
	, doctypeID :: Maybe ExternalID
	}
	deriving (Data, Eq, Ord, Show, Typeable)

instance NFData Doctype where
	rnf (Doctype a b) = rnf a `seq` rnf b `seq` ()

data ExternalID
	= SystemID Text
	| PublicID Text Text
	deriving (Data, Eq, Ord, Show, Typeable)

instance NFData ExternalID where
	rnf (SystemID a)   = rnf a `seq` ()
	rnf (PublicID a b) = rnf a `seq` rnf b `seq` ()

-- | Some XML processing tools are incremental, and work in terms of events
-- rather than node trees. The 'Event' type allows a document to be fully
-- specified as a sequence of events.
--
-- Event-based XML libraries include:
--
-- * <http://hackage.haskell.org/package/xml-enumerator>
--
-- * <http://hackage.haskell.org/package/libxml-enumerator>
--
-- * <http://hackage.haskell.org/package/expat-enumerator>
--
data Event
	= EventBeginDocument
	| EventEndDocument
	| EventBeginDoctype Text (Maybe ExternalID)
	| EventEndDoctype
	| EventInstruction Instruction
	| EventBeginElement Name [(Name, [Content])]
	| EventEndElement Name
	| EventContent Content
	| EventComment Text
	| EventCDATA Text
	deriving (Data, Eq, Ord, Show, Typeable)

instance NFData Event where
	rnf (EventBeginDoctype a b) = rnf a `seq` rnf b `seq` ()
	rnf (EventInstruction a)    = rnf a `seq` ()
	rnf (EventBeginElement a b) = rnf a `seq` rnf b `seq` ()
	rnf (EventEndElement a)     = rnf a `seq` ()
	rnf (EventContent a)        = rnf a `seq` ()
	rnf (EventComment a)        = rnf a `seq` ()
	rnf (EventCDATA a)          = rnf a `seq` ()
	rnf _                       = ()

isElement :: Node -> [Element]
isElement (NodeElement e) = [e]
isElement _ = []

isInstruction :: Node -> [Instruction]
isInstruction (NodeInstruction i) = [i]
isInstruction _ = []

isContent :: Node -> [Content]
isContent (NodeContent c) = [c]
isContent _ = []

isComment :: Node -> [Text]
isComment (NodeComment t) = [t]
isComment _ = []

isNamed :: Name -> Element -> [Element]
isNamed n e = [e | elementName e == n]

elementChildren :: Element -> [Element]
elementChildren = elementNodes >=> isElement

elementContent :: Element -> [Content]
elementContent = elementNodes >=> isContent

elementText :: Element -> [Text]
elementText = elementContent >=> contentText

nodeChildren :: Node -> [Node]
nodeChildren = isElement >=> elementNodes

nodeContent :: Node -> [Content]
nodeContent = nodeChildren >=> isContent

nodeText :: Node -> [Text]
nodeText = nodeContent >=> contentText

hasAttribute :: Name -> Element -> [Element]
hasAttribute name e = [e | isJust (attributeContent name e)]

hasAttributeText :: Name -> (Text -> Bool) -> Element -> [Element]
hasAttributeText name p e = [e | maybe False p (attributeText name e)]

attributeContent :: Name -> Element -> Maybe [Content]
attributeContent name e = lookup name (elementAttributes e)

attributeText :: Name -> Element -> Maybe Text
attributeText name e = fmap contentFlat (attributeContent name e)

contentText :: Content -> [Text]
contentText (ContentText t) = [t]
contentText (ContentEntity entity) = [T.pack "&", entity, T.pack ";"]

contentFlat :: [Content] -> Text
contentFlat cs = T.concat (cs >>= contentText)