{-# LANGUAGE ExistentialQuantification #-}

-- | The class 'XmlContent' is a kind of replacement for Read and Show:
--   it provides conversions between a generic XML tree representation
--   and your own more specialised typeful Haskell data trees.
--
--   If you are starting with a set of Haskell datatypes, use DrIFT to
--   derive instances of this class for you:
--       http:\/\/repetae.net\/john\/computer\/haskell\/DrIFT
--   If you are starting with an XML DTD, use HaXml's tool DtdToHaskell
--   to generate both the Haskell types and the corresponding instances.
--
--   This unified class interface replaces two previous (somewhat similar)
--   classes: Haskell2Xml and Xml2Haskell.  There was no real reason to have
--   separate classes depending on how you originally defined your datatypes.
--   However, some instances for basic types like lists will depend on which
--   direction you are using.  See Text.XML.HaXml.XmlContent and
--   Text.XML.HaXml.XmlContent.Haskell.

--   The methods 'toContents' and 'parseContents' convert a value to and from
--   a generic internal representation of an XML document /without/ a DTD.
--   The functions 'toXml' and 'fromXml' convert a value to and from a generic
--   internal representation of an XML document /including/ a DTD.
--   The functions 'readXml' and 'showXml' convert to and from Strings.
--   The functions 'fReadXml' and 'fWriteXml' do the conversion to and from
--   the given filenames.
--   The functions 'hGetXml' and 'hPutXml' do the conversion to and from
--   the given file handles.
--   (See the type signatures.)
--

module Text.XML.HaXml.XmlContent.Parser
  ( -- * Re-export the relevant set of generic XML document type definitions
    Document(..)
  , Element(..)
  , ElemTag(..)
  , Content(..)
  , Attribute()
  , AttValue(..)
  , Prolog(..)
  , Reference(..)
  -- * The enabling classes, that define parsing\/unparsing between Haskell
  --   datatypes and a generic XML representation.
  , XmlContent(..)
  , XmlAttributes(..)
  , XmlAttrType(..)
  -- ** Auxiliaries for writing parsers in the XmlContent class
  , module Text.ParserCombinators.Poly
  , XMLParser
  , content, posnElement, element, interior, inElement, text, attributes
  , posnElementWith, elementWith, inElementWith
  , choice, definite -- ???
  -- ** Auxiliaries for generating in the XmlContent class
  , mkElem, mkElemC, mkAttr
  , toText, toCData
  -- ** Auxiliaries for the attribute-related classes
  , maybeToAttr, defaultToAttr
  , definiteA, defaultA, possibleA, fromAttrToStr, toAttrFrStr
  , Defaultable(..)
  , str2attr, attr2str, attval
  , catMaybes   -- re-exported from Maybe
  -- * Explicit representation of Haskell datatype information
  --   (for conversion to a DTD)
  , module Text.XML.HaXml.TypeMapping
  -- * Types useful for some content models
  , List1(..)
  , ANYContent(..)
  ) where

import Control.Monad (void)
import Data.Maybe (catMaybes)
import Data.Char  (chr, isSpace)

import Text.XML.HaXml.Types
import Text.XML.HaXml.Namespaces
import Text.XML.HaXml.TypeMapping
import Text.XML.HaXml.Posn     (Posn)
import Text.XML.HaXml.Verbatim (Verbatim(verbatim))

import Text.ParserCombinators.Poly

--  #define DEBUG

#if defined(DEBUG)
import Debug.Trace(trace)
debug :: a -> String -> a
v `debug` s = trace s v
#else
debug :: t -> t1 -> t
t
v debug :: t -> t1 -> t
`debug` t1
_ = t
v
#endif


------------------------------------------------------------------------
-- | Read a single attribute called "value".
attval :: (Read a) => Element i -> a
attval :: Element i -> a
attval (Elem QName
_ [(QName
_{-N "value"-},v :: AttValue
v@(AttValue [Either String Reference]
_))] []) = String -> a
forall a. Read a => String -> a
read (AttValue -> String
forall a. Show a => a -> String
show AttValue
v)

-- | Generate a single attribute.
mkAttr :: String -> String -> Attribute
mkAttr :: String -> String -> Attribute
mkAttr String
n String
v = (String -> QName
N String
n, [Either String Reference] -> AttValue
AttValue [String -> Either String Reference
forall a b. a -> Either a b
Left String
v])

-- | Generate an element with no attributes, named for its HType.
mkElem :: XmlContent a => a -> [Content ()] -> Content ()
mkElem :: a -> [Content ()] -> Content ()
mkElem a
x [Content ()]
cs  = Element () -> () -> Content ()
forall i. Element i -> i -> Content i
CElem (QName -> [Attribute] -> [Content ()] -> Element ()
forall i. QName -> [Attribute] -> [Content i] -> Element i
Elem (String -> QName
N (HType -> ShowS
showHType (a -> HType
forall a. HTypeable a => a -> HType
toHType a
x) String
"")) [] [Content ()]
cs) ()

-- | Generate an element with no attributes, named directly.
mkElemC :: String -> [Content ()] -> Content ()
mkElemC :: String -> [Content ()] -> Content ()
mkElemC String
x [Content ()]
cs = Element () -> () -> Content ()
forall i. Element i -> i -> Content i
CElem (QName -> [Attribute] -> [Content ()] -> Element ()
forall i. QName -> [Attribute] -> [Content i] -> Element i
Elem (String -> QName
N String
x) [] [Content ()]
cs) ()

-- | Turn a simple string into XML text.
toText :: String -> [Content ()]
toText :: String -> [Content ()]
toText String
s = [Bool -> String -> () -> Content ()
forall i. Bool -> String -> i -> Content i
CString Bool
False String
s ()]

-- | Turn a string into an XML CDATA section.
--   (i.e. special characters like '&' are preserved without interpretation.)
toCData :: String -> [Content ()]
toCData :: String -> [Content ()]
toCData String
s = [Bool -> String -> () -> Content ()
forall i. Bool -> String -> i -> Content i
CString Bool
True String
s ()]


------------------------------------------------------------------------

-- | We need a parsing monad for reading generic XML Content into specific
--   datatypes.  This is a specialisation of the Text.ParserCombinators.Poly
--   ones, where the input token type is fixed as XML Content.
type XMLParser a = Parser (Content Posn) a


------------------------------------------------------------------------
-- Some useful parsing combinators
------------------------------------------------------------------------

-- | The most primitive combinator for XMLParser - get one content item.
content :: String -> XMLParser (Content Posn)
content :: String -> XMLParser (Content Posn)
content String
word = XMLParser (Content Posn)
forall t. Parser t t
next XMLParser (Content Posn) -> ShowS -> XMLParser (Content Posn)
forall (p :: * -> *) a. Commitment p => p a -> ShowS -> p a
`adjustErr` (String -> ShowS
forall a. [a] -> [a] -> [a]
++String
" when expecting "String -> ShowS
forall a. [a] -> [a] -> [a]
++String
word)

-- | Get the next content element, checking that it has one of the required
--   tags, using the given matching function.
--   (Skips over comments and whitespace, rejects text and refs.
--    Also returns position of element.)
posnElementWith :: (String->String->Bool) -> [String]
                   -> XMLParser (Posn, Element Posn)
posnElementWith :: (String -> String -> Bool)
-> [String] -> XMLParser (Posn, Element Posn)
posnElementWith String -> String -> Bool
match [String]
tags = do
    { Content Posn
c <- String -> XMLParser (Content Posn)
content ([String] -> String
formatted [String]
tags)
    ; case Content Posn
c of
          CElem e :: Element Posn
e@(Elem QName
t [Attribute]
_ [Content Posn]
_) Posn
pos
              | (String -> Bool) -> [String] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (String -> String -> Bool
match (QName -> String
localName QName
t)) [String]
tags -> (Posn, Element Posn) -> XMLParser (Posn, Element Posn)
forall (m :: * -> *) a. Monad m => a -> m a
return (Posn
pos, Element Posn
e)
              | Bool
otherwise   -> String -> XMLParser (Posn, Element Posn)
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String
"Found a <"String -> ShowS
forall a. [a] -> [a] -> [a]
++QName -> String
printableName QName
t
                                     String -> ShowS
forall a. [a] -> [a] -> [a]
++String
">, but expected "
                                     String -> ShowS
forall a. [a] -> [a] -> [a]
++[String] -> String
formatted [String]
tagsString -> ShowS
forall a. [a] -> [a] -> [a]
++String
"\nat "String -> ShowS
forall a. [a] -> [a] -> [a]
++Posn -> String
forall a. Show a => a -> String
show Posn
pos)
          CString Bool
b String
s Posn
pos
              | Bool -> Bool
not Bool
b Bool -> Bool -> Bool
&& (Char -> Bool) -> String -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Char -> Bool
isSpace String
s -> (String -> String -> Bool)
-> [String] -> XMLParser (Posn, Element Posn)
posnElementWith String -> String -> Bool
match [String]
tags
                                                        -- ignore blank space
              | Bool
otherwise -> String -> XMLParser (Posn, Element Posn)
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String
"Found text content, but expected "
                                  String -> ShowS
forall a. [a] -> [a] -> [a]
++[String] -> String
formatted [String]
tagsString -> ShowS
forall a. [a] -> [a] -> [a]
++String
"\ntext is: "String -> ShowS
forall a. [a] -> [a] -> [a]
++String
s
                                  String -> ShowS
forall a. [a] -> [a] -> [a]
++String
"\nat "String -> ShowS
forall a. [a] -> [a] -> [a]
++Posn -> String
forall a. Show a => a -> String
show Posn
pos)
          CRef Reference
