| 1 |
{-# LANGUAGE OverloadedStrings, TypeSynonymInstances, FlexibleInstances, CPP #-} |
| 2 |
{-# OPTIONS_GHC -fno-warn-orphans #-} -- IsString for XML |
| 3 |
|
| 4 |
-- | Overcome XML insanity, node by node. |
| 5 |
-- |
| 6 |
-- > {-# LANGUAGE OverloadedStrings #-} |
| 7 |
-- > |
| 8 |
-- > let doc = document "root" $ do |
| 9 |
-- > element "hello" $ content "world" |
| 10 |
-- > element "hierarchy" $ do |
| 11 |
-- > element "simple" True |
| 12 |
-- > element "as" ("it should be" :: Text) |
| 13 |
-- > toXML $ Just . T.pack $ "like this" |
| 14 |
-- > comment "that's it!" |
| 15 |
-- |
| 16 |
|
| 17 |
module Text.XML.Writer |
| 18 |
( |
| 19 |
-- * Documents |
| 20 |
document |
| 21 |
, documentA |
| 22 |
, documentD |
| 23 |
, documentAD |
| 24 |
, soap |
| 25 |
, pprint |
| 26 |
-- * Elements |
| 27 |
, XML |
| 28 |
-- ** Node creation |
| 29 |
, node |
| 30 |
, instruction |
| 31 |
, comment |
| 32 |
, element, elementMaybe, elementA |
| 33 |
, content |
| 34 |
, empty |
| 35 |
, many |
| 36 |
-- ** Element helpers |
| 37 |
, render, (!:) |
| 38 |
-- ** Converting data |
| 39 |
, ToXML(..) |
| 40 |
) where |
| 41 |
|
| 42 |
import Text.XML |
| 43 |
import Control.Monad.Writer.Strict |
| 44 |
import Data.Default () |
| 45 |
import qualified Data.DList as DL |
| 46 |
import qualified Data.Map as M |
| 47 |
|
| 48 |
import Data.Text (Text) |
| 49 |
import qualified Data.Text as T |
| 50 |
import qualified Data.Text.Lazy.IO as TL |
| 51 |
import Data.String (IsString(..)) |
| 52 |
|
| 53 |
#if MIN_VERSION_mtl(2,3,0) |
| 54 |
import Control.Monad |
| 55 |
#endif |
| 56 |
|
| 57 |
#if MIN_VERSION_xml_conduit(1,10,0) |
| 58 |
import Text.XML.Stream.Render.Internal (rsPretty) |
| 59 |
#endif |
| 60 |
|
| 61 |
-- | Node container to be rendered as children nodes. |
| 62 |
type XML = Writer (DL.DList Node) () |
| 63 |
|
| 64 |
-- | Create a simple Document starting with a root element. |
| 65 |
document :: Name -- ^ Root node name |
| 66 |
-> XML -- ^ Contents |
| 67 |
-> Document |
| 68 |
document name children = Document { documentPrologue = Prologue def def def |
| 69 |
, documentRoot = Element name def (render children) |
| 70 |
, documentEpilogue = def |
| 71 |
} |
| 72 |
|
| 73 |
-- | Create a simple Document starting with a root element with attributes. |
| 74 |
documentA :: Name -- ^ Root node name |
| 75 |
-> [(Name, Text)] -- ^ Attributes |
| 76 |
-> XML -- ^ Contents |
| 77 |
-> Document |
| 78 |
documentA name attrs children = Document { documentPrologue = Prologue def def def |
| 79 |
, documentRoot = Element name (M.fromList attrs) (render children) |
| 80 |
, documentEpilogue = def |
| 81 |
} |
| 82 |
|
| 83 |
-- | Create a simple Document starting with a root element with a doctype. |
| 84 |
documentD :: Name -- ^ Root node name |
| 85 |
-> Maybe Doctype -- ^ DOCTYPE |
| 86 |
-> XML -- ^ Contents |
| 87 |
-> Document |
| 88 |
documentD name dt children = Document { documentPrologue = Prologue def dt def |
| 89 |
, documentRoot = Element name def (render children) |
| 90 |
, documentEpilogue = def |
| 91 |
} |
| 92 |
|
| 93 |
-- | Create a simple Document starting with a root element with attributes and doctype. |
| 94 |
documentAD :: Name -- ^ Root node name |
| 95 |
-> [(Name, Text)] -- ^ Attributes |
| 96 |
-> Maybe Doctype -- ^ DOCTYPE |
| 97 |
-> XML -- ^ Contents |
| 98 |
-> Document |
| 99 |
documentAD name attrs dt children = Document { documentPrologue = Prologue def dt def |
| 100 |
, documentRoot = Element name (M.fromList attrs) (render children) |
| 101 |
, documentEpilogue = def |
| 102 |
} |
| 103 |
|
| 104 |
-- | Render document using xml-conduit's pretty-printer. |
| 105 |
pprint :: Document -> IO () |
| 106 |
pprint = TL.putStrLn . renderText def { rsPretty = True } |
| 107 |
|
| 108 |
-- | Convert collected nodes to a list of child nodes. |
| 109 |
render :: XML -> [Node] |
| 110 |
render = DL.toList . execWriter |
| 111 |
|
| 112 |
-- | Do nothing. |
| 113 |
empty :: XML |
| 114 |
empty = return () |
| 115 |
|
| 116 |
-- | Insert one node. |
| 117 |
node :: Node -> XML |
| 118 |
node = tell . DL.singleton |
| 119 |
|
| 120 |
-- | Insert an "Element" node constructed with name and children. |
| 121 |
element :: (ToXML a) => Name -> a -> XML |
| 122 |
element name children = node . NodeElement $! Element name def (render $ toXML children) |
| 123 |
|
| 124 |
-- | Insert an "Element" node converted from Maybe value or do nothing. |
| 125 |
elementMaybe :: (ToXML a) => Name -> Maybe a -> XML |
| 126 |
elementMaybe name = maybe empty (element name) |
| 127 |
|
| 128 |
-- | Insert an "Element" node constructed with name, attributes and children. |
| 129 |
elementA :: (ToXML a) => Name -> [(Name, Text)] -> a -> XML |
| 130 |
elementA name attrs children = node . NodeElement $! Element name (M.fromList attrs) (render $ toXML children) |
| 131 |
|
| 132 |
-- | Insert an "Instruction" node. |
| 133 |
instruction :: Text -> Text -> XML |
| 134 |
instruction target data_ = node . NodeInstruction $! Instruction target data_ |
| 135 |
|
| 136 |
-- | Insert a text comment node. |
| 137 |
comment :: Text -> XML |
| 138 |
comment = node . NodeComment |
| 139 |
|
| 140 |
-- | Insert text content node. |
| 141 |
content :: Text -> XML |
| 142 |
content = node . NodeContent |
| 143 |
|
| 144 |
-- | Mass-convert to nodes. |
| 145 |
-- |
| 146 |
-- > let array = element "container" $ many "wrapper" [1..3] |
| 147 |
-- |
| 148 |
-- Which gives: |
| 149 |
-- |
| 150 |
-- > <container> |
| 151 |
-- > <wrapper>1</wrapper> |
| 152 |
-- > <wrapper>2</wrapper> |
| 153 |
-- > <wrapper>3</wrapper> |
| 154 |
-- > </container> |
| 155 |
-- |
| 156 |
-- Use `mapM_ toXML xs` to convert a list without wrapping |
| 157 |
-- each item in separate element. |
| 158 |
-- |
| 159 |
-- > let mess = element "container" $ mapM_ toXML ["chunky", "chunk"] |
| 160 |
-- |
| 161 |
-- Content nodes tend to glue together: |
| 162 |
-- |
| 163 |
-- > <container>chunkychunk</container> |
| 164 |
many :: (ToXML a) |
| 165 |
=> Name -- ^ Container element name. |
| 166 |
-> [a] -- ^ Items to convert. |
| 167 |
-> XML |
| 168 |
many n = mapM_ (element n . toXML) |
| 169 |
|
| 170 |
-- | Attach a prefix to a Name. |
| 171 |
-- |
| 172 |
-- Because simply placing a colon in an element name |
| 173 |
-- yields 'Nothing' as a prefix and children will |
| 174 |
-- revert to en empty namespace. |
| 175 |
(!:) :: Text -> Name -> Name |
| 176 |
pref !: name = name { namePrefix = Just pref } |
| 177 |
|
| 178 |
-- | Provide instances for this class to use your data |
| 179 |
-- as "XML" nodes. |
| 180 |
class ToXML a where |
| 181 |
toXML :: a -> XML |
| 182 |
|
| 183 |
-- | Do nothing. |
| 184 |
instance ToXML () where |
| 185 |
toXML () = empty |
| 186 |
|
| 187 |
-- | Insert already prepared nodes. |
| 188 |
instance ToXML XML where |
| 189 |
toXML = id |
| 190 |
|
| 191 |
-- | Don't use [Char] please, it will scare OverloadedStrings. |
| 192 |
instance ToXML Text where |
| 193 |
toXML = content |
| 194 |
|
| 195 |
-- | XML schema uses lower case. |
| 196 |
instance ToXML Bool where |
| 197 |
toXML True = "true" |
| 198 |
toXML False = "false" |
| 199 |
|
| 200 |
instance ToXML Float where |
| 201 |
toXML = content . T.pack . show |
| 202 |
|
| 203 |
instance ToXML Double where |
| 204 |
toXML = content . T.pack . show |
| 205 |
|
| 206 |
instance ToXML Int where |
| 207 |
toXML = content . T.pack . show |
| 208 |
|
| 209 |
instance ToXML Integer where |
| 210 |
toXML = content . T.pack . show |
| 211 |
|
| 212 |
instance ToXML Char where |
| 213 |
toXML = content . T.singleton |
| 214 |
|
| 215 |
-- | Insert node if available. Otherwise do nothing. |
| 216 |
instance (ToXML a) => ToXML (Maybe a) where |
| 217 |
toXML = maybe empty toXML |
| 218 |
|
| 219 |
instance IsString XML where |
| 220 |
fromString = content . T.pack |
| 221 |
|
| 222 |
-- | Generate a SOAPv1.1 document. |
| 223 |
-- |
| 224 |
-- Empty header will be ignored. |
| 225 |
-- Envelope uses a `soapenv` prefix. |
| 226 |
-- Works great with 'ToXML' class. |
| 227 |
-- |
| 228 |
-- > data BigData = BigData { webScale :: Bool } |
| 229 |
-- > instance ToXML BigData where |
| 230 |
-- > toXML (BigData ws) = element ("v" !: "{vendor:uri}bigData") $ toXML ws |
| 231 |
-- > let doc = soap () (BigData True) |
| 232 |
soap :: (ToXML h, ToXML b) |
| 233 |
=> h |
| 234 |
-> b |
| 235 |
-> Document |
| 236 |
soap header body = document (sn "Envelope") $ do |
| 237 |
-- Some servers are allergic to dangling Headers... |
| 238 |
when (not $ null headerContent) $ do |
| 239 |
node . NodeElement $! Element (sn "Header") def headerContent |
| 240 |
element (sn "Body") (toXML body) |
| 241 |
|
| 242 |
where sn n = Name n (Just ns) (Just "soapenv") |
| 243 |
ns = "http://schemas.xmlsoap.org/soap/envelope/" |
| 244 |
headerContent = render (toXML header) |