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

{- |
   Module     : Text.XML.HXT.Arrow.Pickle.Schema
   Copyright  : Copyright (C) 2005 Uwe Schmidt
   License    : MIT

   Maintainer : Uwe Schmidt (uwe@fh-wedel.de)
   Stability  : experimental
   Portability: portable
   Version    : $Id$

Datatypes and functions for building a content model
for XML picklers. A schema is part of every pickler
and can be used to derive a corrensponding DTD (or Relax NG schema).
This schema further enables checking the picklers.

-}

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

module Text.XML.HXT.Arrow.Pickle.Schema
where

import Text.XML.HXT.DOM.TypeDefs
import Text.XML.HXT.XMLSchema.DataTypeLibW3CNames

import Data.List
    ( sort )

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

-- | The datatype for modelling the structure of an

data Schema                     = Any
                                | Seq           { Schema -> [Schema]
sc_l  :: [Schema]
                                                }
                                | Alt           { sc_l  :: [Schema]
                                                }
                                | Rep           { Schema -> Int
sc_lb :: Int
                                                , Schema -> Int
sc_ub :: Int
                                                , Schema -> Schema
sc_1  :: Schema
                                                }
                                | Element       { Schema -> String
sc_n  :: Name
                                                , sc_1  :: Schema
                                                }
                                | Attribute     { sc_n  :: Name
                                                , sc_1  :: Schema
                                                }
                                | ElemRef       { sc_n  :: Name
                                                }
                                | CharData      { Schema -> DataTypeDescr
sc_dt :: DataTypeDescr
                                                }
                                  deriving (Schema -> Schema -> Bool
(Schema -> Schema -> Bool)
-> (Schema -> Schema -> Bool) -> Eq Schema
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Schema -> Schema -> Bool
== :: Schema -> Schema -> Bool
$c/= :: Schema -> Schema -> Bool
/= :: Schema -> Schema -> Bool
Eq, Int -> Schema -> ShowS
[Schema] -> ShowS
Schema -> String
(Int -> Schema -> ShowS)
-> (Schema -> String) -> ([Schema] -> ShowS) -> Show Schema
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Schema -> ShowS
showsPrec :: Int -> Schema -> ShowS
$cshow :: Schema -> String
show :: Schema -> String
$cshowList :: [Schema] -> ShowS
showList :: [Schema] -> ShowS
Show)

type Name                       = String
type Schemas                    = [Schema]

data DataTypeDescr              = DTDescr { DataTypeDescr -> String
dtLib    :: String
                                          , DataTypeDescr -> String
dtName   :: String
                                          , DataTypeDescr -> Attributes
dtParams :: Attributes
                                          }
                                  deriving (Int -> DataTypeDescr -> ShowS
[DataTypeDescr] -> ShowS
DataTypeDescr -> String
(Int -> DataTypeDescr -> ShowS)
-> (DataTypeDescr -> String)
-> ([DataTypeDescr] -> ShowS)
-> Show DataTypeDescr
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DataTypeDescr -> ShowS
showsPrec :: Int -> DataTypeDescr -> ShowS
$cshow :: DataTypeDescr -> String
show :: DataTypeDescr -> String
$cshowList :: [DataTypeDescr] -> ShowS
showList :: [DataTypeDescr] -> ShowS
Show)

instance Eq DataTypeDescr where
    DataTypeDescr
x1 == :: DataTypeDescr -> DataTypeDescr -> Bool
== DataTypeDescr
x2 = DataTypeDescr -> String
dtLib DataTypeDescr
x1 String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== DataTypeDescr -> String
dtLib DataTypeDescr
x2
               Bool -> Bool -> Bool
&&
               DataTypeDescr -> String
dtName DataTypeDescr
x1 String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== DataTypeDescr -> String
dtName DataTypeDescr
x2
               Bool -> Bool -> Bool
&&
               Attributes -> Attributes
forall a. Ord a => [a] -> [a]
sort (DataTypeDescr -> Attributes
dtParams DataTypeDescr
x1) Attributes -> Attributes -> Bool
forall a. Eq a => a -> a -> Bool
== Attributes -> Attributes
forall a. Ord a => [a] -> [a]
sort (DataTypeDescr -> Attributes
dtParams DataTypeDescr
x2)

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

-- | test: is schema a simple XML Schema datatype

isScXsd                 :: (String -> Bool) -> Schema -> Bool

isScXsd :: (String -> Bool) -> Schema -> Bool
isScXsd String -> Bool
p (CharData (DTDescr String
lib String
n Attributes
_ps))
                        = String
lib String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
w3cNS
                          Bool -> Bool -> Bool
&&
                          String -> Bool
p String
n
isScXsd String -> Bool
_ Schema
_             = Bool
False

-- | test: is type a fixed value attribute type

