diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2009-04-06 15:44:26 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2009-04-06 15:44:26 +0200 |
commit | 6e4d86618645c45d07c3a8113f4641cb9161309e (patch) | |
tree | cc03cb8d0c7dbf9ef1cc2335f31034630f49b9a1 /libxsde/xsde/c | |
parent | b76292f8f22817401f61158a42afcfc210102657 (diff) |
Add support for XML pretty-printing
examples/cxx/hybrid/:
examples/cxx/serializer/: turn on pretty-printing
Diffstat (limited to 'libxsde/xsde/c')
-rw-r--r-- | libxsde/xsde/c/genx/genx.c | 80 | ||||
-rw-r--r-- | libxsde/xsde/c/genx/genx.h | 10 |
2 files changed, 85 insertions, 5 deletions
diff --git a/libxsde/xsde/c/genx/genx.c b/libxsde/xsde/c/genx/genx.c index 1ce0527..f838f42 100644 --- a/libxsde/xsde/c/genx/genx.c +++ b/libxsde/xsde/c/genx/genx.c @@ -126,6 +126,11 @@ struct genxWriter_rec char * etext[100]; void * (* alloc)(void * userData, int bytes); void (* dealloc)(void * userData, void * data); + + /* Pretty-printing state */ + int ppIndent; + int ppDepth; + Boolean ppSimple; }; /******************************* @@ -590,6 +595,8 @@ genxWriter genxNew(void * (* alloc)(void * userData, int bytes), xml->declCount = 1; xml->declaration = xml->defaultDecl; + w->ppIndent = 0; /* Pretty-printing is disabled by default. */ + return w; } @@ -630,6 +637,24 @@ void * genxGetUserData(genxWriter w) } /* + * get/set pretty-printing + */ +genxStatus genxSetPrettyPrint(genxWriter w, int ind) +{ + if (w->sequence == SEQUENCE_NO_DOC) + w->ppIndent = ind; + else + w->status = GENX_SEQUENCE_ERROR; + + return w->status; +} + +int genxGetPrettyPrint(genxWriter w) +{ + return w->ppIndent; +} + +/* * get/set allocator */ void genxSetAlloc(genxWriter w, void * (* alloc)(void * userData, int bytes)) @@ -1142,6 +1167,13 @@ genxStatus genxStartDocFile(genxWriter w, FILE * file) w->sequence = SEQUENCE_PRE_DOC; w->file = file; w->sender = NULL; + + if (w->ppIndent) + { + w->ppSimple = True; + w->ppDepth = 0; + } + return GENX_SUCCESS; } @@ -1153,10 +1185,32 @@ genxStatus genxStartDocSender(genxWriter w, genxSender * sender) w->sequence = SEQUENCE_PRE_DOC; w->file = NULL; w->sender = sender; + + if (w->ppIndent) + { + w->ppSimple = True; + w->ppDepth = 0; + } + return GENX_SUCCESS; } /* + * Output new line and indentation. + */ +static genxStatus writeIndentation(genxWriter w) +{ + int i, n; + SendCheck(w, "\n"); + n = w->ppDepth * w->ppIndent; + + for (i = 0; i < n; i++) + SendCheck(w, " "); + + return w->status; +} + +/* * Write out the attributes we've been gathering up for an element. We save * them until we've gathered them all so they can be writen in canonical * order. @@ -1167,7 +1221,7 @@ genxStatus genxStartDocSender(genxWriter w, genxSender * sender) */ static genxStatus writeStartTag(genxWriter w) { - int i; + int i, n; genxAttribute * aa = (genxAttribute *) w->attributes.pointers; genxElement e = w->nowStarting; @@ -1181,6 +1235,16 @@ static genxStatus writeStartTag(genxWriter w) unsetDefaultNamespace(w); w->status = GENX_SUCCESS; + if (w->ppIndent) + { + if (w->ppDepth) + if (writeIndentation (w) != GENX_SUCCESS) + return w->status; + + w->ppDepth++; + w->ppSimple = True; + } + SendCheck(w, "<"); if (e->ns && (e->ns->declaration != w->xmlnsEquals)) { @@ -1596,7 +1660,7 @@ genxStatus genxEndAttribute(genxWriter w) genxStatus genxEndElement(genxWriter w) { genxElement e; - int i; + int i, n; switch (w->sequence) { @@ -1624,6 +1688,17 @@ genxStatus genxEndElement(genxWriter w) ; e = (genxElement) w->stack.pointers[--i]; + if (w->ppIndent) + { + w->ppDepth--; + + if (!w->ppSimple) + if (writeIndentation (w) != GENX_SUCCESS) + return w->status; + + w->ppSimple = False; + } + SendCheck(w, "</"); if (e->ns && e->ns->declaration != w->xmlnsEquals) { @@ -2115,4 +2190,3 @@ char * genxGetVersion() { return GENX_VERSION; } - diff --git a/libxsde/xsde/c/genx/genx.h b/libxsde/xsde/c/genx/genx.h index da9f9b7..1a1ea22 100644 --- a/libxsde/xsde/c/genx/genx.h +++ b/libxsde/xsde/c/genx/genx.h @@ -1,4 +1,3 @@ - /* * genx - C-callable library for generating XML documents */ @@ -105,6 +104,13 @@ void genxSetUserData(genxWriter w, void * userData); void * genxGetUserData(genxWriter w); /* + * Set/get pretty-printing. If indentation is set to 0, then no pretty- + * printing is performed. + */ +genxStatus genxSetPrettyPrint(genxWriter w, int indentation); +int genxGetPrettyPrint(genxWriter w); + +/* * User-provided memory allocator, if desired. For example, if you were * in an Apache module, you could arrange for genx to use ap_palloc by * making the pool accessible via the userData call. @@ -235,7 +241,7 @@ genxStatus genxEndAttribute(genxWriter w); /* * add a namespace declaration */ -genxStatus genxAddNamespaceLiteral(genxWriter w, +genxStatus genxAddNamespaceLiteral(genxWriter w, constUtf8 uri, constUtf8 prefix); /* |