Use =default for skeleton copy constructor
[ACE_TAO.git] / ACE / ACEXML / examples / SAXPrint / main.cpp
blobaaae69da8acbdcfa19cca1fc6a4ef8f13f5a0015
1 #include "ACEXML/common/FileCharStream.h"
2 #include "ACEXML/common/HttpCharStream.h"
3 #include "ACEXML/common/StrCharStream.h"
4 #include "ACEXML/common/ZipCharStream.h"
5 #include "ACEXML/parser/parser/Parser.h"
6 #include "Print_Handler.h"
7 #include "SAXPrint_Handler.h"
8 #include "ace/Get_Opt.h"
9 #include "ace/Log_Msg.h"
10 #include "ace/OS_main.h"
12 static const ACEXML_Char *test_string =
13 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; &quot; <initializer path=\"CCM_App\" init='_make_Test_Task' params='-p 3000'/> </dynamic> </ACE_Svc_Conf>");
15 static void
16 usage (const ACE_TCHAR* program)
18 ACE_ERROR ((LM_ERROR,
19 ACE_TEXT ("Usage: %s [-sl] [-f <filename> | -u <url> | -z <ZIP Archive>]\n")
20 ACE_TEXT (" -s: Use SAXPrint_Handler (Default is Print_Handler)\n")
21 ACE_TEXT (" -l: Parse the internal strings (test the StrCharStream class)\n")
22 ACE_TEXT (" -f: Specify the filename when -l is not specified\n")
23 ACE_TEXT (" -z: Specify that the file is inside a ZIP archive\n")
24 ACE_TEXT (" -u: URL specifying the path to the file\n"),
25 program));
28 int
29 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
31 ACEXML_Char* filename = 0;
32 int sax = 0; // Use SAXPrint handler or not.
33 int str = 0;
34 #ifdef USE_ZZIP
35 int zip = 0;
36 #endif
37 ACEXML_Char* url = 0;
39 ACE_Get_Opt get_opt (argc, argv, ACE_TEXT ("sf:lu:z"));
40 int c;
42 while ((c = get_opt ()) != EOF)
44 switch (c)
46 case 's':
47 sax = 1;
48 break;
49 case 'l':
50 str = 1;
51 break;
52 case 'f':
53 filename = get_opt.opt_arg ();
54 break;
55 case 'u':
56 url = get_opt.opt_arg();
57 break;
58 case 'z':
59 #ifndef USE_ZZIP
60 ACE_ERROR ((LM_ERROR, ACE_TEXT ("ZZIPLIB support has not been")
61 ACE_TEXT (" compiled in. Refer to ")
62 ACE_TEXT ("$ACE_ROOT/ACEXML/README for more ")
63 ACE_TEXT ("information.\n")));
64 return -1;
65 #else
66 zip = 1;
67 break;
68 #endif /* USE_ZZIP */
69 default:
70 usage(argv[0]);
71 return -1;
75 if (str == 0 && filename == 0 && url == 0) {
76 usage(argv[0]);
77 return -1;
80 ACEXML_DefaultHandler *handler = 0;
81 ACEXML_CharStream *stm = 0;
82 ACEXML_FileCharStream *fstm = 0;
83 ACEXML_HttpCharStream *ustm = 0;
84 ACEXML_StrCharStream* sstm = 0;
85 #ifdef USE_ZZIP
86 ACEXML_ZipCharStream* zstm = 0;
87 #endif /* USE_ZZIP */
88 if (filename != 0)
90 #ifdef USE_ZZIP
91 if (zip)
93 ACE_NEW_RETURN (zstm, ACEXML_ZipCharStream(), -1);
94 if (zstm->open (filename) != 0)
95 ACE_ERROR_RETURN ((LM_ERROR,
96 ACE_TEXT ("Failed to open XML file: %s\n"),
97 filename),
98 -1);
99 stm = zstm;
101 else
103 #endif /* USE_ZZIP */
104 ACE_NEW_RETURN (fstm, ACEXML_FileCharStream (), -1);
105 if (fstm->open (filename) != 0)
106 ACE_ERROR_RETURN ((LM_ERROR,
107 ACE_TEXT ("Failed to open XML file: %s\n"),
108 filename),
109 -1);
110 stm = fstm;
111 #ifdef USE_ZZIP
113 #endif /* USE_ZZIP */
115 else if (url != 0)
117 ACE_NEW_RETURN (ustm, ACEXML_HttpCharStream (), -1);
118 if (ustm->open (url) != 0)
119 ACE_ERROR_RETURN ((LM_ERROR,
120 ACE_TEXT ("Failed to open URL : %s\n"),
121 url),
122 -1);
123 stm = ustm;
125 else
127 ACE_NEW_RETURN (sstm, ACEXML_StrCharStream, -1);
128 if (sstm->open (test_string, ACE_TEXT ("test_string")) < 0)
129 ACE_ERROR_RETURN ((LM_ERROR,
130 ACE_TEXT ("Failed to open string : %s\n"),
131 test_string),
132 -1);
133 stm = sstm;
136 ACEXML_Char* name = (filename == 0) ? url : filename;
137 if (sax == 0)
138 ACE_NEW_RETURN (handler,
139 ACEXML_Print_Handler (name),
140 -1);
141 else
142 ACE_NEW_RETURN (handler,
143 ACEXML_SAXPrint_Handler (name),
144 -1);
146 std::unique_ptr<ACEXML_DefaultHandler> cleanup_handler (handler);
148 ACEXML_Parser parser;
149 ACEXML_InputSource input (stm);
151 parser.setContentHandler (handler);
152 parser.setDTDHandler (handler);
153 parser.setErrorHandler (handler);
154 parser.setEntityResolver (handler);
158 parser.parse (&input);
160 catch (const ACEXML_Exception& ex)
162 ex.print();
163 ACE_DEBUG ((LM_ERROR, ACE_TEXT ("Exception occurred. Exiting...\n")));
167 parser.parse (&input);
169 catch (const ACEXML_SAXException& ex)
171 ex.print();
172 ACE_DEBUG ((LM_ERROR, ACE_TEXT ("Exception occurred. Exiting...\n")));
173 return 1;
175 return 0;