You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by Boris Glawe <bo...@boris-glawe.de> on 2003/06/10 22:51:41 UTC

XMLString handler

Hi,

First of all I am newby with xerces(-c).
I wrote a piece of code which I'd like to discuss about.

Problem is , that stringhandling is almost as complicated as assembler 
programming in xerces-c.
There are many many functions, that expect a XMLCh* as parameter, which 
has to be allocated first, then applied, and then released, which 
creates three times more code than one would expect:

I wrote a class XMLStringParameterHandler (the name could be some more 
depictive, I know), which creates an object, wich handles this 
transcoding/releasing.

you can say

XMLStringParameterHandler handler = XMLStringParameterHandler();

DOMDocument* doc = impl-> createDocument(handler.transcode("Range",0), 0);

The handler object holds 5 registers in the background (or more, if a 
number is specified with the constructor).
When you say handler.transcode("text",0), the transcode method returns 
"text" as a XMLCh* string, and as a side effect, it saves this string in 
the first register. If you call this method again with another string, 
the content of the old register is internally first released and than 
reassigned with the new string.

I implemented also some other useful methods:

handler.release_all()   // releases all registers
handler.release(int reg)      // releases register reg
handler.getRegisterContentAsChar(int reg)         // returns the content 
as a char*
handler.getRegisterContentAsXMLCh(int reg)    // returns the content as 
XMLCh*
handler.getNumRegisters()      // returns the number of registers

It's not complete yet. It's just the first implementation of an idea and 
I'd like to hear your opinion.
You will have to adapt the Makefiles to you environment.

greets Boris

P.S. files are attached and are of course released under the GPL.

RE: XMLString handler

Posted by Erik Rydgren <er...@mandarinen.se>.
We all have written such a class to use Xerces in an easier way. :)

Here is my version. I think it is even easier to use and it has small
footprint and overhead. But it is NOT designed as a stringclass. Look at the
examples for the two types of cases it is designed for.

Usage examples:

  // This handles all neccesary transcoding on the fly
  std::string attr = X( doc.getAttribute( X("attributename") ) );

or

  // This only transcodes the string "myvalue" once (and only if it is
actually needed).
  X value("myvalue");
  DOMNode* node = elem.getFirstChild();
  while ( node != null ) {
    node.setNodeValue(value);
    node = node.getNextSibling();
  }

Regards

Erik Rydgren
Mandarinen systems AB
Sweden

- .H file ---------------------------------------------

//
//
****************************************************************************
*****************
// cXML4CTranscode
//
****************************************************************************
*****************
//
class cXML4CTranscode
{
  mutable XMLCh*        m_pzXMLStr;
  mutable bool          m_bDelXMLStr;
  mutable int           m_nLength;

  mutable char*         m_pzStr;
  mutable bool          m_bDelStr;

  // This is a stack-only class
  void* operator new (size_t size) { ASSERT(false); }

public:

  cXML4CTranscode(const XMLCh* pzStr);
  cXML4CTranscode(const XMLCh* pzStr, const unsigned int length);
  cXML4CTranscode(const char* pzStr);
  cXML4CTranscode(const cXML4CTranscode& master);
  ~cXML4CTranscode();

  operator const char* () const;
  operator const XMLCh*() const;
};

typedef cXML4CTranscode X;

- .CPP file ---------------------------------------------

//
//
****************************************************************************
*****************
// cXML4CTranscode
//
****************************************************************************
*****************
//
cXML4CTranscode::cXML4CTranscode(const XMLCh* pzStr)
{
  m_pzXMLStr = (XMLCh*) pzStr;
  m_bDelXMLStr = false;
  m_nLength = -1;

  m_pzStr = 0;
  m_bDelStr = true;
}

cXML4CTranscode::cXML4CTranscode(const cXML4CTranscode& master)
{
  m_pzXMLStr = master.m_pzXMLStr;
  m_bDelXMLStr = false;
  m_nLength = master.m_nLength;
  m_pzStr = master.m_pzStr;
  m_bDelStr = false;
}

cXML4CTranscode::cXML4CTranscode(const XMLCh* pzStr, const unsigned int
length)
{
  m_pzXMLStr = (XMLCh*) pzStr;
  m_bDelXMLStr = false;
  m_nLength = (int) length;

  m_pzStr = 0;
  m_bDelStr = true;
}

cXML4CTranscode::cXML4CTranscode(const char* pzStr)
{
  m_pzStr = (char*) pzStr;
  m_bDelStr = false;

  m_pzXMLStr = 0;
  m_bDelXMLStr = true;
  m_nLength = -1;
}

cXML4CTranscode::~cXML4CTranscode()
{
  if (m_bDelStr) delete m_pzStr;
  if (m_bDelXMLStr) delete m_pzXMLStr;
}


cXML4CTranscode::operator const char* () const
{
  if (!m_pzStr) {
    if (m_nLength >= 0) {
      XMLCh* tmp = new XMLCh[m_nLength+1];
      memcpy(tmp, m_pzXMLStr, sizeof(XMLCh) * m_nLength);
      tmp[m_nLength] = 0;
      m_pzStr = XMLString::transcode(tmp);
      delete tmp;
    }
    else {
      m_pzStr = XMLString::transcode(m_pzXMLStr);
    }
    m_bDelStr = true;
  }
  return m_pzStr;
}

cXML4CTranscode::operator const XMLCh*() const
{
  if (!m_pzXMLStr) {
    m_pzXMLStr = XMLString::transcode(m_pzStr);
    m_bDelXMLStr = true;
  }
  return m_pzXMLStr;
}


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org