My Project
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Private Attributes | List of all members
TemplateContextImpl Class Reference

Internal class representing the implementation of a template context. More...

Inheritance diagram for TemplateContextImpl:
TemplateContext

Public Member Functions

 TemplateContextImpl (const TemplateEngine *e)
 
virtual ~TemplateContextImpl ()
 
void push ()
 
void pop ()
 
void set (const char *name, const TemplateVariant &v)
 
TemplateVariant get (const QCString &name) const
 
const TemplateVariantgetRef (const QCString &name) const
 
void setOutputDirectory (const QCString &dir)
 
void setEscapeIntf (const QCString &ext, TemplateEscapeIntf *intf)
 
void selectEscapeIntf (const QCString &ext)
 
void setActiveEscapeIntf (TemplateEscapeIntf *intf)
 
void setSpacelessIntf (TemplateSpacelessIntf *intf)
 
TemplateBlockContextblockContext ()
 
TemplateVariant getPrimary (const QCString &name) const
 
void setLocation (const QCString &templateName, int line)
 
QCString templateName () const
 
int line () const
 
QCString outputDirectory () const
 
TemplateEscapeIntfescapeIntf () const
 
TemplateSpacelessIntfspacelessIntf () const
 
void enableSpaceless (bool b)
 
bool spacelessEnabled () const
 
void enableTabbing (bool b)
 
bool tabbingEnabled () const
 
bool needsRecoding () const
 
QCString encoding () const
 
void setEncoding (const QCString &file, int line, const QCString &enc)
 
QCString recode (const QCString &s)
 
void warn (const char *fileName, int line, const char *fmt,...) const
 
void openSubIndex (const QCString &indexName)
 
void closeSubIndex (const QCString &indexName)
 
void addIndexEntry (const QCString &indexName, const QValueList< TemplateKeyValue > &arguments)
 
- Public Member Functions inherited from TemplateContext
virtual ~TemplateContext ()
 

Private Attributes

const TemplateEnginem_engine
 
QCString m_templateName
 
int m_line
 
QCString m_outputDir
 
QList< QDict< TemplateVariant > > m_contextStack
 
TemplateBlockContext m_blockContext
 
QDict< TemplateEscapeIntf * > m_escapeIntfDict
 
TemplateEscapeIntfm_activeEscapeIntf
 
TemplateSpacelessIntfm_spacelessIntf
 
bool m_spacelessEnabled
 
bool m_tabbingEnabled
 
TemplateAutoRef< TemplateStructm_indices
 
QDict< QStack< TemplateVariant > > m_indexStacks
 
QCString m_encoding
 
void * m_fromUtf8
 

Detailed Description

Internal class representing the implementation of a template context.

Definition at line 512 of file template.cpp.

Constructor & Destructor Documentation

TemplateContextImpl::TemplateContextImpl ( const TemplateEngine e)

Definition at line 2367 of file template.cpp.

References TemplateAutoRef< T >::get(), m_contextStack, m_escapeIntfDict, m_fromUtf8, m_indexStacks, m_indices, and push().

: m_engine(e), m_templateName("<unknown>"), m_line(1), m_activeEscapeIntf(0),
{
m_indexStacks.setAutoDelete(TRUE);
m_contextStack.setAutoDelete(TRUE);
m_escapeIntfDict.setAutoDelete(TRUE);
m_fromUtf8 = (void*)(-1);
push();
set("index",m_indices.get());
}
TemplateContextImpl::~TemplateContextImpl ( )
virtual

Definition at line 2379 of file template.cpp.

References pop().

{
pop();
}

Member Function Documentation

void TemplateContextImpl::addIndexEntry ( const QCString &  indexName,
const QValueList< TemplateKeyValue > &  arguments 
)

Definition at line 2619 of file template.cpp.

References TemplateList::alloc(), TemplateStruct::alloc(), TemplateList::append(), TemplateList::at(), TemplateList::count(), TemplateVariant::Delegate::fromFunction(), getPathFunc(), TemplateVariant::List, m_indexStacks, m_indices, TemplateStruct::set(), TemplateVariant::Struct, languages::tmp, and TemplateVariant::type().

Referenced by TemplateNodeIndexEntry::render().

{
QValueListConstIterator<TemplateKeyValue> it = arguments.begin();
//printf("TemplateContextImpl::addIndexEntry(%s)\n",indexName.data());
//while (it!=arguments.end())
//{
// printf(" key=%s value=%s\n",(*it).key.data(),(*it).value.toString().data());
// ++it;
//}
TemplateVariant parent(FALSE);
QStack<TemplateVariant> *stack = m_indexStacks.find(indexName);
if (!stack) // no stack yet, create it!
{
stack = new QStack<TemplateVariant>;
stack->setAutoDelete(TRUE);
m_indexStacks.insert(indexName,stack);
}
TemplateList *list = 0;
if (stack->isEmpty()) // first item, create empty list and add it to the index
{
stack->push(new TemplateVariant(list));
m_indices->set(indexName,list); // make list available under index
}
else // stack not empty
{
if (stack->top()->type()==TemplateVariant::Struct) // already an entry in the list
{
// remove current entry from the stack
delete stack->pop();
}
else // first entry after opensubindex
{
ASSERT(stack->top()->type()==TemplateVariant::List);
}
if (stack->count()>1)
{
TemplateVariant *tmp = stack->pop();
// To prevent a cyclic dependency between parent and child which causes a memory
// leak, we wrap the parent into a weak reference version.
parent = new TemplateStructWeakRef(stack->top()->toStruct());
stack->push(tmp);
ASSERT(parent.type()==TemplateVariant::Struct);
}
// get list to add new item
list = dynamic_cast<TemplateList*>(stack->top()->toList());
}
// add user specified fields to the entry
for (it=arguments.begin();it!=arguments.end();++it)
{
entry->set((*it).key,(*it).value);
}
if (list->count()>0)
{
TemplateStruct *lastEntry = dynamic_cast<TemplateStruct*>(list->at(list->count()-1).toStruct());
lastEntry->set("last",false);
}
entry->set("is_leaf_node",true);
entry->set("first",list->count()==0);
entry->set("index",list->count());
entry->set("parent",parent);
entry->set("last",true);
stack->push(new TemplateVariant(entry));
list->append(entry);
}
TemplateBlockContext * TemplateContextImpl::blockContext ( )

Definition at line 2538 of file template.cpp.

References m_blockContext.

Referenced by TemplateImpl::render(), TemplateNodeBlock::render(), and TemplateNodeExtend::render().

{
return &m_blockContext;
}
void TemplateContextImpl::closeSubIndex ( const QCString &  indexName)

Definition at line 2573 of file template.cpp.

References m_indexStacks, m_line, m_templateName, TemplateStruct::set(), TemplateVariant::Struct, and warn().

Referenced by TemplateNodeCloseSubIndex::render().

{
//printf("TemplateContextImpl::closeSubIndex(%s)\n",indexName.data());
QStack<TemplateVariant> *stack = m_indexStacks.find(indexName);
if (!stack || stack->count()<3)
{
warn(m_templateName,m_line,"closesubindex for index %s without matching open",indexName.data());
}
else // stack->count()>=2
{
if (stack->top()->type()==TemplateVariant::Struct)
{
delete stack->pop(); // pop struct
delete stack->pop(); // pop list
}
else // empty list! correct "is_left_node" attribute of the parent entry
{
delete stack->pop(); // pop list
TemplateStruct *entry = dynamic_cast<TemplateStruct*>(stack->top()->toStruct());
if (entry)
{
entry->set("is_leaf_node",true);
}
}
}
//fprintf(stderr,"TemplateContextImpl::closeSubIndex(%s) end g_count=%d\n\n",indexName.data(),g_count);
}
void TemplateContextImpl::enableSpaceless ( bool  b)
inline
void TemplateContextImpl::enableTabbing ( bool  b)
inline
QCString TemplateContextImpl::encoding ( ) const
inline

Definition at line 557 of file template.cpp.

References m_encoding.

Referenced by TemplateNodeEncoding::render().

{ return m_encoding; }
TemplateEscapeIntf* TemplateContextImpl::escapeIntf ( ) const
inline
TemplateVariant TemplateContextImpl::get ( const QCString &  name) const
virtual

Gets the value for a given key

Parameters
[in]nameThe name of key.
Returns
The value, which can be an invalid variant in case the key was not found.

Implements TemplateContext.

Definition at line 2437 of file template.cpp.

References TemplateListIntf::at(), TemplateStructIntf::get(), getPrimary(), TemplateVariant::isValid(), TemplateVariant::List, m_line, m_templateName, TemplateVariant::Struct, TemplateVariant::toList(), TemplateVariant::toStruct(), TemplateVariant::type(), and warn().

{
int i=name.find('.');
if (i==-1) // simple name
{
return getPrimary(name);
}
else // obj.prop
{
QCString objName = name.left(i);
QCString propName = name.mid(i+1);
while (!propName.isEmpty())
{
//printf("getPrimary(%s) type=%d:%s\n",objName.data(),v.type(),v.toString().data());
{
i = propName.find(".");
int l = i==-1 ? propName.length() : i;
v = v.toStruct()->get(propName.left(l));
if (!v.isValid())
{
warn(m_templateName,m_line,"requesting non-existing property '%s' for object '%s'",propName.left(l).data(),objName.data());
}
if (i!=-1)
{
objName = propName.left(i);
propName = propName.mid(i+1);
}
else
{
propName.resize(0);
}
}
else if (v.type()==TemplateVariant::List)
{
i = propName.find(".");
int l = i==-1 ? propName.length() : i;
bool b;
int index = propName.left(l).toInt(&b);
if (b)
{
v = v.toList()->at(index);
}
else
{
warn(m_templateName,m_line,"list index '%s' is not valid",propName.data());
break;
}
if (i!=-1)
{
propName = propName.mid(i+1);
}
else
{
propName.resize(0);
}
}
else
{
warn(m_templateName,m_line,"using . on an object '%s' is not an struct or list",objName.data());
return TemplateVariant();
}
}
return v;
}
}
TemplateVariant TemplateContextImpl::getPrimary ( const QCString &  name) const

Definition at line 2517 of file template.cpp.

References getRef().

Referenced by get().

{
return v ? *v : TemplateVariant();
}
const TemplateVariant * TemplateContextImpl::getRef ( const QCString &  name) const
virtual

Returns a pointer to the value corresponding to a given key.

Parameters
[in]nameThe name of key.
Returns
A pointer to the value, or 0 in case the key was not found.

Implements TemplateContext.

Definition at line 2505 of file template.cpp.

References m_contextStack.

Referenced by getPrimary().

{
QListIterator< QDict<TemplateVariant> > it(m_contextStack);
QDict<TemplateVariant> *dict;
for (it.toFirst();(dict=it.current());++it)
{
TemplateVariant *v = dict->find(name);
if (v) return v;
}
return 0; // not found
}
int TemplateContextImpl::line ( ) const
inline

Definition at line 544 of file template.cpp.

References m_line.

Referenced by ExprAstFilter::apply(), ExprAstVariable::resolve(), ExprAstBinary::resolve(), and setLocation().

{ return m_line; }
bool TemplateContextImpl::needsRecoding ( ) const
inline
void TemplateContextImpl::openSubIndex ( const QCString &  indexName)

Definition at line 2552 of file template.cpp.

References TemplateList::alloc(), TemplateVariant::List, m_indexStacks, m_line, m_templateName, TemplateStruct::set(), and warn().

Referenced by TemplateNodeOpenSubIndex::render().

{
//printf("TemplateContextImpl::openSubIndex(%s)\n",indexName.data());
QStack<TemplateVariant> *stack = m_indexStacks.find(indexName);
if (!stack || stack->isEmpty() || stack->top()->type()==TemplateVariant::List) // error: no stack yet or no entry
{
warn(m_templateName,m_line,"opensubindex for index %s without preceding indexentry",indexName.data());
return;
}
// get the parent entry to add the list to
TemplateStruct *entry = dynamic_cast<TemplateStruct*>(stack->top()->toStruct());
if (entry)
{
// add new list to the stack
stack->push(new TemplateVariant(list));
entry->set("children",list);
entry->set("is_leaf_node",false);
}
}
QCString TemplateContextImpl::outputDirectory ( ) const
inline
void TemplateContextImpl::pop ( )
virtual

Pop the current scope from the stack.

Implements TemplateContext.

Definition at line 2530 of file template.cpp.

References m_contextStack, m_line, m_templateName, and warn().

Referenced by TemplateNodeBlock::render(), and ~TemplateContextImpl().

{
if (!m_contextStack.removeFirst())
{
warn(m_templateName,m_line,"pop() called on empty context stack!\n");
}
}
void TemplateContextImpl::push ( )
virtual

Push a new scope on the stack.

Implements TemplateContext.

Definition at line 2523 of file template.cpp.

References m_contextStack.

Referenced by TemplateNodeBlock::render(), and TemplateContextImpl().

{
QDict<TemplateVariant> *dict = new QDict<TemplateVariant>;
dict->setAutoDelete(TRUE);
m_contextStack.prepend(dict);
}
QCString TemplateContextImpl::recode ( const QCString &  s)

Definition at line 2404 of file template.cpp.

References m_fromUtf8, and portable_iconv().

Referenced by TemplateNodeText::render(), TemplateNodeVariable::render(), TemplateNodeCycle::render(), and TemplateNodeMarkers::render().

{
//printf("TemplateContextImpl::recode(%s)\n",s.data());
int iSize = s.length();
int oSize = iSize*4+1;
QCString output(oSize);
size_t iLeft = iSize;
size_t oLeft = oSize;
char *iPtr = s.rawData();
char *oPtr = output.rawData();
if (!portable_iconv(m_fromUtf8,&iPtr,&iLeft,&oPtr,&oLeft))
{
oSize -= (int)oLeft;
output.resize(oSize+1);
output.at(oSize)='\0';
return output;
}
else
{
return s;
}
}
void TemplateContextImpl::selectEscapeIntf ( const QCString &  ext)
inline

Definition at line 531 of file template.cpp.

References m_activeEscapeIntf, and m_escapeIntfDict.

Referenced by TemplateNodeCreate::render().

{ TemplateEscapeIntf **ppIntf = m_escapeIntfDict.find(ext);
m_activeEscapeIntf = ppIntf ? *ppIntf : 0;
}
void TemplateContextImpl::set ( const char *  name,
const TemplateVariant v 
)
virtual

Sets a value in the current scope.

Parameters
[in]nameThe name of the value; the key in the dictionary.
[in]vThe value associated with the key.
Note
When a given key is already present, its value will be replaced by v

Implements TemplateContext.

Definition at line 2427 of file template.cpp.

References m_contextStack.

Referenced by TemplateNodeBlock::render(), TemplateNodeWith::render(), and TemplateNodeSet::render().

{
TemplateVariant *pv = m_contextStack.getFirst()->find(name);
if (pv)
{
m_contextStack.getFirst()->remove(name);
}
m_contextStack.getFirst()->insert(name,new TemplateVariant(v));
}
void TemplateContextImpl::setActiveEscapeIntf ( TemplateEscapeIntf intf)
inline

Definition at line 535 of file template.cpp.

References m_activeEscapeIntf.

Referenced by TemplateNodeMsg::render(), and TemplateNodeCreate::render().

{ m_activeEscapeIntf = intf; }
void TemplateContextImpl::setEncoding ( const QCString &  file,
int  line,
const QCString &  enc 
)

Definition at line 2384 of file template.cpp.

References m_encoding, m_fromUtf8, portable_iconv_close(), portable_iconv_open(), and warn().

Referenced by TemplateNodeEncoding::render().

{
if (enc==m_encoding) return; // nothing changed
if (m_fromUtf8!=(void *)(-1))
{
m_fromUtf8 = (void*)(-1);
}
if (!enc.isEmpty())
{
if (m_fromUtf8==(void*)(-1))
{
warn(templateName,line,"unsupported character conversion: '%s'->'UTF-8'\n", enc.data());
}
}
//printf("TemplateContextImpl::setEncoding(%s)\n",enc.data());
}
void TemplateContextImpl::setEscapeIntf ( const QCString &  extension,
TemplateEscapeIntf intf 
)
inlinevirtual

Sets the interface that will be used for escaping the result of variable expansion before writing it to the output.

Implements TemplateContext.

Definition at line 526 of file template.cpp.

References m_escapeIntfDict.

{
int i=(!ext.isEmpty() && ext.at(0)=='.') ? 1 : 0;
m_escapeIntfDict.insert(ext.mid(i),new TemplateEscapeIntf*(intf));
}
void TemplateContextImpl::setLocation ( const QCString &  templateName,
int  line 
)
inline
void TemplateContextImpl::setOutputDirectory ( const QCString &  dir)
inlinevirtual