r Posn
pos -> String -> XMLParser (Posn, Element Posn)
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String
"Found reference, but expected "
                             String -> ShowS
forall a. [a] -> [a] -> [a]
++[String] -> String
formatted [String]
tagsString -> ShowS
forall a. [a] -> [a] -> [a]
++String
"\nreference is: "String -> ShowS
forall a. [a] -> [a] -> [a]
++Reference -> String
forall a. Verbatim a => a -> String
verbatim Reference
r
                             String -> ShowS
forall a. [a] -> [a] -> [a]
++String
"\nat "String -> ShowS
forall a. [a] -> [a] -> [a]
++Posn -> String
forall a. Show a => a -> String
show Posn
pos)
          CMisc Misc
_ Posn
_ -> (String -> String -> Bool)
-> [String] -> XMLParser (Posn, Element Posn)
posnElementWith String -> String -> Bool
match [String]
tags  -- skip comments, PIs, etc.
    }
  where
    formatted :: [String] -> String
formatted [String
t]  = String
"a <"String -> ShowS
forall a. [a] -> [a] -> [a]
++String
tString -> ShowS
forall a. [a] -> [a] -> [a]
++String
">"
    formatted [String]
tgs = String
"one of"String -> ShowS
forall a. [a] -> [a] -> [a]
++ ShowS -> [String] -> String
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\String
t->String
" <"String -> ShowS
forall a. [a] -> [a] -> [a]
++String
tString -> ShowS
forall a. [a] -> [a] -> [a]
++String
">") [String]
tgs

-- | A specialisation of @posnElementWith (==)@.
posnElement :: [String] -> XMLParser (Posn, Element Posn)
posnElement :: [String] -> XMLParser (Posn, Element Posn)
posnElement = (String -> String -> Bool)
-> [String] -> XMLParser (Posn, Element Posn)
posnElementWith String -> String -> Bool
forall a. Eq a => a -> a -> Bool
(==)

-- | Get the next content element, checking that it has one of the required
--   tags.  (Skips over comments and whitespace, rejects text and refs.)
element :: [String] -> XMLParser (Element Posn)
element :: [String] -> XMLParser (Element Posn)
element [String]
tags = ((Posn, Element Posn) -> Element Posn)
-> XMLParser (Posn, Element Posn) -> XMLParser (Element Posn)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Posn, Element Posn) -> Element Posn
forall a b. (a, b) -> b
snd ([String] -> XMLParser (Posn, Element Posn)
posnElement [String]
tags)
                                XMLParser (Element Posn) -> String -> XMLParser (Element Posn)
forall t t1. t -> t1 -> t
`debug` (String
"Element: "String -> ShowS
forall a. [a] -> [a] -> [a]
++[String] -> String
unwords [String]
tagsString -> ShowS
forall a. [a] -> [a] -> [a]
++String
"\n")

-- | Like element, only permits a more flexible match against the tagname.
elementWith :: (String->String->Bool) -> [String] -> XMLParser (Element Posn)
elementWith :: (String -> String -> Bool) -> [String] -> XMLParser (Element Posn)
elementWith String -> String -> Bool
match [String]
tags = ((Posn, Element Posn) -> Element Posn)
-> XMLParser (Posn, Element Posn) -> XMLParser (Element Posn)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Posn, Element Posn) -> Element Posn
forall a b. (a, b) -> b
snd ((String -> String -> Bool)
-> [String] -> XMLParser (Posn, Element Posn)
posnElementWith String -> String -> Bool
match [String]
tags)
                                XMLParser (Element Posn) -> String -> XMLParser (Element Posn)
forall t t1. t -> t1 -> t
`debug` (String
"Element: "String -> ShowS
forall a. [a] -> [a] -> [a]
++[String] -> String
unwords [String]
tagsString -> ShowS
forall a. [a] -> [a] -> [a]
++String
"\n")

-- | Run an XMLParser on the contents of the given element (i.e. not on the
--   current monadic content sequence), checking that the contents are
--   exhausted, before returning the calculated value within the current
--   parser context.
interior :: Element Posn -> XMLParser a -> XMLParser a
interior :: Element Posn -> XMLParser a -> XMLParser a
interior (Elem QName
e [Attribute]
_ [Content Posn]
cs) XMLParser a
p =
    case XMLParser a -> [Content Posn] -> (Either String a, [Content Posn])
forall t a. Parser t a -> [t] -> (Either String a, [t])
runParser XMLParser a
p [Content Posn]
cs of
        (Left String
msg, [Content Posn]
_) -> String -> XMLParser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
msg
        (Right a
x, []) -> a -> XMLParser a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x
        (Right a
x, ds :: [Content Posn]
ds@(Content Posn
d:[Content Posn]
_))
            | (Content Posn -> Bool) -> [Content Posn] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Content Posn -> Bool
forall i. Content i -> Bool
onlyMisc [Content Posn]
ds -> a -> XMLParser a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x
            | Bool
otherwise       -> String -> XMLParser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String
"Too many elements inside <"
                                      String -> ShowS
forall a. [a] -> [a] -> [a]
++QName -> String
printableName QName
eString -> ShowS
forall a. [a] -> [a] -> [a]
++String
"> at\n"
                                      String -> ShowS
forall a. [a] -> [a] -> [a]
++Posn -> String
forall a. Show a => a -> String
show (Content Posn -> Posn
forall t. Content t -> t
info Content Posn
d)String -> ShowS
forall a. [a] -> [a] -> [a]
++String
"\n"
                                      String -> ShowS
forall a. [a] -> [a] -> [a]
++String
"Found excess: "
                                      String -> ShowS
