Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

outputsaxreceptor.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002     outputsaxreceptor.cpp  -  Definition of OutputSAXReceptor class methods
00003                              -------------------
00004     begin                : October 24 2002
00005     copyright            : (C) 2003 by Vojtìch Toman
00006     email                : vtoman@lit.cz
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00025 #ifdef __GNUG__
00026 # pragma implementation
00027 #endif
00028 
00029 
00030 #include "outputsaxreceptor.h"
00031 
00032 
00036 #define ENSURE_TEXT_CODEC_EXISTS        \
00037 {                                       \
00038   if (!textCodec)                       \
00039     createDefaultTextCodec();           \
00040 }
00041 
00042 
00043 
00049 #define OUTPUT_TO_DEVICE(str)                                                   \
00050 {                                                                               \
00051   ENSURE_TEXT_CODEC_EXISTS;                                                     \
00052   if (outputDevice)                                                             \
00053     textCodec->output(outputDevice, str,                                        \
00054                       (Encodings::MIB)ExaltOptions::getOption(ExaltOptions::Encoding)); \
00055 }
00056 
00057 
00058 
00065 #define OUTPUT_TO_DEVICE_LENGTH(str, length)                                    \
00066 {                                                                               \
00067   ENSURE_TEXT_CODEC_EXISTS;                                                     \
00068   if (outputDevice)                                                             \
00069     textCodec->output(outputDevice, str, length,                                \
00070                       (Encodings::MIB)ExaltOptions::getOption(ExaltOptions::Encoding)); \
00071 }
00072 
00073 
00079 #define OUTPUT_TO_DEVICE_CHAR(c)                                                \
00080 {                                                                               \
00081   ENSURE_TEXT_CODEC_EXISTS;                                                     \
00082   if (outputDevice)                                                             \
00083     textCodec->output(outputDevice, c,                                          \
00084                       (Encodings::MIB)ExaltOptions::getOption(ExaltOptions::Encoding)); \
00085 }
00086 
00087 
00088 
00089 
00093 OutputSAXReceptor::OutputSAXReceptor(void)
00094   : SAXReceptor(), UserOfTextCodec()
00095 {
00096   outputDevice = 0;
00097   init();
00098 }
00099 
00100 
00101 
00107 OutputSAXReceptor::OutputSAXReceptor(IODevice *outDevice)
00108   : SAXReceptor(), UserOfTextCodec()
00109 {
00110   outputDevice = outDevice;
00111   init();
00112 }
00113 
00114 
00118 OutputSAXReceptor::~OutputSAXReceptor(void)
00119 {
00120   deleteDefaultTextCodec();
00121 }
00122 
00123 
00127 void OutputSAXReceptor::init(void)
00128 {
00129   dtdHasInternalSubset = false;
00130   inCDATA = false;
00131 }
00132 
00133 
00134 
00140 void OutputSAXReceptor::setOutputDevice(IODevice *outDevice)
00141 {
00142   outputDevice = outDevice;
00143 }
00144 
00145 
00146 
00147 //+++++++++++++++++++++++++++++++++++++++++++++++++
00148 
00149 
00150 
00154 void OutputSAXReceptor::reportError(void)
00155 {
00156   FATAL("Output SAX receptor error!");
00157 }
00158 
00159 
00160 
00170 void OutputSAXReceptor::startElement(void *userData, const XmlChar *name, const XmlChar **attr)
00171 {
00172   OUTPUT_TO_DEVICE("<");
00173   OUTPUT_TO_DEVICE(name);
00174 
00175 //   DBG("<" << name << ">");
00176   if (attr)
00177     if (attr[0])
00178       {
00179         for (int i = 0; attr[i]; i += 2)
00180           {
00181             OUTPUT_TO_DEVICE(" ");
00182             OUTPUT_TO_DEVICE(attr[i]);
00183             OUTPUT_TO_DEVICE("=\"");
00184             OUTPUT_TO_DEVICE(attr[i + 1]);
00185             OUTPUT_TO_DEVICE("\"");
00186 
00187 //          DBG(attr[i] << " = " << attr[i + 1]);
00188           }
00189       }
00190 
00191   OUTPUT_TO_DEVICE(">");
00192 }
00193 
00194 
00195 
00204 void OutputSAXReceptor::endElement(void *userData,
00205                                    const XmlChar *name)
00206 {
00207   OUTPUT_TO_DEVICE("</");
00208   OUTPUT_TO_DEVICE(name);
00209   OUTPUT_TO_DEVICE(">");
00210 }
00211 
00212 
00213 
00214 
00224 void OutputSAXReceptor::characterData(void *userData,
00225                                       const XmlChar *data,
00226                                       int length)
00227 {
00228   if (!inCDATA)
00229     {
00230       //output character data and substitute XML's reserved entites
00231       for (int i = 0; i < length; i++)
00232         {
00233           switch (data[i])
00234             {
00235             case AMP_REPLACEMENT:
00236               //&gt;
00237               OUTPUT_TO_DEVICE("&");
00238               break;
00239           
00240             case '>':
00241               //&gt;
00242               OUTPUT_TO_DEVICE(ENT_GT);
00243               break;
00244           
00245             case '<':
00246               //&lt;
00247               OUTPUT_TO_DEVICE(ENT_LT);
00248               break;
00249           
00250             case '&':
00251               //&amp;
00252               OUTPUT_TO_DEVICE(ENT_AMP);
00253               break;
00254           
00255             case '\'':
00256               //&apos;
00257               OUTPUT_TO_DEVICE(ENT_APOS);
00258               break;
00259           
00260             case '\"':
00261               //&quot;
00262               OUTPUT_TO_DEVICE(ENT_QUOT);
00263               break;
00264           
00265           
00266           
00267             default:
00268               OUTPUT_TO_DEVICE_CHAR(data[i]);
00269             }
00270         }
00271     }
00272   else
00273     //we are in CDATA --> no substitutions
00274     OUTPUT_TO_DEVICE_LENGTH(data, length);
00275 }
00276 
00277 
00278 
00286 void OutputSAXReceptor::processingInstruction(void *userData,
00287                                               const XmlChar *target,
00288                                               const XmlChar *data)
00289 {
00290   OUTPUT_TO_DEVICE("<?");
00291   OUTPUT_TO_DEVICE(target);
00292 
00293   if (xmlchar_strlen(data))
00294     {
00295       OUTPUT_TO_DEVICE(" ");
00296       OUTPUT_TO_DEVICE(data);
00297     }
00298   OUTPUT_TO_DEVICE("?>");
00299 }
00300 
00301 
00302 
00309 void OutputSAXReceptor::comment(void *userData,
00310                                 const XmlChar *data)
00311 {
00312   OUTPUT_TO_DEVICE("<!--");
00313   OUTPUT_TO_DEVICE(data);
00314   OUTPUT_TO_DEVICE("-->");
00315 }
00316 
00317 
00318 
00324 void OutputSAXReceptor::startCDATASection(void *userData)
00325 {
00326   OUTPUT_TO_DEVICE("<![CDATA[");
00327   inCDATA = true;
00328 }
00329 
00330 
00331 
00337 void OutputSAXReceptor::endCDATASection(void *userData)
00338 {
00339   OUTPUT_TO_DEVICE("]]>");
00340   inCDATA = false;
00341 }
00342 
00343 
00344 
00352 void OutputSAXReceptor::defaultHandler(void *userData,
00353                                        const XmlChar *data,
00354                                        int length)
00355 {
00356   OUTPUT_TO_DEVICE_LENGTH(data, length);
00357 }
00358 
00359 
00360 
00370 int OutputSAXReceptor::unknownEncoding(void *unknownEncodingData,
00371                                        const XmlChar *name,
00372                                        XML_Encoding *info)
00373 {
00374   return 1;
00375 }
00376 
00377 
00378 
00388 void OutputSAXReceptor::xmlDecl(void *userData,
00389                                 const XmlChar *version,
00390                                 const XmlChar *encoding,
00391                                 int standalone)
00392 {
00393   OUTPUT_TO_DEVICE("<?xml");
00394   if (version)
00395     {
00396       OUTPUT_TO_DEVICE(" version=\"");
00397       OUTPUT_TO_DEVICE(version);
00398       OUTPUT_TO_DEVICE("\"");
00399     }
00400 
00401   if (encoding)
00402     {
00403       OUTPUT_TO_DEVICE(" encoding=\"");
00404       OUTPUT_TO_DEVICE(encoding);
00405       OUTPUT_TO_DEVICE("\"");
00406     }
00407 
00408   if (standalone != -1)
00409     {
00410       OUTPUT_TO_DEVICE(" standalone=\"");
00411       if (standalone)
00412         {
00413           OUTPUT_TO_DEVICE("yes");
00414         }
00415       else
00416         {
00417           OUTPUT_TO_DEVICE("no");
00418         }
00419 
00420       OUTPUT_TO_DEVICE("\"");
00421     }
00422 
00423   OUTPUT_TO_DEVICE("?>");
00424 }
00425 
00426 
00427 
00438 void OutputSAXReceptor::startDoctypeDecl(void *userData,
00439                                          const XmlChar *doctypeName,
00440                                          const XmlChar *systemId,
00441                                          const XmlChar *publicId,
00442                                          int hasInternalSubset)
00443 {
00444   OUTPUT_TO_DEVICE("<!DOCTYPE ");
00445   OUTPUT_TO_DEVICE(doctypeName);
00446 
00447   if (publicId)
00448     {
00449       OUTPUT_TO_DEVICE(" PUBLIC \"");
00450       OUTPUT_TO_DEVICE(publicId);
00451       OUTPUT_TO_DEVICE("\"");
00452     }
00453 
00454   if (systemId)
00455     {
00456       if (!publicId)
00457         OUTPUT_TO_DEVICE(" SYSTEM");
00458 
00459       OUTPUT_TO_DEVICE(" \"");
00460       OUTPUT_TO_DEVICE(systemId);
00461       OUTPUT_TO_DEVICE("\"");
00462     }
00463 
00464   if (hasInternalSubset)
00465     {
00466       OUTPUT_TO_DEVICE(" [");
00467       dtdHasInternalSubset = true;
00468     }
00469   else
00470     dtdHasInternalSubset = false;
00471 
00472 }
00473 
00474 
00475 
00481 void OutputSAXReceptor::endDoctypeDecl(void *userData)
00482 {
00483   if (dtdHasInternalSubset)
00484     {
00485       OUTPUT_TO_DEVICE("]");
00486       dtdHasInternalSubset = false;
00487     }
00488 
00489   OUTPUT_TO_DEVICE(">");
00490 }
00491 
00492 
00493 
00494 
00508 void OutputSAXReceptor::entityDecl(void *userData,
00509                                    const XmlChar *entityName,
00510                                    int isParameterEntity,
00511                                    const XmlChar *value,
00512                                    int valueLength,
00513                                    const XmlChar *base,
00514                                    const XmlChar *systemId,
00515                                    const XmlChar *publicId,
00516                                    const XmlChar *notationName)
00517 {
00518   OUTPUT_TO_DEVICE("<!ENTITY ");
00519 
00520   if (isParameterEntity)
00521     OUTPUT_TO_DEVICE("% ");
00522 
00523   OUTPUT_TO_DEVICE(entityName);
00524 
00525   if (value)
00526     {
00527       OUTPUT_TO_DEVICE(" \"");
00528       OUTPUT_TO_DEVICE_LENGTH(value, valueLength);
00529       OUTPUT_TO_DEVICE("\"");
00530     }
00531 
00532   if (publicId)
00533     {
00534       OUTPUT_TO_DEVICE(" PUBLIC \"");
00535       OUTPUT_TO_DEVICE(publicId);
00536       OUTPUT_TO_DEVICE("\"");
00537     }
00538 
00539   if (systemId)
00540     {
00541       if (!publicId)
00542         OUTPUT_TO_DEVICE(" SYSTEM");
00543 
00544       OUTPUT_TO_DEVICE(" \"");
00545       OUTPUT_TO_DEVICE(systemId);
00546       OUTPUT_TO_DEVICE("\"");
00547     }
00548 
00549   if (notationName)
00550     {
00551       OUTPUT_TO_DEVICE(" NDATA ");
00552       OUTPUT_TO_DEVICE(notationName);
00553     }
00554 
00555   OUTPUT_TO_DEVICE(">");
00556 }
00557 
00558 
00559 
00569 void OutputSAXReceptor::notationDecl(void *userData,
00570                                      const XmlChar *notationName,
00571                                      const XmlChar *base,
00572                                      const XmlChar *systemId,
00573                                      const XmlChar *publicId)
00574 {
00575   OUTPUT_TO_DEVICE("<!NOTATION ");
00576 
00577   OUTPUT_TO_DEVICE(notationName);
00578 
00579   if (publicId)
00580     {
00581       OUTPUT_TO_DEVICE(" PUBLIC \"");
00582       OUTPUT_TO_DEVICE(publicId);
00583       OUTPUT_TO_DEVICE("\"");
00584     }
00585 
00586   if (systemId)
00587     {
00588       if (!publicId)
00589         OUTPUT_TO_DEVICE(" SYSTEM");
00590 
00591       OUTPUT_TO_DEVICE(" \"");
00592       OUTPUT_TO_DEVICE(systemId);
00593       OUTPUT_TO_DEVICE("\"");
00594     }
00595 
00596   OUTPUT_TO_DEVICE(">");
00597 }
00598 

Generated on Wed Feb 5 10:43:02 2003 for Exalt by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002