@@ -1,17 +1,17 @@
1module Text.XML.DOM.Parser.Types
1module Text.XML.DOM.Parser.Types
2+ (-- * Element matching
2− ( -- * Name matching
8+ -- * Name matching
3− NameMatcher(..)
9+ , NameMatcher(..)
6 , matchName
12 , matchName
7 , matchLocalName
13 , matchLocalName
8 , matchCILocalName
14 , matchCILocalName
15 -- * Parser internals
15 -- * Parser internals
16 , DomPath(..)
16 , DomPath(..)
17 , ParserError(..)
17 , ParserError(..)
@@ -43,6 +43,8 @@ import Text.XML
43import Text.XML.Lens
43import Text.XML.Lens
45-- | Arbitrary element matcher
45-- | Arbitrary element matcher
46data ElemMatcher = ElemMatcher
48data ElemMatcher = ElemMatcher
47 { _emMatch :: Element -> Bool
49 { _emMatch :: Element -> Bool
48 , _emShow :: Text
50 , _emShow :: Text
@@ -51,6 +53,7 @@ data ElemMatcher = ElemMatcher
52makeLenses ''ElemMatcher
54makeLenses ''ElemMatcher
56+-- | Instance using instance of 'NameMatcher'
54instance IsString ElemMatcher where
57instance IsString ElemMatcher where
55 fromString = matchElemName . fromString
58 fromString = matchElemName . fromString
@@ -58,17 +61,24 @@ instance Show ElemMatcher where
58 show = T.unpack . _emShow
61 show = T.unpack . _emShow
60-- | Match element by name
63-- | Match element by name
61matchElemName :: NameMatcher -> ElemMatcher
66matchElemName :: NameMatcher -> ElemMatcher
62matchElemName (NameMatcher matchName showName) = ElemMatcher
67matchElemName (NameMatcher matchName showName) = ElemMatcher
63 { _emMatch = views name matchName
68 { _emMatch = views name matchName
64 , _emShow = showName
69 , _emShow = showName
72+-- | Match over elements
67elMatch :: ElemMatcher -> Traversal' Element Element
75elMatch :: ElemMatcher -> Traversal' Element Element
68elMatch (ElemMatcher match _) = filtered match
76elMatch (ElemMatcher match _) = filtered match
70-- | Arbitrary name matcher. Match name any way you want, but
78-- | Arbitrary name matcher. Match name any way you want, but
71-- considered to be used as comparator with some name with some rules
79-- considered to be used as comparator with some name with some rules
72data NameMatcher = NameMatcher
82data NameMatcher = NameMatcher
73 { _nmMatch :: Name -> Bool
83 { _nmMatch :: Name -> Bool
74 -- ^ Name matching function, usually should be simple comparsion
84 -- ^ Name matching function, usually should be simple comparsion
@@ -80,6 +90,10 @@ data NameMatcher = NameMatcher
81makeLenses ''NameMatcher
91makeLenses ''NameMatcher
93+-- | Instance use 'matchCILocalName' as most general and liberal
94+-- matching strategy (while XML is often malformed).
83instance IsString NameMatcher where
97instance IsString NameMatcher where
84 fromString = matchCILocalName . T.pack
98 fromString = matchCILocalName . T.pack
@@ -88,6 +102,8 @@ instance Show NameMatcher where
89-- | Makes matcher which matches only local part of name igoring
103-- | Makes matcher which matches only local part of name igoring
90-- namespace and prefix. Local name matching is case sensitive.
104-- namespace and prefix. Local name matching is case sensitive.
91matchLocalName :: Text -> NameMatcher
107matchLocalName :: Text -> NameMatcher
92matchLocalName tname = NameMatcher
108matchLocalName tname = NameMatcher
93 { _nmMatch = \n -> nameLocalName n == tname
109 { _nmMatch = \n -> nameLocalName n == tname
@@ -97,6 +113,8 @@ matchLocalName tname = NameMatcher
97-- | Makes matcher which matches only local part of name igoring
113-- | Makes matcher which matches only local part of name igoring
98-- namespace and prefix. Local name matching is case insensitive. This
114-- namespace and prefix. Local name matching is case insensitive. This
99-- is the most common case.
115-- is the most common case.
100matchCILocalName :: Text -> NameMatcher
118matchCILocalName :: Text -> NameMatcher
101matchCILocalName tname = NameMatcher
119matchCILocalName tname = NameMatcher
102 { _nmMatch = \n -> CI.mk (nameLocalName n) == CI.mk tname
120 { _nmMatch = \n -> CI.mk (nameLocalName n) == CI.mk tname
@@ -104,6 +122,8 @@ matchCILocalName tname = NameMatcher
106-- | Makes matcher which match name by 'Eq' with given
124-- | Makes matcher which match name by 'Eq' with given
107matchName :: Name -> NameMatcher
127matchName :: Name -> NameMatcher
108matchName n = NameMatcher
128matchName n = NameMatcher
109 { _nmMatch = (== n)
129 { _nmMatch = (== n)
@@ -124,13 +144,17 @@ data ParserError
124 -- ^ Path of element error occured in
144 -- ^ Path of element error occured in
127− -- | Tag contents has wrong format, (could not read text to value)
147+ -- | Expected attribute but not found
128− | PEWrongFormat
148+ --
129− { _peDetails :: Text
149+ -- @since 1.0.0
150+ | PEAttributeNotFound
151+ { _peAttributeName :: NameMatcher
130− , _pePath :: DomPath
152+ , _pePath :: DomPath
133 -- | Could not parse attribute
155 -- | Could not parse attribute
134 | PEAttributeWrongFormat
158 | PEAttributeWrongFormat
135 { _peAttributeName :: NameMatcher
159 { _peAttributeName :: NameMatcher
136 , _peDetails :: Text
160 , _peDetails :: Text
@@ -142,10 +166,10 @@ data ParserError
142 { _pePath :: DomPath
166 { _pePath :: DomPath
145− -- | Expected attribute but not found
169+ -- | Tag contents has wrong format, (could not read text to value)
146− | PEAttributeNotFound
170+ | PEContentWrongFormat
147− { _peAttributeName :: NameMatcher
171+ { _peDetails :: Text
148− , _pePath :: DomPath
172+ , _pePath :: DomPath
151 -- | Some other error
175 -- | Some other error