git.iko.soy/typeable/xsd-parser
on master help sign in
Account to top-level complex types
commit925b3e67337929fcd516cf8f51c7ab3a3019cc58 authorDenis Redozubov <denis.redozubov@gmail.com> dateFri 11 Oct 2019, 15:25 (7 years ago) parent5d0f008
3 files changed+32−17
src/Text/XSD/Internal.hs+8−3 src/Text/XSD/Parser.hs+20−13 xsd-isogen.cabal+4−1
src/Text/XSD/Internal.hs+8−3
@@ -1,3 +1,4 @@
1+{-# LANGUAGE OverloadedStrings #-}
12{-# LANGUAGE TemplateHaskell #-}
23
34-- | A subset of XSD specification.
@@ -171,10 +172,14 @@ type QName = (Maybe Namespace, LocalName)
171172-- deriving (Show)
172173
173174data Element = Element
174− { name :: QName
175− , xtype :: DatatypeRef
175+ { name :: !QName
176+ , xtype :: !DatatypeRef
177+ , minOccurs :: !Int
178+ , maxOccurs :: !(Maybe Int)
176179 } deriving (Show, Eq)
177180
181+type DatatypeMap = M.Map Text Datatype
182+
178183-- | Embodies an XSD document
179−newtype XSD = XSD { unXSD :: [Element] }
184+newtype XSD = XSD { unXSD :: ([Element], DatatypeMap) }
180185 deriving (Show, Eq)
src/Text/XSD/Parser.hs+20−13
@@ -1,3 +1,6 @@
1+{-# LANGUAGE LambdaCase #-}
2+{-# LANGUAGE OverloadedStrings #-}
3+
14module Text.XSD.Parser where
25
36import Control.Exception
@@ -23,10 +26,10 @@ newtype XSDException = XSDException String
2326
2427instance Exception XSDException
2528
26−type XSDMonad a = WriterT (M.Map Text Datatype) (Either SomeException) a
29+type XSDMonad a = WriterT DatatypeMap (Either SomeException) a
2730
2831parseXSD :: ParseSettings -> ByteString -> Either SomeException XSD
29−parseXSD ps bs = parseLBS ps bs >>= fmap fst . toXsd
32+parseXSD ps bs = parseLBS ps bs >>= toXsd
3033
3134-- Show is Meh
3235throwXsd :: Show s => s -> XSDMonad a
@@ -51,13 +54,6 @@ toElemsAndDatatypes cursor = do
5154 throwXsd $ "unnamed type ref for datatype: " <> show datatype
5255 pure elems
5356
54−toSchema :: XML.Cursor -> XSDMonad XSD
55−toSchema cursor = do
56− let topLevel = cursor $| laxElement "schema"
57− if P.null topLevel
58− then throwXsd "Top-level schema definition not found"
59− else XSD <$> toElemsAndDatatypes cursor
60−
6157safeHead :: [a] -> Maybe a
6258safeHead [] = Nothing
6359safeHead (a:_) = Just a
@@ -74,6 +70,9 @@ toElem cursor = do
7470 ns = nameNamespace $ elementName el
7571 name <- attrThrowXsd "name" el
7672 $ "Expected attribute 'name' for element: " <> show (node cursor)
73+ let
74+ minOc = maybe 0 (read . T.unpack) (M.lookup "minOccurs" attrs)
75+ maxOc = read . T.unpack <$> M.lookup "maxOccurs" attrs
7776 datatypeRef <- case mAttrType of
7877 Just dtn -> pure $ DatatypeRef dtn
7978 Nothing -> do
@@ -86,8 +85,10 @@ toElem cursor = do
8685 let name = safeHead $ childCursor $| attribute "name"
8786 InlineComplex . TypeComplex <$> toComplexType childCursor
8887 pure $ XSD.Element
89− { name = (ns, name)
90− , xtype = datatypeRef }
88+ { name = (ns, name)
89+ , xtype = datatypeRef
90+ , minOccurs = minOc
91+ , maxOccurs = maxOc }
9192 e -> throwXsd $ "[toElem] xsd node not supported: " <> show e
9293 e ->
9394 throwXsd $ "[toElem] element expected, got: " <> show e
@@ -174,5 +175,11 @@ toAttribute cursor = do
174175 Nothing -> Optional
175176 pure $ Attribute name simpleType useProp
176177
177−toXsd :: Document -> Either SomeException (XSD, M.Map Text Datatype)
178−toXsd doc = runWriterT $ toSchema (fromDocument doc)
178+toXsd :: Document -> Either SomeException XSD
179+toXsd doc = do
180+ let cursor = fromDocument doc
181+ XSD <$> (runWriterT $ do
182+ let topLevel = cursor $| laxElement "schema"
183+ if P.null topLevel
184+ then throwXsd "Top-level schema definition not found"
185+ else toElemsAndDatatypes cursor)
xsd-isogen.cabal+4−1
@@ -26,7 +26,8 @@ library
2626
2727 default-extensions: OverloadedStrings
2828 , LambdaCase
29− other-extensions: TemplateHaskell
29+ , QuasiQuotes
30+ , TemplateHaskell
3031
3132 -- Other library packages from which modules are imported.
3233 build-depends: base ^>=4.12.0.0
@@ -35,8 +36,10 @@ library
3536 , lens >= 4.13
3637 , mtl
3738 , text
39+ , template-haskell
3840 , transformers
3941 , xml-conduit
42+ , xml-isogen
4043 , writer-cps-mtl
4144 , uniplate
4245