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
|
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleContexts #-}
module Text.XML.Stream.Token
( tokenToBuilder
, TName (..)
, Token (..)
, TAttribute
, NSLevel (..)
) where
import Data.XML.Types (Instruction (..), Content (..), ExternalID (..))
import qualified Data.Text as T
import Data.Text (Text)
import Data.String (IsString (fromString))
import Blaze.ByteString.Builder
(Builder, fromByteString, writeByteString, copyByteString)
import Blaze.ByteString.Builder.Internal.Write (fromWriteList)
import Blaze.ByteString.Builder.Char.Utf8 (writeChar, fromText)
import Data.Monoid (mconcat, mempty, mappend)
import Data.ByteString.Char8 ()
import Data.Map (Map)
import qualified Blaze.ByteString.Builder.Char8 as BC8
import qualified Data.Set as Set
import Data.List (foldl')
import Control.Arrow (first)
oneSpace :: Builder
oneSpace = copyByteString " "
data Token = TokenBeginDocument [TAttribute]
| TokenInstruction Instruction
| TokenBeginElement TName [TAttribute] Bool Int -- ^ indent
| TokenEndElement TName
| TokenContent Content
| TokenComment Text
| TokenDoctype Text (Maybe ExternalID) [(Text, Text)]
| TokenCDATA Text
deriving Show
tokenToBuilder :: Token -> Builder
tokenToBuilder (TokenBeginDocument attrs) =
fromByteString "<?xml"
`mappend` foldAttrs oneSpace attrs (fromByteString "?>")
tokenToBuilder (TokenInstruction (Instruction target data_)) = mconcat
[ fromByteString "<?"
, fromText target
, fromByteString " "
, fromText data_
, fromByteString "?>"
]
tokenToBuilder (TokenBeginElement name attrs' isEmpty indent) =
copyByteString "<"
`mappend` tnameToText name
`mappend` foldAttrs
(if indent == 0 || lessThan3 attrs
then oneSpace
else BC8.fromString ('\n' : replicate indent ' '))
attrs
(if isEmpty then fromByteString "/>" else fromByteString ">")
where
attrs = nubAttrs $ map (first splitTName) attrs'
lessThan3 [] = True
lessThan3 [_] = True
lessThan3 [_, _] = True
lessThan3 _ = False
tokenToBuilder (TokenEndElement name) = mconcat
[ fromByteString "</"
, tnameToText name
, fromByteString ">"
]
tokenToBuilder (TokenContent c) = contentToText c
tokenToBuilder (TokenCDATA t) =
copyByteString "<![CDATA["
`mappend` fromText t
`mappend` copyByteString "]]>"
tokenToBuilder (TokenComment t) = mconcat [fromByteString "<!--", fromText t, fromByteString "-->"]
tokenToBuilder (TokenDoctype name eid _) = mconcat
[ fromByteString "<!DOCTYPE "
, fromText name
, go eid
, fromByteString ">"
]
where
go Nothing = mempty
go (Just (SystemID uri)) = mconcat
[ fromByteString " SYSTEM \""
, fromText uri
, fromByteString "\""
]
go (Just (PublicID pid uri)) = mconcat
[ fromByteString " PUBLIC \""
, fromText pid
, fromByteString "\" \""
, fromText uri
, fromByteString "\""
]
data TName = TName (Maybe Text) Text
deriving (Show, Eq, Ord)
tnameToText :: TName -> Builder
tnameToText (TName Nothing name) = fromText name
tnameToText (TName (Just prefix) name) = mconcat [fromText prefix, fromByteString ":", fromText name]
contentToText :: Content -> Builder
contentToText (ContentText t) =
fromWriteList go $ T.unpack t
where
go '<' = writeByteString "<"
go '>' = writeByteString ">"
go '&' = writeByteString "&"
-- Not escaping quotes, since this is only called outside of attributes
go c = writeChar c
contentToText (ContentEntity e) = mconcat
[ fromByteString "&"
, fromText e
, fromByteString ";"
]
type TAttribute = (TName, [Content])
foldAttrs :: Builder -- ^ before
-> [TAttribute]
-> Builder
-> Builder
foldAttrs before attrs rest' =
foldr go rest' attrs
where
go (key, val) rest =
before
`mappend` tnameToText key
`mappend` copyByteString "=\""
`mappend` foldr go' (fromByteString "\"" `mappend` rest) val
go' (ContentText t) rest =
fromWriteList h (T.unpack t) `mappend` rest
where
h '<' = writeByteString "<"
h '>' = writeByteString ">"
h '&' = writeByteString "&"
h '"' = writeByteString """
-- Not escaping single quotes, since our attributes are always double
-- quoted
h c = writeChar c
go' (ContentEntity t) rest =
fromByteString "&"
`mappend` fromText t
`mappend` fromByteString ";"
`mappend` rest
instance IsString TName where
fromString = TName Nothing . T.pack
data NSLevel = NSLevel
{ defaultNS :: Maybe Text
, prefixes :: Map Text Text
}
deriving Show
nubAttrs :: [TAttribute] -> [TAttribute]
nubAttrs orig =
front []
where
(front, _) = foldl' go (id, Set.empty) orig
go (dlist, used) (k, v)
| k `Set.member` used = (dlist, used)
| otherwise = (dlist . ((k, v):), Set.insert k used)
splitTName :: TName -> TName
splitTName x@(TName Just{} _) = x
splitTName x@(TName Nothing t)
| T.null b = x
| otherwise = TName (Just a) $ T.drop 1 b
where
(a, b) = T.break (== ':') t
|