summaryrefslogtreecommitdiff
path: root/ACE/ACEXML/common/NamespaceSupport.h
blob: 7ddf776214d38494ac4578ce863cfd3a04312eb5 (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
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
// -*- C++ -*-

//=============================================================================
/**
 *  @file    NamespaceSupport.h
 *
 *  @author Nanbor Wang <nanbor@cs.wustl.edu>
 */
//=============================================================================

#ifndef ACEXML_NAMESPACESUPPORT_H
#define ACEXML_NAMESPACESUPPORT_H

#include /**/ "ace/pre.h"
#include "ACEXML/common/ACEXML_Export.h"

#if !defined (ACE_LACKS_PRAGMA_ONCE)
#pragma once
#endif /* ACE_LACKS_PRAGMA_ONCE */

#include "ACEXML/common/XML_Types.h"
#include "ace/Functor.h"
#include "ace/Hash_Map_Manager.h"
#include "ace/Containers_T.h"
#include "ace/Null_Mutex.h"


typedef ACE_Hash_Map_Entry<ACEXML_String,
                           ACEXML_String> ACEXML_NS_CONTEXT_ENTRY;

typedef ACE_Hash_Map_Manager_Ex<ACEXML_String,
                                ACEXML_String,
                                ACE_Hash<ACEXML_String>,
                                ACE_Equal_To<ACEXML_String>,
                                ACE_Null_Mutex> ACEXML_NS_CONTEXT;

typedef ACE_Hash_Map_Iterator_Ex<ACEXML_String,
                                 ACEXML_String,
                                 ACE_Hash<ACEXML_String>,
                                 ACE_Equal_To<ACEXML_String>,
                                 ACE_Null_Mutex> ACEXML_NS_CONTEXT_ITER;

typedef ACE_Hash_Map_Reverse_Iterator_Ex<ACEXML_String,
                                         ACEXML_String,
                                         ACE_Hash<ACEXML_String>,
                                         ACE_Equal_To<ACEXML_String>,
                                         ACE_Null_Mutex> ACEXML_NS_CONTEXT_REVERSE_ITER;

typedef ACE_Unbounded_Queue<const ACEXML_Char *> ACEXML_STR_LIST;

/**
 * @class ACEXML_Namespace_Context_Stack
 *
 * @brief ACEXML_Namespace_Context_Stack implements a simple stack
 * that ACEXML_NamespaceSupport uses to keep track of namespace scopes.
 *
 * @sa ACEXML_NamespaceSupport
 */
class ACEXML_Export ACEXML_Namespace_Context_Stack
{
public:
  /// Default constructor.
  ACEXML_Namespace_Context_Stack ();

  /// Destructor.
  ~ACEXML_Namespace_Context_Stack ();

  /// Push the old namespace before entering into a new namespace scope.
  int push (ACEXML_NS_CONTEXT * old);

  /// Pop the old namespace when exiting a namespace scope.
  ACEXML_NS_CONTEXT *pop ();

private:
  /// Internal stack structure to hold namespace context.
  ACE_Unbounded_Stack<ACEXML_NS_CONTEXT*> stack_;
};

/**
 * @class ACEXML_NamespaceSupport NamespaceSupport.h "ACEXML/common/NamespaceSupport.h"
 *
 * @brief ACEXML_NamespaceSupport provides namespace management
 * operation for an XML parser.
 *
 * This class encapsulates the logic of Namespace processing: it
 * tracks the declarations currently in force for each context and
 * automatically processes qualified XML 1.0 names into their
 * Namespace parts; it can also be used in reverse for generating XML
 * 1.0 from Namespaces.
 *
 * Namespace support objects are reusable, but the reset method must
 * be invoked between each session.
 *
 * Here is a simple session (in Java :-p):
 * @code
 *  String parts[] = new String[3];
 *  NamespaceSupport support = new NamespaceSupport();
 *
 *  support.pushContext();
 *  support.declarePrefix("", "http://www.w3.org/1999/xhtml");
 *  support.declarePrefix("dc", "http://www.purl.org/dc#");
 *
 *  String parts[] = support.processName("p", parts, false);
 *  System.out.println("Namespace URI: " + parts[0]);
 *  System.out.println("Local name: " + parts[1]);
 *  System.out.println("Raw name: " + parts[2]);
 *
 *  String parts[] = support.processName("dc:title", parts, false);
 *  System.out.println("Namespace URI: " + parts[0]);
 *  System.out.println("Local name: " + parts[1]);
 *  System.out.println("Raw name: " + parts[2]);
 *
 *  support.popContext();
 * @endcode
 *
 * Note that this class is optimized for the use case where most
 * elements do not contain Namespace declarations: if the same
 * prefix/URI mapping is repeated for each context (for example), this
 * class will be somewhat less efficient.
 *
 * @sa ACEXML_Exception
 */
class ACEXML_Export ACEXML_NamespaceSupport
{
public:
  /**
   * Default constructor.
   */
  ACEXML_NamespaceSupport ();

  /**
   * Default destructor.
   */
  ~ACEXML_NamespaceSupport ();

  /**
   *  Initialize the namespace support object
   */
  int init();

  /**
   * XMLNS default prefix and URI strings.
   */
  static const ACEXML_Char *XMLNS_PREFIX;
  static const ACEXML_Char *XMLNS;

  /**
   * Declare a Namespace prefix.  Return -1 if the prefix was illegal
   * or an internal error occurred.  Return 0 if the prefix gets declared
   * successfully, 1 if the prefix replaces an existing prefix definition.
   */
  int declarePrefix (const ACEXML_Char *prefix,
                     const ACEXML_Char *uri);

  /**
   * Return all prefixes declared in current context in
   * the user-supplied list @a prefixes.  It is user's reponsibility
   * to ensure the list was empty originally.
   */
  int getDeclaredPrefixes (ACEXML_STR_LIST &prefixes) const;

  /**
   * Return one of the prefixes mapped to a Namespace URI.
   */
  const ACEXML_Char *getPrefix (const ACEXML_Char *uri) const;

  /**
   * Return all prefixes currently declared in the user-supplied list.
   * @@ Known bug: This function should only return user-defined prefixes.
   */
  int getPrefixes (ACEXML_STR_LIST &prefixes) const;

  /**
   * Return all prefixes currently declared for a URI in the
   * user-supplied list.
   */
  int getPrefixes (const ACEXML_Char *uri,
                   ACEXML_STR_LIST &prefixes) const;

  /**
   * Look up a prefix and get the currently-mapped Namespace URI.
   */
  const ACEXML_Char *getURI (const ACEXML_Char *prefix) const;

  /**
   * Revert to the previous namespace context.
   */
  int popContext ();

  /**
   * Process a raw XML 1.0 name.
   * @a qName is the raw XML name we want to parse,
   * @a uri contains the URI string of the raw name.  It points to a null
   *    string if the namespace is not valid or there's no namespace defined.
   * @a name contains the original name without the prefix.
   * @a is_attribute specifies whether the name is an attribute or not.
   *    Attributes have different scoping rules from elements.
   */
  int processName (const ACEXML_Char *qName,
                   const ACEXML_Char *&uri,
                   const ACEXML_Char *&name,
                   int is_attribute) const;

  /**
   * Start a new Namespace context.  Prefixes defined in previous
   * context are copied over to the new context.
   */
  int pushContext ();

  /**
   * Reset this Namespace support object for reuse.
   *
   */
  int reset ();

private:
  /**
   * Namespace Context stack.  When we entering a new namespace
   * context, the old context is duplicated and pushed into
   * this stack.
   */
  ACEXML_Namespace_Context_Stack ns_stack_;

  /**
   * The effective namespace context.
   */
  ACEXML_NS_CONTEXT *effective_context_;
};

#include /**/ "ace/post.h"

#endif /* ACEXML_NAMESPACESUPPORT_H */