git.iko.soy/typeable/dom-parser
on master help sign in
12 files changed+173−67
since splitsnapshots
unifiedsplit
.travis.yml+7−5 CHANGELOG.md+44 README.md+5 dom-parser.cabal+10−10 ghc-963.yaml+7 src/Text/XML/DOM/Parser/Combinators.hs−1 src/Text/XML/DOM/Parser/Content.hs+5−4 src/Text/XML/DOM/Parser/FromAttribute.hs+2 src/Text/XML/DOM/Parser/FromDom.hs+2−21 src/Text/XML/DOM/Parser/Types.hs+44−20 src/Text/XML/Soap.hs+46 stack.yaml+1−6
.travis.yml+7−5
@@ -4,6 +4,12 @@ sudo: false
44# Do not choose a language; we provide our own build tools.
55language: generic
66
7+env:
8+ - STACK_YAML=stack.yaml
9+ - STACK_YAML=ghc-802.yaml
10+ - STACK_YAML=ghc-822.yaml
11+ - STACK_YAML=ghc-841.yaml
12+
713# Caching so the next build will be fast too.
814cache:
915 directories:
@@ -21,10 +27,6 @@ before_install:
2127- export PATH=$HOME/.local/bin:$PATH
2228- travis_retry curl -L https://www.stackage.org/stack/linux-x86_64 | tar xz --wildcards --strip-components=1 -C ~/.local/bin '*/stack'
2329
24−install:
25−# Build dependencies
26−- stack --no-terminal --install-ghc build --only-dependencies
27−
2830script:
2931# Build the package, its tests, and its docs and run the tests
30−- stack --no-terminal test --haddock --no-haddock-deps
32+- stack --no-terminal --install-ghc test --haddock --no-haddock-deps
CHANGELOG.md+44
@@ -1,10 +1,54 @@
11# CHANGELOG
22
3+## HEAD
4+
5+## 3.2.0
6+
7+### Added
8+
9+* Text.XML.Soap module with XmlEnvelope stuff in it.
10+* GHC 9 compatibility.
11+
12+### Removed
13+
14+* Old stackage files.
15+
16+## 3.1.0
17+
18+### Added
19+
20+* Additional builds with GHC 8.0.2 and 8.2.1
21+* Compatibility with GHC 8.4.1
22+
23+## 3.0.0
24+
25+* GHC 8 compatible release
26+
27+### Removed
28+
29+* `open-union` dependency
30+
31+## 2.0.0
32+
33+### Added
34+
35+* `NameMatcher` and `ElemMatcher` as more flexible mechanisms for
36+ matching names and elements instead of just matching by name.
37+* Class `FromAttribute` and some default instances
38+
39+### Changed
40+
41+* `DomTraversable` renamed to `Buildable`
42+* Combinators use `NameMatcher` and `ElemMatcher`
43+* Documentation improved
44+
345## 1.0.0
46+
447* Added support for parsing attributes
548* Changed public interface for 'parseContent'
649* Added useful functions
750* Dropped unnecessary dependency from shakespeare
851
952## 0.1.0
53+
1054* Public release
README.mdnew file+5
@@ -0,0 +1,5 @@
1+# dom-parser
2+
3+[![Build Status](https://travis-ci.org/typeable/dom-parser.svg?branch=master)](https://travis-ci.org/typeable/dom-parser)
4+
5+Simple monadic DOM parser
dom-parser.cabal+10−10
@@ -1,5 +1,5 @@
11name: dom-parser
2−version: 1.0.0
2+version: 3.2.0
33synopsis: Simple monadic DOM parser
44license: MIT
55license-file: LICENSE
@@ -7,12 +7,13 @@ author: Typeable.io contributors
77maintainer: makeit@typeable.io
88category: XML
99build-type: Simple
10−extra-source-files: CHANGELOG.md
1110cabal-version: >=1.10
12−
13−extra-source-files: CHANGELOG.md
14−
1511homepage: https://github.com/typeable/dom-parser
12+tested-with: GHC == 9.2.7
13+ , GHC == 9.4.8
14+ , GHC == 9.6.3
15+extra-source-files: CHANGELOG.md
16+ , README.md
1617
1718source-repository head
1819 type: git
@@ -43,18 +44,16 @@ library
4344 , TypeFamilies
4445 , TypeOperators
4546 , UndecidableInstances
46− build-depends: base >= 4.7 && < 5
47+ build-depends: base >= 4.11 && < 5
4748 , case-insensitive
4849 , containers
4950 , lens
50− , mtl
51− , open-union >= 0.2
51+ , mtl < 2.3 || > 2.3
5252 , scientific
53− , semigroups
5453 , text
5554 , transformers
56− , type-fun
5755 , xml-conduit
56+ , xml-conduit-writer
5857 , xml-lens
5958 exposed-modules: Text.XML.DOM.Parser
6059 , Text.XML.DOM.Parser.Attributes
@@ -64,6 +63,7 @@ library
6463 , Text.XML.DOM.Parser.FromAttribute
6564 , Text.XML.DOM.Parser.FromDom
6665 , Text.XML.DOM.Parser.Types
66+ , Text.XML.Soap
6767
6868test-suite test
6969 type: exitcode-stdio-1.0
ghc-963.yamlnew file+7
@@ -0,0 +1,7 @@
1+resolver: lts-22.6
2+
3+extra-deps:
4+ - xml-conduit-writer-0.1.1.5
5+
6+nix:
7+ packages: [zlib]
src/Text/XML/DOM/Parser/Combinators.hs−1
@@ -22,7 +22,6 @@ import Control.Monad.Reader
2222import Data.Foldable as F
2323import Data.List as L
2424import Data.List.NonEmpty (NonEmpty(..))
25−import Data.Monoid
2625import Data.Text as T
2726import Data.Traversable
2827import Text.XML
src/Text/XML/DOM/Parser/Content.hs+5−4
@@ -20,6 +20,7 @@ import Data.List as L
2020import Data.Monoid
2121import Data.Text as T
2222import Data.Typeable
23+import Prelude as P
2324import Text.Read
2425import Text.XML.DOM.Parser.Types
2526import Text.XML.Lens
@@ -35,7 +36,7 @@ parseContent
3536parseContent parse = getCurrentContent >>= \case
3637 Nothing -> throwParserError PEContentNotFound
3738 Just c -> case parse c of
38− Left e -> throwParserError $ PEWrongFormat e
39+ Left e -> throwParserError $ PEContentWrongFormat e
3940 Right a -> return a
4041
4142
@@ -93,7 +94,7 @@ maybeReadContent
9394maybeReadContent f t = maybe (Left msg) Right $ f t
9495 where
9596 msg = "Not readable " <> n <> ": " <> t
96− n = T.pack $ show $ typeRep (Proxy :: Proxy a)
97+ n = T.pack $ P.show $ typeRep (Proxy :: Proxy a)
9798
9899
99100-- | Tries to read given text to value using 'Read'. Useful to use
@@ -114,8 +115,8 @@ readBool t =
114115 tvals = ["y", "yes", "t", "true", "1"]
115116 fvals = ["n", "no", "f", "false", "0"]
116117 in if
117− | lowt `elem` tvals -> Right True
118− | lowt `elem` fvals -> Right False
118+ | lowt `L.elem` tvals -> Right True
119+ | lowt `L.elem` fvals -> Right False
119120 | otherwise ->
120121 Left $ "Could not read " <> t <> " as Bool"
121122
src/Text/XML/DOM/Parser/FromAttribute.hs+2
@@ -26,6 +26,8 @@ import Text.XML.DOM.Parser.Content
2626-- | Class of types which can be get from attribute value. Method
2727-- 'fromAttribute' is a convenient default parameter for
2828-- 'parseAttribute'
29+--
30+-- @since 2.0.0
2931class FromAttribute a where
3032 fromAttribute
3133 :: Text
src/Text/XML/DOM/Parser/FromDom.hs+2−21
@@ -4,7 +4,6 @@ module Text.XML.DOM.Parser.FromDom
44 , proxyFromDom
55 -- * Explicit methods for convenience
66 , elementFromDom
7− , unionFromDom
87 , textFromDom
98 , stringFromDom
109 , charFromDom
@@ -21,7 +20,6 @@ module Text.XML.DOM.Parser.FromDom
2120import Control.Applicative
2221import Control.Lens
2322import Data.Fixed
24−import Data.OpenUnion
2523import Data.Scientific
2624import Data.Text as T hiding (empty)
2725import Data.Typeable
@@ -29,7 +27,6 @@ import Data.Void
2927import Text.XML
3028import Text.XML.DOM.Parser.Content
3129import Text.XML.DOM.Parser.Types
32−import TypeFun.Data.List hiding (Union)
3330
3431proxyFromDom
3532 :: forall proxy m a
@@ -38,7 +35,8 @@ proxyFromDom
3835 -> DomParserT Identity m a
3936proxyFromDom _ = fromDom
4037
41−-- | Class of types which can be parsed from single XML element.
38+-- | Class of types which can be parsed from single XML element. The
39+-- method 'fromDom' is convenient default to use with 'inElem'
4240class FromDom a where
4341 fromDom :: (Monad m) => DomParserT Identity m a
4442
@@ -115,23 +113,6 @@ instance FromDom Bool where
115113boolFromDom :: (Monad m) => DomParserT Identity m Bool
116114boolFromDom = parseContent readBool
117115
118−instance FromDom (Union '[]) where
119− fromDom = empty
120−
121−instance
122− ( Typeable a, FromDom a, FromDom (Union as)
123− , SubList as (a ': as) )
124− => FromDom (Union (a ': as)) where
125− -- fromDom :: forall m. (DomParserMonad m) => m a
126− fromDom = (liftUnion <$> (proxyFromDom (Proxy :: Proxy a)))
127− <|> (reUnion <$> (proxyFromDom (Proxy :: Proxy (Union as))))
128−
129−unionFromDom
130− :: (Monad m, FromDom (Union as))
131− => proxy as
132− -> DomParserT Identity m (Union as)
133−unionFromDom _ = fromDom
134−
135116instance FromDom Element where
136117 fromDom = elementFromDom
137118
src/Text/XML/DOM/Parser/Types.hs+44−20
@@ -1,17 +1,17 @@
11module Text.XML.DOM.Parser.Types
2− ( -- * Name matching
3− NameMatcher(..)
2+ (-- * Element matching
3+ ElemMatcher(..)
4+ , emMatch
5+ , emShow
6+ , matchElemName
7+ , elMatch
8+ -- * Name matching
9+ , NameMatcher(..)
410 , nmMatch
511 , nmShow
612 , matchName
713 , matchLocalName
814 , matchCILocalName
9− -- * Element matching
10− , ElemMatcher(..)
11− , emMatch
12− , emShow
13− , matchElemName
14− , elMatch
1515 -- * Parser internals
1616 , DomPath(..)
1717 , ParserError(..)
@@ -43,6 +43,8 @@ import Text.XML
4343import Text.XML.Lens
4444
4545-- | Arbitrary element matcher
46+--
47+-- @since 2.0.0
4648data ElemMatcher = ElemMatcher
4749 { _emMatch :: Element -> Bool
4850 , _emShow :: Text
@@ -51,6 +53,7 @@ data ElemMatcher = ElemMatcher
5153
5254makeLenses ''ElemMatcher
5355
56+-- | Instance using instance of 'NameMatcher'
5457instance IsString ElemMatcher where
5558 fromString = matchElemName . fromString
5659
@@ -58,17 +61,24 @@ instance Show ElemMatcher where
5861 show = T.unpack . _emShow
5962
6063-- | Match element by name
64+--
65+-- @since 2.0.0
6166matchElemName :: NameMatcher -> ElemMatcher
6267matchElemName (NameMatcher matchName showName) = ElemMatcher
6368 { _emMatch = views name matchName
6469 , _emShow = showName
6570 }
6671
72+-- | Match over elements
73+--
74+-- @since 2.0.0
6775elMatch :: ElemMatcher -> Traversal' Element Element
6876elMatch (ElemMatcher match _) = filtered match
6977
7078-- | Arbitrary name matcher. Match name any way you want, but
7179-- considered to be used as comparator with some name with some rules
80+--
81+-- @since 2.0.0
7282data NameMatcher = NameMatcher
7383 { _nmMatch :: Name -> Bool
7484 -- ^ Name matching function, usually should be simple comparsion
@@ -78,8 +88,10 @@ data NameMatcher = NameMatcher
7888 -- ^ Field for 'Show' instance and bulding usefull errors
7989 }
8090
81−makeLenses ''NameMatcher
82−
91+-- | Instance use 'matchCILocalName' as most general and liberal
92+-- matching strategy (while XML is often malformed).
93+--
94+-- @since 2.0.0
8395instance IsString NameMatcher where
8496 fromString = matchCILocalName . T.pack
8597
@@ -88,6 +100,8 @@ instance Show NameMatcher where
88100
89101-- | Makes matcher which matches only local part of name igoring
90102-- namespace and prefix. Local name matching is case sensitive.
103+--
104+-- @since 2.0.0
91105matchLocalName :: Text -> NameMatcher
92106matchLocalName tname = NameMatcher
93107 { _nmMatch = \n -> nameLocalName n == tname
@@ -97,13 +111,19 @@ matchLocalName tname = NameMatcher
97111-- | Makes matcher which matches only local part of name igoring
98112-- namespace and prefix. Local name matching is case insensitive. This
99113-- is the most common case.
114+--
115+-- @since 2.0.0
100116matchCILocalName :: Text -> NameMatcher
101117matchCILocalName tname = NameMatcher
102118 { _nmMatch = \n -> CI.mk (nameLocalName n) == CI.mk tname
103119 , _nmShow = tname
104120 }
105121
122+makeLenses ''NameMatcher
123+
106124-- | Makes matcher which match name by 'Eq' with given
125+--
126+-- @since 2.0.0
107127matchName :: Name -> NameMatcher
108128matchName n = NameMatcher
109129 { _nmMatch = (== n)
@@ -114,7 +134,7 @@ matchName n = NameMatcher
114134-- element of the document. Errors are much more usefull with path.
115135newtype DomPath = DomPath
116136 { unDomPath :: [Text]
117− } deriving (Eq, Ord, Show, Monoid)
137+ } deriving (Eq, Ord, Show, Semigroup, Monoid)
118138
119139-- | DOM parser error description.
120140data ParserError
@@ -124,13 +144,17 @@ data ParserError
124144 -- ^ Path of element error occured in
125145 }
126146
127− -- | Tag contents has wrong format, (could not read text to value)
128− | PEWrongFormat
129− { _peDetails :: Text
130− , _pePath :: DomPath
147+ -- | Expected attribute but not found
148+ --
149+ -- @since 1.0.0
150+ | PEAttributeNotFound
151+ { _peAttributeName :: NameMatcher
152+ , _pePath :: DomPath
131153 }
132154
133155 -- | Could not parse attribute
156+ --
157+ -- @since 1.0.0
134158 | PEAttributeWrongFormat
135159 { _peAttributeName :: NameMatcher
136160 , _peDetails :: Text
@@ -142,10 +166,10 @@ data ParserError
142166 { _pePath :: DomPath
143167 }
144168
145− -- | Expected attribute but not found
146− | PEAttributeNotFound
147− { _peAttributeName :: NameMatcher
148− , _pePath :: DomPath
169+ -- | Tag contents has wrong format, (could not read text to value)
170+ | PEContentWrongFormat
171+ { _peDetails :: Text
172+ , _pePath :: DomPath
149173 }
150174
151175 -- | Some other error
@@ -160,7 +184,7 @@ instance Exception ParserError
160184
161185newtype ParserErrors = ParserErrors
162186 { unParserErrors :: [ParserError]
163− } deriving (Show, Monoid, Generic)
187+ } deriving (Show, Semigroup, Monoid, Generic)
164188
165189makePrisms ''ParserErrors
166190
src/Text/XML/Soap.hsnew file+46
@@ -0,0 +1,46 @@
1+module Text.XML.Soap where
2+
3+import Control.Lens
4+import Data.String
5+import Text.XML
6+import Text.XML.DOM.Parser
7+import Text.XML.Writer
8+
9+data XmlEnvelope h b = XmlEnvelope
10+ { _xeHeader :: !(Maybe h)
11+ , _xeBody :: !(Maybe b)
12+ } deriving (Eq, Show)
13+
14+makeLenses ''XmlEnvelope
15+
16+xmlEnvelopeToDocument :: (ToXML h, ToXML b) => XmlEnvelope h b -> Document
17+xmlEnvelopeToDocument xe = doc
18+ where
19+ sname n = fromString $ "{http://schemas.xmlsoap.org/soap/envelope/}" <> n
20+ doc =
21+ Document
22+ { documentPrologue = Prologue [] Nothing []
23+ , documentRoot =
24+ Element
25+ { elementName = sname "Envelope"
26+ , elementAttributes = mempty
27+ , elementNodes = [NodeElement header, NodeElement body]
28+ }
29+ , documentEpilogue = []
30+ }
31+ header =
32+ Element
33+ { elementName = sname "Header"
34+ , elementAttributes = mempty
35+ , elementNodes = render . toXML $ xe ^. xeHeader
36+ }
37+ body =
38+ Element
39+ { elementName = sname "Body"
40+ , elementAttributes = mempty
41+ , elementNodes = render . toXML $ xe ^. xeBody
42+ }
43+
44+instance (FromDom h, FromDom b) => FromDom (XmlEnvelope h b) where
45+ fromDom = XmlEnvelope <$> inElemMay "Header" fromDom
46+ <*> inElemMay "Body" fromDom
stack.yamlmode 100644 → 120000+1−6
@@ -1,6 +0,0 @@
1−packages:
2− - .
3−extra-deps:
4− - open-union-0.2.0.0
5− - type-fun-0.1.1
6−resolver: lts-6.25
@@ -0,0 +1 @@
1+ghc-963.yaml