Example #1
0
XmlWriter::XmlWriter(std::ostream &os) :
    os_(os),
    xmlWriter_(xmlNewTextWriter(xmlOutputBufferCreateIO(xmlOutputWriteCallback, 0, &os_, 0)))
{
    if (!xmlWriter_)
    {
        BOOST_THROW_EXCEPTION(XmlWriterException("xmlNewTextWriter failed"));
    }

    int written = xmlTextWriterStartDocument(xmlWriter_, 0, 0, 0);
    if (-1 == written)
    {
        BOOST_THROW_EXCEPTION(XmlWriterException(std::string("xmlTextWriterStartDocument returned -1")));
    }

    if (-1 == xmlTextWriterSetIndent(xmlWriter_, 1))
    {
        BOOST_THROW_EXCEPTION(XmlWriterException(std::string("xmlTextWriterSetIndent returned -1")));
    }

    if (-1 == xmlTextWriterSetIndentString(xmlWriter_, BAD_CAST "  "))
    {
        BOOST_THROW_EXCEPTION(XmlWriterException(std::string("xmlTextWriterSetIndentString returned -1")));
    }
}
Example #2
0
static void
output (char **roots)
{
  xmlOutputBufferPtr ob = xmlOutputBufferCreateFd (1, NULL);
  if (ob == NULL) {
    fprintf (stderr,
             _("%s: xmlOutputBufferCreateFd: failed to open stdout\n"),
             guestfs_int_program_name);
    exit (EXIT_FAILURE);
  }

  /* 'ob' is freed when 'xo' is freed.. */
  CLEANUP_XMLFREETEXTWRITER xmlTextWriterPtr xo = xmlNewTextWriter (ob);
  if (xo == NULL) {
    fprintf (stderr,
             _("%s: xmlNewTextWriter: failed to create libxml2 writer\n"),
             guestfs_int_program_name);
    exit (EXIT_FAILURE);
  }

  /* Pretty-print the output. */
  XMLERROR (-1, xmlTextWriterSetIndent (xo, 1));
  XMLERROR (-1, xmlTextWriterSetIndentString (xo, BAD_CAST "  "));

  XMLERROR (-1, xmlTextWriterStartDocument (xo, NULL, NULL, NULL));
  output_roots (xo, roots);
  XMLERROR (-1, xmlTextWriterEndDocument (xo));
}
Example #3
0
  bool XMLConversion::SetupWriter()
  {
    //Set up XML writer if one does not already exist
    if(_writer)
      return true;

    _buf = xmlOutputBufferCreateIO  (
                                     WriteStream, //xmlOutputWriteCallback
                                     NULL,         //xmlOutputCloseCallback
                                     this,        //context
                                     NULL);        //xmlCharEncodingHandlerPtr
    _writer = xmlNewTextWriter(_buf);

    if(!_buf || !_writer)
      {
        cerr << "Error setting up xml writer\n" << endl;
        return false;
      }

    int ret;
    if(IsOption("c"))
      ret = xmlTextWriterSetIndent(_writer,0);
    else
      {
        ret = xmlTextWriterSetIndent(_writer,1);
        ret = xmlTextWriterSetIndentString(_writer, BAD_CAST " ");
      }
    return ret==0;
  }
