{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE RecordWildCards #-} {- Copyright (c) 2019 Herbert Valerio Riedel This file is free software: you may copy, redistribute and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This file is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program (see `LICENSE.GPLv3`). If not, see . This file incorporates work covered by the following copyright and permission notice: (c) 2007 Galois Inc. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the author nor the names of his contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -} -- | -- Module : Text.XML.Output -- Copyright : (c) Galois, Inc. 2007 -- (c) Herbert Valerio Riedel 2019 -- SPDX-License-Identifier: BSD-3-Clause AND GPL-3.0-or-later -- -- Output handling for the lightweight XML lib. -- module Text.XML.Output ( serializeXML , serializeXMLDoc , serializeXMLRoot , SerializeXMLOptions(..), defaultSerializeXMLOptions ) where import Common import qualified Data.Text as T import qualified Data.Text.Lazy as TL import qualified Data.Text.Lazy.Builder as TLB import qualified Data.Text.Short as TS import Text.XML.Types import Utils -- | Serialize XML 1.0 document prefixed by the XML prologue -- \"@\@\" -- serializeXMLDoc :: Element -> TL.Text serializeXMLDoc el = serializeXMLRoot defaultSerializeXMLOptions (Root (Just (XmlDeclaration Nothing Nothing)) [] Nothing el []) -- | Serialize a sequence of XML 'Content' nodes serializeXML :: [Content] -> TL.Text serializeXML = TL.pack . foldr (ppContentS defaultSerializeXMLOptions) "" -- | Default rendering options -- -- * Allow empty tags for all non-special elements -- -- * Don't insert newlines between prolog/epilog nodes -- -- * Do not sort attributes -- defaultSerializeXMLOptions :: SerializeXMLOptions defaultSerializeXMLOptions = SerializeXMLOptions { serializeAllowEmptyTag = const True , serializeProEpilogAddNLs = False , serializeSortAttributes = False } -- | Options for tweaking XML serialization output data SerializeXMLOptions = SerializeXMLOptions { serializeAllowEmptyTag :: QName -> Bool , serializeProEpilogAddNLs :: Bool , serializeSortAttributes :: Bool } -- | Serialize a XML 'Root' serializeXMLRoot :: SerializeXMLOptions -> Root -> TL.Text serializeXMLRoot sopts Root{..} = TLB.toLazyText $ (if serializeProEpilogAddNLs sopts then bUnlines else mconcat) $ maybeToList xmldecl ++ map bMisc rootPreElem ++ (case rootDoctype of Nothing -> [] Just (dtd,moreMisc) -> (" TLB.fromText dtd <+> ">") : map bMisc moreMisc ) ++ [TLB.fromString (ppElementS sopts rootElement "")] ++ map bMisc rootPostElem where xmldecl = case rootXmlDeclaration of Nothing -> Nothing Just (XmlDeclaration Nothing Nothing) -> Just "" Just (XmlDeclaration menc mstand) -> Just $ (") $ (maybe id (\enc cont -> " encoding=\"" <+> bFromShortText enc <+> "\"" <+> cont) menc) $ (maybe id (\b cont -> " standalone=\"" <+> (if b then "yes" else "no") <+> "\"" <+> cont) mstand) $ "?>" bMisc (Left (Comment t)) = "" bMisc (Right (PI tgt dat)) = " bFromShortText tgt <+> (if T.null dat then mempty else " ") <+> TLB.fromText dat <+> "?>" -------------------------------------------------------------------------------- -- | Pretty printing content using ShowS ppContentS :: SerializeXMLOptions -> Content -> ShowS ppContentS c x xs = case x of Elem e -> ppElementS c e xs Text t -> showCDataS t xs CRef r -> showCRefS r xs Proc p -> ppProcS p xs Comm t -> ppCommS t xs ppElementS :: SerializeXMLOptions -> Element -> ShowS ppElementS c e xs = tagStart (serializeSortAttributes c) (elName e) (elAttribs e) $ case elContent e of [] | allowEmpty -> "/>" ++ xs [Text t] -> ">" ++ showCDataS t (tagEnd name xs) cs -> '>' : foldr (ppContentS c) (tagEnd name xs) cs where name = elName e allowEmpty = serializeAllowEmptyTag c name ppCommS :: Comment -> ShowS ppCommS (Comment t) xs = "" ++ xs ppProcS :: PI -> ShowS ppProcS (PI tgt dat) xs = "" ++ xs -------------------------------------------------------------------------------- -- Note: crefs should not contain '&', ';', etc. showCRefS :: ShortText -> ShowS showCRefS r xs = '&' : TS.unpack r ++ ';' : xs -- | Convert a text element to characters. showCDataS :: CData -> ShowS showCDataS cd = case cdVerbatim cd of CDataText -> escStr (T.unpack $ cdData cd) CDataVerbatim -> showString "" CDataRaw -> \ xs -> T.unpack (cdData cd) ++ xs -- escape text in `` blocks escCData :: String -> ShowS escCData (']' : ']' : '>' : cs) = showString "]]]]>" . escCData cs escCData (c : cs) = showChar c . escCData cs escCData [] = id -- escape char in text-nodes escChar :: Char -> ShowS escChar c = case c of '<' -> showString "<" -- MUST '>' -> showString ">" -- MUST ("for compatibility") '&' -> showString "&" -- MUST '\x0D' -> showString " " -- MUST (due to EOL normalization) _ -> showChar c -- escape char in attribute value escCharAttr :: Char -> ShowS escCharAttr c = case c of '<' -> showString "<" -- MUST '&' -> showString "&" -- MUST '"' -> showString """ -- MUST (for attr enclosed by ") '\x09' -> showString " " -- MUST (due to attr WS normalization) '\x0A' -> showString " " -- MUST (due to attr WS normalization) '\x0D' -> showString " " -- MUST (due to EOL normalization) _ -> showChar c escStr :: String -> ShowS escStr cs rs = foldr escChar rs cs escStrAttr :: String -> ShowS escStrAttr cs rs = foldr escCharAttr rs cs tagEnd :: QName -> ShowS tagEnd qn rs = '<':'/':showQName qn ++ '>':rs tagStart :: Bool -> QName -> [Attr] -> ShowS tagStart sortAttr qn as rs = '<':showQName qn ++ as_str ++ rs where as_str = if null as then "" else ' ' : unwords (map showAttr as') as' | sortAttr = sort as | otherwise = as showAttr :: Attr -> String showAttr (Attr qn v) = showQName qn ++ '=' : '"' : escStrAttr (T.unpack v) "\"" showQName :: QName -> String showQName q = pre ++ showLName (qLName q) where pre = case qPrefix q of Nothing -> "" Just p -> TS.unpack p ++ ":" showLName :: LName -> String showLName = TS.unpack . unLName