Classes | Public Types | Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
GenericSchemaDocument< ValueT, Allocator > Class Template Reference

JSON schema document. More...

#include <fwd.h>

Classes

struct  SchemaEntry
 
struct  SchemaRefEntry
 

Public Types

typedef ValueT ValueType
 
typedef IGenericRemoteSchemaDocumentProvider< GenericSchemaDocumentIRemoteSchemaDocumentProviderType
 
typedef Allocator AllocatorType
 
typedef ValueType::EncodingType EncodingType
 
typedef EncodingType::Ch Ch
 
typedef internal::Schema< GenericSchemaDocumentSchemaType
 
typedef GenericPointer< ValueType, Allocator > PointerType
 
typedef GenericValue< EncodingType, Allocator > URIType
 

Public Member Functions

 GenericSchemaDocument (const ValueType &document, const Ch *uri=0, SizeType uriLength=0, IRemoteSchemaDocumentProviderType *remoteProvider=0, Allocator *allocator=0)
 Constructor. More...
 
 ~GenericSchemaDocument ()
 Destructor. More...
 
const URITypeGetURI () const
 
const SchemaTypeGetRoot () const
 Get the root schema. More...
 

Private Member Functions

 GenericSchemaDocument (const GenericSchemaDocument &)
 Prohibit copying. More...
 
GenericSchemaDocumentoperator= (const GenericSchemaDocument &)
 Prohibit assignment. More...
 
void CreateSchemaRecursive (const SchemaType **schema, const PointerType &pointer, const ValueType &v, const ValueType &document)
 
void CreateSchema (const SchemaType **schema, const PointerType &pointer, const ValueType &v, const ValueType &document)
 
bool HandleRefSchema (const PointerType &source, const SchemaType **schema, const ValueType &v, const ValueType &document)
 
const SchemaTypeGetSchema (const PointerType &pointer) const
 
PointerType GetPointer (const SchemaType *schema) const
 
const SchemaTypeGetTypeless () const
 

Private Attributes

IRemoteSchemaDocumentProviderTyperemoteProvider_
 
Allocator * allocator_
 
Allocator * ownAllocator_
 
const SchemaTyperoot_
 Root schema. More...
 
SchemaTypetypeless_
 
internal::Stack< Allocator > schemaMap_
 
internal::Stack< Allocator > schemaRef_
 
URIType uri_
 

Static Private Attributes

static const size_t kInitialSchemaMapSize = 64
 
static const size_t kInitialSchemaRefSize = 64
 

Friends

class internal::Schema< GenericSchemaDocument >
 
template<typename , typename , typename >
class GenericSchemaValidator
 

Detailed Description

template<typename ValueT, typename Allocator = CrtAllocator>
class GenericSchemaDocument< ValueT, Allocator >

JSON schema document.

A JSON schema document is a compiled version of a JSON schema. It is basically a tree of internal::Schema.

Note
This is an immutable class (i.e. its instance cannot be modified after construction).
Template Parameters
ValueTType of JSON value (e.g. Value ), which also determine the encoding.
AllocatorAllocator type for allocating memory of this document.

Definition at line 136 of file fwd.h.

Member Typedef Documentation

template<typename ValueT , typename Allocator = CrtAllocator>
typedef Allocator GenericSchemaDocument< ValueT, Allocator >::AllocatorType

Definition at line 1503 of file schema.h.

template<typename ValueT , typename Allocator = CrtAllocator>
typedef EncodingType::Ch GenericSchemaDocument< ValueT, Allocator >::Ch

Definition at line 1505 of file schema.h.

template<typename ValueT , typename Allocator = CrtAllocator>
typedef ValueType::EncodingType GenericSchemaDocument< ValueT, Allocator >::EncodingType

Definition at line 1504 of file schema.h.

template<typename ValueT , typename Allocator = CrtAllocator>
typedef IGenericRemoteSchemaDocumentProvider<GenericSchemaDocument> GenericSchemaDocument< ValueT, Allocator >::IRemoteSchemaDocumentProviderType

Definition at line 1502 of file schema.h.

template<typename ValueT , typename Allocator = CrtAllocator>
typedef GenericPointer<ValueType, Allocator> GenericSchemaDocument< ValueT, Allocator >::PointerType

Definition at line 1507 of file schema.h.

