@@ -1,24 +1,31 @@
1{-# LANGUAGE Rank2Types, FlexibleContexts #-}
1{-# LANGUAGE Rank2Types, FlexibleContexts #-}
2+{-# OPTIONS_GHC -fno-warn-orphans #-}
2-----------------------------------------------------------------------------
3-----------------------------------------------------------------------------
4-- Module : Text.XML.Lens
5-- Module : Text.XML.Lens
5−-- Copyright : (C) 2013 Fumiaki Kinoshita
6+-- Copyright : (C) 2015 Fumiaki Kinoshita
6-- License : BSD-style (see the file LICENSE)
7-- License : BSD-style (see the file LICENSE)
8-- Maintainer : Fumiaki Kinoshita <fumiexcel@gmail.com>
9-- Maintainer : Fumiaki Kinoshita <fumiexcel@gmail.com>
9−-- Stability : experimental
10+-- Stability : provisional
10-- Portability : non-portable
11-- Portability : non-portable
11−-- Useful traversals inspired by XPath
12+--
12----------------------------------------------------------------------------
13----------------------------------------------------------------------------
13module Text.XML.Lens (
14module Text.XML.Lens (
14 -- * Lenses, traversals for 'Element'
15 -- * Lenses, traversals for 'Element'
16 -- ** Names
18 -- ** Names
19 -- ** Attributes
24 -- ** Attributes
20 , attributeIs
25 , attributeIs
21 , attributeSatisfies
26 , attributeSatisfies
27+ , attributeSatisfies'
23 , attribute
30 , attribute
@@ -26,23 +33,26 @@ module Text.XML.Lens (
28 -- ** Children
35 -- ** Children
31 -- * Prisms for 'Node'
37 -- * Prisms for 'Node'
32 , _Element
39 , _Element
33 , _Content
40 , _Content
34 , AsInstruction(..)
41 , AsInstruction(..)
35 , AsComment(..)
42 , AsComment(..)
36 -- * Lenses for 'Document'
43 -- * Lenses for 'Document'
38 , prologue
46 , prologue
39 , epilogue
47 , epilogue
41 -- * Lenses for 'Name'
49 -- * Lenses for 'Name'
42 , _nameLocalName
51 , _nameLocalName
43 , _nameNamespace
52 , _nameNamespace
44 , _namePrefix
53 , _namePrefix
45 -- * Lenses for 'Instruction'
54 -- * Lenses for 'Instruction'
46 , _instructionTarget
56 , _instructionTarget
47 , _instructionData
57 , _instructionData
@@ -50,41 +60,48 @@ import Text.XML
50import Control.Lens
60import Control.Lens
51import Data.Text (Text)
61import Data.Text (Text)
52import Data.Map (Map)
62import Data.Map (Map)
53−import Control.Applicative
63+import qualified Data.CaseInsensitive as CI
54−
64+import Data.Maybe (isNothing)
57prologue :: Lens' Document Prologue
66prologue :: Lens' Document Prologue
58prologue f doc = fmap (\p -> doc { documentPrologue = p} ) $ f $ documentPrologue doc
67prologue f doc = fmap (\p -> doc { documentPrologue = p} ) $ f $ documentPrologue doc
68+{-# INLINE prologue #-}
60-- | The root element of the document.
70-- | The root element of the document.
61root :: Lens' Document Element
71root :: Lens' Document Element
62root f doc = fmap (\p -> doc { documentRoot = p} ) $ f $ documentRoot doc
72root f doc = fmap (\p -> doc { documentRoot = p} ) $ f $ documentRoot doc
64epilogue :: Lens' Document [Miscellaneous]
75epilogue :: Lens' Document [Miscellaneous]
65epilogue f doc = fmap (\p -> doc { documentEpilogue = p} ) $ f $ documentEpilogue doc
76epilogue f doc = fmap (\p -> doc { documentEpilogue = p} ) $ f $ documentEpilogue doc
77+{-# INLINE epilogue #-}
67doctype :: Lens' Prologue (Maybe Doctype)
79doctype :: Lens' Prologue (Maybe Doctype)
68doctype f doc = fmap (\p -> doc { prologueDoctype = p} ) $ f $ prologueDoctype doc
80doctype f doc = fmap (\p -> doc { prologueDoctype = p} ) $ f $ prologueDoctype doc
81+{-# INLINE doctype #-}
70class AsInstruction t where
83class AsInstruction t where
71 _Instruction :: Prism' t Instruction
84 _Instruction :: Prism' t Instruction
73_instructionTarget :: Lens' Instruction Text
86_instructionTarget :: Lens' Instruction Text
74_instructionTarget f (Instruction t d) = f t <&> \t' -> Instruction t' d
87_instructionTarget f (Instruction t d) = f t <&> \t' -> Instruction t' d
88+{-# INLINE _instructionTarget #-}
76_instructionData :: Lens' Instruction Text
90_instructionData :: Lens' Instruction Text
77_instructionData f (Instruction t d) = f d <&> \d' -> Instruction t d'
91_instructionData f (Instruction t d) = f d <&> \d' -> Instruction t d'
92+{-# INLINE _instructionData #-}
79instance AsInstruction Node where
94instance AsInstruction Node where
80 _Instruction = prism' NodeInstruction $ \s -> case s of
95 _Instruction = prism' NodeInstruction $ \s -> case s of
81 NodeInstruction e -> Just e
96 NodeInstruction e -> Just e
82 _ -> Nothing
97 _ -> Nothing
98+ {-# INLINE _Instruction #-}
84instance AsInstruction Miscellaneous where
100instance AsInstruction Miscellaneous where
85 _Instruction = prism' MiscInstruction $ \s -> case s of
101 _Instruction = prism' MiscInstruction $ \s -> case s of
86 MiscInstruction e -> Just e
102 MiscInstruction e -> Just e
87 _ -> Nothing
103 _ -> Nothing
104+ {-# INLINE _Instruction #-}
89class AsComment t where
106class AsComment t where
90 _Comment :: Prism' t Text
107 _Comment :: Prism' t Text
@@ -93,78 +110,107 @@ instance AsComment Node where
93 _Comment = prism' NodeComment $ \s -> case s of
110 _Comment = prism' NodeComment $ \s -> case s of
94 NodeComment e -> Just e
111 NodeComment e -> Just e
95 _ -> Nothing
112 _ -> Nothing
113+ {-# INLINE _Comment #-}
97instance AsComment Miscellaneous where
115instance AsComment Miscellaneous where
98 _Comment = prism' MiscComment $ \s -> case s of
116 _Comment = prism' MiscComment $ \s -> case s of
99 MiscComment e -> Just e
117 MiscComment e -> Just e
100 _ -> Nothing
118 _ -> Nothing
119+ {-# INLINE _Comment #-}
102_nameLocalName :: Lens' Name Text
121_nameLocalName :: Lens' Name Text
103−_nameLocalName f n = fmap (\x -> n { nameLocalName = x }) $ f $ nameLocalName n
122+_nameLocalName f n = f (nameLocalName n) <&> \x -> n { nameLocalName = x }
123+{-# INLINE _nameLocalName #-}
105_nameNamespace :: Lens' Name (Maybe Text)
125_nameNamespace :: Lens' Name (Maybe Text)
106−_nameNamespace f n = fmap (\x -> n { nameNamespace = x }) $ f $ nameNamespace n
126+_nameNamespace f n = f (nameNamespace n) <&> \x -> n { nameNamespace = x }
127+{-# INLINE _nameNamespace #-}
108_namePrefix :: Lens' Name (Maybe Text)
129_namePrefix :: Lens' Name (Maybe Text)
109−_namePrefix f n = fmap (\x -> n { namePrefix = x }) $ f $ namePrefix n
130+_namePrefix f n = f (namePrefix n) <&> \x -> n { namePrefix = x }
131+{-# INLINE _namePrefix #-}
111_Element :: Prism' Node Element
133_Element :: Prism' Node Element
112_Element = prism' NodeElement $ \s -> case s of
134_Element = prism' NodeElement $ \s -> case s of
113 NodeElement e -> Just e
135 NodeElement e -> Just e
114 _ -> Nothing
136 _ -> Nothing
137+{-# INLINE _Element #-}
116_Content :: Prism' Node Text
139_Content :: Prism' Node Text
117_Content = prism' NodeContent $ \s -> case s of
140_Content = prism' NodeContent $ \s -> case s of
118 NodeContent e -> Just e
141 NodeContent e -> Just e
119 _ -> Nothing
142 _ -> Nothing
143+{-# INLINE _Content #-}
121name :: Lens' Element Name
145name :: Lens' Element Name
122−name f e = fmap (\x -> e { elementName = x }) $ f $ elementName e
146+name f e = f (elementName e) <&> \x -> e { elementName = x }
149+localName :: Lens' Element Text
150+localName = name . _nameLocalName
151+{-# INLINE localName #-}
124attrs :: Lens' Element (Map Name Text)
153attrs :: Lens' Element (Map Name Text)
125attrs f e = fmap (\x -> e { elementAttributes = x }) $ f $ elementAttributes e
154attrs f e = fmap (\x -> e { elementAttributes = x }) $ f $ elementAttributes e
127nodes :: Lens' Element [Node]
157nodes :: Lens' Element [Node]
128nodes f e = fmap (\x -> e { elementNodes = x }) $ f $ elementNodes e
158nodes f e = fmap (\x -> e { elementNodes = x }) $ f $ elementNodes e
130−attr :: Name -> IndexedTraversal' Name Element Text
161+attr :: Name -> Traversal' Element Text
131attr n = attrs . ix n
162attr n = attrs . ix n
133−attribute :: Name -> IndexedLens' Name Element (Maybe Text)
165+attribute :: Name -> Lens' Element (Maybe Text)
134attribute n = attrs . at n
166attribute n = attrs . at n
167+{-# INLINE attribute #-}
169+-- | Traverse elements which has the specified *local* name (case-insensitive).
170+named :: CI.CI Text -> Traversal' Element Element
172+ | CI.mk (nameLocalName (elementName s)) == n = f s
173+ | otherwise = pure s
136−-- | Traverse itself with its all children.
176+-- | Old name for 'named'
137−entire :: Traversal' Element Element
177+ell :: Text -> Traversal' Element Element
138−entire f e@(Element _ _ ns) = com <$> f e <*> traverse (_Element (entire f)) ns where
178+ell n = named $ CI.mk n
139− com (Element n a _) ns = Element n a ns
141-- | Traverse elements which has the specified name.
180-- | Traverse elements which has the specified name.
142el :: Name -> Traversal' Element Element
181el :: Name -> Traversal' Element Element
144 | elementName s == n = f s
183 | elementName s == n = f s
145 | otherwise = pure s
184 | otherwise = pure s
185+{-# DEPRECATED el "Use named instead" #-}
147attributeSatisfies :: Name -> (Text -> Bool) -> Traversal' Element Element
187attributeSatisfies :: Name -> (Text -> Bool) -> Traversal' Element Element
148−attributeSatisfies n p = filtered (maybe False p . preview (attrs . ix n))
188+attributeSatisfies n p = attributeSatisfies' n (maybe False p)
189+{-# INLINE attributeSatisfies #-}
191+attributeSatisfies' :: Name -> (Maybe Text -> Bool) -> Traversal' Element Element
192+attributeSatisfies' n p = filtered (p . preview (attrs . ix n))
193+{-# INLINE attributeSatisfies' #-}
195+withoutAttribute :: Name -> Traversal' Element Element
196+withoutAttribute n = attributeSatisfies' n isNothing
197+{-# INLINE withoutAttribute #-}
150attributeIs :: Name -> Text -> Traversal' Element Element
199attributeIs :: Name -> Text -> Traversal' Element Element
151attributeIs n v = attributeSatisfies n (==v)
200attributeIs n v = attributeSatisfies n (==v)
201+{-# INLINE attributeIs #-}
153-- | Traverse all contents of the element.
203-- | Traverse all contents of the element.
154text :: Traversal' Element Text
204text :: Traversal' Element Text
155text = nodes . traverse . _Content
205text = nodes . traverse . _Content
157-- | Traverse all comments of the element.
208-- | Traverse all comments of the element.
158comment :: Traversal' Element Text
209comment :: Traversal' Element Text
159comment = nodes . traverse . _Comment
210comment = nodes . traverse . _Comment
211+{-# INLINE comment #-}
213+-- | 'plate' traverses over its sub-elements.
161instance Plated Element where
214instance Plated Element where
162 plate = nodes . traverse . _Element
215 plate = nodes . traverse . _Element
164−-- | Combine two 'Traversal's just like XPath's slash.
167−-- l ./ m ≡ l . 'plate' . m
169−(./) :: Plated a => Traversal s t a a -> Traversal a a u v -> Traversal s t u v
170−l ./ m = l . plate . m
216+ {-# INLINE plate #-}