summaryrefslogtreecommitdiff
path: root/ACEXML/examples/SAXPrint/main.cpp
blob: 1430df1d2a2c7db978112d9a62ea5af956fa71b5 (plain)
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
// $Id$

#include "ACEXML/common/FileCharStream.h"
#include "ACEXML/common/HttpCharStream.h"
#include "ACEXML/common/StrCharStream.h"
#include "ACEXML/parser/parser/Parser.h"
#include "ACEXML/parser/parser/Parser.h"
#include "Print_Handler.h"
#include "SAXPrint_Handler.h"
#include "ace/Get_Opt.h"

static const ACEXML_Char *test_string =
ACE_TEXT ("<?xml version='1.0'?> <ACE_Svc_Conf> <static id=\"ACE_Service_Manager\" params='-d -p 4911'/> <dynamic id=\"Test_Task\" type=\"service_object\"> &#65; &amp; <initializer path=\"CCM_App\" init='_make_Test_Task' params='-p 3000'/> </dynamic> </ACE_Svc_Conf>");

static void
usage (const ACE_TCHAR* program)
{
  ACE_ERROR ((LM_ERROR,
              ACE_TEXT ("Usage: %s [-sl] [-f <filename> | -u <url>]\n")
              ACE_TEXT ("  -s: Use SAXPrint_Handler (Default is Print_Handler)\n")
              ACE_TEXT ("  -l: Parse the internal strings (test the StrCharStream class)\n")
              ACE_TEXT ("  -f: Specify the filename when -l is not specified\n")
              ACE_TEXT ("  -u: URL specifying the path to the file\n"),
              program));
}

int
ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
  ACE_TCHAR *filename = 0;
  int sax = 0;                  // Use SAXPrint handler or not.
  int str = 0;
  ACEXML_Char* url = 0;

  ACE_Get_Opt get_opt (argc, argv, ACE_TEXT ("sf:lu:"));
  char c;

  while ((c = get_opt ()) != EOF)
    {
      switch (c)
        {
        case 's':
          sax = 1;
          break;
        case 'l':
          str = 1;
          break;
        case 'f':
          filename = get_opt.opt_arg ();
          break;
        case 'u':
          url = get_opt.opt_arg();
          break;
        default:
          usage(argv[0]);
          return -1;
        }
    }

  if (str == 0 && filename == 0 && url == 0) {
    usage(argv[0]);
    return -1;
  }

  ACEXML_DefaultHandler *handler = 0;
  ACEXML_CharStream *stm = 0;
  ACEXML_FileCharStream *fstm = 0;
  ACEXML_HttpCharStream *ustm = 0;
  {
    if (filename != 0)
      {
        ACE_NEW_RETURN (fstm,
                        ACEXML_FileCharStream (),
                        -1);

        if (fstm->open (filename) != 0)
          ACE_ERROR_RETURN ((LM_ERROR,
                             ACE_TEXT ("Failed to open XML file: %s\n"),
                             filename),
                            -1);
        stm = fstm;
      }
    else if (url != 0)
      {

        ACE_NEW_RETURN (ustm,
                        ACEXML_HttpCharStream (),
                        -1);
        if (ustm->open (url) != 0)
          ACE_ERROR_RETURN ((LM_ERROR,
                             ACE_TEXT ("Failed to open URL : %s\n"),
                             url),
                            -1);
        stm = ustm;
      }
    else {
      ACE_NEW_RETURN (stm,
                        ACEXML_StrCharStream (test_string),
                        -1);
      }

    if (sax == 0)
      ACE_NEW_RETURN (handler,
                      ACEXML_Print_Handler (filename),
                      -1);
    else
      ACE_NEW_RETURN (handler,
                      ACEXML_SAXPrint_Handler (),
                      -1);

    ACEXML_Parser parser;
    ACEXML_InputSource input(stm);

    parser.setContentHandler (handler);
    parser.setDTDHandler (handler);
    parser.setErrorHandler (handler);
    parser.setEntityResolver (handler);

    ACEXML_Env xmlenv;

    parser.parse (&input, xmlenv);
  }
  delete handler;
  delete fstm;
  delete ustm;
  return 0;
}