~ubuntu-branches/ubuntu/hardy/pxp/hardy

« back to all changes in this revision

Viewing changes to rtests/codewriter/xmlcompile.ml

  • Committer: Bazaar Package Importer
  • Author(s): Stefano Zacchiroli
  • Date: 2005-03-29 11:06:39 UTC
  • mfrom: (2.1.2 hoary)
  • Revision ID: james.westby@ubuntu.com-20050329110639-5p39hz1d4aq3r2ec
Tags: 1.1.95-6
* Rebuilt against ocaml 3.08.3
* No longer built with wlex support (since wlex is no longer supported
  upstream and corresponding package has been removed from the debian
  archive)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
(* $Id: xmlcompile.ml 675 2004-06-13 16:03:16Z gerd $
 
2
 * ----------------------------------------------------------------------
 
3
 *
 
4
 *)
 
5
 
 
6
 
 
7
open Pxp_document;;
 
8
open Pxp_yacc;;
 
9
open Pxp_types;;
 
10
 
 
11
let error_happened = ref false;;
 
12
 
 
13
let rec prerr_error e =
 
14
  prerr_endline (string_of_exn e)
 
15
;;
 
16
 
 
17
 
 
18
class warner =
 
19
  object 
 
20
    method warn w =
 
21
      prerr_endline ("WARNING: " ^ w)
 
22
  end
 
23
;;
 
24
 
 
25
 
 
26
let compile in_filename out_filename print super_root pis comments =
 
27
  let spec =
 
28
    let e = new element_impl default_extension in
 
29
    make_spec_from_mapping
 
30
      ~super_root_exemplar:      (new super_root_impl default_extension)
 
31
      ~default_pinstr_exemplar:  (new pinstr_impl default_extension)
 
32
      ~comment_exemplar:         (new comment_impl default_extension)
 
33
      ~data_exemplar:            (new data_impl default_extension)
 
34
      ~default_element_exemplar: e
 
35
      ~element_mapping:          (Hashtbl.create 1)
 
36
      ()
 
37
  in
 
38
  let config =
 
39
      { default_config with 
 
40
          encoding = `Enc_utf8;
 
41
          warner = new warner;
 
42
          enable_super_root_node = super_root;
 
43
          enable_pinstr_nodes = pis;
 
44
          enable_comment_nodes = comments;
 
45
      }
 
46
  in
 
47
  try 
 
48
    let tree =
 
49
      parse_document_entity
 
50
        config
 
51
        (from_file in_filename)
 
52
        spec 
 
53
    in
 
54
    
 
55
    let ch = open_out out_filename in
 
56
    Pxp_codewriter.write_document ch tree;
 
57
    output_string ch "(create_document { Pxp_yacc.default_config with Pxp_yacc.encoding = `Enc_utf8 } Pxp_yacc.default_spec) # write (`Out_channel stdout) `Enc_utf8;;\n";
 
58
    close_out ch;
 
59
 
 
60
    if print then
 
61
      tree # write (`Out_channel stdout) `Enc_utf8;
 
62
  with
 
63
      e ->
 
64
        error_happened := true;
 
65
        prerr_error e
 
66
;;
 
67
 
 
68
 
 
69
let main() =
 
70
  let in_file = ref "" in
 
71
  let out_file = ref "" in
 
72
  let print_file = ref false in
 
73
  let super_root = ref false in
 
74
  let pis = ref false in
 
75
  let comments = ref false in
 
76
  Arg.parse
 
77
      [ "-in", (Arg.String (fun s -> in_file := s)),
 
78
            " <file>      Set the XML file to read";
 
79
        "-out", (Arg.String (fun s -> out_file := s)),
 
80
             " <file>     Set the Ocaml file to write";
 
81
        "-print", (Arg.Set print_file),
 
82
               "          Print the XML file in standard form";
 
83
        "-super-root", Arg.Set super_root,
 
84
                    "     Generate a super root node";
 
85
        "-pis", Arg.Set pis,
 
86
             "            Generate wrapper nodes for processing instructions";
 
87
        "-comments", Arg.Set comments,
 
88
                  "       Generate nodes for comments";
 
89
      ]
 
90
      (fun x -> raise (Arg.Bad "Unexpected argument"))
 
91
      "
 
92
usage: compile [ options ]
 
93
 
 
94
List of options:";
 
95
  if !in_file = "" then begin
 
96
    prerr_endline "No input file specified.";
 
97
    exit 1
 
98
  end;
 
99
  if !out_file = "" then begin
 
100
    prerr_endline "No output file specified.";
 
101
    exit 1
 
102
  end;
 
103
  compile !in_file !out_file !print_file !super_root !pis !comments
 
104
;;
 
105
 
 
106
 
 
107
main();
 
108
if !error_happened then exit(1);;
 
109