Example #4
0
void DFSerializeXMLBuffer(DFDocument *doc, NamespaceID defaultNS, int indent, DFBuffer *buf)
{
    xmlOutputBufferPtr output = xmlOutputBufferCreateIO(StringBufferWrite,
                                                        StringBufferClose,
                                                        buf,
                                                        NULL);
    xmlTextWriterPtr writer = xmlNewTextWriter(output);

    int html = 0;
    for (DFNode *child = doc->docNode->first; child != NULL; child = child->next) {
        if (child->tag == HTML_HTML)
            html = 1;
    }

    Serialization serialization;
    bzero(&serialization,sizeof(serialization));
    serialization.ic = iconv_open("UTF-8","UTF-16LE");
    if (serialization.ic == ((iconv_t)-1)) {
        fprintf(stderr,"FATAL: Can't open iconv descriptor\n");
        abort();
    }
    serialization.writer = writer;
    serialization.doc = doc;
    serialization.defaultNS = defaultNS;
    serialization.html = html;
    serialization.indent = indent;
    writeNode(&serialization,doc->docNode,0);
    iconv_close(serialization.ic);
    xmlFreeTextWriter(writer);
}
Example #5
0
bool c_XMLWriter::t_openuri(const String& uri) {
  Variant file = File::Open(uri, "wb");
  if (same(file, false)) {
    return false;
  }
  m_uri = file.toResource().getTyped<File>();

  m_uri_output = xmlOutputBufferCreateIO(write_file, close_file, this, NULL);
  if (m_uri_output == NULL) {
    raise_warning("Unable to create output buffer");
    return false;
  }
  m_ptr = xmlNewTextWriter(m_uri_output);
  return true;
}
Example #6
0
bool c_XMLWriter::t_openuri(const String& uri) {
  m_uri = File::Open(uri, "wb");
  if (m_uri.isNull()) {
    return false;
  }

  xmlOutputBufferPtr uri_output = xmlOutputBufferCreateIO(
    write_file, close_file, this, nullptr);
  if (uri_output == nullptr) {
    raise_warning("Unable to create output buffer");
    return false;
  }
  m_ptr = xmlNewTextWriter(uri_output);
  return true;
}
Example #7
0
static inline int setupCopyToStringWriter(MwsHarvest_SaxUserData* data) {
    xmlOutputBuffer* outPtr;

    data->buffer.clear();
    if ((outPtr = xmlOutputBufferCreateIO(copyToCharBufCallback, nullptr,
                                          &data->buffer, nullptr)) == nullptr) {
        PRINT_WARN("Error while creating the OutputBuffer\n");
        return -1;
    } else if ((data->stringWriter = xmlNewTextWriter(outPtr)) == nullptr) {
        PRINT_WARN("Error while creating the XML Writer\n");
        return -1;
    }

    return 0;
}
bool c_XMLWriter::t_openuri(CStrRef uri) {
  INSTANCE_METHOD_INJECTION_BUILTIN(XMLWriter, XMLWriter::openuri);
  Variant file = File::Open(uri, "wb");
  if (same(file, false)) {
    return false;
  }
  m_uri = file.toObject().getTyped<File>();

  m_uri_output = xmlOutputBufferCreateIO(write_file, close_file, this, NULL);
  if (m_uri_output == NULL) {
    raise_warning("Unable to create output buffer");
    return false;
  }
  m_ptr = xmlNewTextWriter(m_uri_output);
  return true;
}
Example #9
0
// create a new XML writer using writer callback functions
xml_writer::xml_writer(boost::shared_ptr<output_buffer> &out, bool indent)
    : pimpl(new pimpl_()) {
  xmlOutputBufferPtr output_buffer =
      xmlOutputBufferCreateIO(wrap_write, wrap_close, out.get(), NULL);

  // allocate a writer using the output buffer object
  pimpl->writer = xmlNewTextWriter(output_buffer);

  // check the return value
  if (pimpl->writer == NULL) {
    // free the output buffer
    free(output_buffer);

    throw std::runtime_error("error creating xml writer.");
  }

  init(indent);
}
Example #10
0
/**
 * Write the xml document out to a file descriptor.
 * @param   xml The xml document
 * @param   fd The xml output descriptor
 * @return  @c TRUE if successful, @c FALSE if an error occurs.
 * @ingroup EXML_Write_Group
 */
