module Text.XML.HXT.Arrow.WriteDocument
( writeDocument
, writeDocument'
, writeDocumentToString
, prepareContents
)
where
import Control.Arrow
import Control.Arrow.ArrowList
import Control.Arrow.ArrowIf
import Control.Arrow.ArrowTree
import Text.XML.HXT.DOM.Interface
import Text.XML.HXT.Arrow.XmlArrow
import Text.XML.HXT.Arrow.XmlState
import Text.XML.HXT.Arrow.XmlState.TypeDefs
import Text.XML.HXT.Arrow.XmlState.RunIOStateArrow
( initialSysState
)
import Text.XML.HXT.Arrow.Edit ( haskellRepOfXmlDoc
, indentDoc
, addDefaultDTDecl
, preventEmptyElements
, removeDocWhiteSpace
, treeRepOfXmlDoc
)
import Text.XML.HXT.Arrow.DocumentOutput ( putXmlDocument
, encodeDocument
, encodeDocument'
)
writeDocument :: SysConfigList -> String -> IOStateArrow s XmlTree XmlTree
writeDocument :: SysConfigList -> String -> IOStateArrow s XmlTree XmlTree
writeDocument SysConfigList
config String
dst
= IOStateArrow s XmlTree XmlTree -> IOStateArrow s XmlTree XmlTree
forall s a b. IOStateArrow s a b -> IOStateArrow s a b
localSysEnv
(IOStateArrow s XmlTree XmlTree -> IOStateArrow s XmlTree XmlTree)
-> IOStateArrow s XmlTree XmlTree -> IOStateArrow s XmlTree XmlTree
forall a b. (a -> b) -> a -> b
$
SysConfigList -> IOStateArrow s XmlTree XmlTree
forall s c. SysConfigList -> IOStateArrow s c c
configSysVars SysConfigList
config
IOStateArrow s XmlTree XmlTree
-> IOStateArrow s XmlTree XmlTree -> IOStateArrow s XmlTree XmlTree
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>>
IOStateArrow s XmlTree XmlTree -> IOStateArrow s XmlTree XmlTree
forall (a :: * -> * -> *) b c. ArrowList a => a b c -> a b b
perform ( ((Bool -> String -> IOStateArrow s XmlTree XmlTree)
-> String -> Bool -> IOStateArrow s XmlTree XmlTree
forall a b c. (a -> b -> c) -> b -> a -> c
flip Bool -> String -> IOStateArrow s XmlTree XmlTree
forall s. Bool -> String -> IOStateArrow s XmlTree XmlTree
writeDocument') String
dst (Bool -> IOStateArrow s XmlTree XmlTree)
-> IOSLA (XIOState s) XmlTree Bool
-> IOStateArrow s XmlTree XmlTree
forall (a :: * -> * -> *) c b d.
ArrowList a =>
(c -> a b d) -> a b c -> a b d
$< Selector XIOSysState Bool -> IOSLA (XIOState s) XmlTree Bool
forall c s b. Selector XIOSysState c -> IOStateArrow s b c
getSysVar Selector XIOSysState Bool
theTextMode )
writeDocument' :: Bool -> String -> IOStateArrow s XmlTree XmlTree
writeDocument' :: Bool -> String -> IOStateArrow s XmlTree XmlTree
writeDocument' Bool
textMode String
dst
= ( Int -> String -> IOStateArrow s XmlTree XmlTree
forall s b. Int -> String -> IOStateArrow s b b
traceMsg Int
1 (String
"writeDocument: destination is " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> String
forall a. Show a => a -> String
show String
dst)
IOStateArrow s XmlTree XmlTree
-> IOStateArrow s XmlTree XmlTree -> IOStateArrow s XmlTree XmlTree
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>>
( ((XIOSysState
-> (Bool -> Bool -> String -> IOStateArrow s XmlTree XmlTree)
-> IOStateArrow s XmlTree XmlTree)
-> (Bool -> Bool -> String -> IOStateArrow s XmlTree XmlTree)
-> XIOSysState
-> IOStateArrow s XmlTree XmlTree
forall a b c. (a -> b -> c) -> b -> a -> c
flip XIOSysState
-> (Bool -> Bool -> String -> IOStateArrow s XmlTree XmlTree)
-> IOStateArrow s XmlTree XmlTree
forall (a :: * -> * -> *).
ArrowXml a =>
XIOSysState
-> (Bool -> Bool -> String -> a XmlTree XmlTree)
-> a XmlTree XmlTree
prepareContents) Bool -> Bool -> String -> IOStateArrow s XmlTree XmlTree
forall s. Bool -> Bool -> String -> IOStateArrow s XmlTree XmlTree
encodeDocument (XIOSysState -> IOStateArrow s XmlTree XmlTree)
-> IOSLA (XIOState s) XmlTree XIOSysState
-> IOStateArrow s XmlTree XmlTree
forall (a :: * -> * -> *) c b d.
ArrowList a =>
(c -> a b d) -> a b c -> a b d
$< Selector XIOSysState XIOSysState
-> IOSLA (XIOState s) XmlTree XIOSysState
forall c s b. Selector XIOSysState c -> IOStateArrow s b c
getSysVar Selector XIOSysState XIOSysState
forall s. Selector s s
idS )
IOStateArrow s XmlTree XmlTree
-> IOStateArrow s XmlTree XmlTree -> IOStateArrow s XmlTree XmlTree
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>>
String -> IOStateArrow s XmlTree XmlTree
forall s. String -> IOStateArrow s XmlTree XmlTree
traceDoc String
"document after encoding"
IOStateArrow s XmlTree XmlTree
-> IOStateArrow s XmlTree XmlTree -> IOStateArrow s XmlTree XmlTree
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>>
Bool -> String -> IOStateArrow s XmlTree XmlTree
forall s. Bool -> String -> IOStateArrow s XmlTree XmlTree
putXmlDocument Bool
textMode String
dst
IOStateArrow s XmlTree XmlTree
-> IOStateArrow s XmlTree XmlTree -> IOStateArrow s XmlTree XmlTree
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>>
Int -> String -> IOStateArrow s XmlTree XmlTree
forall s b. Int -> String -> IOStateArrow s b b
traceMsg Int
1 String
"writeDocument: finished"
)
IOStateArrow s XmlTree XmlTree
-> IOStateArrow s XmlTree XmlTree -> IOStateArrow s XmlTree XmlTree
forall (a :: * -> * -> *) b c. ArrowIf a => a b b -> a b c -> a b b
`when`
IOStateArrow s XmlTree XmlTree
forall (a :: * -> * -> *). ArrowXml a => a XmlTree XmlTree
documentStatusOk
writeDocumentToString :: ArrowXml a => SysConfigList -> a XmlTree String
writeDocumentToString :: SysConfigList -> a XmlTree String
writeDocumentToString SysConfigList
config
= XIOSysState
-> (Bool -> Bool -> String -> a XmlTree XmlTree)
-> a XmlTree XmlTree
forall (a :: * -> * -> *).
ArrowXml a =>
XIOSysState
-> (Bool -> Bool -> String -> a XmlTree XmlTree)
-> a XmlTree XmlTree
prepareContents ( ((XIOSysState -> XIOSysState)
-> (XIOSysState -> XIOSysState) -> XIOSysState -> XIOSysState)
-> (XIOSysState -> XIOSysState)
-> SysConfigList
-> XIOSysState
-> XIOSysState
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (XIOSysState -> XIOSysState)
-> (XIOSysState -> XIOSysState) -> XIOSysState -> XIOSysState
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
(>>>) XIOSysState -> XIOSysState
forall a. a -> a
id (String -> XIOSysState -> XIOSysState
withOutputEncoding String
unicodeString (XIOSysState -> XIOSysState) -> SysConfigList -> SysConfigList
forall a. a -> [a] -> [a]
:
Bool -> XIOSysState -> XIOSysState
withXmlPi Bool
no (XIOSysState -> XIOSysState) -> SysConfigList -> SysConfigList
forall a. a -> [a] -> [a]
:
SysConfigList
config
)
(XIOSysState -> XIOSysState) -> XIOSysState -> XIOSysState
forall a b. (a -> b) -> a -> b
$ XIOSysState
initialSysState
) Bool -> Bool -> String -> a XmlTree XmlTree
forall (a :: * -> * -> *).
ArrowXml a =>
Bool -> Bool -> String -> a XmlTree XmlTree
encodeDocument'
a XmlTree XmlTree -> a XmlTree String -> a XmlTree String
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>>
a XmlTree XmlTree -> a XmlTree String
forall (a :: * -> * -> *) n.
ArrowXml a =>
a n XmlTree -> a n String
xshow a XmlTree XmlTree
forall (a :: * -> * -> *) (t :: * -> *) b.
(ArrowTree a, Tree t) =>
a (t b) (t b)
getChildren
prepareContents :: ArrowXml a => XIOSysState -> (Bool -> Bool -> String -> a XmlTree XmlTree) -> a XmlTree XmlTree
prepareContents :: XIOSysState
-> (Bool -> Bool -> String -> a XmlTree XmlTree)
-> a XmlTree XmlTree
prepareContents XIOSysState
config Bool -> Bool -> String -> a XmlTree XmlTree
encodeDoc
= a XmlTree XmlTree
indent
a XmlTree XmlTree -> a XmlTree XmlTree -> a XmlTree XmlTree
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>>
a XmlTree XmlTree
addDtd
a XmlTree XmlTree -> a XmlTree XmlTree -> a XmlTree XmlTree
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>>
a XmlTree XmlTree
format
where
indent' :: Bool
indent' = Selector XIOSysState Bool -> XIOSysState -> Bool
forall s a. Selector s a -> s -> a
getS Selector XIOSysState Bool
theIndent XIOSysState
config
removeWS' :: Bool
removeWS' = Selector XIOSysState Bool -> XIOSysState -> Bool
forall s a. Selector s a -> s -> a
getS Selector XIOSysState Bool
theRemoveWS XIOSysState
config
showTree' :: Bool
showTree' = Selector XIOSysState Bool -> XIOSysState -> Bool
forall s a. Selector s a -> s -> a
getS Selector XIOSysState Bool
theShowTree XIOSysState
config
showHaskell' :: Bool
showHaskell' = Selector XIOSysState Bool -> XIOSysState -> Bool
forall s a. Selector s a -> s -> a
getS Selector XIOSysState Bool
theShowHaskell XIOSysState
config
outHtml' :: Bool
outHtml' = Selector XIOSysState XIOXoutConfig -> XIOSysState -> XIOXoutConfig
forall s a. Selector s a -> s -> a
getS Selector XIOSysState XIOXoutConfig
theOutputFmt XIOSysState
config XIOXoutConfig -> XIOXoutConfig -> Bool
forall a. Eq a => a -> a -> Bool
== XIOXoutConfig
HTMLoutput
outXhtml' :: Bool
outXhtml' = Selector XIOSysState XIOXoutConfig -> XIOSysState -> XIOXoutConfig
forall s a. Selector s a -> s -> a
getS Selector XIOSysState XIOXoutConfig
theOutputFmt XIOSysState
config XIOXoutConfig -> XIOXoutConfig -> Bool
forall a. Eq a => a -> a -> Bool
== XIOXoutConfig
XHTMLoutput
outXml' :: Bool
outXml' = Selector XIOSysState XIOXoutConfig -> XIOSysState -> XIOXoutConfig
forall s a. Selector s a -> s -> a
getS Selector XIOSysState XIOXoutConfig
theOutputFmt XIOSysState
config XIOXoutConfig -> XIOXoutConfig -> Bool
forall a. Eq a => a -> a -> Bool
== XIOXoutConfig
XMLoutput
noPi' :: Bool
noPi' = Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Selector XIOSysState Bool -> XIOSysState -> Bool
forall s a. Selector s a -> s -> a
getS Selector XIOSysState Bool
theXmlPi XIOSysState
config
noEEsFor' :: [String]
noEEsFor' = Selector XIOSysState [String] -> XIOSysState -> [String]
forall s a. Selector s a -> s -> a
getS Selector XIOSysState [String]
theNoEmptyElemFor XIOSysState
config
addDDTD' :: Bool
addDDTD' = Selector XIOSysState Bool -> XIOSysState -> Bool
forall s a. Selector s a -> s -> a
getS Selector XIOSysState Bool
theAddDefaultDTD XIOSysState
config
outEnc' :: String
outEnc' = Selector XIOSysState String -> XIOSysState -> String
forall s a. Selector s a -> s -> a
getS Selector XIOSysState String
theOutputEncoding XIOSysState
config
addDtd :: a XmlTree XmlTree
addDtd
| Bool
addDDTD' = a XmlTree XmlTree
forall (a :: * -> * -> *). ArrowList a => a XmlTree XmlTree
addDefaultDTDecl
| Bool
otherwise = a XmlTree XmlTree
forall (a :: * -> * -> *) b. ArrowList a => a b b
this
indent :: a XmlTree XmlTree
indent
| Bool
indent' = a XmlTree XmlTree
forall (a :: * -> * -> *). ArrowXml a => a XmlTree XmlTree
indentDoc
| Bool
removeWS' = a XmlTree XmlTree
forall (a :: * -> * -> *). ArrowXml a => a XmlTree XmlTree
removeDocWhiteSpace
| Bool
otherwise = a XmlTree XmlTree
forall (a :: * -> * -> *) b. ArrowList a => a b b
this
format :: a XmlTree XmlTree
format
| Bool
showTree' = a XmlTree XmlTree
forall (a :: * -> * -> *). ArrowList a => a XmlTree XmlTree
treeRepOfXmlDoc
| Bool
showHaskell' = a XmlTree XmlTree
forall (a :: * -> * -> *). ArrowList a => a XmlTree XmlTree
haskellRepOfXmlDoc
| Bool
outHtml' = [String] -> Bool -> a XmlTree XmlTree
forall (a :: * -> * -> *).
ArrowList a =>
[String] -> Bool -> a XmlTree XmlTree
preventEmptyElements [String]
noEEsFor' Bool
True
a XmlTree XmlTree -> a XmlTree XmlTree -> a XmlTree XmlTree
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>>
Bool -> Bool -> String -> a XmlTree XmlTree
encodeDoc
Bool
False Bool
noPi' ( if String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
outEnc' then String
usAscii else String
outEnc' )
| Bool
outXhtml' = [String] -> Bool -> a XmlTree XmlTree
forall (a :: * -> * -> *).
ArrowList a =>
[String] -> Bool -> a XmlTree XmlTree
preventEmptyElements [String]
noEEsFor' Bool
True
a XmlTree XmlTree -> a XmlTree XmlTree -> a XmlTree XmlTree
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>>
Bool -> Bool -> String -> a XmlTree XmlTree
encodeDoc
Bool
True Bool
noPi' String
outEnc'
| Bool
outXml' = ( if [String] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [String]
noEEsFor'
then a XmlTree XmlTree
forall (a :: * -> * -> *) b. ArrowList a => a b b
this
else [String] -> Bool -> a XmlTree XmlTree
forall (a :: * -> * -> *).
ArrowList a =>
[String] -> Bool -> a XmlTree XmlTree
preventEmptyElements [String]
noEEsFor' Bool
False
)
a XmlTree XmlTree -> a XmlTree XmlTree -> a XmlTree XmlTree
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>>
Bool -> Bool -> String -> a XmlTree XmlTree
encodeDoc
Bool
True Bool
noPi' String
outEnc'
| Bool
otherwise = a XmlTree XmlTree
forall (a :: * -> * -> *) b. ArrowList a => a b b
this