template<typename ValueT , typename Allocator = CrtAllocator>
typedef internal::Schema<GenericSchemaDocument> GenericSchemaDocument< ValueT, Allocator >::SchemaType

Definition at line 1506 of file schema.h.

template<typename ValueT , typename Allocator = CrtAllocator>
typedef GenericValue<EncodingType, Allocator> GenericSchemaDocument< ValueT, Allocator >::URIType

Definition at line 1508 of file schema.h.

template<typename ValueT , typename Allocator = CrtAllocator>
typedef ValueT GenericSchemaDocument< ValueT, Allocator >::ValueType

Definition at line 1501 of file schema.h.

Constructor & Destructor Documentation

template<typename ValueT , typename Allocator = CrtAllocator>
GenericSchemaDocument< ValueT, Allocator >::GenericSchemaDocument ( const ValueType document,
const Ch uri = 0,
SizeType  uriLength = 0,
IRemoteSchemaDocumentProviderType remoteProvider = 0,
Allocator *  allocator = 0 
)
inlineexplicit

Constructor.

Compile a JSON document into schema document.

Parameters
documentA JSON document as source.
uriThe base URI of this schema document for purposes of violation reporting.
uriLengthLength of name, in code points.
remoteProviderAn optional remote schema document provider for resolving remote reference. Can be null.
allocatorAn optional allocator instance for allocating memory. Can be null.

Definition at line 1523 of file schema.h.

1524  :
1525  remoteProvider_(remoteProvider),
1526  allocator_(allocator),
1527  ownAllocator_(),
1528  root_(),
1529  typeless_(),
1530  schemaMap_(allocator, kInitialSchemaMapSize),
1531  schemaRef_(allocator, kInitialSchemaRefSize)
1532  {
1533  if (!allocator_)
1534  ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator)();
1535 
1536  Ch noUri[1] = {0};
1537  uri_.SetString(uri ? uri : noUri, uriLength, *allocator_);
1538 
1539  typeless_ = static_cast<SchemaType*>(allocator_->Malloc(sizeof(SchemaType)));
1541 
1542  // Generate root schema, it will call CreateSchema() to create sub-schemas,
1543  // And call AddRefSchema() if there are $ref.
1544  CreateSchemaRecursive(&root_, PointerType(), document, document);
1545 
1546  // Resolve $ref
1547  while (!schemaRef_.Empty()) {
1548  SchemaRefEntry* refEntry = schemaRef_.template Pop<SchemaRefEntry>(1);
1549  if (const SchemaType* s = GetSchema(refEntry->target)) {
1550  if (refEntry->schema)
1551  *refEntry->schema = s;
1552 
1553  // Create entry in map if not exist
1554  if (!GetSchema(refEntry->source)) {
1555  new (schemaMap_.template Push<SchemaEntry>()) SchemaEntry(refEntry->source, const_cast<SchemaType*>(s), false, allocator_);
1556  }
1557  }
1558  else if (refEntry->schema)
1559  *refEntry->schema = typeless_;
1560 
1561  refEntry->~SchemaRefEntry();
1562  }
1563 
1564  RAPIDJSON_ASSERT(root_ != 0);
1565 
1566  schemaRef_.ShrinkToFit(); // Deallocate all memory for ref
1567  }
Allocator * ownAllocator_
Definition: schema.h:1729
void CreateSchemaRecursive(const SchemaType **schema, const PointerType &pointer, const ValueType &v, const ValueType &document)
Definition: schema.h:1632
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:406
object
Definition: rapidjson.h:622
const SchemaType * GetSchema(const PointerType &pointer) const
Definition: schema.h:1708
internal::Stack< Allocator > schemaRef_
Definition: schema.h:1733
IRemoteSchemaDocumentProviderType * remoteProvider_
Definition: schema.h:1727
internal::Schema< GenericSchemaDocument > SchemaType
Definition: schema.h:1506
Allocator * allocator_
Definition: schema.h:1728
#define RAPIDJSON_NEW(TypeName)
! customization point for global new
Definition: rapidjson.h:601
static const size_t kInitialSchemaMapSize
Definition: schema.h:1724
SchemaType * typeless_
Definition: schema.h:1731
static const size_t kInitialSchemaRefSize
Definition: schema.h:1725
internal::Stack< Allocator > schemaMap_
Definition: schema.h:1732
const SchemaType * root_
Root schema.
Definition: schema.h:1730
EncodingType::Ch Ch
Definition: schema.h:1505
static QCString * s
Definition: config.cpp:1042
GenericPointer< ValueType, Allocator > PointerType
Definition: schema.h:1507
template<typename ValueT , typename Allocator = CrtAllocator>
GenericSchemaDocument< ValueT, Allocator >::~GenericSchemaDocument ( )
inline

