@@ -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