1 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
2 {-# LANGUAGE OverloadedStrings #-}
3 {-# OPTIONS_GHC -fno-warn-orphans #-}
4 module Language.XML where
6 import Control.Applicative (Applicative(..))
8 import Data.Default.Class (Default(..))
9 import Data.Eq (Eq(..))
10 import Data.Function (($), (.))
12 import Data.Map.Strict (Map)
13 import Data.Maybe (Maybe(..))
14 import Data.Monoid (Monoid(..))
15 import Data.Ord (Ord(..))
16 import Data.Semigroup (Semigroup(..))
17 import Data.Sequence (Seq)
18 import Data.String (IsString(..))
19 import Data.Text (Text)
20 import Data.TreeSeq.Strict (Tree)
21 import Prelude (error, pred)
22 import Text.Show (Show(..), showsPrec, showChar, showString)
23 import qualified Data.List as List
24 import qualified Data.Text as Text
26 import Language.TCT.Cell
29 type XML = Tree (Cell XmlName) (Cell XmlLeaf)
35 { xmlNamePrefix :: Text
36 , xmlNameSpace :: Text
37 , xmlNameLocal :: Text
39 instance Show XmlName where
40 showsPrec _p XmlName{xmlNameSpace="", ..} =
41 showString (Text.unpack xmlNameLocal)
42 showsPrec _p XmlName{..} =
43 if Text.null xmlNameSpace
44 then showString (Text.unpack xmlNameLocal)
47 showString (Text.unpack xmlNameSpace) .
49 showString (Text.unpack xmlNameLocal)
50 instance Eq XmlName where
51 XmlName _ sx lx == XmlName _ sy ly = sx == sy && lx == ly
52 instance Ord XmlName where
53 XmlName _ sx lx `compare` XmlName _ sy ly = compare sx sy <> compare lx ly
54 instance IsString XmlName where
55 fromString "" = XmlName "" "" ""
56 fromString full@('{':rest) =
57 case List.break (== '}') rest of
58 (_, "") -> error ("Invalid Clark notation: " <> show full)
59 (ns, local) -> XmlName "" (Text.pack ns) (Text.pack $ List.drop 1 local)
60 fromString local = XmlName "" "" (Text.pack local)
62 xmlLocalName :: Text -> XmlName
63 xmlLocalName = XmlName "" ""
67 = XmlAttr XmlName Text
70 deriving (Eq,Ord,Show)
76 newtype Nat = Nat { unNat :: Int }
77 deriving (Eq, Ord, Show)
79 predNat :: Nat -> Maybe Nat
80 predNat (Nat n) | n <= 0 = Nothing
81 | otherwise = Just $ Nat $ pred n
84 newtype Nat1 = Nat1 { unNat1 :: Int }
85 deriving (Eq, Ord, Show)
87 predNat1 :: Nat1 -> Maybe Nat1
88 predNat1 (Nat1 n) | n <= 1 = Nothing
89 | otherwise = Just $ Nat1 $ pred n
92 newtype Ident = Ident { unIdent :: Text }
93 deriving (Eq,Show,Default,IsString)
94 instance Default Text where
98 newtype URL = URL Text
99 deriving (Eq,Show,Default)
102 newtype Path = Path Text
103 deriving (Eq,Show,Default)
107 = MayText { unMayText :: Text }
108 deriving (Eq,Show,Default)
109 instance Semigroup MayText where
114 whenMayText :: Applicative m => MayText -> (MayText -> m ()) -> m ()
115 whenMayText (MayText "") _f = pure ()
116 whenMayText t f = f t