Destructor.

Definition at line 1589 of file schema.h.

1589  {
1590  while (!schemaMap_.Empty())
1591  schemaMap_.template Pop<SchemaEntry>(1)->~SchemaEntry();
1592 
1593  if (typeless_) {
1594  typeless_->~SchemaType();
1595  Allocator::Free(typeless_);
1596  }
1597 
1599  }
Allocator * ownAllocator_
Definition: schema.h:1729
#define RAPIDJSON_DELETE(x)
! customization point for global delete
Definition: rapidjson.h:605
SchemaType * typeless_
Definition: schema.h:1731
internal::Stack< Allocator > schemaMap_
Definition: schema.h:1732
template<typename ValueT , typename Allocator = CrtAllocator>
GenericSchemaDocument< ValueT, Allocator >::GenericSchemaDocument ( const GenericSchemaDocument< ValueT, Allocator > &  )
private

Prohibit copying.

Member Function Documentation

template<typename ValueT , typename Allocator = CrtAllocator>
void GenericSchemaDocument< ValueT, Allocator >::CreateSchema ( const SchemaType **  schema,
const PointerType pointer,
const ValueType v,
const ValueType document 
)
inlineprivate

Definition at line 1649 of file schema.h.

1649  {
1650  RAPIDJSON_ASSERT(pointer.IsValid());
1651  if (v.IsObject()) {
1652  if (!HandleRefSchema(pointer, schema, v, document)) {
1653  SchemaType* s = new (allocator_->Malloc(sizeof(SchemaType))) SchemaType(this, pointer, v, document, allocator_);
1654  new (schemaMap_.template Push<SchemaEntry>()) SchemaEntry(pointer, s, true, allocator_);
1655  if (schema)
1656  *schema = s;
1657  }
1658  }
1659  }
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:406
bool HandleRefSchema(const PointerType &source, const SchemaType **schema, const ValueType &v, const ValueType &document)
Definition: schema.h:1661
internal::Schema< GenericSchemaDocument > SchemaType
Definition: schema.h:1506
Allocator * allocator_
Definition: schema.h:1728
internal::Stack< Allocator > schemaMap_
Definition: schema.h:1732
const GenericPointer< typename T::ValueType > & pointer
Definition: pointer.h:1124
static QCString * s
Definition: config.cpp:1042
template<typename ValueT , typename Allocator = CrtAllocator>
void GenericSchemaDocument< ValueT, Allocator >::CreateSchemaRecursive ( const SchemaType **  schema,
const PointerType pointer,
const ValueType v,
const ValueType document 
)
inlineprivate

Definition at line 1632 of file schema.h.

1632  {
1633  if (schema)
1634  *schema = typeless_;
1635 
1636  if (v.GetType() == kObjectType) {
1637  const SchemaType* s = GetSchema(pointer);
1638  if (!s)
1639  CreateSchema(schema, pointer, v, document);
1640 
1641  for (typename ValueType::ConstMemberIterator itr = v.MemberBegin(); itr != v.MemberEnd(); ++itr)
1642  CreateSchemaRecursive(0, pointer.Append(itr->name, allocator_), itr->value, document);
1643  }
1644  else if (v.GetType() == kArrayType)
1645  for (SizeType i = 0; i < v.Size(); i++)
1646  CreateSchemaRecursive(0, pointer.Append(i, allocator_), v[i], document);
1647  }
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition: rapidjson.h:384
void CreateSchemaRecursive(const SchemaType **schema, const PointerType &pointer, const ValueType &v, const ValueType &document)
Definition: schema.h:1632
object
Definition: rapidjson.h:622
const SchemaType * GetSchema(const PointerType &pointer) const
Definition: schema.h:1708
array
Definition: rapidjson.h:623
internal::Schema< GenericSchemaDocument > SchemaType
Definition: schema.h:1506
Allocator * allocator_
Definition: schema.h:1728
void CreateSchema(const SchemaType **schema, const PointerType &pointer, const ValueType &v, const ValueType &document)
Definition: schema.h:1649
SchemaType * typeless_
Definition: schema.h:1731
const GenericPointer< typename T::ValueType > & pointer
Definition: pointer.h:1124
static QCString * s
Definition: config.cpp:1042
template<typename ValueT , typename Allocator = CrtAllocator>
PointerType GenericSchemaDocument< ValueT, Allocator >::GetPointer ( const SchemaType schema) const
inlineprivate

Definition at line 1715 of file schema.h.

1715  {
1716  for (const SchemaEntry* target = schemaMap_.template Bottom<SchemaEntry>(); target != schemaMap_.template End<SchemaEntry>(); ++target)
1717  if (schema == target->schema)
1718  return target->pointer;
1719  return PointerType();
1720  }
internal::Stack< Allocator > schemaMap_
Definition: schema.h:1732
GenericPointer< ValueType, Allocator > PointerType
Definition: schema.h:1507
template<typename ValueT , typename Allocator = CrtAllocator>
const SchemaType& GenericSchemaDocument< ValueT, Allocator >::GetRoot ( ) const
inline

Get the root schema.

Definition at line 1604 of file schema.h.

1604 { return *root_; }
const SchemaType * root_
Root schema.
Definition: schema.h:1730
template<typename ValueT , typename Allocator = CrtAllocator>
const SchemaType* GenericSchemaDocument< ValueT, Allocator >::GetSchema ( const PointerType pointer) const
inlineprivate

Definition at line 1708 of file schema.h.

1708  {
1709  for (const SchemaEntry* target = schemaMap_.template Bottom<SchemaEntry>(); target != schemaMap_.template End<SchemaEntry>(); ++target)
1710  if (pointer == target->pointer)
1711  return target->schema;
1712  return 0;
1713  }
internal::Stack< Allocator > schemaMap_
Definition: schema.h:1732
const GenericPointer< typename T::ValueType > & pointer
Definition: pointer.h:1124
template<typename ValueT , typename Allocator = CrtAllocator>
const SchemaType* GenericSchemaDocument< ValueT, Allocator >::GetTypeless ( ) const
inlineprivate

Definition at line 1722 of file schema.h.

1722 { return typeless_; }
SchemaType * typeless_
Definition: schema.h:1731
template<typename ValueT , typename Allocator = CrtAllocator>
const URIType& GenericSchemaDocument< ValueT, Allocator >::GetURI ( ) const
inline

Definition at line 1601 of file schema.h.

1601 { return uri_; }
template<typename ValueT , typename Allocator = CrtAllocator>
bool GenericSchemaDocument< ValueT, Allocator >::HandleRefSchema ( const PointerType source,
const SchemaType **  schema,
const ValueType v,
const ValueType document 
)
inlineprivate

Definition at line 1661 of file schema.h.

1661  {
1662  static const Ch kRefString[] = { '$', 'r', 'e', 'f', '\0' };
1663  static const ValueType kRefValue(kRefString, 4);
1664 
1665  typename ValueType::ConstMemberIterator itr = v.FindMember(kRefValue);
1666  if (itr == v.MemberEnd())
1667  return false;
1668 
1669  if (itr->value.IsString()) {
1670  SizeType len = itr->value.GetStringLength();
1671  if (len > 0) {
1672  const Ch* s = itr->value.GetString();
1673  SizeType i = 0;
1674  while (i < len && s[i] != '#') // Find the first #
1675  i++;
1676 
1677  if (i > 0) { // Remote reference, resolve immediately
1678  if (remoteProvider_) {
1679  if (const GenericSchemaDocument* remoteDocument = remoteProvider_->GetRemoteDocument(s, i)) {
1680  PointerType pointer(&s[i], len - i, allocator_);
1681  if (pointer.IsValid()) {
1682  if (const SchemaType* sc = remoteDocument->GetSchema(pointer)) {
1683  if (schema)
1684  *schema = sc;
1685  new (schemaMap_.template Push<SchemaEntry>()) SchemaEntry(source, const_cast<SchemaType*>(sc), false, allocator_);
1686  return true;
1687  }
1688  }
1689  }
1690  }
1691  }
1692  else if (s[i] == '#') { // Local reference, defer resolution
1693  PointerType pointer(&s[i], len - i, allocator_);
1694  if (pointer.IsValid()) {
1695  if (const ValueType* nv = pointer.Get(document))
1696  if (HandleRefSchema(source, schema, *nv, document))
1697  return true;
1698 
1699  new (schemaRef_.template Push<SchemaRefEntry>()) SchemaRefEntry(source, pointer, schema, allocator_);
1700  return true;
1701  }
1702  }
1703  }
1704  }
1705  return false;
1706  }
const CharType(& source)[N]
Definition: pointer.h:1147
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition: rapidjson.h:384
JSON schema document.
Definition: fwd.h:136
internal::Stack< Allocator > schemaRef_
Definition: schema.h:1733
IRemoteSchemaDocumentProviderType * remoteProvider_
Definition: schema.h:1727
bool HandleRefSchema(const PointerType &source, const SchemaType **schema, const ValueType &v, const ValueType &document)
Definition: schema.h:1661
internal::Schema< GenericSchemaDocument > SchemaType
Definition: schema.h:1506
virtual const SchemaDocumentType * GetRemoteDocument(const Ch *uri, SizeType length)=0
Allocator * allocator_
Definition: schema.h:1728
internal::Stack< Allocator > schemaMap_
Definition: schema.h:1732
EncodingType::Ch Ch
Definition: schema.h:1505
const GenericPointer< typename T::ValueType > & pointer
Definition: pointer.h:1124
static QCString * s
Definition: config.cpp:1042
GenericPointer< ValueType, Allocator > PointerType
Definition: schema.h:1507
template<typename ValueT , typename Allocator = CrtAllocator>
GenericSchemaDocument& GenericSchemaDocument< ValueT, Allocator >::operator= ( const GenericSchemaDocument< ValueT, Allocator > &  )
private

Prohibit assignment.

Friends And Related Function Documentation

template<typename ValueT , typename Allocator = CrtAllocator>
template<typename , typename , typename >
friend class GenericSchemaValidator
friend

Definition at line 1511 of file schema.h.

template<typename ValueT , typename Allocator = CrtAllocator>
friend class internal::Schema< GenericSchemaDocument >
friend

Definition at line 1509 of file schema.h.

Member Data Documentation

template<typename ValueT , typename Allocator = CrtAllocator>
Allocator* GenericSchemaDocument< ValueT, Allocator >::allocator_
private

Definition at line 1728 of file schema.h.

template<typename ValueT , typename Allocator = CrtAllocator>
const size_t GenericSchemaDocument< ValueT, Allocator >::kInitialSchemaMapSize = 64
staticprivate

Definition at line 1724 of file schema.h.

template<typename ValueT , typename Allocator = CrtAllocator>
const size_t GenericSchemaDocument< ValueT, Allocator >::kInitialSchemaRefSize = 64
staticprivate

Definition at line 1725 of file schema.h.

template<typename ValueT , typename Allocator = CrtAllocator>
Allocator* GenericSchemaDocument< ValueT, Allocator >::ownAllocator_
private

Definition at line 1729 of file schema.h.

template<typename ValueT , typename Allocator = CrtAllocator>
IRemoteSchemaDocumentProviderType* GenericSchemaDocument< ValueT, Allocator >::remoteProvider_
private

Definition at line 1727 of file schema.h.

template<typename ValueT , typename Allocator = CrtAllocator>
const SchemaType* GenericSchemaDocument< ValueT, Allocator >::root_
private

Root schema.

Definition at line 1730 of file schema.h.

template<typename ValueT , typename Allocator = CrtAllocator>
internal::Stack<Allocator> GenericSchemaDocument< ValueT, Allocator >::schemaMap_
private

Definition at line 1732 of file schema.h.

template<typename ValueT , typename Allocator = CrtAllocator>
internal::Stack<Allocator> GenericSchemaDocument< ValueT, Allocator >::schemaRef_
private

Definition at line 1733 of file schema.h.

template<typename ValueT , typename Allocator = CrtAllocator>
SchemaType* GenericSchemaDocument< ValueT, Allocator >::typeless_
private

Definition at line 1731 of file schema.h.

template<typename ValueT , typename Allocator = CrtAllocator>
URIType GenericSchemaDocument< ValueT, Allocator >::uri_
private

Definition at line 1734 of file schema.h.


The documentation for this class was generated from the following files: