// Path: spring-influxdb-orm/src/main/java/com/vgerbot/orm/influxdb/InfluxDBException.java
// public class InfluxDBException extends RuntimeException {
// 
// 	private static final long serialVersionUID = -4410268653794610252L;
// 
// 	public InfluxDBException() {
// 		super();
// 	}
// 
// 	public InfluxDBException(String message, Throwable cause) {
// 		super(message, cause);
// 	}
// 
// 	public InfluxDBException(String message) {
// 		super(message);
// 	}
// 
// 	public InfluxDBException(Throwable cause) {
// 		super(cause);
// 	}
// }
// 
// Path: spring-influxdb-orm/src/main/java/com/vgerbot/orm/influxdb/ql/InfluxQLStatement.java
// public static enum ACTION {
// 	SELECT, EXECUTE
// }


import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.beans.factory.xml.DefaultDocumentLoader;
import org.springframework.beans.factory.xml.DocumentLoader;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.xml.XmlValidationModeDetector;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import com.vgerbot.orm.influxdb.InfluxDBException;
import com.vgerbot.orm.influxdb.ql.InfluxQLStatement.ACTION;
		}
		Element root = document.getDocumentElement();
		NodeList includeNodeList = root.getElementsByTagName("include");

		InfluxQLMapper mapper = new InfluxQLMapper();

		for (int i = 0; i < includeNodeList.getLength(); i++) {
			Node item = includeNodeList.item(i);
			String location = item.getAttributes().getNamedItem("path").getNodeValue();
			Resource includedResource = resourceLoader.getResource(location);
			InfluxQLMapper includedMapper = parseFrom(includedResource);
			mapper = mapper.union(includedMapper);
		}

		mapper.statementsMap.putAll(parseQLTag(root, "select"));
		mapper.statementsMap.putAll(parseQLTag(root, "execute"));

		return mapper;
	}

	private static Map<String, InfluxQLStatement> parseQLTag(Element root, String tagName) {
		NodeList nodeList = root.getElementsByTagName(tagName);
		Map<String, InfluxQLStatement> map = new HashMap<>();
		for (int i = 0; i < nodeList.getLength(); i++) {
			Node item = nodeList.item(i);
			String id = item.getAttributes().getNamedItem("id").getNodeValue();
			String ql = item.getTextContent().trim();
			if (map.containsKey(id)) {
				throw new InfluxDBException("Duplicate " + tagName + " ql id: " + id);
			}
// Path: spring-influxdb-orm/src/main/java/com/vgerbot/orm/influxdb/InfluxDBException.java
// public class InfluxDBException extends RuntimeException {
// 
// 	private static final long serialVersionUID = -4410268653794610252L;
// 
// 	public InfluxDBException() {
// 		super();
// 	}
// 
// 	public InfluxDBException(String message, Throwable cause) {
// 		super(message, cause);
// 	}
// 
// 	public InfluxDBException(String message) {
// 		super(message);
// 	}
// 
// 	public InfluxDBException(Throwable cause) {
// 		super(cause);
// 	}
// }
// 
// Path: spring-influxdb-orm/src/main/java/com/vgerbot/orm/influxdb/ql/InfluxQLStatement.java
// public static enum ACTION {
// 	SELECT, EXECUTE
// }


// Path: spring-influxdb-orm/src/main/java/com/vgerbot/orm/influxdb/ql/InfluxQLMapper.java
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.beans.factory.xml.DefaultDocumentLoader;
import org.springframework.beans.factory.xml.DocumentLoader;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.xml.XmlValidationModeDetector;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import com.vgerbot.orm.influxdb.InfluxDBException;
import com.vgerbot.orm.influxdb.ql.InfluxQLStatement.ACTION;

		}
		Element root = document.getDocumentElement();
		NodeList includeNodeList = root.getElementsByTagName("include");

		InfluxQLMapper mapper = new InfluxQLMapper();

		for (int i = 0; i < includeNodeList.getLength(); i++) {
			Node item = includeNodeList.item(i);
			String location = item.getAttributes().getNamedItem("path").getNodeValue();
			Resource includedResource = resourceLoader.getResource(location);
			InfluxQLMapper includedMapper = parseFrom(includedResource);
			mapper = mapper.union(includedMapper);
		}

		mapper.statementsMap.putAll(parseQLTag(root, "select"));
		mapper.statementsMap.putAll(parseQLTag(root, "execute"));

		return mapper;
	}

	private static Map<String, InfluxQLStatement> parseQLTag(Element root, String tagName) {
		NodeList nodeList = root.getElementsByTagName(tagName);
		Map<String, InfluxQLStatement> map = new HashMap<>();
		for (int i = 0; i < nodeList.getLength(); i++) {
			Node item = nodeList.item(i);
			String id = item.getAttributes().getNamedItem("id").getNodeValue();
			String ql = item.getTextContent().trim();
			if (map.containsKey(id)) {
				throw new InfluxDBException("Duplicate " + tagName + " ql id: " + id);
			}
			map.put(id, new InfluxQLStatement(ql, ACTION.valueOf(tagName.toUpperCase())));