@@ -1,17 +1,17 @@
1module Text.XML.DOM.Parser.Types
1module Text.XML.DOM.Parser.Types
2− (-- * Element matching
8− -- * Name matching
2+ ( -- * Name matching
9− , NameMatcher(..)
3+ NameMatcher(..)
12 , matchName
6 , matchName
13 , matchLocalName
7 , matchLocalName
14 , matchCILocalName
8 , matchCILocalName
15 -- * Parser internals
15 -- * Parser internals
16 , DomPath(..)
16 , DomPath(..)
17 , ParserError(..)
17 , ParserError(..)
@@ -43,17 +43,13 @@ import Text.XML
43import Text.XML.Lens
43import Text.XML.Lens
45-- | Arbitrary element matcher
45-- | Arbitrary element matcher
48data ElemMatcher = ElemMatcher
46data ElemMatcher = ElemMatcher
49 { _emMatch :: Element -> Bool
47 { _emMatch :: Element -> Bool
50 , _emShow :: Text
48 , _emShow :: Text
51− -- ^ Field for 'Show' instance and bulding usefull errors
54makeLenses ''ElemMatcher
51makeLenses ''ElemMatcher
56−-- | Instance using instance of 'NameMatcher'
57instance IsString ElemMatcher where
53instance IsString ElemMatcher where
58 fromString = matchElemName . fromString
54 fromString = matchElemName . fromString
@@ -61,37 +57,29 @@ instance Show ElemMatcher where
61 show = T.unpack . _emShow
57 show = T.unpack . _emShow
63-- | Match element by name
59-- | Match element by name
66matchElemName :: NameMatcher -> ElemMatcher
60matchElemName :: NameMatcher -> ElemMatcher
67matchElemName (NameMatcher matchName showName) = ElemMatcher
61matchElemName (NameMatcher matchName showName) = ElemMatcher
68 { _emMatch = views name matchName
62 { _emMatch = views name matchName
69 , _emShow = showName
63 , _emShow = showName
72−-- | Match over elements
75elMatch :: ElemMatcher -> Traversal' Element Element
66elMatch :: ElemMatcher -> Traversal' Element Element
76elMatch (ElemMatcher match _) = filtered match
67elMatch (ElemMatcher match _) = filtered match
78-- | Arbitrary name matcher. Match name any way you want, but
69-- | Arbitrary name matcher. Match name any way you want, but
79-- considered to be used as comparator with some name with some rules
70-- considered to be used as comparator with some name with some rules
82data NameMatcher = NameMatcher
71data NameMatcher = NameMatcher
83 { _nmMatch :: Name -> Bool
72 { _nmMatch :: Name -> Bool
84 -- ^ Name matching function, usually should be simple comparsion
73 -- ^ Name matching function, usually should be simple comparsion
85 -- function takin in account only local name or other components
74 -- function takin in account only local name or other components
86 -- of 'Name'
75 -- of 'Name'
87 , _nmShow :: Text
76 , _nmShow :: Text
88− -- ^ Field for 'Show' instance and bulding usefull errors
77+ -- ^ How to show the name matcher, since there is no 'Read'
78+ -- instance we can do it any way we want
91−-- | Instance use 'matchCILocalName' as most general and liberal
81+makeLenses ''NameMatcher
92−-- matching strategy (while XML is often malformed).
82+
95instance IsString NameMatcher where
83instance IsString NameMatcher where
96 fromString = matchCILocalName . T.pack
84 fromString = matchCILocalName . T.pack
@@ -100,8 +88,6 @@ instance Show NameMatcher where
101-- | Makes matcher which matches only local part of name igoring
89-- | Makes matcher which matches only local part of name igoring
102-- namespace and prefix. Local name matching is case sensitive.
90-- namespace and prefix. Local name matching is case sensitive.
105matchLocalName :: Text -> NameMatcher
91matchLocalName :: Text -> NameMatcher
106matchLocalName tname = NameMatcher
92matchLocalName tname = NameMatcher
107 { _nmMatch = \n -> nameLocalName n == tname
93 { _nmMatch = \n -> nameLocalName n == tname
@@ -111,19 +97,13 @@ matchLocalName tname = NameMatcher
111-- | Makes matcher which matches only local part of name igoring
97-- | Makes matcher which matches only local part of name igoring
112-- namespace and prefix. Local name matching is case insensitive. This
98-- namespace and prefix. Local name matching is case insensitive. This
113-- is the most common case.
99-- is the most common case.
116matchCILocalName :: Text -> NameMatcher
100matchCILocalName :: Text -> NameMatcher
117matchCILocalName tname = NameMatcher
101matchCILocalName tname = NameMatcher
118 { _nmMatch = \n -> CI.mk (nameLocalName n) == CI.mk tname
102 { _nmMatch = \n -> CI.mk (nameLocalName n) == CI.mk tname
119 , _nmShow = tname
103 , _nmShow = tname
122−makeLenses ''NameMatcher
124-- | Makes matcher which match name by 'Eq' with given
106-- | Makes matcher which match name by 'Eq' with given
127matchName :: Name -> NameMatcher
107matchName :: Name -> NameMatcher
128matchName n = NameMatcher
108matchName n = NameMatcher
129 { _nmMatch = (== n)
109 { _nmMatch = (== n)
@@ -134,7 +114,7 @@ matchName n = NameMatcher
134-- element of the document. Errors are much more usefull with path.
114-- element of the document. Errors are much more usefull with path.
135newtype DomPath = DomPath
115newtype DomPath = DomPath
136 { unDomPath :: [Text]
116 { unDomPath :: [Text]
137− } deriving (Eq, Ord, Show, Semigroup, Monoid)
117+ } deriving (Eq, Ord, Show, Monoid)
139-- | DOM parser error description.
119-- | DOM parser error description.
140data ParserError
120data ParserError
@@ -144,17 +124,13 @@ data ParserError
144 -- ^ Path of element error occured in
124 -- ^ Path of element error occured in
147− -- | Expected attribute but not found
127+ -- | Tag contents has wrong format, (could not read text to value)
148− --
128+ | PEWrongFormat
149− -- @since 1.0.0
129+ { _peDetails :: Text
150− | PEAttributeNotFound
151− { _peAttributeName :: NameMatcher
152− , _pePath :: DomPath
130+ , _pePath :: DomPath
155 -- | Could not parse attribute
133 -- | Could not parse attribute
158 | PEAttributeWrongFormat
134 | PEAttributeWrongFormat
159 { _peAttributeName :: NameMatcher
135 { _peAttributeName :: NameMatcher
160 , _peDetails :: Text
136 , _peDetails :: Text
@@ -166,10 +142,10 @@ data ParserError
166 { _pePath :: DomPath
142 { _pePath :: DomPath
169− -- | Tag contents has wrong format, (could not read text to value)
145+ -- | Expected attribute but not found
170− | PEContentWrongFormat
146+ | PEAttributeNotFound
171− { _peDetails :: Text
147+ { _peAttributeName :: NameMatcher
172− , _pePath :: DomPath
148+ , _pePath :: DomPath
175 -- | Some other error
151 -- | Some other error
@@ -184,7 +160,7 @@ instance Exception ParserError
185newtype ParserErrors = ParserErrors
161newtype ParserErrors = ParserErrors
186 { unParserErrors :: [ParserError]
162 { unParserErrors :: [ParserError]
187− } deriving (Show, Semigroup, Monoid, Generic)
163+ } deriving (Show, Monoid, Generic)
189makePrisms ''ParserErrors
165makePrisms ''ParserErrors