int exml_fd_write(EXML *xml, int fd)
{
	xmlTextWriterPtr writer;
	xmlOutputBufferPtr out;

	out = xmlOutputBufferCreateFd(fd, NULL);
	if (out == NULL) {
		xmlGenericError(xmlGenericErrorContext,
										"xmlNewTextWriterFd : out of memory!\n");
		return FALSE;
	}

	CHECK_PARAM_POINTER_RETURN("xml", xml, FALSE);

	writer = xmlNewTextWriter( out );

	return _exml_write(xml, writer);
}
Example #11
0
static void
output (char **roots)
{
  xmlOutputBufferPtr ob = xmlOutputBufferCreateFd (1, NULL);
  if (ob == NULL)
    error (EXIT_FAILURE, 0,
           _("xmlOutputBufferCreateFd: failed to open stdout"));

  /* 'ob' is freed when 'xo' is freed.. */
  CLEANUP_XMLFREETEXTWRITER xmlTextWriterPtr xo = xmlNewTextWriter (ob);
  if (xo == NULL)
    error (EXIT_FAILURE, 0,
           _("xmlNewTextWriter: failed to create libxml2 writer"));

  /* Pretty-print the output. */
  XMLERROR (-1, xmlTextWriterSetIndent (xo, 1));
  XMLERROR (-1, xmlTextWriterSetIndentString (xo, BAD_CAST "  "));

  XMLERROR (-1, xmlTextWriterStartDocument (xo, NULL, NULL, NULL));
  output_roots (xo, roots);
  XMLERROR (-1, xmlTextWriterEndDocument (xo));
}
Example #12
0
void DFSerializeXMLBuffer(DFDocument *doc, NamespaceID defaultNS, int indent, DFBuffer *buf)
{
    xmlOutputBufferPtr output = xmlOutputBufferCreateIO(StringBufferWrite,
                                                        StringBufferClose,
                                                        buf,
                                                        NULL);
    xmlTextWriterPtr writer = xmlNewTextWriter(output);

    int html = 0;
    for (DFNode *child = doc->docNode->first; child != NULL; child = child->next) {
        if (child->tag == HTML_HTML)
            html = 1;
    }

    Serialization serialization;
    bzero(&serialization,sizeof(serialization));
    serialization.writer = writer;
    serialization.doc = doc;
    serialization.defaultNS = defaultNS;
    serialization.html = html;
    serialization.indent = indent;
    writeNode(&serialization,doc->docNode,0);
    xmlFreeTextWriter(writer);
}
Example #13
0
bool XmlWriter::Create(IOStream *output, XmlWriter **r) {
  if (!LibXmlInitialized) {
    Error(StringPieceFromLiteral("XmlWriter::Create(): call LibXmlInit first()"));
    return false;
  }
  if (!output)
    return false;

  xmlOutputBufferPtr output_buffer = xmlOutputBufferCreateIO(_writeCallback, _closeCallback, output, NULL);
  if (!output_buffer) {
    Error(StringPieceFromLiteral("XmlWriter::Create(): xmlOutputBufferCreateIO fails"));
    return false;
  }

  std::auto_ptr<XmlWriter> xml(new XmlWriter());
  xml->libxml_stuff.reset(new LibXmlStuff());

  /* NOTE: the xmlOutputBufferPtr parameter will be deallocated when the writer is closed (if the call succeed.) */
  xml->libxml_stuff->writer = xmlNewTextWriter(output_buffer);
  if (!xml->libxml_stuff->writer) {
    xmlOutputBufferClose(output_buffer);
    output_buffer = NULL;
    Error(StringPieceFromLiteral("XmlWriter::Create(): xmlNewTextWriter fails"));
    return false;
  }

  if (r) {
    xmlTextWriterSetIndent(xml->libxml_stuff->writer, 1);
    /*if (!xml->StartDocument(output)) {
     cpcl::Error(cpcl::StringPieceFromLiteral("XmlWriter::Create(): xmlTextWriterStartDocument fails"));
     return false;
     }*/
    *r = xml.release();
  }
  return true;
}
Example #14
0
int main( int argc, const char* argv[] )
{
#ifdef WIN32
     _CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif
    int ret=-1;
    time_t start_time=time(NULL);
    FILE *file=NULL;
    const xmlChar *containerPath8=NULL;
    const xmlChar *partName8=NULL;
    xmlTextWriter *writer=NULL;
    int writer_indent=0;
    opc_bool_t reader_mce=OPC_TRUE;
    for(int i=1;i<argc;i++) {
        if ((0==xmlStrcmp(_X("--understands"), _X(argv[i])) || 0==xmlStrcmp(_X("-u"), _X(argv[i]))) && i+1<argc) {
	    i++; // skip namespace, registered later when parser was created.
        } else if ((0==xmlStrcmp(_X("--out"), _X(argv[i])) || 0==xmlStrcmp(_X("--out"), _X(argv[i]))) && i+1<argc && NULL==file) {
            const char *filename=argv[++i];
            file=fopen(filename, "w");
        } else if (0==xmlStrcmp(_X("--indent"), _X(argv[i]))) {
            writer_indent=1;
        } else if (0==xmlStrcmp(_X("--raw"), _X(argv[i]))) {
            reader_mce=OPC_FALSE;
        } else if (NULL==containerPath8) {
            containerPath8=_X(argv[i]);
        } else if (NULL==partName8) {
            partName8=_X(argv[i]);
        } else {
            fprintf(stderr, "IGNORED: %s\n", argv[i]);
        }
    }
    if (NULL!=file) {
        xmlOutputBuffer *out=xmlOutputBufferCreateIO(xmlOutputWrite, xmlOutputClose, file, NULL);
        if (NULL!=out) {
            writer=xmlNewTextWriter(out);
        }
    } else {
        xmlOutputBuffer *out=xmlOutputBufferCreateIO(xmlOutputWrite, xmlOutputClose, stdout, NULL);
        if (NULL!=out) {
            writer=xmlNewTextWriter(out);
        }
    }
    if (NULL==containerPath8 || NULL==writer) {
        printf("mce_extract FILENAME.\n\n");
        printf("Sample: mce_extract test.docx word/document.xml\n");
    } else if (OPC_ERROR_NONE==opcInitLibrary()) {
        xmlTextWriterSetIndent(writer, writer_indent);
        opcContainer *c=NULL;
        if (NULL!=(c=opcContainerOpen(containerPath8, OPC_OPEN_READ_ONLY, NULL, NULL))) {
            if (NULL==partName8) {
                dumpPartsAsJSON(c, writer_indent);
            } else {
                opcPart part=OPC_PART_INVALID;
                if ((part=opcPartFind(c, partName8, NULL, 0))!=OPC_PART_INVALID) {
                    mceTextReader_t reader;
                    if (OPC_ERROR_NONE==opcXmlReaderOpen(c, &reader, part, NULL, NULL, 0)) {
                        mceTextReaderDisableMCE(&reader, !reader_mce);
                        for(int i=1;i<argc;i++) {
                            if ((0==xmlStrcmp(_X("--understands"), _X(argv[i])) || 0==xmlStrcmp(_X("-u"), _X(argv[i]))) && i+1<argc) {
                                const xmlChar *ns=_X(argv[++i]);
                                mceTextReaderUnderstandsNamespace(&reader, ns);
                            }
                        }

                        if (-1==mceTextReaderDump(&reader, writer, PTRUE)) {
                            ret=mceTextReaderGetError(&reader);
                        } else {
                            ret=0;
                        }
                        mceTextReaderCleanup(&reader);
                    } else {
                        fprintf(stderr, "ERROR: part \"%s\" could not be opened for XML reading.\n", argv[2]);
                    }
                } else {
                    fprintf(stderr, "ERROR: part \"%s\" could not be opened in \"%s\".\n", argv[2], argv[1]);
                }
            }
            opcContainerClose(c, OPC_CLOSE_NOW);
        } else {
            fprintf(stderr, "ERROR: file \"%s\" could not be opened.\n", argv[1]);
        }
        opcFreeLibrary();
    } else {
        fprintf(stderr, "ERROR: initialization of libopc failed.\n");    
    }
    if (NULL!=writer) xmlFreeTextWriter(writer);
    if (NULL!=file) fclose(file);
    time_t end_time=time(NULL);
    fprintf(stderr, "time %.2lfsec\n", difftime(end_time, start_time));
    return ret;
}
Example #15
0
int MwsXmlResponseFormatter::writeData(const GenericAnswer* ans,
                                       FILE* output) const {
    const MwsAnswset& answerSet = *((const MwsAnswset*)ans);
    xmlOutputBuffer* outPtr;
    xmlTextWriter* writerPtr;
    size_t qvarNr;
    int ret;
    unsigned int i;
    LocalContext ctxt;

    // Initializing values
    outPtr = nullptr;
    writerPtr = nullptr;
    ret = -1;
    qvarNr = answerSet.qvarNames.size();
    ctxt.file = output;
    ctxt.total_bytes_written = 0;

    if ((outPtr = xmlOutputBufferCreateIO(fileXmlOutputWriteCallback, nullptr,
                                          &ctxt, nullptr)) ==
        nullptr) {
        PRINT_WARN("Error while creating the OutputBuffer\n");
    } else if ((writerPtr = xmlNewTextWriter(outPtr)) == nullptr) {
        PRINT_WARN("Error while creating the TextWriter\n");
    } else if ((ret =
                    xmlTextWriterStartDocument(writerPtr,  // xmlTextWriter
                                               nullptr,   // XML version ("1.0")
                                               nullptr,   // Encoding ("UTF-8")
                                               nullptr))  // Standalone ("yes")
               ==
               -1) {
        PRINT_WARN("Error at xmlTextWriterStartDocument\n");
    } else if ((ret = xmlTextWriterWriteComment(
                    writerPtr,
                    BAD_CAST "MwsAnswset generated by " MWS_BUILD)) ==
               -1) {
        PRINT_WARN("Error at xmlTextWriterStartDocument\n");
    } else if ((ret = xmlTextWriterStartElement(
                    writerPtr, BAD_CAST MWSANSWSET_MAIN_NAME)) ==
               -1) {
        PRINT_WARN("Error at xmlTextWriterStartElement\n");
    } else if ((ret = xmlTextWriterWriteAttribute(
                    writerPtr, BAD_CAST "xmlns:mws",
                    BAD_CAST "http://www.mathweb.org/mws/ns")) ==
               -1) {
        PRINT_WARN("Error at xmlTextWriterWriteAttribute\n");
    } else if ((ret = xmlTextWriterWriteAttribute(
                    writerPtr, BAD_CAST "size",
                    BAD_CAST std::to_string(answerSet.answers.size())
                        .c_str())) ==
               -1) {
        PRINT_WARN("Error at xmlTextWriterWriteAttribute\n");
    } else if ((ret = xmlTextWriterWriteAttribute(
                    writerPtr, BAD_CAST "total",
                    BAD_CAST std::to_string(answerSet.total).c_str())) ==
               -1) {
        PRINT_WARN("Error at xmlTextWriterWriteAttribute\n");
    } else {
        for (auto& answer : answerSet.answers) {
            if ((ret = xmlTextWriterStartElement(
                     writerPtr, BAD_CAST MWSANSWSET_ANSW_NAME)) ==
                -1) {
                PRINT_WARN("Error at xmlTextWriterStartElement\n");
                break;
            } else if ((ret = xmlTextWriterWriteAttribute(
                            writerPtr, BAD_CAST MWSANSWSET_URI_NAME,
                            BAD_CAST answer->uri.c_str())) ==
                       -1) {
                PRINT_WARN("Error at xmlTextWriterWriteAttribute\n");
                break;
            } else if ((ret = xmlTextWriterWriteAttribute(
                            writerPtr, BAD_CAST MWSANSWSET_XPATH_NAME,
                            BAD_CAST answer->xpath.c_str())) ==
                       -1) {
                PRINT_WARN("Error at xmlTextWriterWriteAttribute\n");
                break;
            } else {
                // Writing the substitutions
                for (i = 0; i < qvarNr; i++) {
                    string qvarXpath = answer->xpath + answerSet.qvarXpaths[i];
                    if ((ret = xmlTextWriterStartElement(
                             writerPtr, BAD_CAST MWSANSWSET_SUBSTPAIR_NAME)) ==
                        -1) {
                        PRINT_WARN("Error at xmlTextWriterStartElement\n");
                        break;
                    } else if ((ret = xmlTextWriterWriteAttribute(
                                    writerPtr, BAD_CAST "qvar",
                                    BAD_CAST answerSet.qvarNames[i].c_str())) ==
                               -1) {
                        PRINT_WARN("Error at xmlTextWriterWriteAttribute\n");
                        break;
                    } else if ((ret = xmlTextWriterWriteAttribute(
                                    writerPtr, BAD_CAST "xpath",
                                    BAD_CAST qvarXpath.c_str())) ==
                               -1) {
                        PRINT_WARN("Error at xmlTextWriterWriteAttribute\n");
                        break;
                    } else if ((ret = xmlTextWriterEndElement(writerPtr)) ==
                               -1) {
                        PRINT_WARN("Error at xmlTextWriterEndElement\n");
                        break;
                    }
                }
                // <data> ... </data>
                xmlTextWriterWriteElement(writerPtr, BAD_CAST "data",
                                          BAD_CAST answer->data.c_str());
            }
            if (ret == -1) {
                PRINT_WARN("Error while writing xml substpairs\n");
                break;
            } else if ((ret = xmlTextWriterEndElement(writerPtr)) == -1) {
                PRINT_WARN("Error at xmlTextWriterEndElement\n");
                break;
            }
        }
    }
    if (ret == -1) {
        PRINT_WARN("Error while writing xml answers\n");
    } else if ((ret = xmlTextWriterEndElement(writerPtr)) == -1) {
        PRINT_WARN("Error at xmlTextWriterEndElement\n");
    } else if ((ret = xmlTextWriterEndDocument(writerPtr)) == -1) {
        PRINT_WARN("Error at xmlTextWriterEndDocument\n");
    } else if ((ret = xmlTextWriterFlush(writerPtr)) == -1) {
        PRINT_WARN("Error at xmlTextWriterFlush\n");
    } else {
        ret = 0;
    }

    // Cleaning data
    if (writerPtr) {
        // This also cleans the outPtr
        xmlFreeTextWriter(writerPtr);
    } else if (outPtr) {
        xmlOutputBufferClose(outPtr);
    }

    if (ret == 0) {
        return ctxt.total_bytes_written;
    } else {
        return ret;
    }
}
Example #16
0
int writeXmlAnswsetToFd(MwsAnswset* answset, int fd)
{
#ifdef TRACE_FUNC_CALLS
    LOG_TRACE_IN;
#endif

    xmlOutputBuffer* outPtr;
    xmlTextWriter*   writerPtr;
    size_t           qvarNr;
    int              ret;
    unsigned int     i;
    vector<MwsAnsw*>::iterator it;

    // Initializing values
    outPtr    = NULL;
    writerPtr = NULL;
    ret       = -1;
    qvarNr    = answset->qvarNames.size();


    if (answset == NULL)
    {
        fprintf(stderr, "NULL answset passed to writeXmlAnswsetToFd");
    }
    // Creating the xmlOutputBuffer
    else if ((outPtr = xmlOutputBufferCreateIO(fdXmlOutputWriteCallback, 
                                               NULL,
                                               &fd, 
                                               NULL))
            == NULL)
    {
        fprintf(stderr, "Error while creating the OutputBuffer\n");
    }
    // Creating the xmlTextWriter
    else if ((writerPtr = xmlNewTextWriter(outPtr))
            == NULL)
    {
        fprintf(stderr, "Error while creating the TextWriter\n");
    }
    else if ((ret = xmlTextWriterStartDocument(writerPtr, // xmlTextWriter
                                               NULL,      // XML version ("1.0")
                                               NULL,      // Encoding ("UTF-8")
                                               NULL))     // Standalone ("yes")
            == -1)
    {
        fprintf(stderr, "Error at xmlTextWriterStartDocument\n");
    }
    // MWS timestamp comment
    else if ((ret = xmlTextWriterWriteComment(writerPtr,
                    BAD_CAST "MwsAnswset generated by " __MWSTIMESTAMP__ " "))
            == -1)
    {
        fprintf(stderr, "Error at xmlTextWriterStartDocument\n");
    }
    // MWS answset
    else if ((ret = xmlTextWriterStartElement(writerPtr, 
                    BAD_CAST MWSANSWSET_MAIN_NAME))
            == -1)
    {
        fprintf(stderr, "Error at xmlTextWriterStartElement\n");
    }
    // mws namespace
    else if ((ret = xmlTextWriterWriteAttribute(writerPtr,
                    BAD_CAST "xmlns:mws",
                    BAD_CAST "http://www.mathweb.org/mws/ns"))
            == -1)
    {
        fprintf(stderr, "Error at xmlTextWriterWriteAttribute\n");
    }
    // attribute size
    else if ((ret = xmlTextWriterWriteAttribute(writerPtr,
                    BAD_CAST "size",
                    BAD_CAST ToString(answset->answers.size()).c_str()))
            == -1)
    {
        fprintf(stderr, "Error at xmlTextWriterWriteAttribute\n");
    }
    // attribute total
    else if ((ret = xmlTextWriterWriteAttribute(writerPtr,
                    BAD_CAST "total",
                    BAD_CAST ToString(answset->total).c_str()))
            == -1)
    {
        fprintf(stderr, "Error at xmlTextWriterWriteAttribute\n");
    }
    else
    // Writing the answers
    {
        for (it  = answset->answers.begin();
             it != answset->answers.end();
             it++)
        {
            if ((ret = xmlTextWriterStartElement(writerPtr,
                        BAD_CAST MWSANSWSET_ANSW_NAME))
                    == -1)
            {
                fprintf(stderr, "Error at xmlTextWriterStartElement\n");
                break;
            }
            else if ((ret = xmlTextWriterWriteAttribute(writerPtr,
                        BAD_CAST MWSANSWSET_URI_NAME,
                        BAD_CAST (*it)->uri.c_str()))
                    == -1)
            {
                fprintf(stderr, "Error at xmlTextWriterWriteAttribute\n");
                break;
            }
            else if ((ret = xmlTextWriterWriteAttribute(writerPtr,
                        BAD_CAST MWSANSWSET_XPATH_NAME,
                        BAD_CAST (*it)->xpath.c_str()))
                    == -1)
            {
                fprintf(stderr, "Error at xmlTextWriterWriteAttribute\n");
                break;
            }
            else
            {
                // Writing the substitutions
                for (i = 0; i < qvarNr; i++)
                {
                    if ((ret = xmlTextWriterStartElement(writerPtr,
                                BAD_CAST MWSANSWSET_SUBSTPAIR_NAME))
                            == -1)
                    {
                        fprintf(stderr, "Error at xmlTextWriterStartElement\n");
                        break;
                    }
                    else if ((ret = xmlTextWriterWriteAttribute(writerPtr,
                                BAD_CAST "qvar",
                                BAD_CAST answset->qvarNames[i].c_str()))
                            == -1)
                    {
                        fprintf(stderr,
                                "Error at xmlTextWriterWriteAttribute\n");
                        break;
                    }
                    else if ((ret = xmlTextWriterWriteAttribute(writerPtr,
                                BAD_CAST "xpath",
                                BAD_CAST (*it)->subst.qvarXpaths[i].c_str()))
                            == -1)
                    {
                        fprintf(stderr,
                                "Error at xmlTextWriterWriteAttribute\n");
                        break;
                    }
                    else if ((ret = xmlTextWriterEndElement(writerPtr))
                            == -1)
                    {
                        fprintf(stderr, "Error at xmlTextWriterEndElement\n");
                        break;
                    }
                }
            }
            if (ret == -1)
            {
                fprintf(stderr, "Error while writing xml substpairs\n");
                break;
            }
            else if ((ret = xmlTextWriterEndElement(writerPtr))
                    == -1)
            {
                fprintf(stderr, "Error at xmlTextWriterEndElement\n");
                break;
            }
        }
    }
    if (ret == -1)
    {
        fprintf(stderr, "Error while writing xml answers\n");
    }
    else if ((ret = xmlTextWriterEndElement(writerPtr))
            == -1)
    {
        fprintf(stderr, "Error at xmlTextWriterEndElement\n");
    }
    // Closing the document
    else if ((ret = xmlTextWriterEndDocument(writerPtr))
            == -1)
    {
        fprintf(stderr, "Error at xmlTextWriterEndDocument\n");
    }
    // Flushing the buffer
    else if ((ret = xmlTextWriterFlush(writerPtr))
            == -1)
    {
        fprintf(stderr, "Error at xmlTextWriterFlush\n");
    }
    // Everything ok
    else
    {
        ret = 0;
    }

    // Cleaning data
    if (writerPtr)
    {
        // This also cleans the outPtr
        xmlFreeTextWriter(writerPtr);
    }
    else if (outPtr)
    {
        xmlOutputBufferClose(outPtr);
    }

#ifdef TRACE_FUNC_CALLS
    LOG_TRACE_OUT;
#endif

    return ret;
}
Example #17
0
static void Save(const SObject *object, const char *path, s_erc *error)
{
	int rc;
	SUtterance *utt = S_UTTERANCE(object);
	SDatasource *ds;
	xmlTextWriterPtr writer;
	xmlOutputBufferPtr out;
	const char * xsi = "http://www.w3.org/2001/XMLSchema-instance";


	S_CLR_ERR(error);
	ds = SFilesourceOpenFile(path, "wt", error);
	if (S_CHK_ERR(error, S_CONTERR,
		      "Save",
		      "Call to \"SFilesourceOpenFile\" failed"))
		return;

	out = xmlOutputBufferCreateIO(_ds_write,
				      _ds_close,
				      ds,
				      NULL);
	if (out == NULL)
	{
		S_CTX_ERR(error, S_CONTERR,
			  "Save",
			  "Call to \"xmlOutputBufferCreateIO\" failed");
		return;
	}
	writer = xmlNewTextWriter(out);
	if (writer == NULL)
	{
                xmlOutputBufferClose(out);
		S_CTX_ERR(error, S_CONTERR,
			  "Save",
			  "Call to \"xmlNewTextWriter\" failed");
		return;
	}

	/* Start Document */
	rc = xmlTextWriterStartDocument(writer, NULL, ENCODING, NULL);
	if (rc < 0) {
		S_CTX_ERR(error, S_CONTERR,
			  "Save",
			  "Call to \"xmlTextWriterStartDocument\" failed");
		goto s_write_utt_exit;
	}

	/* Write the maryxml namespace */
	rc = xmlTextWriterStartElement(writer, BAD_CAST "xml");
	if (rc < 0) {
		S_CTX_ERR(error, S_CONTERR,
			  "Save",
			  "Call to \"xmlTextWriterStartElement\" failed");
		goto s_write_utt_exit;
	}

	rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "xmlns:xsi", BAD_CAST xsi);
	if (rc < 0) {
		S_CTX_ERR(error, S_CONTERR,
			  "Save",
			  "Call to \"xmlTextWriterWriteAttribute\" failed");
		goto s_write_utt_exit;
	}

	rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "version", BAD_CAST "0.5");
	if (rc < 0) {
		S_CTX_ERR(error, S_CONTERR,
			  "Save",
			  "Call to \"xmlTextWriterWriteAttribute\" failed");
		goto s_write_utt_exit;
	}


	// print labels
	/* get to the first syllable of the current word */
	const SItem* itrSegments = SRelationHead(SUtteranceGetRelation(utt, "Segment", error), error);
	if (S_CHK_ERR(error, S_CONTERR,
		      "Save",
		      "Call to \"SItemPathToItem\" failed"))
 		goto s_write_utt_exit;
	while (itrSegments != NULL)
	{
		/* get segment content */
		const char* label = SItemGetString(itrSegments, "hts_label", error);
		if (S_CHK_ERR(error, S_CONTERR,
			      "Save",
			      "Call to \"SItemGetName\" failed"))
			goto s_write_utt_exit;

		/* get next segment */
		itrSegments = SItemNext(itrSegments, error);
		if (S_CHK_ERR(error, S_CONTERR,
			      "Save",
			      "Call to \"SItemNext\" failed"))
			goto s_write_utt_exit;

		xmlTextWriterWriteElement(writer, BAD_CAST "label", BAD_CAST label);

	}

	/* Close the tag xml */
	rc = xmlTextWriterEndElement(writer);
	if (rc < 0)
	{
		S_CTX_ERR(error, S_CONTERR,
			  "Save",
			  "Call to \"xmlTextWriterEndDocument\" failed");
		goto s_write_utt_exit;
	}
	/* Close the document */
	rc = xmlTextWriterEndDocument(writer);
	if (rc < 0)
	{
		S_CTX_ERR(error, S_CONTERR,
			  "Save",
			  "Call to \"xmlTextWriterEndDocument\" failed");
		goto s_write_utt_exit;
	}
s_write_utt_exit:
        xmlFreeTextWriter(writer);
}