basic_xml_parser.anubis 14.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
/*
 * Created by PyramIDE.
 * User: Totoro
 * Date: 07/12/2011
 * Time: 14:23
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
 
read system/data_io.anubis
read tools/streams.anubis
read tools/basis.anubis
read system/string.anubis


read lexical_analysis/fast_lexer.anubis   
execute anbexec apg xml_parser.apg
read xml_parser.apg.anubis

   
public type XML_Value:
  xml_value(String name, String value).

 type XML_Value:   
   value(String name, String value). 
   
*******************************************************************************************

define One print_ind (Int n) = print(constant_string(n,' ')). 

define One print
  (
    XML_Element e,
    Int ind
  ).

  
define One
  print
  (
    XML_Element e,
    Int ind
  ) = 
  if e is
  {
    node(name,l)            then print("\n");print_ind(ind);print("<"+name+">"); 
                                 map_forget((XML_Element e1) |-> print(e1,ind+2),l); 
                                 print("\n");print_ind(ind);print("</"+name+">"), 
    leaf(name,val)          then print("\n");print_ind(ind);print("<"+name+">"+*val+"</"+name+">"), 
    no_match(o, c)          then println("NO MATCH "+o+" /= "+c)
  }.

public define One 
  print
  (
    XML_File f
  )=
  if f is xml_file(header, element) then
    println("<?"+header+"?>");
    print(element,0).
    
define Maybe(One) 
  write_ind 
  ( 
  Int n,
  WStream file
  )= 
  if write(file,to_byte_array(constant_string(n,' '))) is
  {
    failure     then failure,
    success(_)  then success(unique)
  }.
  
define Maybe(One)
  write_xml
  (
    XML_Element e,
    Int ind,
    WStream file
  ) = 
  if e is
  {
    node(name,l)    then if write(file,to_byte_array("\n")) is
                         {
                           failure     then failure,
                           success(_)  then 
                           if  write_ind(ind,file) is
                           {
                             failure     then failure,
                             success(_)  then
                             if write(file,to_byte_array("<"+name+">")) is
                             {
                               failure    then failure,
                               success(_) then 
                               map_forget((XML_Element e1) |-> write_xml(e1,ind+2,file),l);    
                               if write(file,to_byte_array("\n")) is
                               {
                                failure     then failure,
                                success(_)  then
                                if write_ind(ind,file) is
                                {
                                  failure     then failure,
                                  success(_)  then
                                  if write(file,to_byte_array("</"+name+">")) is
                                  {
                                  failure     then failure,
                                  success(_)  then success(unique)
                                  }}}}}},
                           
    leaf(name,val)   then if write(file,to_byte_array("\n")) is
                          {
                            failure     then failure
                            success(_)  then
                            if write_ind(ind,file) is
                            {
                              failure     then failure,
                              success(_)  then
                              if write(file,to_byte_array("<"+name+">"+*val+"</"+name+">")) is
                              {
                                failure     then failure,
                                success(_)  then success(unique)
                              }}},
                              
    no_match(o, c)    then  if write(file,to_byte_array("NO MATCH "+o+" /= "+c+"\n")) is
                            {
                              failure     then failure,
                              success(_)  then  success(unique)
                            }
  }.

public define Maybe(One) 
  write_xml
  (
    WStream     file,
    XML_File    f
  )=
  if f is xml_file(header, element) then
    if write(file,to_byte_array("<?"+header+"?>")) is
    {
      failure     then failure,
      success(_)  then 
      if write_xml(element,0,file) is
      {
        failure     then failure
        success(_)  then success(unique)
      }
    }.

public define Maybe(One)
  create_file
  (
    XML_File xml_file,
    String   target_filename
  )=
  	if (Maybe(RWStream))file(target_filename, new) is
		{
			failure     then failure,
			success(f)  then 
			if write_xml(weaken(f),xml_file) is
			{
			  failure   then failure,
			  success(_) then success(unique)
			}
		}.	

define String to_String
  (
    Token_Value_xml_parser token
  )=
  if token is
  {
    eof(_)        then "[EOF]",
    _close(_)     then "[>]", 
    _open(_)      then "[<]",
    _open_question(_)  then "[?>]", 
    _open_slash(_)then "[</]",
    _question(_)     then "[?]",
    _question_close(_)  then "[<?]", 
    _slash(_)     then  "[/]",
    _slash_close(_)then "[/>]", 
    _text(t)      then "[text="+t+"]",
  }.
  

   
define List(LexerItem(Token_Value_xml_parser))
   xml_lexer_description =
   [
     /* recognize < */ 
    lexer_item("</", return((ByteArray b,Int l, Int c) |-> token(_open_slash(unique)))),
    lexer_item(">", return((ByteArray b,Int l, Int c) |-> token(_close(unique)))),
    lexer_item("<", return((ByteArray b,Int l, Int c) |-> token(_open(unique)))),
    lexer_item("/>", return((ByteArray b,Int l, Int c) |-> token(_slash_close(unique)))),
    lexer_item("<#?", return((ByteArray b,Int l, Int c) |-> token(_open_question(unique)))),
    lexer_item("#?>", return((ByteArray b,Int l, Int c) |-> token(_question_close(unique)))),
    lexer_item("#?", return((ByteArray b,Int l, Int c) |-> token(_question(unique)))),
    lexer_item("#/", return((ByteArray b,Int l, Int c) |-> token(_slash(unique)))),
    lexer_item("[#n#r#t# ]*", ignore),    
    lexer_item("[^#/#?#<#>]*", return((ByteArray b,Int l, Int c) |-> token(_text(to_string(b))))),
    
     /* ignore everything else */
    //lexer_item(".",ignore)             /* "." represents any character except '\n' */
                
   ]. 
  
  
