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