summaryrefslogtreecommitdiff
path: root/ACEXML/tests/ContentHandler_Test.cpp
blob: 604cf9873d3536c816abfd274521cc7e6f29cf22 (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
//=============================================================================
/**
 *  @file    ContentHandler_Test.cpp
 *
 *  $Id$
 *
 *  @author Steve Huston  <shuston@riverace.com>
 */
//=============================================================================

#include "ACEXML/common/DefaultHandler.h"
#include "ACEXML/common/InputSource.h"
#include "ACEXML/common/StrCharStream.h"
#include "ACEXML/parser/parser/Parser.h"
#include "ace/OS_NS_string.h"
#include "ace/OS_main.h"

class Basic_Content_Tester : public ACEXML_DefaultHandler
{
public:
  /**
   * Receive notification of character data.
   */
  virtual void characters (const ACEXML_Char *ch,
                           int start,
                           int length ACEXML_ENV_ARG_DECL)
        ACE_THROW_SPEC ((ACEXML_SAXException));

  const ACEXML_Char *get_test_string (void)
    { return Basic_Content_Tester::test_string_; }

private:
  static const ACEXML_Char *test_string_;
};

const ACEXML_Char * Basic_Content_Tester::test_string_ =
  ACE_TEXT ("<?xml version=\"1.0\"?>")
  ACE_TEXT ("<translation type=\"unfinished\">Example\n")
  ACE_TEXT ("d&apos;internationalisation</translation>");

void
Basic_Content_Tester::characters (const ACEXML_Char *ch,
                                  int start,
                                  int length ACEXML_ENV_ARG_DECL)
        ACE_THROW_SPEC ((ACEXML_SAXException))
{
  static int already_called = 0;
  static const ACEXML_Char *expect =
    ACE_TEXT ("Example\nd'internationalisation");

  if (already_called)
    {
      ACEXML_THROW (ACEXML_SAXException
                    (ACE_TEXT ("characters() called too much\n")));
    }
  already_called = 1;

  int expected_len = ACE_static_cast (int, ACE_OS::strlen (expect));
  if (length != expected_len)
    {
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("characters() expected len %d (%*s); ")
                  ACE_TEXT ("got %d (%*s)\n"),
                  expected_len, expected_len, ch + start,
                  length, length, ch + start));
      ACEXML_THROW (ACEXML_SAXException (ACE_TEXT ("Functionality failure")));
    }
  return;
}

int
ACE_TMAIN (int, ACE_TCHAR *[])
{
  int status = 0;
  Basic_Content_Tester tester;
  ACEXML_StrCharStream *test_stream = 0;
  ACE_NEW_RETURN (test_stream, ACEXML_StrCharStream, -1);
  if (test_stream->open (tester.get_test_string (),
                         ACE_TEXT ("test_stream")) < 0)
    {
      ACE_ERROR ((LM_ERROR, ACE_TEXT ("Unable to create input stream\n")));
      return -1;
    }
  ACEXML_InputSource input (test_stream);
  ACEXML_Parser parser;
  parser.setContentHandler (&tester);
  ACEXML_TRY_NEW_ENV
  {
    parser.setFeature (ACE_TEXT ("http://xml.org/sax/features/validation"),
                       0
                       ACEXML_ENV_ARG_PARAMETER);
    ACEXML_TRY_CHECK;
    parser.parse (&input ACEXML_ENV_ARG_PARAMETER);
    ACEXML_TRY_CHECK;
  }
  ACEXML_CATCH (ACEXML_SAXException, ex)
  {
    ex.print();
    status = 1;
  }
  ACEXML_ENDTRY;
  return status;
}