|
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | +Module: XML repair tool |
| 4 | +
|
| 5 | +Author: Peter Schrammel |
| 6 | +
|
| 7 | +\*******************************************************************/ |
| 8 | + |
| 9 | +#include <util/unicode.h> |
| 10 | + |
| 11 | +#include <fstream> |
| 12 | +#include <iostream> |
| 13 | +#include <stack> |
| 14 | +#include <sstream> |
| 15 | + |
| 16 | +#include "xml_repair.h" |
| 17 | + |
| 18 | +// cut non-closed branches at this level |
| 19 | +#define BACKTRACK_LEVEL 1 |
| 20 | + |
| 21 | +/*******************************************************************\ |
| 22 | +
|
| 23 | +Function: xml_repair |
| 24 | +
|
| 25 | + Inputs: |
| 26 | +
|
| 27 | + Outputs: |
| 28 | +
|
| 29 | + Purpose: |
| 30 | +
|
| 31 | +\*******************************************************************/ |
| 32 | + |
| 33 | +void xml_repair(std::ifstream &infile, std::ofstream &outfile) |
| 34 | +{ |
| 35 | + std::stack<std::string> tags; // tag stack |
| 36 | + bool intag=false; // within a tag |
| 37 | + bool intagname=false; // within a tag name |
| 38 | + bool closingtag=false; // it's a closing tag |
| 39 | + std::stringstream tag; // the tag name |
| 40 | + std::stringstream backtrackbuffer; // buffer for cutting branches |
| 41 | + char c, lastc=' '; // current and last character |
| 42 | + |
| 43 | + while(infile >> std::noskipws >> c) |
| 44 | + { |
| 45 | + backtrackbuffer << c; |
| 46 | + switch(c) |
| 47 | + { |
| 48 | + case '<' : intag=intagname=true; break; |
| 49 | + case ' ' : |
| 50 | + if(intag) |
| 51 | + intagname=false; |
| 52 | + break; |
| 53 | + case '/' : |
| 54 | + if(intag) |
| 55 | + { |
| 56 | + if(lastc=='<') |
| 57 | + closingtag=true; |
| 58 | + else |
| 59 | + intagname=false; |
| 60 | + } |
| 61 | + break; |
| 62 | + case '>' : |
| 63 | + if(!closingtag && lastc!='/') // end of opening tag |
| 64 | + { |
| 65 | + if(lastc!='?') |
| 66 | + { |
| 67 | + tags.push(tag.str()); |
| 68 | + } |
| 69 | + } |
| 70 | + else // end of closing tag |
| 71 | + { |
| 72 | + if(lastc!='/') |
| 73 | + { |
| 74 | + tags.pop(); |
| 75 | + } |
| 76 | + } |
| 77 | + // clear tag name buffer |
| 78 | + tag.seekp(0); |
| 79 | + tag.seekg(0); |
| 80 | + tag.str(""); |
| 81 | + tag.clear(); |
| 82 | + |
| 83 | + if(tags.size()<=BACKTRACK_LEVEL) |
| 84 | + { |
| 85 | + // at this level everything in the buffer is part of the output |
| 86 | + outfile << backtrackbuffer.str(); |
| 87 | + |
| 88 | + // clear buffer |
| 89 | + backtrackbuffer.seekp(0); |
| 90 | + backtrackbuffer.seekg(0); |
| 91 | + backtrackbuffer.str(""); |
| 92 | + backtrackbuffer.clear(); |
| 93 | + } |
| 94 | + intag=intagname=closingtag=false; |
| 95 | + break; |
| 96 | + default : |
| 97 | + if(intagname) |
| 98 | + tag << c; |
| 99 | + break; |
| 100 | + } |
| 101 | + lastc=c; |
| 102 | + } |
| 103 | + if(tags.size()>0) // truncated |
| 104 | + { |
| 105 | + // everything above BACKTRACK_LEVEL is discarded |
| 106 | + // now, add missing closing tags below it: |
| 107 | + while(!tags.empty()) |
| 108 | + { |
| 109 | + if(tags.size()<=BACKTRACK_LEVEL) |
| 110 | + { |
| 111 | + outfile << std::endl << "</" << tags.top() << ">" << std::endl; |
| 112 | + } |
| 113 | + tags.pop(); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments