| 
    1module XMLSyntaxTree 
   2where 
   3 
   4import Prelude hiding (lookup, map) 
   5import qualified Prelude as P 
   6 
   7import Map 
   8 
   9data XMLTree    = PlainText     Text 
  10                | Comment       Comment 
  11                | CharRef       Unicode 
  12                | EntityRef     String 
  13                | SimpleElem    Tag 
  14                | CompoundElem  Tag Content 
  15 
  16type Content    = [XMLTree] 
  17 
  18data Tag        = Tag ElemName  Attributes 
  19 
  20type Attributes = Map AttrName  AttrValue 
  21 
  22-- alias names 
  23 
  24type Text       = String 
  25type Comment    = String 
  26type ElemName   = String 
  27type AttrName   = String 
  28type AttrValue  = String 
  29 
  30type Unicode    = Int   -- or perhaps Char ? 
  31 
  32-- questions 
  33-- 
  34-- posible simplifications? 
  35-- entity- or char references in attributes? 
  36-- processing instructions? 
  37 
  38-- ------------------------------------------------------------ 
  39-- 
  40-- one processing method 
  41 
  42toString                :: XMLTree -> String 
  43toString (PlainText t)  = t 
  44toString (Comment c)    = "<!--" ++ c ++ "-->" 
  45toString (CharRef r)    = "&#" ++ show r ++ ";" 
  46toString (EntityRef e)  = "&" ++ e ++ ";" 
  47toString (SimpleElem t) = stagToString t 
  48toString (CompoundElem t c) 
  49                        = ctagToString t c 
  50 
  51stagToString            :: Tag -> String 
  52stagToString (Tag n al) = "<" ++ n ++ attrToString al ++ "/>" 
  53 
  54ctagToString            :: Tag -> Content -> String 
  55ctagToString (Tag n al) cont 
  56                        = "<" ++ n ++ attrToString al ++ ">" ++ 
  57                          concatMap toString cont ++ 
  58                          "</" ++ n ++ ">" 
  59 
  60attrToString            :: Attributes -> String 
  61attrToString al 
  62    = foldWithKey aToString "" al 
  63    where 
  64    aToString k a rest = " " ++ k ++ "=\"" ++ a ++ "\"" ++ rest 
  65 
  66-- ------------------------------------------------------------ 
  67-- 
  68-- select the plain text of a document 
  69 
  70toText                  :: XMLTree -> String 
  71toText (PlainText t)    = t 
  72toText (Comment c)      = "" 
  73toText (CharRef r)      = [toEnum r] 
  74toText (EntityRef e)    = "&" ++ e ++ ";" 
  75toText (SimpleElem t)   = "" 
  76toText (CompoundElem t c) 
  77                        = concatMap toText c 
 | 
    
  | 
    
| Letzte Änderung: 13.04.2012 | © Prof. Dr. Uwe Schmidt |