encoding/Data/Encoding/Helper/XML.hs
Henning Guenther dc47984bf6 Implemented GB18030 encoding
This is a bit of a hack, because the static lookup data this encoding requires brings template haskell to it's knees. So I've got a program that generates a haskell module file from the XML mapping.

darcs-hash:20070823034759-a4fee-883359c8951d4376fc2d783cd7be352d6c5b2111
2007-08-22 20:47:59 -07:00

42 lines
1.2 KiB
Haskell

module Data.Encoding.Helper.XML where
import Data.Char
import Data.List (find)
import Data.Word
import Data.Maybe (mapMaybe)
import Text.XML.HaXml.Parse
import Text.XML.HaXml.Types
import Text.XML.HaXml.Verbatim
import Numeric
readDecodeTable :: String -> String -> [(Char,[Word8])]
readDecodeTable name str = let
Document _ _ (Elem root_name _ conts) _ = xmlParse name str
in concat $ mapMaybe findAssignments conts
findAssignments :: Content i -> Maybe [(Char,[Word8])]
findAssignments (CElem (Elem "assignments" _ conts) _)
= Just $ mapMaybe findAssignment conts
findAssignments _ = Nothing
findAssignment :: Content i -> Maybe (Char,[Word8])
findAssignment (CElem (Elem "a" attrs _) _) = do
u <- lookup "u" attrs
b <- lookup "b" attrs
return (chr $ readHexInt (showAttValue u),parseBinary b)
findAssignment _ = Nothing
parseBinary :: AttValue -> [Word8]
parseBinary val = map (fromIntegral.readHexInt) (words (showAttValue val))
showAttValue :: AttValue -> String
showAttValue (AttValue lst) = concat $ map (\el -> case el of
Left str -> str
Right ref -> verbatim ref) lst
readHexInt :: String -> Int
readHexInt str = case find (\x -> snd x == "") (readHex str) of
Nothing -> error "Not a hex"
Just (x,_) -> x