When files are created (i.e. by {% create ... %}) they written to the directory dir.

Implements TemplateContext.

Definition at line 524 of file template.cpp.

References m_outputDir.

{ m_outputDir = dir; }
void TemplateContextImpl::setSpacelessIntf ( TemplateSpacelessIntf intf)
inlinevirtual

Sets the interface that will be used inside a spaceless block to remove any redundant whitespace.

Implements TemplateContext.

Definition at line 536 of file template.cpp.

References m_spacelessIntf.

{ m_spacelessIntf = intf; }
bool TemplateContextImpl::spacelessEnabled ( ) const
inline
TemplateSpacelessIntf* TemplateContextImpl::spacelessIntf ( ) const
inline

Definition at line 547 of file template.cpp.

References m_spacelessIntf.

Referenced by TemplateNodeText::render().

{ return m_spacelessIntf; }
bool TemplateContextImpl::tabbingEnabled ( ) const
inline

Definition at line 555 of file template.cpp.

References m_tabbingEnabled.

Referenced by TemplateNodeTabbing::render().

{ return m_tabbingEnabled; }
QCString TemplateContextImpl::templateName ( ) const
inline
void TemplateContextImpl::warn ( const char *  fileName,
int  line,
const char *  fmt,
  ... 
) const

Member Data Documentation

TemplateEscapeIntf* TemplateContextImpl::m_activeEscapeIntf
private

Definition at line 575 of file template.cpp.

Referenced by enableTabbing(), escapeIntf(), selectEscapeIntf(), and setActiveEscapeIntf().

TemplateBlockContext TemplateContextImpl::m_blockContext
private

Definition at line 573 of file template.cpp.

Referenced by blockContext().

QList< QDict<TemplateVariant> > TemplateContextImpl::m_contextStack
private

Definition at line 572 of file template.cpp.

Referenced by getRef(), pop(), push(), set(), and TemplateContextImpl().

QCString TemplateContextImpl::m_encoding
private

Definition at line 581 of file template.cpp.

Referenced by encoding(), needsRecoding(), and setEncoding().

const TemplateEngine* TemplateContextImpl::m_engine
private

Definition at line 568 of file template.cpp.

Referenced by warn().

QDict<TemplateEscapeIntf*> TemplateContextImpl::m_escapeIntfDict
private

Definition at line 574 of file template.cpp.

Referenced by selectEscapeIntf(), setEscapeIntf(), and TemplateContextImpl().

void* TemplateContextImpl::m_fromUtf8
private

Definition at line 582 of file template.cpp.

Referenced by recode(), setEncoding(), and TemplateContextImpl().

QDict< QStack<TemplateVariant> > TemplateContextImpl::m_indexStacks
private

Definition at line 580 of file template.cpp.

Referenced by addIndexEntry(), closeSubIndex(), openSubIndex(), and TemplateContextImpl().

TemplateAutoRef<TemplateStruct> TemplateContextImpl::m_indices
private

Definition at line 579 of file template.cpp.

Referenced by addIndexEntry(), and TemplateContextImpl().

int TemplateContextImpl::m_line
private

Definition at line 570 of file template.cpp.

Referenced by closeSubIndex(), get(), line(), openSubIndex(), pop(), and setLocation().

QCString TemplateContextImpl::m_outputDir
private

Definition at line 571 of file template.cpp.

Referenced by outputDirectory(), and setOutputDirectory().

bool TemplateContextImpl::m_spacelessEnabled
private

Definition at line 577 of file template.cpp.

Referenced by enableSpaceless(), and spacelessEnabled().

TemplateSpacelessIntf* TemplateContextImpl::m_spacelessIntf
private

Definition at line 576 of file template.cpp.

Referenced by enableSpaceless(), setSpacelessIntf(), spacelessEnabled(), and spacelessIntf().

bool TemplateContextImpl::m_tabbingEnabled
private

Definition at line 578 of file template.cpp.

Referenced by enableTabbing(), and tabbingEnabled().

QCString TemplateContextImpl::m_templateName
private

Definition at line 569 of file template.cpp.

Referenced by closeSubIndex(), get(), openSubIndex(), pop(), setLocation(), and templateName().


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