14#include "arcane/utils/ArcanePrecomp.h"
16#include "arcane/utils/Array.h"
17#include "arcane/utils/Iostream.h"
18#include "arcane/utils/Iterator.h"
19#include "arcane/utils/StdHeader.h"
20#include "arcane/utils/String.h"
21#include "arcane/utils/StringBuilder.h"
22#include "arcane/utils/NotImplementedException.h"
23#include "arcane/utils/FatalErrorException.h"
25#include "arcane/Dom.h"
26#include "arcane/DomUtils.h"
27#include "arcane/XmlNode.h"
28#include "arcane/IXmlDocumentHolder.h"
37ARCANE_BEGIN_NAMESPACE_DOMUTILS
39extern "C++" ARCANE_CORE_EXPORT
void
40removeAllChildren(
const dom::Node& parent);
42extern "C++" ARCANE_CORE_EXPORT
bool
43writeNode(std::ostream& ostr,
const dom::Node&);
45extern "C++" ARCANE_CORE_EXPORT
bool
46writeNodeChildren(std::ostream& ostr,
const dom::Node&);
48ARCANE_END_NAMESPACE_DOMUTILS
60_notImplemented(
const char* reason)
62 cerr <<
"* DOMUTILS NOT YET IMPLEMENTED: " << reason <<
'\n';
63 throw dom::DOMException(dom::NOT_IMPLEMENTED_ERR);
70_writeNodeChildren(std::ostream& o,
const dom::Node& node)
73 dom::Node next = node.firstChild();
74 while (!next._null()){
75 domutils::writeNode(o,next);
76 next = next.nextSibling();
86textContent(
const dom::Node& node)
91 return str.toString();
93 if (node.nodeType()!=Node::ELEMENT_NODE)
94 ARCANE_THROW(NotImplementedException,
"get text value for non ELEMENT_NODE");
95 for( Node i=node.firstChild(); !i._null(); i=i.nextSibling() ){
96 UShort ntype = i.nodeType();
97 if (ntype==Node::TEXT_NODE)
99 else if (ntype==Node::CDATA_SECTION_NODE){
100 str += i.nodeValue();
102 else if (ntype==Node::ENTITY_REFERENCE_NODE){
103 ARCANE_THROW(NotImplementedException,
"get text value for non ENTITY_REFERENCE_NODE");
106 return str.toString();
113textContent(dom::Node& node,
const String& new_value)
119 if (node.nodeType()!=Node::ELEMENT_NODE)
120 ARCANE_THROW(NotImplementedException,
"set text value for non ELEMENT_NODE");
121 removeAllChildren(node);
122 if (!new_value.null()){
123 Text text_node = node.ownerDocument().createTextNode(new_value);
124 node.appendChild(text_node);
132textValue(
const dom::Node& node)
134 return domutils::textContent(node);
141textValue(dom::Node& node,
const String& new_value)
143 textContent(node,new_value);
149dom::Element domutils::
150createElement(
const dom::Node& parent,
const String& name,
const String& value)
153 return dom::Element();
154 dom::Document doc = parent.ownerDocument();
156 return dom::Element();
157 dom::Element elem = doc.createElement(name);
158 textContent(elem,value);
159 parent.appendChild(elem);
167attrValue(
const dom::Node& node,
const String& attr_name)
174 const dom::NamedNodeMap& attr = node.attributes();
177 const dom::Node& n = attr.getNamedItem(attr_name);
185 dom::Element element{node};
186 String str2 = element.getAttribute(attr_name);
188 ARCANE_FATAL(
"Bad new value for attribute '{0}' new={1} current={2}",attr_name,str2,str);
199setAttr(
const dom::Element& elem,
const String& name,
const String& value)
203 elem.setAttribute(name,value);
210childNode(
const dom::Node& parent,
const String& child_name)
212 dom::DOMString ref_name(child_name);
213 for( dom::Node i=parent.firstChild(); !i._null(); i=i.nextSibling() ){
214 if (i.nodeName()==ref_name)
224removeAllChildren(
const dom::Node& parent)
230 dom::Node n = parent.firstChild();
232 parent.removeChild(n);
234 n = parent.firstChild();
248nodeFromXPath(
const dom::Node& context_node,
const String& xpath_expr)
250 const char* expr = xpath_expr.
localstr();
251 if (context_node._null()){
257 const char* separator = ::strchr(expr,
'/');
261 std::string_view buf1(expr,(Int64)(separator-expr));
263 dom::Node child = childNode(context_node,buf);
269 return childNode(context_node,xpath_expr);
276writeNodeChildren(std::ostream& ostr,
const dom::Node& node)
278 _writeNodeChildren(ostr,node);
286writeNode(std::ostream& o,
const dom::Node& node)
289 switch(node.nodeType()){
290 case Node::ELEMENT_NODE:
292 o <<
'<' << node.nodeName();
293 NamedNodeMap attr_list = node.attributes();
294 for( ULong i=0, s=attr_list.length(); i<s; ++i ){
296 writeNode(o,attr_list.item(i));
299 writeNodeChildren(o,node);
300 o <<
"</" << node.nodeName() <<
'>';
303 case Node::ATTRIBUTE_NODE:
304 o << node.nodeName() <<
'='
305 <<
'"' << node.nodeValue() <<
'"';
307 case Node::TEXT_NODE:
308 o << node.nodeValue();
311 case Node::CDATA_SECTION_NODE:
312 o << node.nodeValue();
313 cerr <<
"** Dom::writeNode() for CDATA_SECTION_NODE Not fully implemented\n";
316 case Node::ENTITY_REFERENCE_NODE:
317 _notImplemented(
"Dom::writeNode() for ENTITY_REFERENCE_NODE");
319 case Node::ENTITY_NODE:
320 _notImplemented(
"Dom::writeNode() for ENTITY_NODE");
322 case Node::PROCESSING_INSTRUCTION_NODE:
323 _notImplemented(
"Dom::writeNode() for PROCESSING_INSTRUCTION_NODE");
325 case Node::COMMENT_NODE:
326 o <<
"<!--" << node.nodeValue() <<
"-->";
328 case Node::DOCUMENT_NODE:
329 _writeNodeChildren(o,node);
331 case Node::DOCUMENT_TYPE_NODE:
332 _writeNodeChildren(o,node);
334 case Node::DOCUMENT_FRAGMENT_NODE:
335 _notImplemented(
"Dom::writeNode() for DOCUMENT_FRAGMENT_NODE");
337 case Node::NOTATION_NODE:
338 _notImplemented(
"Dom::writeNode() for NOTATION_NODE");
341 _notImplemented(
"Dom::writeNode() for unknown node");
351saveDocument(std::ostream& ostr,
const dom::Document& doc,
int indent_level)
354 saveDocument(bytes,doc,indent_level);
355 ostr.write((
const char*)bytes.data(),bytes.size());
363saveDocument(ByteArray& bytes,
const dom::Document& doc,
int indent_level)
365 dom::DOMImplementation domimp;
366 domimp._save(bytes,doc,indent_level);
367 Integer nb_byte = bytes.size();
368 if (nb_byte>=1 && bytes[nb_byte-1]==
'\0'){
369 ARCANE_FATAL(
"Invalid null charactere at end of XML stream");
377IXmlDocumentHolder* domutils::
380 dom::DOMImplementation domimp;
381 return domimp._newDocument();
390domutils::NameIterator::
391NameIterator(
const dom::Node& from,
const String& ref_name)
394, m_ref_name(ref_name)
396 _findNextValid(
true);
402void domutils::NameIterator::
403_findNextValid(
bool is_init)
406 m_current = m_parent.firstChild();
408 if (m_current._null())
410 m_current = m_current.nextSibling();
412 while (!m_current._null()){
413 if (m_current.nodeName()==m_ref_name)
415 m_current = m_current.nextSibling();
428 dom::DOMImplementation domimp;
430 return domimp._load(asBytes(buffer),name,tm);
436 dom::DOMImplementation domimp;
438 return domimp._load(buffer,name,tm);
447 return loadFromFile(filename,
String(),tm);
456 dom::DOMImplementation domimp;
457 return domimp._load(filename,tm,schema_filename);
#define ARCANE_THROW(exception_class,...)
Macro pour envoyer une exception avec formattage.
#define ARCANE_FATAL(...)
Macro envoyant une exception FatalErrorException.
Gestionnaire d'un document DOM.
Interface du gestionnaire de traces.
Vue d'un tableau d'éléments de type T.
Chaîne de caractères unicode.
const char * localstr() const
Retourne la conversion de l'instance dans l'encodage UTF-8.
Span< const std::byte > ByteConstSpan
Vue en lecture seule d'un tableau à une dimension de caractères.
dom::Node nodeFromXPath(const dom::Node &context_node, const String &xpath_expr)
Retourne le noeud correspondant à une expression XPath. Retourne le noeud correspondant à l'expressio...
UniqueArray< Byte > ByteUniqueArray
Tableau dynamique à une dimension de caractères.
Int32 Integer
Type représentant un entier.