1 | #include "xml_parser.hpp" |
---|
2 | |
---|
3 | #include "context.hpp" |
---|
4 | |
---|
5 | #include "attribute_template.hpp" |
---|
6 | #include "object_template.hpp" |
---|
7 | #include "group_template.hpp" |
---|
8 | |
---|
9 | namespace xios |
---|
10 | { |
---|
11 | namespace xml |
---|
12 | { |
---|
13 | /// ////////////////////// Définitions ////////////////////// /// |
---|
14 | |
---|
15 | void CXMLParser::ParseFile(const StdString & filename, const std::set<StdString>& parseContextList) |
---|
16 | { |
---|
17 | StdIFStream ifs ( filename.c_str() , StdIFStream::in ); |
---|
18 | if ( (ifs.rdstate() & std::ifstream::failbit ) != 0 ) |
---|
19 | ERROR("void CXMLParser::ParseFile(const StdString & filename)", |
---|
20 | <<endl<< "Can not open <"<<filename<<"> file" ); |
---|
21 | |
---|
22 | CXMLParser::ParseStream(ifs, filename, parseContextList); |
---|
23 | } |
---|
24 | |
---|
25 | void CXMLParser::ParseString(const StdString & xmlContent) |
---|
26 | { |
---|
27 | StdIStringStream iss ( xmlContent /*, StdIStringStream::in*/ ); |
---|
28 | std::set<StdString> contxtList; |
---|
29 | CXMLParser::ParseStream(iss,"string", contxtList); |
---|
30 | } |
---|
31 | |
---|
32 | void CXMLParser::ParseStream(StdIStream & stream, const string& fluxId, const std::set<StdString>& parseContextList) |
---|
33 | { |
---|
34 | if (!stream.good()) |
---|
35 | ERROR("CXMLParser::ParseStream(const StdIStream & stream)", |
---|
36 | << "Bad xml stream !"); |
---|
37 | StdOStringStream oss; |
---|
38 | while(!stream.eof() && !stream.fail ()) oss.put(stream.get()); |
---|
39 | const StdString xmlcontent( oss.str(), 0, oss.str().size()-1 ); |
---|
40 | try |
---|
41 | { |
---|
42 | rapidxml::xml_document<char> doc; |
---|
43 | doc.parse<0>(const_cast<char*>(xmlcontent.c_str())); |
---|
44 | |
---|
45 | CXMLNode node(doc.first_node()); |
---|
46 | THashAttributes attributes; |
---|
47 | |
---|
48 | if (node.getElementName().compare(CXMLNode::GetRootName()) != 0) |
---|
49 | ERROR("CXMLParser::ParseStream(StdIStream & stream)", |
---|
50 | << "Root element should be named simulation (actual = \'" |
---|
51 | << node.getElementName() << "\')!"); |
---|
52 | |
---|
53 | std::set<StdString>::iterator it; |
---|
54 | std::set<StdString>::const_iterator itE = parseContextList.end(); |
---|
55 | bool isParseAll = (parseContextList.empty()); |
---|
56 | if (node.goToChildElement()) |
---|
57 | { |
---|
58 | do |
---|
59 | { |
---|
60 | CContextGroup* group_context = CContext::getRoot() ; |
---|
61 | |
---|
62 | attributes = node.getAttributes(); |
---|
63 | |
---|
64 | if (attributes.end() == attributes.find("id")) |
---|
65 | { |
---|
66 | DEBUG("The context will not be processed because it is not identified (missing id)"); |
---|
67 | continue; |
---|
68 | } |
---|
69 | |
---|
70 | CContext::setCurrent(attributes["id"]) ; |
---|
71 | |
---|
72 | bool hasctxt = CContext::has(attributes["id"]); |
---|
73 | |
---|
74 | if(hasctxt) |
---|
75 | { |
---|
76 | DEBUG("The context will not be processed because it exist an other context with the same id" ); |
---|
77 | continue; |
---|
78 | } |
---|
79 | |
---|
80 | if (isParseAll) |
---|
81 | { |
---|
82 | CContext* context = CContext::create(attributes["id"]); |
---|
83 | // if (!hasctxt) group_context->addChild(context); |
---|
84 | context->parse(node); |
---|
85 | |
---|
86 | attributes.clear(); |
---|
87 | } |
---|
88 | else |
---|
89 | { |
---|
90 | it = parseContextList.find(attributes["id"]); |
---|
91 | if (itE != it) |
---|
92 | { |
---|
93 | CContext* context = CContext::create(*it); |
---|
94 | // if (!hasctxt) group_context->addChild(context); |
---|
95 | context->parse(node); |
---|
96 | |
---|
97 | attributes.clear(); |
---|
98 | } |
---|
99 | } |
---|
100 | } while (node.goToNextElement()); |
---|
101 | } |
---|
102 | } |
---|
103 | catch (rapidxml::parse_error & exc) |
---|
104 | { |
---|
105 | const char* ptr = exc.where<char>() ; |
---|
106 | const char* begin = xmlcontent.c_str() ; |
---|
107 | const char* content=oss.str().c_str() ; |
---|
108 | size_t pos=ptr-begin ; |
---|
109 | int lineNumber = 1 ; |
---|
110 | int columnNumber = 0 ; |
---|
111 | const char* line; |
---|
112 | const char* endLine; |
---|
113 | |
---|
114 | for(const char* i=content;i<content+pos; ++i, ++columnNumber) if (*i=='\n') { lineNumber++ ; line=i ; columnNumber=0 ;} |
---|
115 | for(endLine=content+pos; *endLine!='\n' && *endLine!='\0' ; ++endLine) ; |
---|
116 | string strLine(line,endLine-line) ; |
---|
117 | |
---|
118 | ERROR("CXMLParser::ParseStream(StdIStream & stream)", << endl |
---|
119 | << "Error is occuring when parsing XML flux from <"<<fluxId<<"> at character "<< pos<<" line "<<lineNumber<<" column "<< columnNumber<< endl |
---|
120 | << strLine<<endl |
---|
121 | << string(columnNumber-1,'x')<<'^'<<endl |
---|
122 | <<" Error : " << exc.what() ) |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | }// namespace xml |
---|
127 | } // namespace xios |
---|