Blame view

src/TimeTableCatalog/XMLReader.cc 4.01 KB
fbe3c2bb   Benjamin Renard   First commit
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
/*
 * XMLReader.cc
 *
 *  Created on: 7 août 2013
 *      Author: CS
 */

#include "XMLReader.hh"
#include <boost/algorithm/string.hpp>

namespace TimeTableCatalog {

XMLReader::XMLReader(const std::string& pPath) :
		AbstractReader(pPath), _tmpIntervalstartdate(0), _step(READ_STEP::NO_TAG) {

}

XMLReader::~XMLReader() {
}

// ----------------- PUBLIC --------------------------

void XMLReader::read(TimeTable& pTT) {
	/*
	 * this initialize the library and check potential ABI mismatches
	 * between the version it was compiled for and the actual shared
	 * library used.
	 */LIBXML_TEST_VERSION

	// -- parse xml file and process nodes
	parse(pTT);
7b2b006f   Hacene SI HADJ MOHAND   default liststart...
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
                    // update listStopDate  and listStartDate 
                   if(pTT._listStopDate <=0 || pTT._listStartDate <=0){
                        double startDate=std::numeric_limits<double>::max();
                        double stopDate = -1;
                        
                        for (auto intrval : pTT.getIntervals()){
                            if(intrval._startTime <startDate)
                                startDate = intrval._startTime;
                            if(intrval._stopTime > stopDate)
                                stopDate = intrval._stopTime;
                          }
                        if(pTT._listStopDate <=0)
                            pTT._listStopDate = stopDate;
                        if(pTT._listStartDate <=0)
                            pTT._listStartDate = startDate;
                }
fbe3c2bb   Benjamin Renard   First commit
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
	// -- cleanup function for the XML library.
	xmlCleanupParser();
}

bool XMLReader::canRead(const std::string& pPath) {
	std::string tmpPath(pPath);
	std::transform(tmpPath.begin(), tmpPath.end(), tmpPath.begin(), ::tolower);
	if(!boost::algorithm::ends_with(tmpPath, ".xml")){
		return false;
	}
	/*
	 * this initialize the library and check potential ABI mismatches
	 * between the version it was compiled for and the actual shared
	 * library used.
	 */LIBXML_TEST_VERSION

	 std::string localPath = TimeTable::download(pPath);

	// -- parse xml file and process nodes
	bool found = containsNode(localPath, getFirstNode());

	// -- cleanup function for the XML library.
	xmlCleanupParser();

	return found;
}
// ----------------- PROTECTED --------------------------

/**
 * Parses and processes XML file denoted by _path.
 */
void XMLReader::parse(TimeTable& pTT) {
	xmlTextReaderPtr reader;

	// created XML reader
	reader = xmlReaderForFile(getLocalPath().c_str(), NULL, 0);
	_step = READ_STEP::NO_TAG;
	if (reader != NULL) {
		// read xml file tag by tag
		int crtIndex = 0;
		int ret = xmlTextReaderRead(reader);
		while (ret == 1) {
			processNode(pTT, reader, crtIndex);
			ret = xmlTextReaderRead(reader);
		}
		xmlFreeTextReader(reader);
		if (ret != 0) {
			LOG4CXX_WARN(_logger, "Failed to parse " + getPath());
		}
	} else {
		LOG4CXX_WARN(_logger, "Unable to open " + getPath());
	}
}

/**
 * Parses and search for a given node in XML file denoted by _path.
 */
bool XMLReader::containsNode(const std::string& pPath, std::string pNodeName,
		int pMaxTagToRead) {
	xmlTextReaderPtr reader;

	// created XML reader
	reader = xmlReaderForFile(pPath.c_str(), NULL, 0);
	if (reader != NULL) {
		// read xml file tag by tag
		int ret = xmlTextReaderRead(reader);
		bool found = false;
		int i = 0;
		while (ret == 1 && !found && i < pMaxTagToRead) {
			found = containsNode(reader, pNodeName);
			ret = xmlTextReaderRead(reader);
			++i;
		}
		xmlFreeTextReader(reader);
		if (!found && i < pMaxTagToRead && ret != 0) {
			LOG4CXX_WARN(_logger, "Failed to parse " + pPath);
		}
		return found;
	} else {
		LOG4CXX_WARN(_logger, "Unable to open " + pPath);
	}
	return false;
}

bool XMLReader::containsNode(xmlTextReaderPtr reader,
		std::string& pNodeName) {
	const xmlChar *name;

	// -- read tag
	name = xmlTextReaderConstName(reader);
	if (name == NULL)
		name = BAD_CAST "--";

	// get tag name to easily handle it
	std::string tagName = reinterpret_cast<const char*>(name);

	if (tagName == pNodeName) {
		return true;
	}
	return false;
}

} /* namespace TimeTableCatalog */