forall a. [a] -> [a] -> [a]
++[Content Posn] -> String
forall a. Verbatim a => a -> String
verbatim (Int -> [Content Posn] -> [Content Posn]
forall a. Int -> [a] -> [a]
take Int
7 [Content Posn]
ds)
                                      String -> ShowS
forall a. [a] -> [a] -> [a]
++String
"\n[...]")
  where onlyMisc :: Content i -> Bool
onlyMisc (CMisc Misc
_ i
_) = Bool
True
        onlyMisc (CString Bool
False String
s i
_) | (Char -> Bool) -> String -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Char -> Bool
isSpace String
s = Bool
True
        onlyMisc Content i
_ = Bool
False

-- | A combination of element + interior.
inElement :: String -> XMLParser a -> XMLParser a
inElement :: String -> XMLParser a -> XMLParser a
inElement String
tag XMLParser a
p = do { Element Posn
e <- [String] -> XMLParser (Element Posn)
element [String
tag]; XMLParser a -> XMLParser a
forall (p :: * -> *) a. Commitment p => p a -> p a
commit (Element Posn -> XMLParser a -> XMLParser a
forall a. Element Posn -> XMLParser a -> XMLParser a
interior Element Posn
e XMLParser a
p) }

-- | A combination of elementWith + interior.
inElementWith :: (String->String->Bool) -> String -> XMLParser a -> XMLParser a
inElementWith :: (String -> String -> Bool) -> String -> XMLParser a -> XMLParser a
inElementWith String -> String -> Bool
match String
tag XMLParser a
p = do { Element Posn
e <- (String -> String -> Bool) -> [String] -> XMLParser (Element Posn)
elementWith String -> String -> Bool
match [String
tag]
                               ; XMLParser a -> XMLParser a
forall (p :: * -> *) a. Commitment p => p a -> p a
commit (Element Posn -> XMLParser a -> XMLParser a
forall a. Element Posn -> XMLParser a -> XMLParser a
interior Element Posn
e XMLParser a
p) }

-- | Do some parsing of the attributes of the given element
attributes :: XmlAttributes a => Element Posn -> XMLParser a
attributes :: Element Posn -> XMLParser a
attributes (Elem QName
_ [Attribute]
as [Content Posn]
_) = a -> XMLParser a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Attribute] -> a
forall a. XmlAttributes a => [Attribute] -> a
fromAttrs [Attribute]
as)

-- | 'text' is a counterpart to 'element', parsing text content if it
--   exists.  Adjacent text and references are coalesced.
text :: XMLParser String
text :: XMLParser String
text = [String] -> XMLParser String
text' []
  where text' :: [String] -> XMLParser String
text' [String]
acc =
          do { Content Posn
c <- String -> XMLParser (Content Posn)
content String
"plain text"
             ; case Content Posn
c of
                 CString Bool
_ String
s Posn
_        -> [String] -> XMLParser String
text' (String
sString -> [String] -> [String]
forall a. a -> [a] -> [a]
:[String]
acc)
                 CRef (RefChar Int
s) Posn
_   -> [String] -> XMLParser String
text' ((String
"&#"String -> ShowS
forall a. [a] -> [a] -> [a]
++Int -> String
forall a. Show a => a -> String
show Int
sString -> ShowS
forall a. [a] -> [a] -> [a]
++String
";") String -> [String] -> [String]
forall a. a -> [a] -> [a]
:[String]
acc)
                 CRef (RefEntity String
s) Posn
_ -> [String] -> XMLParser String
text' ((Char
'&'Char -> ShowS
forall a. a -> [a] -> [a]
:String
sString -> ShowS
forall a. [a] -> [a] -> [a]
++String
";")String -> [String] -> [String]
forall a. a -> [a] -> [a]
:[String]
acc)
                 CMisc Misc
_ Posn
_            -> [String] -> XMLParser String
text' [String]
acc
                 CElem Element Posn
_ Posn
_         -> do { [Content Posn] -> Parser (Content Posn) ()
forall t. [t] -> Parser t ()
reparse [Content Posn
c] -- put it back!
                                         ; if [String] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [String]
acc then String -> XMLParser String
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"empty string"
                                           else String -> XMLParser String
forall (m :: * -> *) a. Monad m => a -> m a
return ([String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([String] -> [String]
forall a. [a] -> [a]
reverse [String]
acc))
                                         }
             }
          XMLParser String -> XMLParser String -> XMLParser String
