/** * Creates a new element node and sets it as the root element. This will * only work if the document is empty; otherwise EDocumentNotEmpty is thrown. */ ElementNode* Document::createRootElement(const char *pcszRootElementName, const char *pcszComment /* = NULL */) { if (m->pRootElement || m->plibDocument) throw EDocumentNotEmpty(RT_SRC_POS); // libxml side: create document, create root node m->plibDocument = xmlNewDoc((const xmlChar*)"1.0"); xmlNode *plibRootNode; if (!(plibRootNode = xmlNewNode(NULL, // namespace (const xmlChar*)pcszRootElementName))) throw std::bad_alloc(); xmlDocSetRootElement(m->plibDocument, plibRootNode); // now wrap this in C++ m->pRootElement = new ElementNode(NULL, NULL, plibRootNode); // add document global comment if specified if (pcszComment != NULL) { xmlNode *pComment; if (!(pComment = xmlNewDocComment(m->plibDocument, (const xmlChar *)pcszComment))) throw std::bad_alloc(); xmlAddPrevSibling(plibRootNode, pComment); // now wrap this in C++ m->pComment = new ElementNode(NULL, NULL, pComment); } return m->pRootElement; }
xmlNode* child_node_list::create_comment_(const std::string& value) { xmlNode* px = xmlNewDocComment(raw_->doc, detail::to_xml_chars(value.c_str())); if (px == 0) { std::string what = "fail to create libxml2 comment node for " + value; throw internal_dom_error(what); } else { return px; } }
* * Create a new Comment element on the +document+ with +content+ */ static VALUE new(int argc, VALUE *argv, VALUE klass) { xmlDocPtr xml_doc; VALUE document; VALUE content; VALUE rest; rb_scan_args(argc, argv, "2*", &document, &content, &rest); Data_Get_Struct(document, xmlDoc, xml_doc); xmlNodePtr node = xmlNewDocComment( xml_doc, (const xmlChar *)StringValuePtr(content) ); VALUE rb_node = Nokogiri_wrap_xml_node(klass, node); rb_funcall2(rb_node, rb_intern("initialize"), argc, argv); NOKOGIRI_ROOT_NODE(node); if(rb_block_given_p()) rb_yield(rb_node); return rb_node; } VALUE cNokogiriXmlComment; void init_xml_comment() {
xupl* xupl_parse(xupl *xup) { xupl_*_ = (xupl_*) xup; FILE* in = _->in; off_t buffsize = _->buffsize; unsigned short bit = 0x0001; unsigned short WHITESPACE = bit; unsigned short DOUBLE_STRING = (bit <<= 1); unsigned short SINGLE_STRING = (bit <<= 1); unsigned short STRING = DOUBLE_STRING | SINGLE_STRING; unsigned short LINE_COMMENT = (bit <<= 1); unsigned short MULTI_COMMENT = (bit <<= 1); unsigned short COMMENT = LINE_COMMENT | MULTI_COMMENT; unsigned short _state = 0; const int default_tksize = 12; int tksize = default_tksize + 1; unsigned char *tk = NULL; int tkndx = 0; int chars_read; char* buf = malloc(buffsize + 1); xmlNodePtr xroot = NULL; xmlNodePtr xc = NULL; xmlAttrPtr xprop = NULL; const xmlChar* xuplAttr = (const xmlChar*) "data-xupl"; const xmlChar* xuplClosed = (const xmlChar*) "closed"; xmlDocPtr xdoc = xmlNewDoc((const unsigned char*) "1.1"); //xmlNsPtr xuplNs = xmlNewGlobalNs(xdoc,"http://ns.xupl.org/1.1","xupl"); while ((chars_read = fread(buf, 1, buffsize, in)) > 0) { for (int i = 0; i < chars_read; i++) { const char c = buf[i]; switch (c) { case '\'': case '"': IF(COMMENT) break; IF(STR(c)) { DISABLE(STR(c)); } else if (NOT(STRING)) { ALLOW(STR(c)); break; } case '{': case '}': case ' ': case '\n': case '\r': case '\t': case '\f': case '\v': case ',': case '=': // Comment characters case '*': case '/': case '#': case '!': IF(STRING) break; switch (c) { case ',': case '{': xprop = NULL; } if (tk) { tk[tkndx] = 0; char p = 0; if (tkndx >= 1) p = tk[tkndx - 1]; unsigned char* m = NULL; unsigned int tklen = tkndx + 1; unsigned char* t = tk; IF(COMMENT) { if ('\n' == c && IS(LINE_COMMENT)) { if (tkndx + 1 < tksize) { tk[tkndx++] = ' '; tk[tkndx] = 0; } } else if ('*' == p && '/' == c && IS(MULTI_COMMENT)) { tk[tkndx - 1] = 0; } else break; DISABLE(COMMENT); m = tk + 2; } else if ('/' == p && '*' == c) { ALLOW(MULTI_COMMENT); break; // Single-line comments can be #! #* #/ ## } else if ('#' == p && ('!' == c || '*' == c || '/' == c || '#' == c)) { ALLOW(LINE_COMMENT); break; // If these characters were in the token and not part of a comment, // then continue as if they are normal characters of a token. } else if ('!' == c || '/' == c || '*' == c || '#' == c) break; if (!xc) { if (m) { xroot = xmlNewDocComment(xdoc, m); xmlDocSetRootElement(xdoc, xroot); } else { xc = xmlNewNode(NULL, tk); xmlDocSetRootElement(xdoc, xc); if (!xroot) xroot = xc; } } else if (m) { xmlAddChild(xc, xmlNewComment(m)); } else { char *attr = NULL; xmlAttrPtr closed = xmlHasProp(xc, xuplAttr); switch (tk[0]) { case '\'': case '"': t += 1; xmlAddChild(xc, xmlNewText(t)); break; // TODO make this parameterized, characters and names. case '.': attr = "class"; break; case '#': attr = "id"; break; case '@': attr = "project"; break; case '/': attr = "href"; break; case '[': attr = "data"; break; case '~': attr = "duration"; break; case '=': attr = "location"; break; case '^': attr = "at"; break; case ':': attr = "type"; break; case '!': attr = "priority"; break; default: { regmatch_t pmatch[1]; unsigned int isword = 0 == regexec(&re_word, (char*) tk, 1, pmatch, 0); if (closed) { if (isword) { xc = xmlNewChild(xc, NULL, tk, NULL); } else { xmlAddChild(xc, xmlNewText(tk)); } } else { if (xprop) { xmlNewProp(xc, xprop->name, tk); xmlRemoveProp(xprop); xprop = NULL; } else if (isword) { xprop = xmlNewProp(xc, tk, (unsigned char*) "True"); } else { xprop = xmlNewProp(xc, (unsigned char*) ".fake", tk); } switch (c) { case ',': case '{': xprop = NULL; } } } break; } if (attr) { if (closed) { xmlAddChild(xc, xmlNewText(t)); } else { xmlNewProp(xc, (xmlChar*) attr, t); } } } free(tk); tk = NULL; if (tksize > default_tksize && tkndx < default_tksize) { tksize /= 2; } tkndx = 0; if (m) continue; } default: break; } switch (c) { case '{': xmlNewProp(xc, xuplAttr, xuplClosed); continue; case '}': if (xc) { xmlAttrPtr data = xmlHasProp(xc, xuplAttr); if (data) xmlRemoveProp(data); xc = xc->parent; } continue; default: break; } // Accumulate the tk. if (!tk || tkndx >= tksize) { // If the tk buffer is too small, double it. tk = realloc(tk, tksize *= 2); } tk[tkndx++] = c; }
/** * gstyle_palette_save_to_xml: * @self: a #GstylePalette * @file: a #GFile * @error: (nullable): a #GError location or %NULL * * Save a palette to Gnome-Builder .xml palette format. * * Returns: %TRUE if succesfull, %FALSE otherwise. */ gboolean gstyle_palette_save_to_xml (GstylePalette *self, GFile *file, GError **error) { g_autofree gchar *file_path = NULL; const gchar *id; const gchar *name; xmlDocPtr doc; xmlNodePtr root_node; xmlNodePtr palette_node; xmlNodePtr color_node; gint n_colors; gint succes; gchar *header = "Copyright (C) 2016 GNOME Builder Team at irc.gimp.net/#gnome-builder\n" \ "This program is free software: you can redistribute it and/or modify\n" \ "it under the terms of the GNU General Public License as published by\n" \ "the Free Software Foundation, either version 3 of the License, or\n" \ "(at your option) any later version.\n\n" \ "This program is distributed in the hope that it will be useful,\n" \ "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" \ "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" \ "GNU General Public License for more details.\n\n" \ "You should have received a copy of the GNU General Public License\n" \ "along with this program. If not, see <http://www.gnu.org/licenses/>\n"; g_return_val_if_fail (GSTYLE_IS_PALETTE (self), FALSE); g_return_val_if_fail (G_IS_FILE (file), FALSE); doc = xmlNewDoc(CHAR_TO_XML ("1.0")); root_node = xmlNewDocComment (doc, CHAR_TO_XML (header)); xmlDocSetRootElement(doc, root_node); palette_node = xmlNewNode (NULL, CHAR_TO_XML ("palette")); xmlAddSibling (root_node, palette_node); id = gstyle_palette_get_id (self); name = gstyle_palette_get_name (self); xmlNewProp (palette_node, CHAR_TO_XML ("id"), CHAR_TO_XML (id)); if (self->gettext_domain) { xmlNewProp (palette_node, CHAR_TO_XML ("_name"), CHAR_TO_XML (name)); xmlNewProp (palette_node, CHAR_TO_XML ("gettext-domain"), CHAR_TO_XML (self->gettext_domain)); } else xmlNewProp (palette_node, CHAR_TO_XML ("name"), CHAR_TO_XML (name)); n_colors = gstyle_palette_get_len (self); for (gint i = 0; i < n_colors; ++i) { const GstyleColor *color; const gchar *color_name; g_autofree gchar *color_string = NULL; color = gstyle_palette_get_color_at_index (self, i); color_name = gstyle_color_get_name ((GstyleColor *)color); if (gstyle_color_get_kind ((GstyleColor *)color) == GSTYLE_COLOR_KIND_PREDEFINED) color_string = gstyle_color_to_string ((GstyleColor *)color, GSTYLE_COLOR_KIND_RGB_HEX6); else color_string = gstyle_color_to_string ((GstyleColor *)color, GSTYLE_COLOR_KIND_ORIGINAL); color_node = xmlNewChild (palette_node, NULL, CHAR_TO_XML ("color"), NULL); xmlNewProp (color_node, CHAR_TO_XML ("name"), CHAR_TO_XML (color_name)); xmlNewProp (color_node, CHAR_TO_XML ("value"), CHAR_TO_XML (color_string)); } file_path = g_file_get_path (file); succes = xmlSaveFormatFileEnc (file_path, doc, "UTF-8", 1); xmlFreeDoc(doc); if (succes == -1) { g_set_error (error, GSTYLE_PALETTE_ERROR, GSTYLE_PALETTE_ERROR_FILE, _("Unable to save %s\n"), file_path); return FALSE; } else { gstyle_palette_set_changed (self, FALSE); return TRUE; } }
Node * Document::AddComment(const string &comment, bool beforeRoot) { return AddNode(Wrapped<Node, _xmlNode>(xmlNewDocComment(xml(), comment.utf8())), beforeRoot); }