<commit_before>{-
 This file is part of the Clafer Translator (clafer).

 Copyright (C) 2010 <NAME> <http://gsd.uwaterloo.ca/kbak>

 clafer is free software: you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 clafer 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 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public License
 along with clafer. (See files COPYING and COPYING.LESSER.)  If not,
 see <http://www.gnu.org/licenses/>.
-}
module Generator.Xml where

-- import Text.XML.HaXml.XmlContent.Haskell hiding (Result)

import Common
import Front.Absclafer
import Intermediate.Intclafer
-- import Generator.XmlHelp

tag name exp = concat ["<", name, ">", exp, "</", name, ">"]

optTag elem f = maybe "" f elem

tagType name typename exp = opening ++ rest
  where
  opening = concat ["<", name, " xsi:type=\"cl:", typename, "\""]
  rest
    | null exp  = " />"
    | otherwise = concat [">", exp, "</", name, ">"]

genXmlInteger n = tag "IntLiteral" $ show n

genXmlModule :: IModule -> Result
genXmlModule decls = concat
  [ "<Module xmlns=\"http://gsd.uwaterloo.ca/clafer\""
  , " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
  , " xmlns:cl=\"http://gsd.uwaterloo.ca/clafer\">"
  ,  concatMap genXmlDeclaration decls
  , "</Module>"]

genXmlDeclaration :: IDeclaration -> Result
genXmlDeclaration x = case x of
  IClaferDecl clafer  -> tagType "Declaration" "IClaferDecl" $ genXmlClafer clafer
  IConstDecl lexp  -> tagType "Declaration" "IConstraintDecl" $ genXmlLExp lexp

genXmlClafer :: IClafer -> Result
genXmlClafer x = case x of
  IClafer abstract gcard id uid super card glcard elements  ->
    tag "Clafer" $ concat [ genXmlAbstract abstract
                          , optTag gcard genXmlGCard
                          , genXmlId id
                          , genXmlUid uid
                          , genXmlSuper super
                          , optTag card genXmlCard
                          , genXmlGlCard glcard
                          , concatMap genXmlElement elements]

genXmlAbstract isAbstract = tag "IsAbstract" $ show isAbstract

genXmlGCard (IGCard isKeyword interval) = tag "GroupCard" $ concat<commit_msg>new xml generator<commit_after>{-
 This file is part of the Clafer Translator (clafer).

 Copyright (C) 2010 <NAME> <http://gsd.uwaterloo.ca/kbak>

 clafer is free software: you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 clafer 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 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public License
 along with clafer. (See files COPYING and COPYING.LESSER.)  If not,
 see <http://www.gnu.org/licenses/>.
-}
module Generator.Xml where

-- import Text.XML.HaXml.XmlContent.Haskell hiding (Result)

import Common
import Front.Absclafer
import Intermediate.Intclafer
-- import Generator.XmlHelp

tag name exp = concat ["<", name, ">", exp, "</", name, ">"]

optTag elem f = maybe "" f elem

tagType name typename exp = opening ++ rest
  where
  opening = concat ["<", name, " xsi:type=\"cl:", typename, "\""]
  rest
    | null exp  = " />"
    | otherwise = concat [">", exp, "</", name, ">"]

genXmlInteger n = tag "IntLiteral" $ show n

genXmlModule :: IModule -> Result
genXmlModule decls = concat
  [ "<?xml version=\"1.0\"?>"
  , "<Module xmlns=\"http://gsd.uwaterloo.ca/clafer\""
  , " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
  , " xmlns:cl=\"http://gsd.uwaterloo.ca/clafer\">"
  ,  concatMap genXmlDeclaration decls
  , "</Module>"]

genXmlDeclaration :: IDeclaration -> Result
genXmlDeclaration x = case x of
  IClaferDecl clafer  -> tagType "Declaration" "IClaferDecl" $ genXmlClafer clafer
  IConstDecl lexp  -> tagType "Declaration" "IConstraintDecl" $ genXmlLExp lexp

genXmlClafer :: IClafer -> Result
genXmlClafer x = case x of
  IClafer abstract gcard id uid super card glcard elements  ->
    tag "Clafer" $ concat [ genXmlAbstract abstract
                          , optTag gcard genXmlGCard
                          , genXmlId id
                          , genXmlUid uid
                          , genXmlSuper super
                          , optTag card genXmlCard
                          , genXmlGlCard glcard
                          , concatMap genXmlElement elements]

genXmlAbstract isAbstract = tag "IsAbstract" $ show isAbstract

genXmlGCard (IGCard isKeyword interval) = tag "GroupCard" $ concat
{'fraction_non_alphanumeric': 0.10074921135646688, 'fraction_numerical': 0.004337539432176656, 'mean_word_length': 2.9295120061967466, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 1, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '618388', 'n_tokens_mistral': 1543, 'n_tokens_neox': 1474, 'n_words': 523}