forall t a. Parser t a -> Parser t a -> Parser t a
`onFail` ( if [String] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [String]
acc then String -> XMLParser String
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"empty string"
                     else String -> XMLParser String
forall (m :: * -> *) a. Monad m => a -> m a
return ([String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([String] -> [String]
forall a. [a] -> [a]
reverse [String]
acc)) )


-- | 'choice f p' means if parseContents succeeds, apply f to the result,
--   otherwise use the continuation parser.
choice :: XmlContent a => (a -> b) -> XMLParser b -> XMLParser b
choice :: (a -> b) -> XMLParser b -> XMLParser b
choice a -> b
cons (P [Content Posn] -> Result [Content Posn] b
other) =
    ([Content Posn] -> Result [Content Posn] b) -> XMLParser b
forall t a. ([t] -> Result [t] a) -> Parser t a
P (\[Content Posn]
cs-> case Parser (Content Posn) a
-> [Content Posn] -> (Either String a, [Content Posn])
forall t a. Parser t a -> [t] -> (Either String a, [t])
runParser Parser (Content Posn) a
forall a. XmlContent a => XMLParser a
parseContents [Content Posn]
cs of
                 (Left String
_, [Content Posn]
_)  -> [Content Posn] -> Result [Content Posn] b
other [Content Posn]
cs
                 (Right a
x, [Content Posn]
cs') -> [Content Posn] -> b -> Result [Content Posn] b
forall z a. z -> a -> Result z a
Success [Content Posn]
cs' (a -> b
cons a
x) )

--choice cons other = fmap cons parseContents `onFail` other

-- | not sure this is needed now.   'definite p' previously ensured that
--   an element was definitely present.  Now I think the monad might take
--   care of that for us.
definite :: XmlContent a => XMLParser a -> String -> String -> XMLParser a
definite :: XMLParser a -> String -> String -> XMLParser a
definite XMLParser a
p String
inner String
tag = ([Content Posn] -> Result [Content Posn] a) -> XMLParser a
forall t a. ([t] -> Result [t] a) -> Parser t a
P (\[Content Posn]
cs-> case XMLParser a -> [Content Posn] -> (Either String a, [Content Posn])
forall t a. Parser t a -> [t] -> (Either String a, [t])
runParser XMLParser a
p [Content Posn]
cs of
                                   (Left String
_, [Content Posn]
cs')   -> [Content Posn] -> String -> Result [Content Posn] a
forall z a. z -> String -> Result z a
Failure [Content Posn]
cs' String
msg'
                                   (Right a
x, [Content Posn]
cs')  -> [Content Posn] -> a -> Result [Content Posn] a
forall z a. z -> a -> Result z a
Success [Content Posn]
cs' a
x )
  where msg' :: String
msg' = String
"content error: expected "String -> ShowS
forall a. [a] -> [a] -> [a]
++String
innerString -> ShowS
forall a. [a] -> [a] -> [a]
++String
" inside <"String -> ShowS
forall a. [a] -> [a] -> [a]
++String
tag
               String -> ShowS
forall a. [a] -> [a] -> [a]
++String
"> element\n"

------------------------------------------------------------------------

-- | The @XmlContent@ class promises that an XML Content element can be
--   converted to and from a Haskell value.
class HTypeable a => XmlContent a where
    -- | Convert from XML to Haskell
    parseContents :: XMLParser a
    -- | Convert from Haskell to XML
    toContents    :: a -> [Content ()]

    -- | Dummy functions (for most types): used /only/ in the Char instance
    --   for coercing lists of Char into String.
    xToChar       :: a -> Char
    xFromChar     :: Char -> a
    xToChar        = String -> a -> Char
forall a. HasCallStack => String -> a
error String
"HaXml.XmlContent.xToChar used in error"
    xFromChar      = String -> Char -> a
forall a. HasCallStack => String -> a
error String
"HaXml.XmlContent.xFromChar used in error"

-- | The @XmlAttributes@ class promises that a list of XML tag attributes
--   can be converted to and from a Haskell value.
class XmlAttributes a where
    fromAttrs :: [Attribute] -> a
    toAttrs   :: a -> [Attribute]
-- | The @XmlAttrType@ class promises that an attribute taking an XML
--   enumerated type can be converted to and from a Haskell value.
class XmlAttrType a where
    fromAttrToTyp :: String -> Attribute -> Maybe a
    toAttrFrTyp   :: String -> a -> Maybe Attribute


------------------------------------------------------------------------
-- Instances for some of the standard basic datatypes.
-- Both DtdToHaskell and Haskell2Xml share these instances.
------------------------------------------------------------------------

instance (XmlContent a, XmlContent b) => XmlContent (a,b) where
    toContents :: (a, b) -> [Content ()]
toContents (a
a,b
b) = a -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents a
a [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ b -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents b
b
    parseContents :: XMLParser (a, b)
parseContents    = do
        { a
a <- XMLParser a
forall a. XmlContent a => XMLParser a
parseContents
        ; b
b <- XMLParser b
forall a. XmlContent a => XMLParser a
parseContents
        ; (a, b) -> XMLParser (a, b)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a,b
b)
        }

instance (XmlContent a, XmlContent b, XmlContent c) => XmlContent (a,b,c) where
    toContents :: (a, b, c) -> [Content ()]
toContents (a
a,b
b,c
c) = a -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents a
a [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ b -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents b
b [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ c -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents c
c
    parseContents :: XMLParser (a, b, c)
parseContents = do
        { a
a <- XMLParser a
forall a. XmlContent a => XMLParser a
parseContents
        ; b
b <- XMLParser b
forall a. XmlContent a => XMLParser a
parseContents
        ; c
c <- XMLParser c
forall a. XmlContent a => XMLParser a
parseContents
        ; (a, b, c) -> XMLParser (a, b, c)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a,b
b,c
c)
        }

instance (XmlContent a, XmlContent b, XmlContent c, XmlContent d) =>
         XmlContent (a,b,c,d) where
    toContents :: (a, b, c, d) -> [Content ()]
toContents (a
a,b
b,c
c,d
d) = a -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents a
a [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ b -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents b
b [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ c -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents c
c
                           [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ d -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents d
d
    parseContents :: XMLParser (a, b, c, d)
parseContents = do
        { a
a <- XMLParser a
forall a. XmlContent a => XMLParser a
parseContents
        ; b
b <- XMLParser b
forall a. XmlContent a => XMLParser a
parseContents
        ; c
c <- XMLParser c
forall a. XmlContent a => XMLParser a
parseContents
        ; d
d <- XMLParser d
forall a. XmlContent a => XMLParser a
parseContents
        ; (a, b, c, d) -> XMLParser (a, b, c, d)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a,b
b,c
c,d
d)
        }

instance ( XmlContent a, XmlContent b, XmlContent c, XmlContent d
         , XmlContent e ) =>
         XmlContent (a,b,c,d,e) where
    toContents :: (a, b, c, d, e) -> [Content ()]
toContents (a
a,b
b,c
c,d
d,e
e) = a -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents a
a [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ b -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents b
b [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ c -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents c
c
                             [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ d -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents d
d [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ e -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents e
e
    parseContents :: XMLParser (a, b, c, d, e)
parseContents = do
        { a
a <- XMLParser a
forall a. XmlContent a => XMLParser a
parseContents
        ; b
b <- XMLParser b
forall a. XmlContent a => XMLParser a
parseContents
        ; c
c <- XMLParser c
forall a. XmlContent a => XMLParser a
parseContents
        ; d
d <- XMLParser d
forall a. XmlContent a => XMLParser a
parseContents
        ; e
e <- XMLParser e
forall a. XmlContent a => XMLParser a
parseContents
        ; (a, b, c, d, e) -> XMLParser (a, b, c, d, e)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a,b
b,c
c,d
d,e
e)
        }

instance ( XmlContent a, XmlContent b, XmlContent c, XmlContent d
         , XmlContent e, XmlContent f ) =>
         XmlContent (a,b,c,d,e,f) where
    toContents :: (a, b, c, d, e, f) -> [Content ()]
toContents (a
a,b
b,c
c,d
d,e
e,f
f) = a -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents a
a [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ b -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents b
b [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ c -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents c
c
                               [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ d -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents d
d [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ e -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents e
e [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ f -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents f
f
    parseContents :: XMLParser (a, b, c, d, e, f)
parseContents = do
        { a
a <- XMLParser a
forall a. XmlContent a => XMLParser a
parseContents
        ; b
b <- XMLParser b
forall a. XmlContent a => XMLParser a
parseContents
        ; c
c <- XMLParser c
forall a. XmlContent a => XMLParser a
parseContents
        ; d
d <- XMLParser d
forall a. XmlContent a => XMLParser a
parseContents
        ; e
e <- XMLParser e
forall a. XmlContent a => XMLParser a
parseContents
        ; f
f <- XMLParser f
forall a. XmlContent a => XMLParser a
parseContents
        ; (a, b, c, d, e, f) -> XMLParser (a, b, c, d, e, f)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a,b
b,c
c,d
d,e
e,f
f)
        }

instance ( XmlContent a, XmlContent b, XmlContent c, XmlContent d
         , XmlContent e, XmlContent f, XmlContent g ) =>
         XmlContent (a,b,c,d,e,f,g) where
    toContents :: (a, b, c, d, e, f, g) -> [Content ()]
toContents (a
a,b
b,c
c,d
d,e
e,f
f,g
g)
        = a -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents a
a [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ b -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents b
b [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ c -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents c
c [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ d -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents d
d
          [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ e -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents e
e [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ f -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents f
f [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ g -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents g
g
    parseContents :: XMLParser (a, b, c, d, e, f, g)
parseContents = do
        { a
a <- XMLParser a
forall a. XmlContent a => XMLParser a
parseContents
        ; b
b <- XMLParser b
forall a. XmlContent a => XMLParser a
parseContents
        ; c
c <- XMLParser c
forall a. XmlContent a => XMLParser a
parseContents
        ; d
d <- XMLParser d
forall a. XmlContent a => XMLParser a
parseContents
        ; e
e <- XMLParser e
forall a. XmlContent a => XMLParser a
parseContents
        ; f
f <- XMLParser f
forall a. XmlContent a => XMLParser a
parseContents
        ; g
g <- XMLParser g
forall a. XmlContent a => XMLParser a
parseContents
        ; (a, b, c, d, e, f, g) -> XMLParser (a, b, c, d, e, f, g)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a,b
b,c
c,d
d,e
e,f
f,g
g)
        }

instance ( XmlContent a, XmlContent b, XmlContent c, XmlContent d
         , XmlContent e, XmlContent f, XmlContent g, XmlContent h ) =>
         XmlContent (a,b,c,d,e,f,g,h) where
    toContents :: (a, b, c, d, e, f, g, h) -> [Content ()]
toContents (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h)
        = a -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents a
a [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ b -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents b
b [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ c -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents c
c [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ d -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents d
d
          [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ e -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents e
e [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ f -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents f
f [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ g -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents g
g [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ h -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents h
h
    parseContents :: XMLParser (a, b, c, d, e, f, g, h)
parseContents = do
        { a
a <- XMLParser a
forall a. XmlContent a => XMLParser a
parseContents
        ; b
b <- XMLParser b
forall a. XmlContent a => XMLParser a
parseContents
        ; c
c <- XMLParser c
forall a. XmlContent a => XMLParser a
parseContents
        ; d
d <- XMLParser d
forall a. XmlContent a => XMLParser a
parseContents
        ; e
e <- XMLParser e
forall a. XmlContent a => XMLParser a
parseContents
        ; f
f <- XMLParser f
forall a. XmlContent a => XMLParser a
parseContents
        ; g
g <- XMLParser g
forall a. XmlContent a => XMLParser a
parseContents
        ; h
h <- XMLParser h
forall a. XmlContent a => XMLParser a
parseContents
        ; (a, b, c, d, e, f, g, h) -> XMLParser (a, b, c, d, e, f, g, h)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h)
        }

instance ( XmlContent a, XmlContent b, XmlContent c, XmlContent d
         , XmlContent e, XmlContent f, XmlContent g, XmlContent h
         , XmlContent i ) =>
         XmlContent (a,b,c,d,e,f,g,h,i) where
    toContents :: (a, b, c, d, e, f, g, h, i) -> [Content ()]
toContents (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h,i
i)
        = a -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents a
a [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ b -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents b
b [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ c -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents c
c [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ d -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents d
d
          [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ e -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents e
e [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ f -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents f
f [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ g -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents g
g [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ h -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents h
h
          [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ i -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents i
i
    parseContents :: XMLParser (a, b, c, d, e, f, g, h, i)
parseContents = do
        { a
a <- XMLParser a
forall a. XmlContent a => XMLParser a
parseContents
        ; b
b <- XMLParser b
forall a. XmlContent a => XMLParser a
parseContents
        ; c
c <- XMLParser c
forall a. XmlContent a => XMLParser a
parseContents
        ; d
d <- XMLParser d
forall a. XmlContent a => XMLParser a
parseContents
        ; e
e <- XMLParser e
forall a. XmlContent a => XMLParser a
parseContents
        ; f
f <- XMLParser f
forall a. XmlContent a => XMLParser a
parseContents
        ; g
g <- XMLParser g
forall a. XmlContent a => XMLParser a
parseContents
        ; h
h <- XMLParser h
forall a. XmlContent a => XMLParser a
parseContents
        ; i
i <- XMLParser i
forall a. XmlContent a => XMLParser a
parseContents
        ; (a, b, c, d, e, f, g, h, i)
-> XMLParser (a, b, c, d, e, f, g, h, i)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h,i
i)
        }

instance ( XmlContent a, XmlContent b, XmlContent c, XmlContent d
         , XmlContent e, XmlContent f, XmlContent g, XmlContent h
         , XmlContent i, XmlContent j ) =>
         XmlContent (a,b,c,d,e,f,g,h,i,j) where
    toContents :: (a, b, c, d, e, f, g, h, i, j) -> [Content ()]
toContents (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h,i
i,j
j)
        = a -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents a
a [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ b -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents b
b [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ c -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents c
c [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ d -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents d
d
          [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ e -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents e
e [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ f -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents f
f [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ g -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents g
g [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ h -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents h
h
          [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ i -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents i
i [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ j -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents j
j
    parseContents :: XMLParser (a, b, c, d, e, f, g, h, i, j)
parseContents = do
        { a
a <- XMLParser a
forall a. XmlContent a => XMLParser a
parseContents
        ; b
b <- XMLParser b
forall a. XmlContent a => XMLParser a
parseContents
        ; c
c <- XMLParser c
forall a. XmlContent a => XMLParser a
parseContents
        ; d
d <- XMLParser d
forall a. XmlContent a => XMLParser a
parseContents
        ; e
e <- XMLParser e
forall a. XmlContent a => XMLParser a
parseContents
        ; f
f <- XMLParser f
forall a. XmlContent a => XMLParser a
parseContents
        ; g
g <- XMLParser g
forall a. XmlContent a => XMLParser a
parseContents
        ; h
h <- XMLParser h
forall a. XmlContent a => XMLParser a
parseContents
        ; i
i <- XMLParser i
forall a. XmlContent a => XMLParser a
parseContents
        ; j
j <- XMLParser j
forall a. XmlContent a => XMLParser a
parseContents
        ; (a, b, c, d, e, f, g, h, i, j)
-> XMLParser (a, b, c, d, e, f, g, h, i, j)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h,i
i,j
j)
        }

instance ( XmlContent a, XmlContent b, XmlContent c, XmlContent d
         , XmlContent e, XmlContent f, XmlContent g, XmlContent h
         , XmlContent i, XmlContent j, XmlContent k ) =>
         XmlContent (a,b,c,d,e,f,g,h,i,j,k) where
    toContents :: (a, b, c, d, e, f, g, h, i, j, k) -> [Content ()]
toContents (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h,i
i,j
j,k
k)
        = a -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents a
a [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ b -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents b
b [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ c -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents c
c [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ d -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents d
d
          [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ e -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents e
e [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ f -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents f
f [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ g -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents g
g [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ h -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents h
h
          [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ i -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents i
i [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ j -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents j
j [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ k -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents k
k
    parseContents :: XMLParser (a, b, c, d, e, f, g, h, i, j, k)
parseContents = do
        { a
a <- XMLParser a
forall a. XmlContent a => XMLParser a
parseContents
        ; b
b <- XMLParser b
forall a. XmlContent a => XMLParser a
parseContents
        ; c
c <- XMLParser c
forall a. XmlContent a => XMLParser a
parseContents
        ; d
d <- XMLParser d
forall a. XmlContent a => XMLParser a
parseContents
        ; e
e <- XMLParser e
forall a. XmlContent a => XMLParser a
parseContents
        ; f
f <- XMLParser f
forall a. XmlContent a => XMLParser a
parseContents
        ; g
g <- XMLParser g
forall a. XmlContent a => XMLParser a
parseContents
        ; h
h <- XMLParser h
forall a. XmlContent a => XMLParser a
parseContents
        ; i
i <- XMLParser i
forall a. XmlContent a => XMLParser a
parseContents
        ; j
j <- XMLParser j
forall a. XmlContent a => XMLParser a
parseContents
        ; k
k <- XMLParser k
forall a. XmlContent a => XMLParser a
parseContents
        ; (a, b, c, d, e, f, g, h, i, j, k)
-> XMLParser (a, b, c, d, e, f, g, h, i, j, k)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h,i
i,j
j,k
k)
        }

instance ( XmlContent a, XmlContent b, XmlContent c, XmlContent d
         , XmlContent e, XmlContent f, XmlContent g, XmlContent h
         , XmlContent i, XmlContent j, XmlContent k, XmlContent l ) =>
         XmlContent (a,b,c,d,e,f,g,h,i,j,k,l) where
    toContents :: (a, b, c, d, e, f, g, h, i, j, k, l) -> [Content ()]
toContents (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h,i
i,j
j,k
k,l
l)
        = a -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents a
a [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ b -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents b
b [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ c -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents c
c [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ d -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents d
d
          [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ e -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents e
e [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ f -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents f
f [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ g -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents g
g [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ h -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents h
h
          [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ i -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents i
i [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ j -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents j
j [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ k -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents k
k [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ l -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents l
l
    parseContents :: XMLParser (a, b, c, d, e, f, g, h, i, j, k, l)
parseContents = do
        { a
a <- XMLParser a
forall a. XmlContent a => XMLParser a
parseContents
        ; b
b <- XMLParser b
forall a. XmlContent a => XMLParser a
parseContents
        ; c
c <- XMLParser c
forall a. XmlContent a => XMLParser a
parseContents
        ; d
d <- XMLParser d
forall a. XmlContent a => XMLParser a
parseContents
        ; e
e <- XMLParser e
forall a. XmlContent a => XMLParser a
parseContents
        ; f
f <- XMLParser f
forall a. XmlContent a => XMLParser a
parseContents
        ; g
g <- XMLParser g
forall a. XmlContent a => XMLParser a
parseContents
        ; h
h <- XMLParser h
forall a. XmlContent a => XMLParser a
parseContents
        ; i
i <- XMLParser i
forall a. XmlContent a => XMLParser a
parseContents
        ; j
j <- XMLParser j
forall a. XmlContent a => XMLParser a
parseContents
        ; k
k <- XMLParser k
forall a. XmlContent a => XMLParser a
parseContents
        ; l
l <- XMLParser l
forall a. XmlContent a => XMLParser a
parseContents
        ; (a, b, c, d, e, f, g, h, i, j, k, l)
-> XMLParser (a, b, c, d, e, f, g, h, i, j, k, l)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h,i
i,j
j,k
k,l
l)
        }

instance ( XmlContent a, XmlContent b, XmlContent c, XmlContent d
         , XmlContent e, XmlContent f, XmlContent g, XmlContent h
         , XmlContent i, XmlContent j, XmlContent k, XmlContent l
         , XmlContent m ) =>
         XmlContent (a,b,c,d,e,f,g,h,i,j,k,l,m) where
    toContents :: (a, b, c, d, e, f, g, h, i, j, k, l, m) -> [Content ()]
toContents (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h,i
i,j
j,k
k,l
l,m
m)
        = a -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents a
a [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ b -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents b
b [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ c -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents c
c [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ d -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents d
d
          [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ e -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents e
e [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ f -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents f
f [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ g -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents g
g [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ h -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents h
h
          [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ i -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents i
i [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ j -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents j
j [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ k -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents k
k [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ l -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents l
l
          [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ m -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents m
m
    parseContents :: XMLParser (a, b, c, d, e, f, g, h, i, j, k, l, m)
parseContents = do
        { a
a <- XMLParser a
forall a. XmlContent a => XMLParser a
parseContents
        ; b
b <- XMLParser b
forall a. XmlContent a => XMLParser a
parseContents
        ; c
c <- XMLParser c
forall a. XmlContent a => XMLParser a
parseContents
        ; d
d <- XMLParser d
forall a. XmlContent a => XMLParser a
parseContents
        ; e
e <- XMLParser e
forall a. XmlContent a => XMLParser a
parseContents
        ; f
f <- XMLParser f
forall a. XmlContent a => XMLParser a
parseContents
        ; g
g <- XMLParser g
forall a. XmlContent a => XMLParser a
parseContents
        ; h
h <- XMLParser h
forall a. XmlContent a => XMLParser a
parseContents
        ; i
i <- XMLParser i
forall a. XmlContent a => XMLParser a
parseContents
        ; j
j <- XMLParser j
forall a. XmlContent a => XMLParser a
parseContents
        ; k
k <- XMLParser k
forall a. XmlContent a => XMLParser a
parseContents
        ; l
l <- XMLParser l
forall a. XmlContent a => XMLParser a
parseContents
        ; m
m <- XMLParser m
forall a. XmlContent a => XMLParser a
parseContents
        ; (a, b, c, d, e, f, g, h, i, j, k, l, m)
-> XMLParser (a, b, c, d, e, f, g, h, i, j, k, l, m)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h,i
i,j
j,k
k,l
l,m
m)
        }

instance ( XmlContent a, XmlContent b, XmlContent c, XmlContent d
         , XmlContent e, XmlContent f, XmlContent g, XmlContent h
         , XmlContent i, XmlContent j, XmlContent k, XmlContent l
         , XmlContent m, XmlContent n ) =>
         XmlContent (a,b,c,d,e,f,g,h,i,j,k,l,m,n) where
    toContents :: (a, b, c, d, e, f, g, h, i, j, k, l, m, n) -> [Content ()]
toContents (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h,i
i,j
j,k
k,l
l,m
m,n
n)
        = a -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents a
a [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ b -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents b
b [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ c -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents c
c [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ d -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents d
d
          [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ e -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents e
e [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ f -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents f
f [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ g -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents g
g [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ h -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents h
h
          [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ i -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents i
i [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ j -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents j
j [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ k -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents k
k [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ l -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents l
l
          [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ m -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents m
m [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ n -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents n
n
    parseContents :: XMLParser (a, b, c, d, e, f, g, h, i, j, k, l, m, n)
parseContents = do
        { a
a <- XMLParser a
forall a. XmlContent a => XMLParser a
parseContents
        ; b
b <- XMLParser b
forall a. XmlContent a => XMLParser a
parseContents
        ; c
c <- XMLParser c
forall a. XmlContent a => XMLParser a
parseContents
        ; d
d <- XMLParser d
forall a. XmlContent a => XMLParser a
parseContents
        ; e
e <- XMLParser e
forall a. XmlContent a => XMLParser a
parseContents
        ; f
f <- XMLParser f
forall a. XmlContent a => XMLParser a
parseContents
        ; g
g <- XMLParser g
forall a. XmlContent a => XMLParser a
parseContents
        ; h
h <- XMLParser h
forall a. XmlContent a => XMLParser a
parseContents
        ; i
i <- XMLParser i
forall a. XmlContent a => XMLParser a
parseContents
        ; j
j <- XMLParser j
forall a. XmlContent a => XMLParser a
parseContents
        ; k
k <- XMLParser k
forall a. XmlContent a => XMLParser a
parseContents
        ; l
l <- XMLParser l
forall a. XmlContent a => XMLParser a
parseContents
        ; m
m <- XMLParser m
forall a. XmlContent a => XMLParser a
parseContents
        ; n
n <- XMLParser n
forall a. XmlContent a => XMLParser a
parseContents
        ; (a, b, c, d, e, f, g, h, i, j, k, l, m, n)
-> XMLParser (a, b, c, d, e, f, g, h, i, j, k, l, m, n)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h,i
i,j
j,k
k,l
l,m
m,n
n)
        }

instance ( XmlContent a, XmlContent b, XmlContent c, XmlContent d
         , XmlContent e, XmlContent f, XmlContent g, XmlContent h
         , XmlContent i, XmlContent j, XmlContent k, XmlContent l
         , XmlContent m, XmlContent n, XmlContent o ) =>
         XmlContent (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o) where
    toContents :: (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) -> [Content ()]
toContents (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h,i
i,j
j,k
k,l
l,m
m,n
n,o
o)
        = a -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents a
a [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ b -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents b
b [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ c -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents c
c [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ d -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents d
d
          [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ e -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents e
e [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ f -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents f
f [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ g -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents g
g [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ h -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents h
h
          [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ i -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents i
i [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ j -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents j
j [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ k -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents k
k [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ l -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents l
l
          [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ m -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents m
m [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ n -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents n
n [Content ()] -> [Content ()] -> [Content ()]
forall a. [a] -> [a] -> [a]
++ o -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents o
o
    parseContents :: XMLParser (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
parseContents = do
        { a
a <- XMLParser a
forall a. XmlContent a => XMLParser a
parseContents
        ; b
b <- XMLParser b
forall a. XmlContent a => XMLParser a
parseContents
        ; c
c <- XMLParser c
forall a. XmlContent a => XMLParser a
parseContents
        ; d
d <- XMLParser d
forall a. XmlContent a => XMLParser a
parseContents
        ; e
e <- XMLParser e
forall a. XmlContent a => XMLParser a
parseContents
        ; f
f <- XMLParser f
forall a. XmlContent a => XMLParser a
parseContents
        ; g
g <- XMLParser g
forall a. XmlContent a => XMLParser a
parseContents
        ; h
h <- XMLParser h
forall a. XmlContent a => XMLParser a
parseContents
        ; i
i <- XMLParser i
forall a. XmlContent a => XMLParser a
parseContents
        ; j
j <- XMLParser j
forall a. XmlContent a => XMLParser a
parseContents
        ; k
k <- XMLParser k
forall a. XmlContent a => XMLParser a
parseContents
        ; l
l <- XMLParser l
forall a. XmlContent a => XMLParser a
parseContents
        ; m
m <- XMLParser m
forall a. XmlContent a => XMLParser a
parseContents
        ; n
n <- XMLParser n
forall a. XmlContent a => XMLParser a
parseContents
        ; o
o <- XMLParser o
forall a. XmlContent a => XMLParser a
parseContents
        ; (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
-> XMLParser (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h,i
i,j
j,k
k,l
l,m
m,n
n,o
o)
        }


------------------------------------------------------------------------
-- Useful auxiliaries for "fromAttributes"
------------------------------------------------------------------------

-- | If an attribute is defaultable, then it either takes the default
--   value (which is omitted from the output), or a non-default value
--   (which obviously must be printed).
data Defaultable a  = Default a    | NonDefault a    deriving (Defaultable a -> Defaultable a -> Bool
(Defaultable a -> Defaultable a -> Bool)
-> (Defaultable a -> Defaultable a -> Bool) -> Eq (Defaultable a)
forall a. Eq a => Defaultable a -> Defaultable a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Defaultable a -> Defaultable a -> Bool
$c/= :: forall a. Eq a => Defaultable a -> Defaultable a -> Bool
== :: Defaultable a -> Defaultable a -> Bool
$c== :: forall a. Eq a => Defaultable a -> Defaultable a -> Bool
Eq,Int -> Defaultable a -> ShowS
[Defaultable a] -> ShowS
Defaultable a -> String
(Int -> Defaultable a -> ShowS)
-> (Defaultable a -> String)
-> ([Defaultable a] -> ShowS)
-> Show (Defaultable a)
forall a. Show a => Int -> Defaultable a -> ShowS
forall a. Show a => [Defaultable a] -> ShowS
forall a. Show a => Defaultable a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Defaultable a] -> ShowS
$cshowList :: forall a. Show a => [Defaultable a] -> ShowS
show :: Defaultable a -> String
$cshow :: forall a. Show a => Defaultable a -> String
showsPrec :: Int -> Defaultable a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> Defaultable a -> ShowS
Show)

searchMaybe :: (a -> Maybe b) -> [a] -> Maybe b
searchMaybe :: (a -> Maybe b) -> [a] -> Maybe b
searchMaybe a -> Maybe b
_ [] = Maybe b
forall a. Maybe a
Nothing
searchMaybe a -> Maybe b
f (a
x:[a]
xs) =
    let fx :: Maybe b
fx = a -> Maybe b
f a
x in
    case Maybe b
fx of
      Maybe b
Nothing  -> (a -> Maybe b) -> [a] -> Maybe b
forall a b. (a -> Maybe b) -> [a] -> Maybe b
searchMaybe a -> Maybe b
f [a]
xs
      (Just b
_) -> Maybe b
fx

maybeToAttr :: (String->a->Maybe Attribute) -> String -> Maybe a
               -> Maybe Attribute
maybeToAttr :: (String -> a -> Maybe Attribute)
-> String -> Maybe a -> Maybe Attribute
maybeToAttr String -> a -> Maybe Attribute
_ String
_ Maybe a
Nothing  = Maybe Attribute
forall a. Maybe a
Nothing
maybeToAttr String -> a -> Maybe Attribute
to String
n (Just a
v) = String -> a -> Maybe Attribute
to String
n a
v

defaultToAttr :: (String->a->Maybe Attribute) -> String -> Defaultable a
                 -> Maybe Attribute
defaultToAttr :: (String -> a -> Maybe Attribute)
-> String -> Defaultable a -> Maybe Attribute
defaultToAttr String -> a -> Maybe Attribute
_ String
_ (Default a
_)  = Maybe Attribute
forall a. Maybe a
Nothing
defaultToAttr String -> a -> Maybe Attribute
to String
n (NonDefault a
v) = String -> a -> Maybe Attribute
to String
n a
v

definiteA :: (String->Attribute->Maybe a) -> String -> String
             -> [Attribute] -> a
definiteA :: (String -> Attribute -> Maybe a)
-> String -> String -> [Attribute] -> a
definiteA String -> Attribute -> Maybe a
from String
tag String
at [Attribute]
as =
    case (Attribute -> Maybe a) -> [Attribute] -> Maybe a
forall a b. (a -> Maybe b) -> [a] -> Maybe b
searchMaybe (String -> Attribute -> Maybe a
from String
at) [Attribute]
as of
      Maybe a
Nothing  -> String -> a
forall a. HasCallStack => String -> a
error (String
"missing attribute "String -> ShowS
forall a. [a] -> [a] -> [a]
++String
atString -> ShowS
forall a. [a] -> [a] -> [a]
++String
" in tag <"String -> ShowS
forall a. [a] -> [a] -> [a]
++String
tagString -> ShowS
forall a. [a] -> [a] -> [a]
++String
">")
      (Just a
a) -> a
a

defaultA :: (String->Attribute->Maybe a) -> a -> String
            -> [Attribute] -> Defaultable a
defaultA :: (String -> Attribute -> Maybe a)
-> a -> String -> [Attribute] -> Defaultable a
defaultA String -> Attribute -> Maybe a
from a
def String
at [Attribute]
as =
    case (Attribute -> Maybe a) -> [Attribute] -> Maybe a
forall a b. (a -> Maybe b) -> [a] -> Maybe b
searchMaybe (String -> Attribute -> Maybe a
from String
at) [Attribute]
as of
      Maybe a
Nothing  -> a -> Defaultable a
forall a. a -> Defaultable a
Default a
def
      (Just a
a) -> a -> Defaultable a
forall a. a -> Defaultable a
NonDefault a
a

possibleA :: (String->Attribute->Maybe a) -> String -> [Attribute] -> Maybe a
possibleA :: (String -> Attribute -> Maybe a)
-> String -> [Attribute] -> Maybe a
possibleA String -> Attribute -> Maybe a
from String
at [Attribute]
as = (Attribute -> Maybe a) -> [Attribute] -> Maybe a
forall a b. (a -> Maybe b) -> [a] -> Maybe b
searchMaybe (String -> Attribute -> Maybe a
from String
at) [Attribute]
as

fromAttrToStr :: String -> Attribute -> Maybe String
fromAttrToStr :: String -> Attribute -> Maybe String
fromAttrToStr String
n (QName
n0,AttValue
v)
        | String
n String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== QName -> String
localName QName
n0   = String -> Maybe String
forall a. a -> Maybe a
Just (AttValue -> String
attr2str AttValue
v)
        | Bool
otherwise           = Maybe String
forall a. Maybe a
Nothing

toAttrFrStr   :: String -> String -> Maybe Attribute
toAttrFrStr :: String -> String -> Maybe Attribute
toAttrFrStr String
n String
v = Attribute -> Maybe Attribute
forall a. a -> Maybe a
Just (String -> QName
N String
n, String -> AttValue
str2attr String
v)

str2attr :: String -> AttValue
str2attr :: String -> AttValue
str2attr String
s =
    let f :: String -> [Either String Reference]
f String
t =
          let (String
l,String
r) = (Char -> Bool) -> String -> (String, String)
forall a. (a -> Bool) -> [a] -> ([a], [a])
span (Char -> String -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` String
"\"&<>'") String
t
          in if String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
r then [String -> Either String Reference
forall a b. a -> Either a b
Left String
l]
             else String -> Either String Reference
forall a b. a -> Either a b
Left String
lEither String Reference
-> [Either String Reference] -> [Either String Reference]
forall a. a -> [a] -> [a]
: Reference -> Either String Reference
forall a b. b -> Either a b
Right (Char -> Reference
g (String -> Char
forall a. [a] -> a
head String
r))Either String Reference
-> [Either String Reference] -> [Either String Reference]
forall a. a -> [a] -> [a]
: String -> [Either String Reference]
f (ShowS
forall a. [a] -> [a]
tail String
r)
        g :: Char -> Reference
g Char
'"'  = String -> Reference
RefEntity String
"quot"
        g Char
'&'  = String -> Reference
RefEntity String
"amp"
        g Char
'<'  = String -> Reference
RefEntity String
"lt"
        g Char
'>'  = String -> Reference
RefEntity String
"gt"
        g Char
'\'' = String -> Reference
RefEntity String
"apos"
    in [Either String Reference] -> AttValue
AttValue (String -> [Either String Reference]
f String
s)

attr2str :: AttValue -> String          -- really needs symbol table
attr2str :: AttValue -> String
attr2str (AttValue [Either String Reference]
xs) =
    let f :: Either String Reference -> String
f (Left String
s) = String
s
        f (Right (RefChar Int
i))        = [Int -> Char
chr Int
i]
        f (Right (RefEntity String
"quot")) = String
"\""
        f (Right (RefEntity String
"amp"))  = String
"&"
        f (Right (RefEntity String
"lt"))   = String
"<"
        f (Right (RefEntity String
"gt"))   = String
">"
        f (Right (RefEntity String
"apos")) = String
"'"
        f (Right Reference
_)                  = String
"*"  -- Ooops, ST needed here.
    in (Either String Reference -> String)
-> [Either String Reference] -> String
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Either String Reference -> String
f [Either String Reference]
xs

------------------------------------------------------------------------
--  New content-model types
------------------------------------------------------------------------

{-
data OneOf2 a b
data OneOf3 a b c
data OneOf4 a b c d
    ... etc are now defined (with instances) in module OneOfN.
-}

-- | A type corresponding to XML's ANY contentspec.
--   It is either a list of unconverted xml 'Content'
--   or some 'XmlContent'-able value.
--
-- Parsing functions (e.g. 'parseContents') will always produce 'UnConverted'.
-- Note: The Show instance for 'UnConverted' uses 'verbatim'.
data ANYContent = forall a . (XmlContent a, Show a) => ANYContent a
                | UnConverted [Content Posn]

instance Show ANYContent where
    show :: ANYContent -> String
show (UnConverted [Content Posn]
c) = String
"UnConverted " String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
forall a. Show a => a -> String
show ((Content Posn -> String) -> [Content Posn] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map Content Posn -> String
forall a. Verbatim a => a -> String
verbatim [Content Posn]
c)
    show (ANYContent a
a)  = String
"ANYContent " String -> ShowS
forall a. [a] -> [a] -> [a]
++ a -> String
forall a. Show a => a -> String
show a
a

instance Eq ANYContent where
    ANYContent
a == :: ANYContent -> ANYContent -> Bool
== ANYContent
b = ANYContent -> String
forall a. Show a => a -> String
show ANYContent
a String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== ANYContent -> String
forall a. Show a => a -> String
show ANYContent
b

-- | The List1 type represents lists with at least one element.
--   It is required for DTD content models that use + as a modifier.
data List1 a = NonEmpty [a]  deriving (List1 a -> List1 a -> Bool
(List1 a -> List1 a -> Bool)
-> (List1 a -> List1 a -> Bool) -> Eq (List1 a)
forall a. Eq a => List1 a -> List1 a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: List1 a -> List1 a -> Bool
$c/= :: forall a. Eq a => List1 a -> List1 a -> Bool
== :: List1 a -> List1 a -> Bool
$c== :: forall a. Eq a => List1 a -> List1 a -> Bool
Eq, Int -> List1 a -> ShowS
[List1 a] -> ShowS
List1 a -> String
(Int -> List1 a -> ShowS)
-> (List1 a -> String) -> ([List1 a] -> ShowS) -> Show (List1 a)
forall a. Show a => Int -> List1 a -> ShowS
forall a. Show a => [List1 a] -> ShowS
forall a. Show a => List1 a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [List1 a] -> ShowS
$cshowList :: forall a. Show a => [List1 a] -> ShowS
show :: List1 a -> String
$cshow :: forall a. Show a => List1 a -> String
showsPrec :: Int -> List1 a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> List1 a -> ShowS
Show)


------------------------------------------------------------------------
--  Instances for new content-model types
------------------------------------------------------------------------
instance (HTypeable a) => HTypeable (List1 a) where
    toHType :: List1 a -> HType
toHType List1 a
m  = String -> [HType] -> [Constr] -> HType
Defined String
"List1" [HType
hx]
                         [String -> [HType] -> [HType] -> Constr
Constr String
"NonEmpty" [HType
hx] [HType -> HType
List HType
hx] {-Nothing-}]
               where (NonEmpty [a]
x) = List1 a
m
                     hx :: HType
hx = [a] -> HType
forall a. HTypeable a => a -> HType
toHType [a]
x
instance (XmlContent a) => XmlContent (List1 a) where
    toContents :: List1 a -> [Content ()]
toContents (NonEmpty [a]
xs) = (a -> [Content ()]) -> [a] -> [Content ()]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap a -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents [a]
xs
    parseContents :: XMLParser (List1 a)
parseContents = [a] -> List1 a
forall a. [a] -> List1 a
NonEmpty ([a] -> List1 a)
-> Parser (Content Posn) [a] -> XMLParser (List1 a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser (Content Posn) a -> Parser (Content Posn) [a]
forall (p :: * -> *) a. PolyParse p => p a -> p [a]
many1 Parser (Content Posn) a
forall a. XmlContent a => XMLParser a
parseContents

instance HTypeable ANYContent where
    toHType :: ANYContent -> HType
toHType ANYContent
_      = String -> String -> HType
Prim String
"ANYContent" String
"ANY"
instance XmlContent ANYContent where
    toContents :: ANYContent -> [Content ()]
toContents (ANYContent a
a)  = a -> [Content ()]
forall a. XmlContent a => a -> [Content ()]
toContents a
a
    toContents (UnConverted [Content Posn]
s) = (Content Posn -> Content ()) -> [Content Posn] -> [Content ()]
forall a b. (a -> b) -> [a] -> [b]
map Content Posn -> Content ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void [Content Posn]
s
    parseContents :: XMLParser ANYContent
parseContents = ([Content Posn] -> Result [Content Posn] ANYContent)
-> XMLParser ANYContent
forall t a. ([t] -> Result [t] a) -> Parser t a
P ([Content Posn] -> ANYContent -> Result [Content Posn] ANYContent
forall z a. z -> a -> Result z a
Success [] (ANYContent -> Result [Content Posn] ANYContent)
-> ([Content Posn] -> ANYContent)
-> [Content Posn]
-> Result [Content Posn] ANYContent
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Content Posn] -> ANYContent
UnConverted)

------------------------------------------------------------------------
--
------------------------------------------------------------------------