File: xml_parser.cpp

package info (click to toggle)
embree 3.13.5%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 27,924 kB
  • sloc: cpp: 180,815; xml: 3,877; ansic: 2,957; python: 1,466; sh: 502; makefile: 229; csh: 42
file content (196 lines) | stat: -rw-r--r-- 6,346 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright 2009-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#include "xml_parser.h"

#include <fstream>

namespace embree
{
  //////////////////////////////////////////////////////////////////////////////
  ///                           XML Input
  //////////////////////////////////////////////////////////////////////////////

  /*! parse a list of XML comments */
  void parseComments(Ref<Stream<Token> >& cin)
  {
    while (cin->peek() == Token::Sym("<!--")) {
      cin->drop();
      while (cin->peek() != Token::Sym("-->")) {
        if (cin->peek() == Token::Eof())
          THROW_RUNTIME_ERROR(cin->get().Location().str()+": --> expected");
        cin->drop();
      }
      cin->drop();
    }
  }

  /*! parse XML parameter */
  void parseParm(Ref<Stream<Token> >& cin, std::map<std::string,std::string>& parms)
  {
    std::string name = cin->get().Identifier();
    if (cin->get() != Token::Sym("=")) THROW_RUNTIME_ERROR(cin->unget().Location().str()+": symbol \"=\" expected");
    parms[name] = cin->get().String();
  }

  /*! parse XML header */
  Ref<XML> parseHeader(Ref<Stream<Token> >& cin)
  {
    Ref<XML> xml = new XML;
    if (cin->get() != Token::Sym("<?")) THROW_RUNTIME_ERROR(cin->unget().Location().str()+": wrong XML header");
    xml->name = cin->get().Identifier();
    parseComments(cin);
    while (cin->peek() != Token::Sym("?>")) {
      parseParm(cin,xml->parms);
      parseComments(cin);
    }
    cin->drop();
    return xml;
  }

  /*! parse XML tag */
  Ref<XML> parseXML(Ref<Stream<Token> >& cin, size_t depth)
  {
    if (depth > 1024)
      THROW_RUNTIME_ERROR(cin->peek().Location().str()+": maximal nesting depth reached");
      
    Ref<XML> xml = new XML;
    xml->loc = cin->peek().Location();

    /* parse tag opening */
    if (cin->get() != Token::Sym("<")) THROW_RUNTIME_ERROR(cin->unget().Location().str()+": tag expected");

    xml->name = cin->get().Identifier();
    parseComments(cin);
    while (cin->peek() != Token::Sym("/>") && cin->peek() != Token::Sym(">")) {
      parseParm(cin,xml->parms);
      parseComments(cin);
    }
    if (cin->peek() == Token::Sym("/>")) {
      cin->drop();
      return xml;
    }
    cin->drop();

    /* parse body token list */
    parseComments(cin);
    while (cin->peek() != Token::Sym("<") && cin->peek() != Token::Sym("</")) {
      xml->body.push_back(cin->get());
      parseComments(cin);
    }

    /* the body also contains children */
    if (cin->peek() == Token::Sym("<")) {
      while (cin->peek() != Token::Sym("</")) {
        xml->children.push_back(parseXML(cin,depth+1));
        parseComments(cin);
      }
    }

    /* parse tag closing */
    if (cin->get() != Token::Sym("</")    ) THROW_RUNTIME_ERROR(cin->unget().Location().str()+": symbol \"</\" expected");
    if (cin->get() != Token::Id(xml->name)) THROW_RUNTIME_ERROR(cin->unget().Location().str()+": closing "+xml->name+" expected");
    if (cin->get() != Token::Sym(">")     ) THROW_RUNTIME_ERROR(cin->unget().Location().str()+": symbol \">\" expected");

    return xml;
  }

  /* load XML from token stream */
  Ref<XML> parseXML(Ref<Stream<int> > chars, std::string id, bool hasHeader = true, bool hasTail = false)
  {
    /* create lexer for XML file */
    std::vector<std::string> symbols;
    symbols.push_back("<!--");
    symbols.push_back("-->");
    symbols.push_back("<?");
    symbols.push_back("?>");
    symbols.push_back("</");
    symbols.push_back("/>");
    symbols.push_back("<");
    symbols.push_back(">");
    symbols.push_back("=");
    Ref<Stream<Token> > cin = new TokenStream(chars,TokenStream::alpha + TokenStream::ALPHA + "_" + id, TokenStream::separators, symbols);

    if (hasHeader) parseHeader(cin);
    parseComments(cin);
    Ref<XML> xml = parseXML(cin,0);
    parseComments(cin);

    if (!hasTail)
      if (cin->peek() != Token::Eof()) THROW_RUNTIME_ERROR(cin->peek().Location().str()+": end of file expected");

    return xml;
  }

  /*! load XML file from stream */
  std::istream& operator>>(std::istream& cin, Ref<XML>& xml) {
    xml = parseXML(new StdStream(cin),"",false,true);
    return cin;
  }

  /*! load XML file from disk */
  Ref<XML> parseXML(const FileName& fileName, std::string id, bool hasHeader) {
    return parseXML(new FileStream(fileName),id,hasHeader,false);
  }


  //////////////////////////////////////////////////////////////////////////////
  ///                           XML Output
  //////////////////////////////////////////////////////////////////////////////


  /* indent to some hierarchy level using spaces */
  void indent(std::ostream& cout, size_t depth) {
    for (size_t i=0; i<2*depth; i++) cout << " ";
  }

  /* store XML to a stream */
  std::ostream& emitXML(std::ostream& cout, const Ref<XML>& xml, size_t depth = 0)
  {
    /* print header */
    if (depth == 0) cout << "<?xml version=\"1.0\"?>" << std::endl << std::endl;

    /* print tag opening */
    indent(cout,depth); cout << "<" << xml->name;
    for (std::map<std::string,std::string>::const_iterator i=xml->parms.begin(); i!=xml->parms.end(); i++)
      cout << " " << i->first << "=" << "\"" << i->second << "\"";
    if (xml->children.size() == 0 && xml->body.size() == 0) {
      cout << "/>" << std::endl;
      return cout;
    }
    cout << ">";

    bool compact = xml->body.size() < 16 && xml->children.size() == 0;
    if (!compact) cout << std::endl;

    /* print token list */
    if (xml->body.size()) {
      if (!compact) indent(cout,depth+1);
      for (size_t i=0; i<xml->body.size(); i++)
        cout << xml->body[i] << (i!=xml->body.size()-1?" ":"");
      if (!compact) cout << std::endl;
    }

    /* print children */
    for (size_t i=0; i<xml->children.size(); i++)
      emitXML(cout,xml->children[i],depth+1);

    /* print tag closing */
    if (!compact) indent(cout,depth);
    return cout << "</" << xml->name << ">" << std::endl;
  }

  /* store XML to stream */
  std::ostream& operator<<(std::ostream& cout, const Ref<XML>& xml) {
    return emitXML(cout,xml);
  }

  /*! store XML to disk */
  void emitXML(const FileName& fileName, const Ref<XML>& xml)
  {
    std::ofstream cout(fileName.c_str());
    if (!cout.is_open()) THROW_RUNTIME_ERROR("cannot open file " + fileName.str() + " for writing");
    emitXML(cout,xml);
    cout.close();
  }
}