[Expat-discuss] (no subject)
Mark Thomson
marktt at excite.com
Wed Jul 2 00:58:11 EDT 2003
>Expat relies on volunteer work, so if you find a nice way to do that, why not share it with the community?
OK.. here's the function. I tested it only three times:
(1) Parser is created without namespace processing (it returns 0--
no need to check if `parser->m_ns' is FALSE)
(2) prefix == 0 (Returns default namespace or 0 if no default
namespace was specified)
(3) prefix != 0 (Get namespace corresponding to `prefix' or 0 if no
namespace corresponds to the given prefix).
I think I am not wrong about the following 2 points:
- The default namespace is not stored in its own variable. Instead,
it is stored as part of the element type name (i.e., there is no
nul-terminated default namespace stored anywhere).
- Every namespace in the hash table that contains the
<prefix, namespace> mappings has the namespaceSeparator character
appended to it (it will be nul-terminated if namespaceSeparator ==
'\0').
Therefore, it is not possible to simply return a constant pointer because the namespace will not be nul-terminated (except when prefix != NULL and namespaceSeparator == '\0').
The function below returns the length of the uri. I think you don't want to included it as is. Here are two other possible candidates
/* REALLOCs(*out) if *size is not enough to store the URI. Returns XML_ERROR_NO_MEMORY on error, XML_ERROR_NONE otherwise; */
enum XML_Error XML_GetNamespace(XML_Parser parser, const XML_Char *prefix, XML_Char **out, int *size);
/* Copies the URI to `out', and returns `out' on success. If *size is not enough, store the required size in *size and return 0. */
XML_Char *XML_GetNamespace(XML_Parser parser, const XML_Char *prefix, XML_Char *out, int *size);
This is not a patch. Simply add this to the xmlparse.c:
/* uriLen must not == 0 */
/* pass 0 for prefix to get the current default namespace */
const XML_Char *
XML_GetNamespace(XML_Parser parser, const XML_Char *prefix, int *uriLen)
{
DTD * const dtd = _dtd;
const BINDING *b;
if (!prefix)
{
b = dtd->defaultPrefix.binding;
}
else
{
PREFIX *p = (PREFIX *)lookup(&dtd->prefixes, prefix, sizeof(PREFIX));
if (p)
b = p->binding;
else
b = 0;
}
if (b)
{
if (namespaceSeparator)
*uriLen = b->uriLen - 1;
else
*uriLen = b->uriLen;
return b->uri;
}
else
*uriLen = 0;
return 0;
}
and this to expat.h:
XMLPARSEAPI(const XML_Char *)
XML_GetNamespace(XML_Parser parser, const XML_Char *prefix, int *uriLen);
BTW, I tried to give expat an illegal URI (h[]p://foo), it didn't complain.
_______________________________________________
Join Excite! - http://www.excite.com
The most personalized portal on the Web!
More information about the Expat-discuss
mailing list