isScFixed               :: Schema -> Bool
isScFixed :: Schema -> Bool
isScFixed Schema
sc            = (String -> Bool) -> Schema -> Bool
isScXsd (String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
xsd_string) Schema
sc
                          Bool -> Bool -> Bool
&&
                          ((Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1) (Int -> Bool) -> (Schema -> Int) -> Schema -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [String] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([String] -> Int) -> (Schema -> [String]) -> Schema -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [String]
words (String -> [String]) -> (Schema -> String) -> Schema -> [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Schema -> String
xsdParam String
xsd_enumeration) Schema
sc

isScEnum                :: Schema -> Bool
isScEnum :: Schema -> Bool
isScEnum Schema
sc             = (String -> Bool) -> Schema -> Bool
isScXsd (String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
xsd_string) Schema
sc
                          Bool -> Bool -> Bool
&&
                          (Bool -> Bool
not (Bool -> Bool) -> (Schema -> Bool) -> Schema -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (String -> Bool) -> (Schema -> String) -> Schema -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Schema -> String
xsdParam String
xsd_enumeration) Schema
sc

isScElem                :: Schema -> Bool
isScElem :: Schema -> Bool
isScElem (Element String
_ Schema
_)  = Bool
True
isScElem Schema
_              = Bool
False

isScAttr                :: Schema -> Bool
isScAttr :: Schema -> Bool
isScAttr (Attribute String
_ Schema
_)= Bool
True
isScAttr Schema
_              = Bool
False

isScElemRef             :: Schema -> Bool
isScElemRef :: Schema -> Bool
isScElemRef (ElemRef String
_) = Bool
True
isScElemRef Schema
_           = Bool
False

isScCharData            :: Schema -> Bool
isScCharData :: Schema -> Bool
isScCharData (CharData DataTypeDescr
_)= Bool
True
isScCharData Schema
_          = Bool
False

isScSARE                :: Schema -> Bool
isScSARE :: Schema -> Bool
isScSARE (Seq [Schema]
_)        = Bool
True
isScSARE (Alt [Schema]
_)        = Bool
True
isScSARE (Rep Int
_ Int
_ Schema
_)    = Bool
True
isScSARE (ElemRef String
_)    = Bool
True
isScSARE Schema
_              = Bool
False

isScList                :: Schema -> Bool
isScList :: Schema -> Bool
isScList (Rep Int
0 (-1) Schema
_) = Bool
True
isScList Schema
_              = Bool
False

isScOpt                 :: Schema -> Bool
isScOpt :: Schema -> Bool
isScOpt (Rep Int
0 Int
1 Schema
_)     = Bool
True
isScOpt Schema
_               = Bool
False

-- | access an attribute of a descr of an atomic type

xsdParam                :: String -> Schema -> String
xsdParam :: String -> Schema -> String
xsdParam String
n (CharData DataTypeDescr
dtd)
                        = String -> Attributes -> String
forall k e. Eq k => k -> AssocList k [e] -> [e]
lookup1 String
n (DataTypeDescr -> Attributes
dtParams DataTypeDescr
dtd)
xsdParam String
_ Schema
_            = String
""

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

-- smart constructors for Schema datatype

-- ------------------------------------------------------------
--
-- predefined xsd data types for representation of DTD types

scDT            :: String -> String -> Attributes -> Schema
scDT :: String -> String -> Attributes -> Schema
scDT String
l String
n Attributes
rl     = DataTypeDescr -> Schema
CharData (DataTypeDescr -> Schema) -> DataTypeDescr -> Schema
forall a b. (a -> b) -> a -> b
$ String -> String -> Attributes -> DataTypeDescr
DTDescr String
l String
n Attributes
rl

scDTxsd         :: String -> Attributes -> Schema
scDTxsd :: String -> Attributes -> Schema
scDTxsd         = String -> String -> Attributes -> Schema
scDT String
w3cNS

scString        :: Schema
scString :: Schema
scString        = String -> Attributes -> Schema
scDTxsd String
xsd_string []

scString1       :: Schema
scString1 :: Schema
scString1       = String -> Attributes -> Schema
scDTxsd String
xsd_string [(String
xsd_minLength, String
"1")]

scFixed         :: String -> Schema
scFixed :: String -> Schema
scFixed String
v       = String -> Attributes -> Schema
scDTxsd String
xsd_string [(String
xsd_enumeration, String
v)]

scEnum          :: [String] -> Schema
scEnum :: [String] -> Schema
scEnum [String]
vs       = String -> Schema
scFixed ([String] -> String
unwords [String]
vs)

scNmtoken       :: Schema
scNmtoken :: Schema
scNmtoken       = String -> Attributes -> Schema
scDTxsd String
xsd_NCName []

scNmtokens      :: Schema
scNmtokens :: Schema
scNmtokens      = Schema -> Schema
scList Schema
scNmtoken

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

scEmpty                         :: Schema
scEmpty :: Schema
scEmpty                         = [Schema] -> Schema
Seq []

scSeq                           :: Schema -> Schema -> Schema
scSeq :: Schema -> Schema -> Schema
scSeq (Seq [])   Schema
sc2            = Schema
sc2
scSeq Schema
sc1        (Seq [])       = Schema
sc1
scSeq (Seq [Schema]
scs1) (Seq [Schema]
scs2)     = [Schema] -> Schema
Seq ([Schema]
scs1 [Schema] -> [Schema] -> [Schema]
forall a. [a] -> [a] -> [a]
++ [Schema]
scs2)    -- prevent nested Seq expr
scSeq (Seq [Schema]
scs1) Schema
sc2            = [Schema] -> Schema
Seq ([Schema]
scs1 [Schema] -> [Schema] -> [Schema]
forall a. [a] -> [a] -> [a]
++ [Schema
sc2])
scSeq Schema
sc1        (Seq [Schema]
scs2)     = [Schema] -> Schema
Seq (Schema
sc1  Schema -> [Schema] -> [Schema]
forall a. a -> [a] -> [a]
:  [Schema]
scs2)
scSeq Schema
sc1        Schema
sc2            = [Schema] -> Schema
Seq [Schema
sc1,Schema
sc2]

scSeqs                          :: [Schema] -> Schema
scSeqs :: [Schema] -> Schema
scSeqs                          = (Schema -> Schema -> Schema) -> Schema -> [Schema] -> Schema
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl Schema -> Schema -> Schema
scSeq Schema
scEmpty

scNull                          :: Schema
scNull :: Schema
scNull                          = [Schema] -> Schema
Alt []

scAlt                           :: Schema -> Schema -> Schema
scAlt :: Schema -> Schema -> Schema
scAlt (Alt [])   Schema
sc2            = Schema
sc2
scAlt Schema
sc1        (Alt [])       = Schema
sc1
scAlt (Alt [Schema]
scs1) (Alt [Schema]
scs2)     = [Schema] -> Schema
Alt ([Schema]
scs1 [Schema] -> [Schema] -> [Schema]
forall a. [a] -> [a] -> [a]
++ [Schema]
scs2)    -- prevent nested Alt expr
scAlt (Alt [Schema]
scs1) Schema
sc2            = [Schema] -> Schema
Alt ([Schema]
scs1 [Schema] -> [Schema] -> [Schema]
forall a. [a] -> [a] -> [a]
++ [Schema
sc2])
scAlt Schema
sc1        (Alt [Schema]
scs2)     = [Schema] -> Schema
Alt (Schema
sc1  Schema -> [Schema] -> [Schema]
forall a. a -> [a] -> [a]
:  [Schema]
scs2)
scAlt Schema
sc1        Schema
sc2            = [Schema] -> Schema
Alt [Schema
sc1,Schema
sc2]

scAlts          :: [Schema] -> Schema
scAlts :: [Schema] -> Schema
scAlts          = (Schema -> Schema -> Schema) -> Schema -> [Schema] -> Schema
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl Schema -> Schema -> Schema
scAlt Schema
scNull

scOption        :: Schema -> Schema
scOption :: Schema -> Schema
scOption     (Seq [])           = Schema
scEmpty
scOption (Attribute String
n Schema
sc2)      = String -> Schema -> Schema
Attribute String
n (Schema -> Schema
scOption Schema
sc2)
scOption Schema
sc1
    | Schema
sc1 Schema -> Schema -> Bool
forall a. Eq a => a -> a -> Bool
== Schema
scString1          = Schema
scString
    | Bool
otherwise                 = Schema -> Schema
scOpt Schema
sc1

scList          :: Schema -> Schema
scList :: Schema -> Schema
scList          = Int -> Int -> Schema -> Schema
scRep Int
0 (-Int
1)

scList1         :: Schema -> Schema
scList1 :: Schema -> Schema
scList1         = Int -> Int -> Schema -> Schema
scRep Int
1 (-Int
1)

scOpt           :: Schema -> Schema
scOpt :: Schema -> Schema
scOpt           = Int -> Int -> Schema -> Schema
scRep Int
0 Int
1

scRep           :: Int -> Int -> Schema -> Schema
scRep :: Int -> Int -> Schema -> Schema
scRep Int
l Int
u Schema
sc1  = Int -> Int -> Schema -> Schema
Rep Int
l Int
u Schema
sc1

scElem          :: String -> Schema -> Schema
scElem :: String -> Schema -> Schema
scElem String
n Schema
sc1    = String -> Schema -> Schema
Element String
n Schema
sc1

scAttr          :: String -> Schema -> Schema
scAttr :: String -> Schema -> Schema
scAttr String
n Schema
sc1    = String -> Schema -> Schema
Attribute String
n Schema
sc1

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