**********************************************************************************************
  
**********************************************************************************************

define Maybe(LexingStream)   
   prepare_input
     (
       String filename
     ) =
   /* try to open the file (see 'predefined.anubis' section 5.1) */
   if file(filename,read) is   
     {
       failure     then failure, 
       success(f)  then make_lexing_stream("", 
                                           f,       /* the opened file */
                                           1000,    /* size of buffer for the lexing stream */
                                           100)     /* timeout (seconds) */
     }. 

define One -> Token_Value_xml_parser
  make_xml_lexer
  (
       One -> LexerOutput(Token_Value_xml_parser)   lexer
  ) = 
   (One u) |->
   if lexer(unique) is 
     {
       end_of_input   then eof(unique),  
   
       error(b,_,_)       then 
         /* should never happen with this lexer (see the above comment) */
         print("Lexical Error: ["+to_string(b)+"]\n");eof(unique)
   
       token(t)       then //println(to_String(t)); 
        t
     }. 
     



define Maybe(XML_Value)
   get_value
      (
        XML_Element   e,
        List(String)  path
      ). 

define Maybe(XML_Value)
   get_value
      (
        List(XML_Element)   l,
        List(String)        path
      ) = 
   if l is 
     {
       [ ] then failure, 
       [h . t] then if get_value(h,path) is 
                      {
                        failure    then get_value(t,path)
                        success(v) then success(v)
                      }
     }. 
     
define Maybe(XML_Value)
  get_value
  (
    XML_Element   e,
    List(String)  path
  ) = 
  if e is 
  {
    node(name,l)  then if path is 
                                   {
                                     [ ] then failure, 
                                     [h . t] then if h = name
                                                  then get_value(l,t)
                                                  else failure
                                   }
    leaf(name,vv) then if path is 
                                   {
                                     [ ] then failure, 
                                     [h . t] then if t is 
                                               { 
                                                 [ ]     then if h = name
                                                              then success(xml_value(name,*vv))
                                                              else failure, 
                                                 [_ . _] then failure
                                               }
                                   }
       no_match(oname,cname)   then failure 
     }. 

    
public define Maybe(XML_Value)
  get_value
  (
    XML_File  e,
    String    path
  ) =
  if e is xml_file(header, body) then
  get_value(body, split_by_token(path,'/')).


define Maybe(XML_Value)
   set_value
      (
        XML_Element   e,
        List(String)  path,
        String        xml_val
      ). 
      
