Skip to content

Commit

Permalink
latest from coda-oss
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Smith committed Dec 28, 2021
1 parent c6a4bd5 commit dc960f7
Show file tree
Hide file tree
Showing 23 changed files with 394 additions and 290 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
*
*/

#ifndef __XML_LITE_ATTRIBUTES_H__
#define __XML_LITE_ATTRIBUTES_H__
#ifndef CODA_OSS_xml_lite_Attributes_h_INCLUDED_
#define CODA_OSS_xml_lite_Attributes_h_INCLUDED_
#pragma once

#include <string>
Expand Down Expand Up @@ -54,11 +54,8 @@ namespace lite
* internal organs. We have a URI, a QName, and a local part
* as well. We also need a value, of course.
*/
class AttributeNode
struct AttributeNode final
{
public:

//! Constructor
AttributeNode() = default;

/*!
Expand All @@ -75,7 +72,6 @@ class AttributeNode
*/
AttributeNode& operator=(const AttributeNode& attributeNode);

//! Destructor
~AttributeNode() = default;

/*!
Expand Down Expand Up @@ -104,7 +100,7 @@ class AttributeNode
* Set the URI association in the QName
* \param uri The new uri
*/
void setUri(const xml::lite::Uri&);
void setUri(const Uri&);
void setUri(const std::string& uri)
{
setUri(Uri(uri));
Expand All @@ -122,7 +118,7 @@ class AttributeNode
* \return The uri
*/
std::string getUri() const;
void getUri(xml::lite::Uri&) const;
void getUri(Uri&) const;
std::string getLocalName() const;
std::string getPrefix() const;
std::string getValue() const;
Expand Down Expand Up @@ -464,24 +460,24 @@ inline bool getValue(const Attributes& attributes, const TKey& k, T& result)
* \return If the uri/localName is not found or not
*/
template <typename ToType>
inline auto castValue(const Attributes& attributes, const std::string & uri, const std::string & localName, ToType toType)
inline auto castValue(const Attributes& attributes, const Uri & uri, const std::string & localName, ToType toType)
-> decltype(toType(std::string()))
{
return castValue(attributes, QName(uri, localName), toType);
}
template <typename T>
inline T getValue(const Attributes& attributes, const std::string & uri, const std::string & localName)
inline T getValue(const Attributes& attributes, const Uri & uri, const std::string & localName)
{
return getValue<T>(attributes, QName(uri, localName));
}

template <typename T, typename ToType>
inline bool castValue(const Attributes& attributes, const std::string & uri, const std::string & localName, T& result, ToType toType)
inline bool castValue(const Attributes& attributes, const Uri & uri, const std::string & localName, T& result, ToType toType)
{
return getValue(attributes, QName(uri, localName), result, toType);
}
template <typename T>
inline bool getValue(const Attributes& attributes, const std::string & uri, const std::string & localName, T& result)
inline bool getValue(const Attributes& attributes, const Uri & uri, const std::string & localName, T& result)
{
return getValue(attributes, QName(uri, localName), result);
}
Expand Down Expand Up @@ -568,13 +564,13 @@ inline bool setValue(Attributes& attributes, const xml::lite::QName& name, const
return setValue_(attributes, name, value, details::toString<T>);
}
template <typename T, typename ToString>
inline bool setValue(Attributes& attributes, const std::string & uri, const std::string & localName, const T& value,
inline bool setValue(Attributes& attributes, const Uri & uri, const std::string & localName, const T& value,
ToString toString)
{
return setValue(attributes, QName(uri, localName), value, toString);
}
template <typename T>
inline bool setValue(Attributes& attributes, const std::string & uri, const std::string & localName, const T& value)
inline bool setValue(Attributes& attributes, const Uri & uri, const std::string & localName, const T& value)
{
return setValue(attributes, uri, localName, value, details::toString<T>);
}
Expand All @@ -583,4 +579,5 @@ inline bool setValue(Attributes& attributes, const std::string & uri, const std:
}
}

#endif
#endif // CODA_OSS_xml_lite_Attributes_h_INCLUDED_

Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
*
*/

#ifndef __XML_LITE_CONTENT_HANDLER_H__
#define __XML_LITE_CONTENT_HANDLER_H__
#ifndef CODA_OSS_xml_lite_ContentHandler_h_INCLUDED_
#define CODA_OSS_xml_lite_ContentHandler_h_INCLUDED_
#pragma once

#include <stdint.h>
Expand All @@ -30,8 +30,7 @@
#include <string>
#include <stdexcept>

#include "sys/CPlusPlus.h"

#include "xml/lite/QName.h" // Uri
#include "xml/lite/Attributes.h"

/*!
Expand Down Expand Up @@ -71,17 +70,12 @@ namespace lite

class ContentHandler
{
public:
protected:
//! Constructor
ContentHandler()
{
}

//! Destructor
virtual ~ContentHandler()
{
}
ContentHandler() = default;
virtual ~ContentHandler() = default;

public:
//! Receive notification of the beginning of a document.
virtual void startDocument()
{
Expand All @@ -99,30 +93,11 @@ class ContentHandler
*/
virtual void characters(const char *data, int length) = 0;

virtual bool wcharacters_(const uint16_t* /*data*/, size_t /*length*/)
{ return false; /* continue on to existing characters()*/ } /* =0 would break existing code */
virtual bool wcharacters_(const uint32_t* /*data*/, size_t /*length*/)
virtual bool vcharacters(const void/*XMLCh*/*, size_t /*length*/) // avoid XMLCh, it's specific to Xerces
{ return false; /* continue on to existing characters()*/ } /* =0 would break existing code */
template <typename T>
inline bool wcharacters(const T* data_, size_t length)
{
static_assert(sizeof(T) == sizeof(wchar_t), "T should be a wchar_t.");
auto sizeof_T = sizeof(T); // "conditional expression is constant"
const void* data = data_;
if (sizeof_T == sizeof(uint16_t))
{
return wcharacters_(static_cast<const uint16_t*>(data), length);
}
if (sizeof_T == sizeof(uint32_t))
{
return wcharacters_(static_cast<const uint32_t*>(data), length);
}
throw std::invalid_argument("Wrong size for T");
}

virtual bool use_wchar_t() const // =0 would break existing code
virtual bool call_vcharacters() const // =0 would break existing code
{
return false; // call characters(const char*)
return false; // don't call vcharacters(const void*)
}

/*!
Expand All @@ -135,7 +110,7 @@ class ContentHandler
virtual void startElement(const std::string & uri,
const std::string & localName,
const std::string & qname,
const xml::lite::Attributes & attributes) = 0;
const Attributes & attributes) = 0;
/*!
* Receive notification of the end of an element.
* \param uri The associated uri
Expand All @@ -156,5 +131,4 @@ class ContentHandler
};
}
}

#endif
#endif // CODA_OSS_xml_lite_ContentHandler_h_INCLUDED_
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <assert.h>

#include "xml/lite/Element.h"
#include "xml/lite/QName.h"

namespace xml
{
Expand Down Expand Up @@ -89,12 +90,18 @@ class Document
virtual Element *createElement(const std::string & qname,
const std::string & uri,
std::string characterData = "");
virtual Element *createElement(const std::string & qname,
#ifndef SWIG // SWIG doesn't like unique_ptr or StringEncoding
virtual Element* createElement(const std::string& qname,
const std::string & uri,
const std::string& characterData, StringEncoding);
virtual Element* createElement(const std::string& qname,
const std::string& uri,
const sys::U8string& characterData);
std::unique_ptr<Element> createElement(const xml::lite::QName& qname, const std::string& characterData) const;
std::unique_ptr<Element> createElement(const xml::lite::QName& qname,
const std::string& characterData, StringEncoding) const;
#endif // SWIG


/*!
* Blanket destructor. This thing deletes everything
Expand Down Expand Up @@ -172,6 +179,16 @@ inline const Element& getRootElement(const Document& doc)
assert(retval != nullptr);
return *retval;
}
inline Element& getRootElement(Document* pDoc)
{
assert(pDoc != nullptr);
return getRootElement(*pDoc);
}
inline const Element& getRootElement(const Document* pDoc)
{
assert(pDoc != nullptr);
return getRootElement(*pDoc);
}

}
}
Expand Down
34 changes: 10 additions & 24 deletions externals/coda-oss/modules/c++/xml.lite/include/xml/lite/Element.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,6 @@ namespace xml
{
namespace lite
{
/*!
* \class StringEncoding
* \brief Specifies how std::string is encoded by MinidomParser.
*
* This is needed because our use of Xerces generates different
* results on Windows/Linux, and changing things might break existing
* code.
*
* On Windows, the UTF-16 strings (internal to Xerces) are converted
* to std::strings with Windows-1252 (more-or-less ISO8859-1) encoding;
* this allows Western European languages to be displayed. On *ix,
* UTF-8 is the norm ...
*/
enum class StringEncoding
{
Windows1252 // more-or-less ISO5589-1, https://en.wikipedia.org/wiki/Windows-1252
, Utf8
};
// Could do the same for std::wstring, but there isn't any code needing it right now.


/*!
* \class Element
* \brief The class defining one element of an XML document
Expand Down Expand Up @@ -106,6 +85,7 @@ class Element
{
setCharacterData(characterData);
}
#ifndef SWIG // SWIG doesn't like unique_ptr or StringEncoding
Element(const std::string& qname, const std::string& uri,
const std::string& characterData, StringEncoding encoding) :
Element(qname, uri, nullptr)
Expand All @@ -126,6 +106,7 @@ class Element
static std::unique_ptr<Element> create(const xml::lite::QName&, const sys::U8string&);
// Encoding of "characterData" is always UTF-8
static std::unique_ptr<Element> createU8(const xml::lite::QName&, const std::string& characterData = "");
#endif // SWIG

//! Destructor
virtual ~Element()
Expand Down Expand Up @@ -317,12 +298,13 @@ class Element
//
// The only valid setting for StringEncoding is Utf8; but defaulting that
// could change behavior on Windows.
void print(io::OutputStream& stream, StringEncoding /*=Utf8*/) const;

void prettyPrint(io::OutputStream& stream,
const std::string& formatter = " ") const;
#ifndef SWIG // SWIG doesn't like unique_ptr or StringEncoding
void print(io::OutputStream& stream, StringEncoding /*=Utf8*/) const;
void prettyPrint(io::OutputStream& stream, StringEncoding /*=Utf8*/,
const std::string& formatter = " ") const;
#endif // SWIG

/*!
* Determines if a child element exists
Expand Down Expand Up @@ -351,6 +333,7 @@ class Element
{
return mCharacterData;
}
#ifndef SWIG // SWIG doesn't like unique_ptr or StringEncoding
const sys::Optional<StringEncoding>& getEncoding() const
{
return mEncoding;
Expand All @@ -361,15 +344,18 @@ class Element
return getEncoding();
}
void getCharacterData(sys::U8string& result) const;
#endif // SWIG

/*!
* Sets the character data for this element.
* \param characters The data to add to this element
*/
void setCharacterData_(const std::string& characters, const StringEncoding*);
void setCharacterData(const std::string& characters);
#ifndef SWIG // SWIG doesn't like unique_ptr or StringEncoding
void setCharacterData_(const std::string& characters, const StringEncoding*);
void setCharacterData(const std::string& characters, StringEncoding);
void setCharacterData(const sys::U8string& characters);
#endif // SWIG

/*!
* Sets the local name for this element.
Expand Down
Loading

0 comments on commit dc960f7

Please sign in to comment.