Provide a unique integer ID for each XML namespace.
More...
Detailed Description
- Outline
-
-
- Purpose:
- Provide a unique integer ID for each XML namespace.
-
- Classes:
-
- See also:
- Component balxml_prefixstack
-
- Description:
- This component provides an in-core value-semantic type,
balxml::NamespaceRegistry
, that associates an integer ID with each registered namespace URI. In typical usage, client code would call the lookupOrRegister
method each time it encounters a namespace URI. The lookupOrRegister
method will return the ID corresponding to the URI, assigning a new ID if none already exists. The client can also retrieve the ID an already-registered namespace by providing the URI to the lookup
method and can retrieve the URI of an already-registered namespace by providing the ID to the lookup
method.
- Note that namespace IDs may be negative. Client code should not assume an incremental assignment of IDs starting at zero. (See Preregistered Namespaces), below.
-
- Preregistered Namespaces:
- Even before any namespaces have been registered, a
balxml::NamespaceRegistry
can be used to lookup several preregistered namespaces. The IDs for these preregistered namespaces are declared as constants within the balxml::NamespaceRegistry
class. These constants and their associated URI's are as follows: Namespace ID URI String
============ ==========
BAEXML_XML http:
BAEXML_XMLNS http:
BAEXML_XMLSCHEMA http:
BAEXML_XMLSCHEMA_INSTANCE http:
BAEXML_WSDL http:
BAEXML_WSDL_SOAP http:
BAEXML_BDEM http:
Note that the above constants are negative numbers. In addition, the value, -1, is permanently assigned to the empty string. The use of predefined namespace IDs allows client code avoid lookups of the above, well-known URIs.
-
- Thread Safety:
- It is safe to read or modify multiple instances of
balxml::NamespaceRegistry
simultaneously, each from a separate thread. It is safe to read a single instance of balxml::NamespaceRegistry
from multiple threads, provided no thread is modifying it at the same time. It is not safe to read or modify an instance of balxml::NamespaceRegistry
from one thread while any other thread is modifying the same instance.
-
- Usage:
- Typically, a program will register namespaces as it encounters them in an XML document. Alternatively, namespaces that are important to the program are registered in advance, as in the following code:
const char googleUri[] = "http://www.google.com/schemas/results.xsd";
const char yahooUri[] = "http://www.yahoo.com/xsd/searchResults.xsd";
balxml::NamespaceRegistry namespaceRegistry;
int googleId = namespaceRegistry.lookupOrRegister(googleUri);
assert(googleId >= 0);
int yahooId = namespaceRegistry.lookupOrRegister(yahooUri);
assert(yahooId >= 0);
assert(yahooId != googleId);
Later, IDs can be looked up without concern for whether they have already been registered. Any new namespaces are simply given a new ID: char input[100];
bsl::strcpy(input, "http://www.bloomberg.com/schemas/example.xsd");
int id1 = namespaceRegistry.lookupOrRegister(input);
assert(id1 >= 0);
assert(id1 != googleId);
assert(id1 != yahooId);
bsl::strcpy(input, "http://www.yahoo.com/xsd/searchResults.xsd");
int id2 = namespaceRegistry.lookupOrRegister(input);
assert(id2 == yahooId);
If one of the preregistered namespaces is presented, it's predefined ID is returned, even though it was never explicitly registered: Using the lookup
method, a namespace ID can be looked up without registering it. In this case, an unregistered namespace will result in an ID of -1: There is also a lookup
method for performing the reverse mapping -- from ID to URI: const char *uri = namespaceRegistry.lookup(googleId);
assert(0 == bsl::strcmp(uri, googleUri));