define Maybe(XML_Value)
   set_value
      (
        List(XML_Element)   l,
        List(String)        path,
        String              xml_val
      ) = 
   if l is 
     {
       [ ] then failure, 
       [h . t] then if set_value(h,path,xml_val) is 
                      {
                        failure    then set_value(t,path,xml_val)
                        success(v) then success(v)
                      }
     }. 
     
define Maybe(XML_Value)
  set_value
  (
    XML_Element   e,
    List(String)  path,
    String        xml_val
  ) = 
  if e is 
  {
    node(name,l)  then if path is 
                                   {
                                     [ ] then failure, 
                                     [h . t] then if h = name
                                                  then set_value(l,t,xml_val)
                                                  else failure
                                   }
    leaf(name,vv) then if path is 
                                   {
                                     [ ] then failure, 
                                     [h . t] then if t is 
                                               { 
                                                 [ ]     then if h = name
                                                              then
                                                                 vv <- xml_val;
                                                                 success(xml_value(name,*vv)) 
                                                                else failure,
                                                               
                                                 [_ . _] then failure
                                               }
                                   }
       no_match(oname,cname)   then failure 
     }. 

    
public define Maybe(XML_Element)
  set_value
  (
    XML_File  e,
    String    path,
    String    xml_value
  ) =
  if e is xml_file(header, body) then
  if set_value(body, split_by_token(path,'/'), xml_value) is
  {
    failure     then failure,
    success(v)  then if v is xml_value(name,val) then println("name:"+name+" "+"value :"+val); success(body)  
  }.


public define Maybe(XML_Element)
  set_values
  (
    XML_File                e,
    List((String,String))   values
  ) =
  if e is xml_file(header, body) then
  if values is 
  {
    []      then success(body),
    [h . t] then
    if h is (name,val) then
    if set_value(body, split_by_token(name,'/'), val) is
    {
      failure     then failure,
      success(v)  then set_values(e,t)  
    }
  }.
  
public define Maybe(XML_File)
  open_xml
  (
    String  filename
  )=
  if prepare_input(filename) is 
  {
    failure     then print("cannot open or read file "); failure, 
    success(ls) then 
      if make_lexer(xml_lexer_description, '#') is
      {
        error(msg)  then println(to_English(msg)); failure,
        ok(lexer)   then  
          if xml_parser(make_xml_lexer(lexer(ls))) is
          {
            error(p)  then 
              if p is (token, l) then println("syntax error:"+to_String(token));failure,
            ok(xml_f) then 
              success(xml_f)
          }
       }
  }.
  
global define One 
  test_xml
  (
    List(String)  args
  )=
  if args is 
    {
      [ ] then print("Usage ....\n "), 
      [h . t] then 
      with pattern = (List(String)) 
         if t is 
          { 
            [ ] then [], 
            [u . _] then split_by_token(u,'/')
          }, 
  if prepare_input(h) is 
  {
    failure     then print("cannot open or read file "), 
    success(ls) then 
                with start_time = unow, 
                 if make_lexer(xml_lexer_description, '#') is
                 {
                  error(msg)  then println(to_English(msg)),
                  ok(lexer)   then  
                          if xml_parser(make_xml_lexer(lexer(ls))) is
                          {
                            error(p)  then 
                              if p is (token, l) then println("syntax error:"+to_String(token)),
                            ok(xml_f) then 
                                /* print(xml_f)*/ unique; 
                    if pattern /= []
                    then if get_value(body(xml_f),pattern) is 
                       {
                         failure then print("\n\n Value not found for "+join("/",pattern)), 
                         success(val)  then print("\n\n"+join("/",pattern)+" = "+value(val))
                       }; print("\n\n")     
                    else unique
                          };
                                    if unow - start_time is utime(secs,microsecs) then 
                                      print("Duration: "+abs_to_decimal(secs)+" seconds, "+
                                                         abs_to_decimal(microsecs)+" microseconds.\n")
                 }
     }
}.
  

 public define One
  test_values
  (
  )=
  with xml_file=snomone_pbx_directory+"test.xml",
  if open_xml(xml_file) is
  {
    failure       then println("can't open"+xml_file);unique
    success(file) then
    if set_values(file,[("/test/a","lol"),("/test/b","test")]) is
    {
      failure    then println("can't set values");unique,
      success(v) then print(file)
    }
  }.