Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

NamespaceSupport.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //=============================================================================
00004 /**
00005  *  @file    NamespaceSupport.h
00006  *
00007  *  $Id: NamespaceSupport.h,v 1.1.1.1 2003/02/21 18:36:32 chad Exp $
00008  *
00009  *  @author Nanbor Wang <nanbor@cs.wustl.edu>
00010  */
00011 //=============================================================================
00012 
00013 #ifndef ACEXML_NAMESPACESUPPORT_H
00014 #define ACEXML_NAMESPACESUPPORT_H
00015 
00016 #include "ace/pre.h"
00017 #include "ACEXML/common/ACEXML_Export.h"
00018 
00019 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00020 #pragma once
00021 #endif /* ACE_LACKS_PRAGMA_ONCE */
00022 
00023 #include "ACEXML/common/XML_Types.h"
00024 #include "ace/Functor.h"
00025 #include "ace/Hash_Map_Manager.h"
00026 #include "ace/Containers_T.h"
00027 #include "ace/Synch.h"
00028 
00029 typedef ACE_Hash_Map_Entry<ACEXML_String,
00030                            ACEXML_String> ACEXML_NS_CONTEXT_ENTRY;
00031 
00032 typedef ACE_Hash_Map_Manager_Ex<ACEXML_String,
00033                                 ACEXML_String,
00034                                 ACE_Hash<ACEXML_String>,
00035                                 ACE_Equal_To<ACEXML_String>,
00036                                 ACE_Null_Mutex> ACEXML_NS_CONTEXT;
00037 
00038 typedef ACE_Hash_Map_Iterator_Ex<ACEXML_String,
00039                                  ACEXML_String,
00040                                  ACE_Hash<ACEXML_String>,
00041                                  ACE_Equal_To<ACEXML_String>,
00042                                  ACE_Null_Mutex> ACEXML_NS_CONTEXT_ITER;
00043 
00044 typedef ACE_Hash_Map_Reverse_Iterator_Ex<ACEXML_String,
00045                                          ACEXML_String,
00046                                          ACE_Hash<ACEXML_String>,
00047                                          ACE_Equal_To<ACEXML_String>,
00048                                          ACE_Null_Mutex> ACEXML_NS_CONTEXT_REVERSE_ITER;
00049 
00050 typedef ACE_Unbounded_Queue<const ACEXML_Char *> ACEXML_STR_LIST;
00051 
00052 /**
00053  * @class ACEXML_Namespace_Context_Stack NamespaceSupport.h "ACEXML/common/NamespaceSupport.h"
00054  *
00055  * @brief ACEXML_Namespace_Context_Stack implements a simple stack
00056  * that ACEXML_NamespaceSupport uses to keep track of namespace scopes.
00057  *
00058  * @sa ACEXML_NamespaceSupport
00059  */
00060 class ACEXML_Export ACEXML_Namespace_Context_Stack
00061 {
00062 public:
00063   /// Default constructor.
00064   ACEXML_Namespace_Context_Stack (void);
00065 
00066   /// Destructor.
00067   ~ACEXML_Namespace_Context_Stack (void);
00068 
00069   /// Push the old namespace before entering into a new namespace scope.
00070   int push (ACEXML_NS_CONTEXT * old);
00071 
00072   /// Pop the old namespace when exiting a namespace scope.
00073   ACEXML_NS_CONTEXT *pop (void);
00074 
00075 private:
00076   /// Internal stack structure to hold namespace context.
00077   struct NS_Node_T {
00078     ACEXML_NS_CONTEXT *item_;
00079     struct NS_Node_T *next_;
00080   };
00081 
00082   /// Anchor point for head of stack.
00083   NS_Node_T *head_;
00084 };
00085 
00086 /**
00087  * @class ACEXML_NamespaceSupport NamespaceSupport.h "ACEXML/common/NamespaceSupport.h"
00088  *
00089  * @brief ACEXML_NamespaceSupport provides namespace management
00090  * operation for an XML parser.
00091  *
00092  * This class encapsulates the logic of Namespace processing: it
00093  * tracks the declarations currently in force for each context and
00094  * automatically processes qualified XML 1.0 names into their
00095  * Namespace parts; it can also be used in reverse for generating XML
00096  * 1.0 from Namespaces.
00097  *
00098  * Namespace support objects are reusable, but the reset method must
00099  * be invoked between each session.
00100  *
00101  * Here is a simple session (in Java :-p):
00102  * @code
00103  *  String parts[] = new String[3];
00104  *  NamespaceSupport support = new NamespaceSupport();
00105  *
00106  *  support.pushContext();
00107  *  support.declarePrefix("", "http://www.w3.org/1999/xhtml");
00108  *  support.declarePrefix("dc", "http://www.purl.org/dc#");
00109  *
00110  *  String parts[] = support.processName("p", parts, false);
00111  *  System.out.println("Namespace URI: " + parts[0]);
00112  *  System.out.println("Local name: " + parts[1]);
00113  *  System.out.println("Raw name: " + parts[2]);
00114  *
00115  *  String parts[] = support.processName("dc:title", parts, false);
00116  *  System.out.println("Namespace URI: " + parts[0]);
00117  *  System.out.println("Local name: " + parts[1]);
00118  *  System.out.println("Raw name: " + parts[2]);
00119  *
00120  *  support.popContext();
00121  * @endcode
00122  *
00123  * Note that this class is optimized for the use case where most
00124  * elements do not contain Namespace declarations: if the same
00125  * prefix/URI mapping is repeated for each context (for example), this
00126  * class will be somewhat less efficient.
00127  *
00128  * @sa ACEXML_Exception
00129  */
00130 class ACEXML_Export ACEXML_NamespaceSupport
00131 {
00132 public:
00133   /**
00134    * Default constructor.
00135    */
00136   ACEXML_NamespaceSupport (void);
00137 
00138   /**
00139    * Default destructor.
00140    */
00141   ~ACEXML_NamespaceSupport (void);
00142 
00143   /**
00144    * XMLNS default prefix and URI strings.
00145    */
00146   static const ACEXML_Char *XMLNS_PREFIX;
00147   static const ACEXML_Char *XMLNS;
00148 
00149   /**
00150    * Declare a Namespace prefix.  Return -1 if the prefix was illegal
00151    * or an internal error occured.  Return 0 if the prefix gets declared
00152    * successfully, 1 if the prefix replaces an existing prefix definition.
00153    */
00154   int declarePrefix (const ACEXML_Char *prefix,
00155                      const ACEXML_Char *uri);
00156 
00157   /**
00158    * Return all prefixes declared in current context in
00159    * the user-supplied list @a prefixes.  It is user's reponsibility
00160    * to ensure the list was empty originally.
00161    */
00162   int getDeclaredPrefixes (ACEXML_STR_LIST &prefixes) const;
00163 
00164   /**
00165    * Return one of the prefixes mapped to a Namespace URI.
00166    */
00167   const ACEXML_Char *getPrefix (const ACEXML_Char *uri) const;
00168 
00169   /**
00170    * Return all prefixes currently declared in the user-supplied list.
00171    * @@ Known bug: This function should only return user-defined prefixes.
00172    */
00173   int getPrefixes (ACEXML_STR_LIST &prefixes) const;
00174 
00175   /**
00176    * Return all prefixes currently declared for a URI in the
00177    * user-supplied list.
00178    */
00179   int getPrefixes (const ACEXML_Char *uri,
00180                    ACEXML_STR_LIST &prefixes) const;
00181 
00182   /**
00183    * Look up a prefix and get the currently-mapped Namespace URI.
00184    */
00185   const ACEXML_Char *getURI (const ACEXML_Char *prefix) const;
00186 
00187   /**
00188    * Revert to the previous namespace context.
00189    */
00190   int popContext (void);
00191 
00192   /**
00193    * Process a raw XML 1.0 name.
00194    * @a qName is the raw XML name we want to parse,
00195    * @a uri contains the URI string of the raw name.  It points to a null
00196    *    string if the namespace is not valid or there's no namespace defined.
00197    * @a name contains the original name without the prefix.
00198    * @a is_attribute specifies whether the name is an attribute or not.
00199    *    Attributes have different scoping rules from elements.
00200    */
00201   int processName (const ACEXML_Char *qName,
00202                    const ACEXML_Char *&uri,
00203                    const ACEXML_Char *&name,
00204                    int is_attribute) const;
00205 
00206   /**
00207    * Start a new Namespace context.  Prefixes defined in previous
00208    * context are copied over to the new context.
00209    */
00210   int pushContext (void);
00211 
00212   /**
00213    * Reset this Namespace support object for reuse.
00214    * @todo Not implemented.
00215    */
00216   int reset (void);
00217 
00218 private:
00219   /**
00220    * Namespace Context stack.  When we entering a new namespace
00221    * context, the old context is duplicated and pushed into
00222    * this stack.
00223    */
00224   ACEXML_Namespace_Context_Stack ns_stack_;
00225 
00226   /**
00227    * The effective namespace context.
00228    */
00229   ACEXML_NS_CONTEXT *effective_context_;
00230 };
00231 
00232 
00233 #if defined (__ACEXML_INLINE__)
00234 # include "ACEXML/common/NamespaceSupport.i"
00235 #endif /* __ACEXML_INLINE__ */
00236 
00237 #include "ace/post.h"
00238 
00239 #endif /* ACEXML_NAMESPACESUPPORT_H */

Generated on Mon Jun 16 13:23:22 2003 for ACEXML by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002