protected virtual int WriteTemplate(StringTemplate self, StringTemplate stToWrite, IStringTemplateWriter @out) { int n = 0; /* failsafe: perhaps enclosing instance not set * Or, it could be set to another context! This occurs * when you store a template instance as an attribute of more * than one template (like both a header file and C file when * generating C code). It must execute within the context of * the enclosing template. */ stToWrite.EnclosingInstance = self; // if self is found up the enclosing instance chain, then infinite recursion if (StringTemplate.LintMode && StringTemplate.IsRecursiveEnclosingInstance(stToWrite)) { // throw exception since sometimes eval keeps going even after I ignore this write of o. throw new InvalidOperationException("infinite recursion to " + stToWrite.GetTemplateDeclaratorString() + " referenced in " + stToWrite.EnclosingInstance.TemplateDeclaratorString + "; stack trace:" + Environment.NewLine + stToWrite.GetEnclosingInstanceStackTrace()); } else { // if we have a wrap string, then inform writer it might need to wrap if (_wrapString != null) { n = @out.WriteWrapSeparator(_wrapString); } // check if formatting needs to be applied to the stToWrite if (_formatString != null) { IAttributeRenderer renderer = self.GetAttributeRenderer(typeof(string)); if (renderer != null) { /* you pay a penalty for applying format option to a template * because the template must be written to a temp StringWriter so it can * be formatted before being written to the real output. */ StringWriter buf = new StringWriter(); IStringTemplateWriter sw = self.Group.GetStringTemplateWriter(buf); stToWrite.Write(sw); n = @out.Write(renderer.ToString(buf.ToString(), _formatString)); return n; } } n = stToWrite.Write(@out); } return n; }
/// <summary> /// Write o relative to self to out /// </summary> /// <remarks> /// John Snyders fixes here for formatString. Basically, any time /// you are about to write a value, check formatting. /// </remarks> protected internal virtual int Write(StringTemplate self, object o, IStringTemplateWriter output) { if (o == null) { if (nullValue == null) { return 0; } // continue with null option if specified o = nullValue; } int n = 0; try { if (o is StringTemplate) { StringTemplate stToWrite = (StringTemplate) o; // failsafe: perhaps enclosing instance not set // Or, it could be set to another context! This occurs // when you store a template instance as an attribute of more // than one template (like both a header file and C file when // generating C code). It must execute within the context of // the enclosing template. stToWrite.EnclosingInstance = self; // if self is found up the enclosing instance chain, then // infinite recursion if (StringTemplate.IsInLintMode && StringTemplate.IsRecursiveEnclosingInstance(stToWrite)) { // throw exception since sometimes eval keeps going // even after I ignore this write of o. throw new SystemException("infinite recursion to " + stToWrite.GetTemplateDeclaratorString() + " referenced in " + stToWrite.EnclosingInstance.GetTemplateDeclaratorString() + "; stack trace:\n" + stToWrite.GetEnclosingInstanceStackTrace()); } else { // if we have a wrap string, then inform writer it // might need to wrap if (wrapString != null) { n = output.WriteWrapSeparator(wrapString); } // check if formatting needs to be applied to the stToWrite if ( formatString != null ) { IAttributeRenderer renderer = self.GetAttributeRenderer(typeof(string)); if ( renderer != null ) { // you pay a penalty for applying format option to a template // because the template must be written to a temp StringWriter so it can // be formatted before being written to the real output. StringWriter buf = new StringWriter(); IStringTemplateWriter sw = self.Group.CreateInstanceOfTemplateWriter(buf); stToWrite.Write(sw); n = output.Write(renderer.ToString(buf.ToString(), formatString)); return n; } } n = stToWrite.Write(output); } return n; } // normalize anything iteratable to iterator o = ConvertAnythingIteratableToIterator(o); if (o is IEnumerator) { IEnumerator iter = (IEnumerator)o; bool seenPrevValue = false; while (iter.MoveNext()) { object iterValue = iter.Current; if (iterValue == null) { iterValue = nullValue; } if (iterValue != null) { if (seenPrevValue /*prevIterValue!=null*/ && (separatorString != null)) { n += output.WriteSeparator(separatorString); } seenPrevValue = true; int nw = Write(self, iterValue, output); n += nw; } } } else { IAttributeRenderer renderer = self.GetAttributeRenderer(o.GetType()); string v = null; if (renderer != null) { if (formatString != null) { v = renderer.ToString(o, formatString); } else { v = renderer.ToString(o); } } else { v = o.ToString(); } if (wrapString != null) { n = output.Write(v, wrapString); } else { n = output.Write(v); } return n; } } catch (IOException io) { self.Error("problem writing object: " + o, io); } return n; }
protected virtual int WriteObject(StringTemplate self, object o, IStringTemplateWriter @out) { int n = 0; IAttributeRenderer renderer = self.GetAttributeRenderer(o.GetType()); string v = null; if (renderer != null) { if (_formatString != null) v = renderer.ToString(o, _formatString); else v = renderer.ToString(o); } else { v = o.ToString(); } if (_wrapString != null) n = @out.Write(v, _wrapString); else n = @out.Write(v); return n; }