// Path: api/src/main/java/org/esbtools/message/admin/model/MetadataType.java
// public enum MetadataType {
//     Entities, Entity, System, SyncKey, SearchKeys, SearchKey, XPATH, Suggestion;
// 
//     public boolean isSearchKeyType() {
//         return this==SearchKeys || this==SearchKey || this==XPATH || this==Suggestion;
//     }
// 
//     public boolean isSyncKeyType() {
//         return !isSearchKeyType();
//     }
// }


import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.esbtools.message.admin.model.MetadataField;
import org.esbtools.message.admin.model.MetadataType;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/*
 Copyright 2015 esbtools Contributors and/or its affiliates.

 This file is part of esbtools.

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.esbtools.message.admin.common.extractor;





/**
 * The KeyExtractor is a xpath based ValueExtractor implementation. 
 * It evaluates a list of xpath operations against the given payload 
 * and puts the value along with the name into map. 
 * The purpose is to enrich the payload with additional metadata so users 
 * can search by them.
 * 
 * @author vrjain
 * @author ykoer
 *
 */
public class KeyExtractorUtil {

    private static final Logger LOGGER=LoggerFactory.getLogger(KeyExtractorUtil.class);
    private String hash;
    private Map<String, List<XPathExpression>> expressions;

    public KeyExtractorUtil(List<MetadataField> searchKeys, String argHash) {
        expressions = new HashMap<>();
        hash = argHash;

        XPath xpath = XPathFactory.newInstance().newXPath();
        for (MetadataField searchKey : searchKeys) {

            for (MetadataField path : searchKey.getChildren()) {
// Path: api/src/main/java/org/esbtools/message/admin/model/MetadataType.java
// public enum MetadataType {
//     Entities, Entity, System, SyncKey, SearchKeys, SearchKey, XPATH, Suggestion;
// 
//     public boolean isSearchKeyType() {
//         return this==SearchKeys || this==SearchKey || this==XPATH || this==Suggestion;
//     }
// 
//     public boolean isSyncKeyType() {
//         return !isSearchKeyType();
//     }
// }


// Path: common/src/main/java/org/esbtools/message/admin/common/extractor/KeyExtractorUtil.java
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.esbtools.message.admin.model.MetadataField;
import org.esbtools.message.admin.model.MetadataType;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/*
 Copyright 2015 esbtools Contributors and/or its affiliates.

 This file is part of esbtools.

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.esbtools.message.admin.common.extractor;





/**
 * The KeyExtractor is a xpath based ValueExtractor implementation. 
 * It evaluates a list of xpath operations against the given payload 
 * and puts the value along with the name into map. 
 * The purpose is to enrich the payload with additional metadata so users 
 * can search by them.
 * 
 * @author vrjain
 * @author ykoer
 *
 */
public class KeyExtractorUtil {

    private static final Logger LOGGER=LoggerFactory.getLogger(KeyExtractorUtil.class);
    private String hash;
    private Map<String, List<XPathExpression>> expressions;

    public KeyExtractorUtil(List<MetadataField> searchKeys, String argHash) {
        expressions = new HashMap<>();
        hash = argHash;

        XPath xpath = XPathFactory.newInstance().newXPath();
        for (MetadataField searchKey : searchKeys) {

            for (MetadataField path : searchKey.getChildren()) {
                if (path.getType() == MetadataType.XPATH) {