Exemplo n.º 1
0
bool c_XMLWriter::t_writecomment(const String& content) {
  int ret = -1;
  if (m_ptr) {
    ret = xmlTextWriterWriteComment(m_ptr, (xmlChar*)content.data());
  }
  return ret != -1;
}
bool TasksetWriter::write(const std::string& filename, vector<Task*>& taskset) const {
    xmlDocPtr doc;
    tDebug() << "Writing Taskset to XML file: " << filename;

    xmlTextWriterPtr writer;
    writer = xmlNewTextWriterDoc(&doc, 0);
    xmlTextWriterSetIndent(writer, 1);
    if (xmlTextWriterSetIndentString(writer, (const xmlChar*) "  ") != 0) {
        tError() << "Fehler beim Setzen des Einrueckens!";
    }

    xmlTextWriterStartDocument(writer, NULL, "UTF-8", NULL);

    xmlTextWriterWriteComment(writer, (xmlChar*) "Hier kommen die Tasks");

    xmlTextWriterStartElement(writer, (xmlChar*) "taskset");
    xmlTextWriterWriteAttributeNS(writer, (xmlChar*) "xsi", (xmlChar*) "schemaLocation", (xmlChar*) "http://www.w3.org/2001/XMLSchema-instance", (xmlChar*) "http://www.tmsxmlns.com taskset.xsd");
    xmlTextWriterWriteAttribute(writer, (xmlChar*) "xmlns", (xmlChar*) "http://www.tmsxmlns.com");

    xmlTextWriterWriteRaw(writer, (xmlChar*) "\n");

    for (size_t i = 0; i < taskset.size(); i++) {
        xmlTextWriterWriteRaw(writer, (xmlChar*) "\n");
        //taskset[i]->write(writer);
        taskset[i]->writeToXML(writer);
        xmlTextWriterWriteRaw(writer, (xmlChar*) "\n");
    }

    xmlTextWriterEndElement(writer); // close TaskSet

    xmlTextWriterEndDocument(writer);
    xmlFreeTextWriter(writer);
    xmlSaveFile(filename.c_str(), doc);

    xmlNodePtr cur = xmlDocGetRootElement(doc);
    if (cur == NULL) {
        tError() << "Empty document.";
        xmlFreeDoc(doc);
        return false;
    }
    if (xmlStrcmp(cur->name, (const xmlChar *) "taskset")) {
        tError() << "Document of the wrong type, root node != taskset";
        xmlFreeDoc(doc);
        return false;
    }

    if (isValid(doc) > 0) {
        tDebug() << "Written document is valid";

    } else {
        tError() << "Written document is invalid";
        xmlFreeDoc(doc);
        return false;
    }

    xmlFreeDoc(doc);

    return true;

}
Exemplo n.º 3
0
bool c_xmlwriter::t_writecomment(CStrRef content) {
  int ret = -1;
  if (m_ptr) {
    ret = xmlTextWriterWriteComment(m_ptr, (xmlChar*)content.data());
  }
  return ret != -1;
}
Exemplo n.º 4
0
bool c_XMLWriter::t_writecomment(CStrRef content) {
  INSTANCE_METHOD_INJECTION_BUILTIN(XMLWriter, XMLWriter::writecomment);
  int ret = -1;
  if (m_ptr) {
    ret = xmlTextWriterWriteComment(m_ptr, (xmlChar*)content.data());
  }
  return ret != -1;
}
Exemplo n.º 5
0
int XMLInOut::AddComment(const char* comment)
{
	int rc;

    rc = xmlTextWriterWriteComment(writer, BAD_CAST comment);
    if (rc < 0)
        cout << "testXmlwriterMemory: Error at xmlTextWriterWriteComment for '" << comment << "'" << endl;

    return rc;
}
Exemplo n.º 6
0
static void writeNode(Serialization *serialization, DFNode *node, int depth)
{
    switch (node->tag) {
        case DOM_DOCUMENT: {
            if (!serialization->html)
                xmlTextWriterStartDocument(serialization->writer,"1.0","UTF-8","yes");
            if (serialization->html)
                xmlTextWriterWriteDTD(serialization->writer,(xmlChar *)"html",NULL,NULL,NULL);
//            xmlTextWriterWriteDTD(writer,
//                                  (xmlChar *)"html",
//                                  (xmlChar *)"-//W3C//DTD XHTML 1.0 Strict//EN",
//                                  (xmlChar *)"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd",
//                                  NULL);
            for (DFNode *child = node->first; child != NULL; child = child->next)
                writeNode(serialization,child,0);
            xmlTextWriterEndDocument(serialization->writer);
            break;
        }
        case DOM_TEXT: {
            if (serialization->indent && ((node->prev != NULL) || (node->next != NULL)))
                xmlTextWriterWriteRawLen(serialization->writer,INDENT,1+depth);
            if (serialization->html && (node->parent != NULL) && (node->parent->tag == HTML_STYLE)) {
                xmlTextWriterWriteRaw(serialization->writer,(const xmlChar *)node->value);
            }
            else {
                xmlTextWriterWriteString(serialization->writer,(const xmlChar *)node->value);
            }
            break;
        }
        case DOM_COMMENT: {
            xmlTextWriterWriteComment(serialization->writer,(const xmlChar *)node->value);
            break;
        }
        case DOM_CDATA: {
            xmlTextWriterWriteCDATA(serialization->writer,(const xmlChar *)node->value);
            break;
        }
        case DOM_PROCESSING_INSTRUCTION: {
            xmlTextWriterWritePI(serialization->writer,
                                 (const xmlChar *)node->target,
                                 (const xmlChar *)node->value);
            break;
        }
        default: {
            if (node->parent == serialization->doc->docNode)
                writeElement(serialization,node,0);
            else
                writeElement(serialization,node,depth);
            break;
        }
    }
}
Exemplo n.º 7
0
//----------------------------------------------------------------------------
// Write a comment in the xml file in the current node
//----------------------------------------------------------------------------
bool CXMLTreeNode::WriteComment(const char* _pszComment)
{
  assert(_pszComment && m_pWriter);

  if (_pszComment && m_pWriter)
  {
    int rc = xmlTextWriterWriteComment(m_pWriter, BAD_CAST _pszComment);
    assert(rc >= 0);

    if (rc < 0)
      return false;
  }
  else
    return false;

  return true;
}
axis2_status_t AXIS2_CALL
axis2_libxml2_writer_wrapper_write_comment(
    axiom_xml_writer_t * writer,
    const axutil_env_t * env,
    axis2_char_t * value)
{
    axis2_libxml2_writer_wrapper_impl_t *writer_impl = NULL;
    int status = 0;
    AXIS2_ENV_CHECK(env, AXIS2_FAILURE);
    AXIS2_PARAM_CHECK(env->error, value, AXIS2_FAILURE);

    writer_impl = AXIS2_INTF_TO_IMPL(writer);
    status = xmlTextWriterWriteComment(writer_impl->xml_writer, BAD_CAST value);
    if(status < 0)
    {
        AXIS2_HANDLE_ERROR(env, AXIS2_ERROR_WRITING_COMMENT, AXIS2_FAILURE);
        return AXIS2_FAILURE;
    }
    return AXIS2_SUCCESS;
}
void CResource::SerializeToXmlFile(const nstring &file)
{
    xmlTextWriterPtr writer = NULL;

    /* Create a new XmlWriter for uri, with no compression. */
    if((writer = xmlNewTextWriterFilename(file.c_str(), 0)) == NULL)
		NOVA_EXP("CResource::SerializeToXmlFile: Error creating the xml writer", BAD_OPERATION);

	// automatic indentation for readability
	xmlTextWriterSetIndent(writer, 1);

	if(xmlTextWriterStartDocument(writer, NULL, "UTF-8", NULL) < 0)
		NOVA_EXP("CResource::SerializeToXmlFile: xmlTextWriterStartDocument fail", BAD_OPERATION);

	if(xmlTextWriterStartElement(writer, BAD_CAST "NovaResource") < 0)
		NOVA_EXP("CResource::SerializeToXmlFile: xmlTextWriterStartElement fail", BAD_OPERATION);

    if(xmlTextWriterWriteComment(writer, BAD_CAST "Common resource header") < 0)
		NOVA_EXP("CResource::SerializeToXmlFile: xmlTextWriterWriteComment fail", BAD_OPERATION);

	if(xmlTextWriterStartElement(writer, BAD_CAST "ResourceHeader") < 0)
		NOVA_EXP("CResource::SerializeToXmlFile: xmlTextWriterStartElement fail", BAD_OPERATION);

	if(xmlTextWriterWriteElement(writer, BAD_CAST "ResourceName", BAD_CAST mName.c_str()) < 0)
		NOVA_EXP("CResource::SerializeToXmlFile: xmlTextWriterWriteElement fail", BAD_OPERATION);

	if(xmlTextWriterWriteElement(writer, BAD_CAST "ResourceGroup", BAD_CAST mGroup.c_str()) < 0)
		NOVA_EXP("CResource::SerializeToXmlFile: xmlTextWriterWriteElement fail", BAD_OPERATION);

	if(xmlTextWriterStartElement(writer, BAD_CAST "ResourceData") < 0)
		NOVA_EXP("CResource::SerializeToXmlFile: xmlTextWriterStartElement fail", BAD_OPERATION);

// Resource manager attribute
	if(xmlTextWriterWriteAttribute(writer, BAD_CAST "ResourceFactory", BAD_CAST GetCreator()->GetResourceFactoryName().c_str()) < 0)
		NOVA_EXP("CImage::SerializeToXmlFileImpl: xmlTextWriterWriteAttribute fail", BAD_OPERATION);

	SerializeToXmlFileImpl(writer);

    xmlTextWriterEndDocument(writer);
	xmlFreeTextWriter(writer);
}
Exemplo n.º 10
0
int
rsstool_write_xml (st_rsstool_t *rt)
{
#define XMLPRINTF(s) xmlTextWriterWriteString(writer,BAD_CAST s)
  st_rss_t rss;
  int i = 0;
  xmlTextWriterPtr writer;
  xmlBufferPtr buffer;
  st_hash_t *dl_url_h = NULL;
  st_hash_t *url_h = NULL;
  st_hash_t *title_h = NULL;
  int items = rsstool_get_item_count (rt);
#define ENCODE(s) s
//#define ENCODE(s) base64_enc(s,0)
//#define ENCODE(s) str_escape_xml(s)

  memset (&rss, 0, sizeof (st_rss_t));

  if (!(buffer = xmlBufferCreate ()))
    return -1;

  if (!(writer = xmlNewTextWriterMemory (buffer, 0)))
    return -1;

  xmlTextWriterStartDocument (writer, NULL, "UTF-8", NULL);

  xmlTextWriterWriteComment (writer, BAD_CAST " RSStool - read, parse, merge and write RSS and Atom feeds\n"                            
    "http://rsstool.berlios.de ");

  XMLPRINTF("\n");

  xmlTextWriterWriteComment (writer, BAD_CAST "\n"
         "format:\n"
         "item[]\n"
         "  dl_url           \n"
         "  dl_url_md5\n"
         "  dl_url_crc32\n"
         "  dl_date\n"
         "  user             author\n"
         "  site\n"
         "  url              \n"
         "  url_md5\n"
         "  url_crc32\n"
         "  date             default: current time\n"
         "  title            used by searches for related items\n"
         "  title_md5\n"
         "  title_crc32\n"
         "  desc             description\n"
         "  media_keywords   default: keywords from title and description\n"
         "  media_duration   event length\n"
         "  media_image      thumbnail\n"
         "  event_start      default: date\n"
         "  event_end        default: event_start + media_duration\n"
);

  XMLPRINTF("\n");

  xmlTextWriterStartElement (writer, BAD_CAST "rsstool");  // <rsstool>
  xmlTextWriterWriteAttribute (writer, BAD_CAST "version", BAD_CAST RSSTOOL_VERSION_S);

  for (i = 0; i < items && i < RSSMAXITEM; i++)
//  for (i = 0; i < items; i++)
    {
      dl_url_h = hash_open (HASH_MD5|HASH_CRC32);
      url_h = hash_open (HASH_MD5|HASH_CRC32);
      title_h = hash_open (HASH_MD5|HASH_CRC32);

      dl_url_h = hash_update (dl_url_h, (const unsigned char *) rt->item[i]->feed_url, strlen (rt->item[i]->feed_url));
      url_h = hash_update (url_h, (const unsigned char *) rt->item[i]->url, strlen (rt->item[i]->url));
      title_h = hash_update (title_h, (const unsigned char *) rt->item[i]->title, strlen (rt->item[i]->title));

      XMLPRINTF("\n  ");

      xmlTextWriterStartElement (writer, BAD_CAST "item"); // <item>

      XMLPRINTF("\n    ");

//      xmlTextWriterWriteElement (writer, BAD_CAST "dl_url", BAD_CAST rt->item[i]->feed_url);
      xmlTextWriterWriteFormatElement (writer, BAD_CAST "dl_url", "%s", rt->item[i]->feed_url);

      XMLPRINTF("\n    ");

      xmlTextWriterWriteFormatElement (writer, BAD_CAST "dl_url_md5", "%s", hash_get_s (dl_url_h, HASH_MD5));

      XMLPRINTF("\n    ");

      xmlTextWriterWriteFormatElement (writer, BAD_CAST "dl_url_crc32", "%u", hash_get_crc32 (dl_url_h));

      XMLPRINTF("\n    ");

      xmlTextWriterWriteFormatElement (writer, BAD_CAST "dl_date", "%ld", rsstool.start_time);

      XMLPRINTF("\n    ");

      xmlTextWriterWriteFormatElement (writer, BAD_CAST "user", "%s", ENCODE (rt->item[i]->user));

      XMLPRINTF("\n    ");

      xmlTextWriterWriteFormatElement (writer, BAD_CAST "site", "%s", ENCODE (rt->item[i]->site));

      XMLPRINTF("\n    ");

      xmlTextWriterWriteFormatElement (writer, BAD_CAST "url", "%s", rt->item[i]->url);

      XMLPRINTF("\n    ");

      xmlTextWriterWriteFormatElement (writer, BAD_CAST "url_md5", "%s", hash_get_s (url_h, HASH_MD5));

      XMLPRINTF("\n    ");

      xmlTextWriterWriteFormatElement (writer, BAD_CAST "url_crc32", "%u", hash_get_crc32 (url_h));

      XMLPRINTF("\n    ");

      xmlTextWriterWriteFormatElement (writer, BAD_CAST "date", "%ld", rt->item[i]->date);

      XMLPRINTF("\n    ");

      xmlTextWriterWriteFormatElement (writer, BAD_CAST "title", "%s", ENCODE (rt->item[i]->title));


      XMLPRINTF("\n    ");

      xmlTextWriterWriteFormatElement (writer, BAD_CAST "title_md5", "%s", hash_get_s (title_h, HASH_MD5));

      XMLPRINTF("\n    ");

      xmlTextWriterWriteFormatElement (writer, BAD_CAST "title_crc32", "%u", hash_get_crc32 (title_h));

      XMLPRINTF("\n    ");

      xmlTextWriterWriteFormatElement (writer, BAD_CAST "desc", "%s", ENCODE (rt->item[i]->desc));

      XMLPRINTF("\n    ");

      xmlTextWriterWriteFormatElement (writer, BAD_CAST "media_keywords", "%s", ENCODE (rt->item[i]->media_keywords));

      XMLPRINTF("\n    ");

      xmlTextWriterWriteFormatElement (writer, BAD_CAST "media_duration", "%ld", rt->item[i]->media_duration);

      XMLPRINTF("\n    ");

      xmlTextWriterWriteFormatElement (writer, BAD_CAST "media_image", "%s", rt->item[i]->media_image);

      XMLPRINTF("\n    ");

      xmlTextWriterWriteFormatElement (writer, BAD_CAST "event_start", "%ld", rt->item[i]->event_start);

      XMLPRINTF("\n    ");

      xmlTextWriterWriteFormatElement (writer, BAD_CAST "event_end", "%ld", rt->item[i]->event_end);

      XMLPRINTF("\n  ");

      xmlTextWriterEndElement (writer); // </item>

      hash_close (dl_url_h);
      hash_close (url_h);
      hash_close (title_h);
    }

  XMLPRINTF("\n");

  xmlTextWriterEndDocument (writer);  // </rsstool>

  xmlFreeTextWriter (writer);

  fputs ((const char *) buffer->content, rt->output_file);

  xmlBufferFree (buffer);

  if (items >= RSSMAXITEM)
    {
      char buf[MAXBUFSIZE];

      sprintf (buf, "can write only RSS feeds with up to %d items (was %d items)\n",
        RSSMAXITEM, items);
      rsstool_log (rt, buf);
    }

  return 0;
}
/**
 * testXmlwriterTree:
 * @file: the output file
 *
 * test the xmlWriter interface when writing to a subtree
 */
void
testXmlwriterTree(const char *file)
{
    int rc;
    xmlTextWriterPtr writer;
    xmlDocPtr doc;
    xmlNodePtr node;
    xmlChar *tmp;

    /* Create a new XML DOM tree, to which the XML document will be
     * written */
    doc = xmlNewDoc(BAD_CAST XML_DEFAULT_VERSION);
    if (doc == NULL) {
        printf
            ("testXmlwriterTree: Error creating the xml document tree\n");
        return;
    }

    /* Create a new XML node, to which the XML document will be
     * appended */
    node = xmlNewDocNode(doc, NULL, BAD_CAST "EXAMPLE", NULL);
    if (node == NULL) {
        printf("testXmlwriterTree: Error creating the xml node\n");
        return;
    }

    /* Make ELEMENT the root node of the tree */
    xmlDocSetRootElement(doc, node);

    /* Create a new XmlWriter for DOM tree, with no compression. */
    writer = xmlNewTextWriterTree(doc, node, 0);
    if (writer == NULL) {
        printf("testXmlwriterTree: Error creating the xml writer\n");
        return;
    }

    /* Start the document with the xml default for the version,
     * encoding ISO 8859-1 and the default for the standalone
     * declaration. */
    rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL);
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterStartDocument\n");
        return;
    }

    /* Write a comment as child of EXAMPLE.
     * Please observe, that the input to the xmlTextWriter functions
     * HAS to be in UTF-8, even if the output XML is encoded
     * in iso-8859-1 */
    tmp = ConvertInput("This is a comment with special chars: <äöü>",
                       MY_ENCODING);
    rc = xmlTextWriterWriteComment(writer, tmp);
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterWriteComment\n");
        return;
    }
    if (tmp != NULL) xmlFree(tmp);

    /* Start an element named "ORDER" as child of EXAMPLE. */
    rc = xmlTextWriterStartElement(writer, BAD_CAST "ORDER");
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterStartElement\n");
        return;
    }

    /* Add an attribute with name "version" and value "1.0" to ORDER. */
    rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "version",
                                     BAD_CAST "1.0");
    if (rc < 0) {
        printf
            ("testXmlwriterTree: Error at xmlTextWriterWriteAttribute\n");
        return;
    }

    /* Add an attribute with name "xml:lang" and value "de" to ORDER. */
    rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "xml:lang",
                                     BAD_CAST "de");
    if (rc < 0) {
        printf
            ("testXmlwriterTree: Error at xmlTextWriterWriteAttribute\n");
        return;
    }

    /* Write a comment as child of ORDER */
    tmp = ConvertInput("<äöü>", MY_ENCODING);
    rc = xmlTextWriterWriteFormatComment(writer,
			 "This is another comment with special chars: %s",
					  tmp);
    if (rc < 0) {
        printf
            ("testXmlwriterTree: Error at xmlTextWriterWriteFormatComment\n");
        return;
    }
    if (tmp != NULL) xmlFree(tmp);

    /* Start an element named "HEADER" as child of ORDER. */
    rc = xmlTextWriterStartElement(writer, BAD_CAST "HEADER");
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterStartElement\n");
        return;
    }

    /* Write an element named "X_ORDER_ID" as child of HEADER. */
    rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "X_ORDER_ID",
                                         "%010d", 53535);
    if (rc < 0) {
        printf
            ("testXmlwriterTree: Error at xmlTextWriterWriteFormatElement\n");
        return;
    }

    /* Write an element named "CUSTOMER_ID" as child of HEADER. */
    rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "CUSTOMER_ID",
                                         "%d", 1010);
    if (rc < 0) {
        printf
            ("testXmlwriterTree: Error at xmlTextWriterWriteFormatElement\n");
        return;
    }

    /* Write an element named "NAME_1" as child of HEADER. */
    tmp = ConvertInput("Müller", MY_ENCODING);
    rc = xmlTextWriterWriteElement(writer, BAD_CAST "NAME_1", tmp);
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterWriteElement\n");
        return;
    }
    if (tmp != NULL) xmlFree(tmp);

    /* Write an element named "NAME_2" as child of HEADER. */
    tmp = ConvertInput("Jörg", MY_ENCODING);
    rc = xmlTextWriterWriteElement(writer, BAD_CAST "NAME_2", tmp);
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterWriteElement\n");
        return;
    }
    if (tmp != NULL) xmlFree(tmp);

    /* Close the element named HEADER. */
    rc = xmlTextWriterEndElement(writer);
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterEndElement\n");
        return;
    }

    /* Start an element named "ENTRIES" as child of ORDER. */
    rc = xmlTextWriterStartElement(writer, BAD_CAST "ENTRIES");
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterStartElement\n");
        return;
    }

    /* Start an element named "ENTRY" as child of ENTRIES. */
    rc = xmlTextWriterStartElement(writer, BAD_CAST "ENTRY");
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterStartElement\n");
        return;
    }

    /* Write an element named "ARTICLE" as child of ENTRY. */
    rc = xmlTextWriterWriteElement(writer, BAD_CAST "ARTICLE",
                                   BAD_CAST "<Test>");
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterWriteElement\n");
        return;
    }

    /* Write an element named "ENTRY_NO" as child of ENTRY. */
    rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ENTRY_NO", "%d",
                                         10);
    if (rc < 0) {
        printf
            ("testXmlwriterTree: Error at xmlTextWriterWriteFormatElement\n");
        return;
    }

    /* Close the element named ENTRY. */
    rc = xmlTextWriterEndElement(writer);
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterEndElement\n");
        return;
    }

    /* Start an element named "ENTRY" as child of ENTRIES. */
    rc = xmlTextWriterStartElement(writer, BAD_CAST "ENTRY");
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterStartElement\n");
        return;
    }

    /* Write an element named "ARTICLE" as child of ENTRY. */
    rc = xmlTextWriterWriteElement(writer, BAD_CAST "ARTICLE",
                                   BAD_CAST "<Test 2>");
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterWriteElement\n");
        return;
    }

    /* Write an element named "ENTRY_NO" as child of ENTRY. */
    rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ENTRY_NO", "%d",
                                         20);
    if (rc < 0) {
        printf
            ("testXmlwriterTree: Error at xmlTextWriterWriteFormatElement\n");
        return;
    }

    /* Close the element named ENTRY. */
    rc = xmlTextWriterEndElement(writer);
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterEndElement\n");
        return;
    }

    /* Close the element named ENTRIES. */
    rc = xmlTextWriterEndElement(writer);
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterEndElement\n");
        return;
    }

    /* Start an element named "FOOTER" as child of ORDER. */
    rc = xmlTextWriterStartElement(writer, BAD_CAST "FOOTER");
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterStartElement\n");
        return;
    }

    /* Write an element named "TEXT" as child of FOOTER. */
    rc = xmlTextWriterWriteElement(writer, BAD_CAST "TEXT",
                                   BAD_CAST "This is a text.");
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterWriteElement\n");
        return;
    }

    /* Close the element named FOOTER. */
    rc = xmlTextWriterEndElement(writer);
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterEndElement\n");
        return;
    }

    /* Here we could close the elements ORDER and EXAMPLE using the
     * function xmlTextWriterEndElement, but since we do not want to
     * write any other elements, we simply call xmlTextWriterEndDocument,
     * which will do all the work. */
    rc = xmlTextWriterEndDocument(writer);
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterEndDocument\n");
        return;
    }

    xmlFreeTextWriter(writer);

    xmlSaveFileEnc(file, doc, MY_ENCODING);

    xmlFreeDoc(doc);
}
Exemplo n.º 12
0
static int
ephy_node_db_write_to_xml_valist (EphyNodeDb *db,
                                  const xmlChar *filename,
                                  const xmlChar *root,
                                  const xmlChar *version,
                                  const xmlChar *comment,
                                  EphyNode *first_node,
                                  va_list argptr)
{
    xmlTextWriterPtr writer;
    EphyNode *node;
    int ret;

    LOG ("Saving node db to %s", filename);

    START_PROFILER ("Saving node db")

    /* FIXME: do we want to turn compression on ? */
    writer = xmlNewTextWriterFilename ((const char *)filename, 0);
    if (writer == NULL)
    {
        return -1;
    }

    ret = xmlTextWriterSetIndent (writer, 1);
    if (ret < 0) goto out;

    ret = xmlTextWriterSetIndentString (writer, (const xmlChar *)"  ");
    if (ret < 0) goto out;

    ret = xmlTextWriterStartDocument (writer, "1.0", NULL, NULL);
    if (ret < 0) goto out;

    ret = xmlTextWriterStartElement (writer, root);
    if (ret < 0) goto out;

    ret = xmlTextWriterWriteAttribute (writer, (const xmlChar *)"version", version);
    if (ret < 0) goto out;

    if (comment != NULL)
    {
        ret = xmlTextWriterWriteComment (writer, comment);
        if (ret < 0) goto out;
    }

    node = first_node;
    while (node != NULL)
    {
        GPtrArray *children;
        EphyNodeFilterFunc filter;
        gpointer user_data;
        int i;

        filter = va_arg (argptr, EphyNodeFilterFunc);
        user_data = va_arg (argptr, gpointer);

        children = ephy_node_get_children (node);
        for (i = 0; i < children->len; i++)
        {
            EphyNode *kid;

            kid = g_ptr_array_index (children, i);

            if (!filter || filter (kid, user_data))
            {
                ret = ephy_node_write_to_xml (kid, writer);
                if (ret < 0) break;
            }
        }
        if (ret < 0) break;

        node = va_arg (argptr, EphyNode *);
    }
    if (ret < 0) goto out;

    ret = xmlTextWriterEndElement (writer); /* root */
    if (ret < 0) goto out;

    ret = xmlTextWriterEndDocument (writer);
    if (ret < 0) goto out;

out:
    xmlFreeTextWriter (writer);

    STOP_PROFILER ("Saving node db")

    return ret >= 0 ? 0 : -1;
}
Exemplo n.º 13
0
/**
 * testXmlwriterTree:
 * @file: the output file
 *
 * test the xmlWriter interface when writing to a subtree
 */
void
testXmlwriterTree(const char *file)
{
    int rc;
    xmlTextWriterPtr writer;
    xmlDocPtr doc;
    xmlNodePtr node;
    xmlChar *tmp;

    /* Create a new XML DOM tree, to which the XML document will be
     * written */
    doc = xmlNewDoc(BAD_CAST XML_DEFAULT_VERSION);
    if (doc == NULL) {
        printf
            ("testXmlwriterTree: Error creating the xml document tree\n");
        return;
    }

    /* Create a new XML node, to which the XML document will be
     * appended */
    node = xmlNewDocNode(doc, NULL, BAD_CAST "EXAMPLE", NULL);
    if (node == NULL) {
        printf("testXmlwriterTree: Error creating the xml node\n");
        return;
    }

    /* Make ELEMENT the root node of the tree */
    xmlDocSetRootElement(doc, node);

    /* Create a new XmlWriter for DOM tree, with no compression. */
    writer = xmlNewTextWriterTree(doc, node, 0);
    if (writer == NULL) {
        printf("testXmlwriterTree: Error creating the xml writer\n");
        return;
    }

    /* Start the document with the xml default for the version,
     * encoding ISO 8859-1 and the default for the standalone
     * declaration. */
    rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL);
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterStartDocument\n");
        return;
    }

    /* Write a comment as child of EXAMPLE.
     * Please observe, that the input to the xmlTextWriter functions
     * HAS to be in UTF-8, even if the output XML is encoded
     * in iso-8859-1 */
    tmp = ConvertInput("This is a comment with special chars: <äöü>",
                       MY_ENCODING);
    rc = xmlTextWriterWriteComment(writer, tmp);
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterWriteComment\n");
        return;
    }
    if (tmp != NULL) xmlFree(tmp);

    /* Start an element named "ORDER" as child of EXAMPLE. */
    rc = xmlTextWriterStartElement(writer, BAD_CAST "ORDER");
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterStartElement\n");
        return;
    }

    /* Add an attribute with name "version" and value "1.0" to ORDER. */
    rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "version",
                                     BAD_CAST "1.0");
    if (rc < 0) {
        printf
            ("testXmlwriterTree: Error at xmlTextWriterWriteAttribute\n");
        return;
    }

    /* Add an attribute with name "xml:lang" and value "de" to ORDER. */
    rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "xml:lang",
                                     BAD_CAST "de");
    if (rc < 0) {
        printf
            ("testXmlwriterTree: Error at xmlTextWriterWriteAttribute\n");
        return;
    }

    /* Write a comment as child of ORDER */
    tmp = ConvertInput("<äöü>", MY_ENCODING);
    rc = xmlTextWriterWriteFormatComment(writer,
			 "This is another comment with special chars: %s",
					  tmp);
    if (rc < 0) {
        printf
            ("testXmlwriterTree: Error at xmlTextWriterWriteFormatComment\n");
        return;
    }
    if (tmp != NULL) xmlFree(tmp);

    /* Start an element named "HEADER" as child of ORDER. */
    rc = xmlTextWriterStartElement(writer, BAD_CAST "HEADER");
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterStartElement\n");
        return;
    }

    /* Write an element named "X_ORDER_ID" as child of HEADER. */
    rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "X_ORDER_ID",
                                         "%010d", 53535L);
    if (rc < 0) {
        printf
            ("testXmlwriterTree: Error at xmlTextWriterWriteFormatElement\n");
        return;
    }

    /* Write an element named "CUSTOMER_ID" as child of HEADER. */
    rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "CUSTOMER_ID",
                                         "%d", 1010);
    if (rc < 0) {
        printf
            ("testXmlwriterTree: Error at xmlTextWriterWriteFormatElement\n");
        return;
    }

    /* Write an element named "NAME_1" as child of HEADER. */
    tmp = ConvertInput("Müller", MY_ENCODING);
    rc = xmlTextWriterWriteElement(writer, BAD_CAST "NAME_1", tmp);
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterWriteElement\n");
        return;
    }
    if (tmp != NULL) xmlFree(tmp);

    /* Write an element named "NAME_2" as child of HEADER. */
    tmp = ConvertInput("Jörg", MY_ENCODING);
    rc = xmlTextWriterWriteElement(writer, BAD_CAST "NAME_2", tmp);
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriter
Exemplo n.º 14
0
gboolean
analyzer_create_mpeg2video_frame_xml (GstMpegVideoMeta * mpeg_meta,
    gchar * location, gint frame_num, Mpeg2Headers * mpeg2_hdrs)
{
  xmlTextWriterPtr writer;
  xmlDocPtr doc;
  xmlBufferPtr buf;
  xmlChar *tmp;
  gchar *file_name;
  gchar *name;
  int fd, i;
  GstMpegVideoSequenceHdr *sequencehdr = NULL;
  GstMpegVideoSequenceExt *sequenceext = NULL;
  GstMpegVideoSequenceDisplayExt *sequencedispext = NULL;
  GstMpegVideoQuantMatrixExt *quantext = NULL;

  if (!mpeg_meta)
    return FALSE;

  xmlKeepBlanksDefault (0);

  writer = xmlNewTextWriterDoc (&doc, 0);
  if (!writer) {
    GST_ERROR ("Error: creating xml text writer \n");
    return FALSE;
  }

  if (xmlTextWriterStartDocument (writer, NULL, "UTF-8", NULL) < 0) {
    GST_ERROR ("Error: xmlTextWriterStartDocument");
    return FALSE;
  }

  if (xmlTextWriterStartElement (writer, (xmlChar *) "mpeg2") < 0) {
    GST_ERROR ("Error: Failed to start the new element (root element) mpeg2");
    return FALSE;
  }

  if (xmlTextWriterWriteComment (writer,
          (xmlChar *) "Data parssed from the mpeg2 stream") < 0) {
    g_error ("Error: Failed to write the comment \n");
    return FALSE;
  }

  /* Each time we save the gerneral headers, which will get appended for
     each frame xml files */

  /* SequenceHdr */
  if (mpeg_meta->sequencehdr) {
    sequencehdr = mpeg_meta->sequencehdr;

    if (mpeg2_hdrs->sequencehdr)
      g_slice_free (GstMpegVideoSequenceHdr, mpeg2_hdrs->sequencehdr);
    mpeg2_hdrs->sequencehdr =
        g_slice_dup (GstMpegVideoSequenceHdr, mpeg_meta->sequencehdr);

  } else if (mpeg2_hdrs->sequencehdr)
    sequencehdr = mpeg2_hdrs->sequencehdr;

  /* SequenceExtHdr */
  if (mpeg_meta->sequenceext) {
    sequenceext = mpeg_meta->sequenceext;

    if (mpeg2_hdrs->sequenceext)
      g_slice_free (GstMpegVideoSequenceExt, mpeg2_hdrs->sequenceext);
    mpeg2_hdrs->sequenceext =
        g_slice_dup (GstMpegVideoSequenceExt, mpeg_meta->sequenceext);

  } else if (mpeg2_hdrs->sequenceext)
    sequenceext = mpeg2_hdrs->sequenceext;

  /* SequenceDisplayExt */
  if (mpeg_meta->sequencedispext) {
    sequencedispext = mpeg_meta->sequencedispext;

    if (mpeg2_hdrs->sequencedispext)
      g_slice_free (GstMpegVideoSequenceDisplayExt,
          mpeg2_hdrs->sequencedispext);
    mpeg2_hdrs->sequencedispext =
        g_slice_dup (GstMpegVideoSequenceDisplayExt,
        mpeg_meta->sequencedispext);

  } else if (mpeg2_hdrs->sequencedispext)
    sequencedispext = mpeg2_hdrs->sequencedispext;

  /* QuantMatrixExt */
  if (mpeg_meta->quantext) {
    quantext = mpeg_meta->quantext;

    if (mpeg2_hdrs->quantext)
      g_slice_free (GstMpegVideoQuantMatrixExt, mpeg2_hdrs->quantext);
    mpeg2_hdrs->quantext =
        g_slice_dup (GstMpegVideoQuantMatrixExt, mpeg_meta->quantext);

  } else if (mpeg2_hdrs->quantext)
    quantext = mpeg2_hdrs->quantext;

  /*Create xmls for each headers */

  if (sequencehdr)
    if (!create_seq_hdr_xml (writer, sequencehdr))
      return FALSE;

  if (sequenceext) {
    if (!create_seq_ext_xml (writer, sequenceext))
      return FALSE;
  }

  if (mpeg_meta->sequencedispext) {
    if (!create_seq_disp_ext_xml (writer, sequencedispext))
      return FALSE;
  }

  if (quantext) {
    if (!create_quant_ext_xml (writer, quantext))
      return FALSE;
  }
#if 0
  if (mpeg_meta->gophdr) {
    if (!create_gop_hdr_xml (writer, mpeg_meta->gophdr))
      return FALSE;
  }
#endif
  if (mpeg_meta->pichdr) {
    if (!create_pic_hdr_xml (writer, mpeg_meta->pichdr))
      return FALSE;
  }

  if (mpeg_meta->picext) {
    if (!create_pic_ext_xml (writer, mpeg_meta->picext))
      return FALSE;
  }
#if 0
  if (mpeg_meta->slice_info_array) {
    for (i = 0; i < mpeg_meta->slice_info_array->len; i++) {
      GstMpegVideoMetaSliceInfo *slice_info = NULL;
      slice_info =
          &g_array_index (mpeg_meta->slice_info_array,
          GstMpegVideoMetaSliceInfo, i);
      if (!slice_info) {
        g_error ("Failed to get slice details from meta.. \n");
        return FALSE;
      }
      if (!create_slice_hdr_xml (writer, slice_info, i))
        return FALSE;
    }
  }
#endif
  if (xmlTextWriterEndElement (writer) < 0) {
    g_error ("Error: Failed to end mpeg2 root element \n");
    return FALSE;
  }

  if (xmlTextWriterEndDocument (writer) < 0) {
    g_error ("Error: Ending document \n");
    return FALSE;
  }

  xmlFreeTextWriter (writer);

  /* create a new xml file for each frame */
  name = g_strdup_printf ("mpeg2-%d.xml", frame_num);
  file_name = g_build_filename (location, "xml", name, NULL);
  GST_LOG ("Created a New xml file %s to dump the parsed info", file_name);

  xmlSaveFormatFile (file_name, doc, 1);

  if (name)
    g_free (name);
  if (file_name)
    g_free (file_name);

  return TRUE;
}
Exemplo n.º 15
0
/*
 * Preference Management : save preferences into '.gdisp+' XML file
 * located in the HOME directory of the user.
 */
void
gdisp_savePreferenceFile ( Kernel_T *kernel )
{

#if defined(XMLWRITER_SUPPORTED)

  int               errorCode      = 0;
  xmlTextWriterPtr  writer         = (xmlTextWriterPtr)NULL;
  char             *attributeValue = NULL;
  xmlChar          *tmp            = (xmlChar*)NULL;
  xmlChar           indentBuffer    [256];
  char              completeFilename[256];
  char              stringValue     [256];

  /*
   * Caution.
   */
  indentBuffer[0] = '\0';
  if (g_get_home_dir() == (char*)NULL) {
    /* No need to print any error message, because gdisp+ is exiting */
    return;
  }
  sprintf(completeFilename,
	  "%s%c.targa",
	  g_get_home_dir(),
	  G_DIR_SEPARATOR);

  /*
   * This initialize the library and check potential ABI mismatches
   * between the version it was compiled for and the actual shared
   * library used.
   */
  LIBXML_TEST_VERSION

  /*
   * Create a new XmlWriter for '.targa', with no compression.
   */
  writer = xmlNewTextWriterFilename(completeFilename,
				    GD_NO_COMPRESSION);

  if (writer == (xmlTextWriterPtr)NULL) {
    /* No need to print any error message, because gdisp+ is exiting */
    return;
  }

  /*
   * Start the document with the xml default for the version,
   * encoding ISO 8859-1 and the default for the standalone
   * declaration.
   */
  errorCode = xmlTextWriterStartDocument(writer,
					 (const char*)NULL, /* version */
					 GD_PREFERENCE_ENCODING,
					 (const char*)NULL); /* standalone */
  if (errorCode < 0) {
    /* No need to print any error message, because gdisp+ is exiting */
    return;
  }

  /*
   * Start an element named "gdisp+".
   * Since thist is the first element, this will be the root element
   * of the document.
   */
  errorCode = xmlTextWriterStartElement(writer,
					(xmlChar*)"Targa");

  if (errorCode < 0) {
    /* No need to print any error message, because gdisp+ is exiting */
    return;
  }

  /*
   * Goto line and ident.
   */
  gdisp_xmlGotoLine(writer);
  gdisp_xmlIndent  (writer,
		    indentBuffer,
		    GD_INCREASE_INDENTATION);

  /*
   * Write a comment as child of the root element.
   * Please observe, that the input to the xmlTextWriter functions
   * HAS to be in UTF-8, even if the output XML is encoded
   * in iso-8859-1.
   */
  tmp = gdisp_xmlConvertInput("Targa General Preferences",
			      GD_PREFERENCE_ENCODING);

  errorCode = xmlTextWriterWriteComment(writer, tmp);
  if (errorCode < 0) {
    /* No need to print any error message, because gdisp+ is exiting */
    return;
  }
  if (tmp != (xmlChar*)NULL) {
    xmlFree(tmp);
  }

  /*
   * Goto line.
   */
  gdisp_xmlGotoLine(writer);
  gdisp_xmlIndent  (writer,
		    indentBuffer,
		    GD_DO_NOT_CHANGE_INDENTATION);

  /*
   * Start an element named 'Kernel' as child of root.
   */
  errorCode = xmlTextWriterStartElement(writer,
					(xmlChar*)"Kernel");
  if (errorCode < 0) {
    /* No need to print any error message, because gdisp+ is exiting */
    return;
  }

  /*
   * Goto line and ident.
   */
  gdisp_xmlGotoLine(writer);
  gdisp_xmlIndent  (writer,
		    indentBuffer,
		    GD_INCREASE_INDENTATION);

  /*
   * Start an element named 'Preferences' as child of 'Kernel'.
   */
  errorCode = xmlTextWriterStartElement(writer,
					(xmlChar*)"Preferences");
  if (errorCode < 0) {
    /* No need to print any error message, because gdisp+ is exiting */
    return;
  }

  /*
   * Preferences to be saved :
   *  kernel->pathToGraphicModules
   *  kernel->sortingMethod
   *  kernel->sortingDirection
   *  kernel->dndScope
   */

  /*
   * Path to graphic modules.
   */
  attributeValue = kernel->pathToGraphicModules;
  if (attributeValue == NULL) {
    attributeValue = "unknown";
  }
  errorCode      = gdisp_xmlWriteAttributes(writer,
					    GD_INCREASE_INDENTATION,
					    indentBuffer,
					    (xmlChar*)"preference",
					    TRUE, /* end up element */
					    (xmlChar*)"pathToGraphicModules",
					    (xmlChar*)attributeValue,
					    (xmlChar*)NULL);

  if (errorCode < 0) {
    /* No need to print any error message, because gdisp+ is exiting */
    return;
  }

  /*
   * Sorting Method.
   */
  attributeValue = gdisp_sortingMethodToString(kernel);
  errorCode      = gdisp_xmlWriteAttributes(writer,
					    GD_DO_NOT_CHANGE_INDENTATION,
					    indentBuffer,
					    (xmlChar*)"preference",
					    TRUE, /* end up element */
					    (xmlChar*)"sortingMethod",
					    (xmlChar*)attributeValue,
					    (xmlChar*)NULL);

  if (errorCode < 0) {
    /* No need to print any error message, because gdisp+ is exiting */
    return;
  }

  /*
   * Sorting Direction.
   */
  attributeValue = gdisp_sortingDirectionToString(kernel);
  errorCode      = gdisp_xmlWriteAttributes(writer,
					    GD_DO_NOT_CHANGE_INDENTATION,
					    indentBuffer,
					    (xmlChar*)"preference",
					    TRUE, /* end up element */
					    (xmlChar*)"sortingDirection",
					    (xmlChar*)attributeValue,
					    (xmlChar*)NULL);

  if (errorCode < 0) {
    /* No need to print any error message, because gdisp+ is exiting */
    return;
  }

  /*
   * DND Scope.
   */
  attributeValue = gdisp_dndScopeToString(kernel);
  errorCode      = gdisp_xmlWriteAttributes(writer,
					    GD_DO_NOT_CHANGE_INDENTATION,
					    indentBuffer,
					    (xmlChar*)"preference",
					    TRUE, /* end up element */
					    (xmlChar*)"dndScope",
					    (xmlChar*)attributeValue,
					    (xmlChar*)NULL);

  if (errorCode < 0) {
    /* No need to print any error message, because gdisp+ is exiting */
    return;
  }


  /*
   * Main board position on the screen.
   */
  sprintf(stringValue,
	  "%d,%d",
	  kernel->widgets.mainBoardWindowXPosition,
	  kernel->widgets.mainBoardWindowYPosition);
  errorCode = gdisp_xmlWriteAttributes(writer,
				       GD_DO_NOT_CHANGE_INDENTATION,
				       indentBuffer,
				       (xmlChar*)"preference",
				       TRUE, /* end up element */
				       (xmlChar*)"mainBoardPosition",
				       (xmlChar*)stringValue,
				       (xmlChar*)NULL);

  if (errorCode < 0) {
    /* No need to print any error message, because gdisp+ is exiting */
    return;
  }


  /*
   * Data window position on the screen.
   */
  sprintf(stringValue,
	  "%d,%d",
	  kernel->widgets.dataBookWindowXPosition,
	  kernel->widgets.dataBookWindowYPosition);
  errorCode = gdisp_xmlWriteAttributes(writer,
				       GD_DO_NOT_CHANGE_INDENTATION,
				       indentBuffer,
				       (xmlChar*)"preference",
				       TRUE, /* end up element */
				       (xmlChar*)"dataBookPosition",
				       (xmlChar*)stringValue,
				       (xmlChar*)NULL);

  if (errorCode < 0) {
    /* No need to print any error message, because gdisp+ is exiting */
    return;
  }


  /*
   * Host window position on the screen.
   */
  sprintf(stringValue,
	  "%d,%d",
	  kernel->widgets.hostWindowXPosition,
	  kernel->widgets.hostWindowYPosition);
  errorCode = gdisp_xmlWriteAttributes(writer,
				       GD_DO_NOT_CHANGE_INDENTATION,
				       indentBuffer,
				       (xmlChar*)"preference",
				       TRUE, /* end up element */
				       (xmlChar*)"hostPosition",
				       (xmlChar*)stringValue,
				       (xmlChar*)NULL);

  if (errorCode < 0) {
    /* No need to print any error message, because gdisp+ is exiting */
    return;
  }


  /*
   * Close 'Preferences' element.
   */
  gdisp_xmlGotoLine(writer);
  gdisp_xmlIndent  (writer,
		    indentBuffer,
		    GD_DECREASE_INDENTATION);

  errorCode = xmlTextWriterEndElement(writer);

  if (errorCode < 0) {
    /* No need to print any error message, because gdisp+ is exiting */
    return;
  }

  /*
   * Close 'Kernel' element.
   */
  gdisp_xmlGotoLine(writer);
  gdisp_xmlIndent  (writer,
		    indentBuffer,
		    GD_DECREASE_INDENTATION);

  errorCode = xmlTextWriterEndElement(writer);

  if (errorCode < 0) {
    /* No need to print any error message, because gdisp+ is exiting */
    return;
  }


  /*
   * Close 'GDisp' element.
   */
  gdisp_xmlGotoLine(writer);
  gdisp_xmlIndent  (writer,
		    indentBuffer,
		    GD_DECREASE_INDENTATION);

  errorCode = xmlTextWriterEndElement(writer);

  if (errorCode < 0) {
    /* No need to print any error message, because gdisp+ is exiting */
    return;
  }

  /*
   * Goto line.
   */
  gdisp_xmlGotoLine(writer);

  /*
   * Free text writer.
   */
  xmlFreeTextWriter(writer);

  /*
   * Cleanup function for the XML library.
   */
  xmlCleanupParser();

#endif

}
Exemplo n.º 16
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;
    }
}
Exemplo n.º 17
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;
}
/**
 * testXmlwriterFilename:
 * @uri: the output URI
 *
 * test the xmlWriter interface when writing to a new file
 */
void
testXmlwriterFilename(const char *uri)
{
    int rc;
    xmlTextWriterPtr writer;
    xmlChar *tmp;

    /* Create a new XmlWriter for uri, with no compression. */
    writer = xmlNewTextWriterFilename(uri, 0);
    if (writer == NULL) {
        printf("testXmlwriterFilename: Error creating the xml writer\n");
        return;
    }

    /* Start the document with the xml default for the version,
     * encoding ISO 8859-1 and the default for the standalone
     * declaration. */
    rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL);
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterStartDocument\n");
        return;
    }

    /* Start an element named "EXAMPLE". Since thist is the first
     * element, this will be the root element of the document. */
    rc = xmlTextWriterStartElement(writer, BAD_CAST "EXAMPLE");
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterStartElement\n");
        return;
    }

    /* Write a comment as child of EXAMPLE.
     * Please observe, that the input to the xmlTextWriter functions
     * HAS to be in UTF-8, even if the output XML is encoded
     * in iso-8859-1 */
    tmp = ConvertInput("This is a comment with special chars: <äöü>",
                       MY_ENCODING);
    rc = xmlTextWriterWriteComment(writer, tmp);
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterWriteComment\n");
        return;
    }
    if (tmp != NULL) xmlFree(tmp);

    /* Start an element named "ORDER" as child of EXAMPLE. */
    rc = xmlTextWriterStartElement(writer, BAD_CAST "ORDER");
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterStartElement\n");
        return;
    }

    /* Add an attribute with name "version" and value "1.0" to ORDER. */
    rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "version",
                                     BAD_CAST "1.0");
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterWriteAttribute\n");
        return;
    }

    /* Add an attribute with name "xml:lang" and value "de" to ORDER. */
    rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "xml:lang",
                                     BAD_CAST "de");
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterWriteAttribute\n");
        return;
    }

    /* Write a comment as child of ORDER */
    tmp = ConvertInput("<äöü>", MY_ENCODING);
    rc = xmlTextWriterWriteFormatComment(writer,
		     "This is another comment with special chars: %s",
		     tmp);
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterWriteFormatComment\n");
        return;
    }
    if (tmp != NULL) xmlFree(tmp);

    /* Start an element named "HEADER" as child of ORDER. */
    rc = xmlTextWriterStartElement(writer, BAD_CAST "HEADER");
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterStartElement\n");
        return;
    }

    /* Write an element named "X_ORDER_ID" as child of HEADER. */
    rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "X_ORDER_ID",
                                         "%010d", 53535);
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
        return;
    }

    /* Write an element named "CUSTOMER_ID" as child of HEADER. */
    rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "CUSTOMER_ID",
                                         "%d", 1010);
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
        return;
    }

    /* Write an element named "NAME_1" as child of HEADER. */
    tmp = ConvertInput("Müller", MY_ENCODING);
    rc = xmlTextWriterWriteElement(writer, BAD_CAST "NAME_1", tmp);
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterWriteElement\n");
        return;
    }
    if (tmp != NULL) xmlFree(tmp);

    /* Write an element named "NAME_2" as child of HEADER. */
    tmp = ConvertInput("Jörg", MY_ENCODING);
    rc = xmlTextWriterWriteElement(writer, BAD_CAST "NAME_2", tmp);
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterWriteElement\n");
        return;
    }
    if (tmp != NULL) xmlFree(tmp);

    /* Close the element named HEADER. */
    rc = xmlTextWriterEndElement(writer);
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
        return;
    }

    /* Start an element named "ENTRIES" as child of ORDER. */
    rc = xmlTextWriterStartElement(writer, BAD_CAST "ENTRIES");
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterStartElement\n");
        return;
    }

    /* Start an element named "ENTRY" as child of ENTRIES. */
    rc = xmlTextWriterStartElement(writer, BAD_CAST "ENTRY");
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterStartElement\n");
        return;
    }

    /* Write an element named "ARTICLE" as child of ENTRY. */
    rc = xmlTextWriterWriteElement(writer, BAD_CAST "ARTICLE",
                                   BAD_CAST "<Test>");
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterWriteElement\n");
        return;
    }

    /* Write an element named "ENTRY_NO" as child of ENTRY. */
    rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ENTRY_NO", "%d",
                                         10);
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
        return;
    }

    /* Close the element named ENTRY. */
    rc = xmlTextWriterEndElement(writer);
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
        return;
    }

    /* Start an element named "ENTRY" as child of ENTRIES. */
    rc = xmlTextWriterStartElement(writer, BAD_CAST "ENTRY");
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterStartElement\n");
        return;
    }

    /* Write an element named "ARTICLE" as child of ENTRY. */
    rc = xmlTextWriterWriteElement(writer, BAD_CAST "ARTICLE",
                                   BAD_CAST "<Test 2>");
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterWriteElement\n");
        return;
    }

    /* Write an element named "ENTRY_NO" as child of ENTRY. */
    rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ENTRY_NO", "%d",
                                         20);
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
        return;
    }

    /* Close the element named ENTRY. */
    rc = xmlTextWriterEndElement(writer);
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
        return;
    }

    /* Close the element named ENTRIES. */
    rc = xmlTextWriterEndElement(writer);
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
        return;
    }

    /* Start an element named "FOOTER" as child of ORDER. */
    rc = xmlTextWriterStartElement(writer, BAD_CAST "FOOTER");
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterStartElement\n");
        return;
    }

    /* Write an element named "TEXT" as child of FOOTER. */
    rc = xmlTextWriterWriteElement(writer, BAD_CAST "TEXT",
                                   BAD_CAST "This is a text.");
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterWriteElement\n");
        return;
    }

    /* Close the element named FOOTER. */
    rc = xmlTextWriterEndElement(writer);
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
        return;
    }

    /* Here we could close the elements ORDER and EXAMPLE using the
     * function xmlTextWriterEndElement, but since we do not want to
     * write any other elements, we simply call xmlTextWriterEndDocument,
     * which will do all the work. */
    rc = xmlTextWriterEndDocument(writer);
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterEndDocument\n");
        return;
    }

    xmlFreeTextWriter(writer);
}