/**
 * Copyright 2014 Telefonica Investigación y Desarrollo, S.A.U
 *
 * This file is part of fiware-connectors (FI-WARE project).
 *
 * fiware-connectors is free software: you can redistribute it and/or modify it under the terms of the GNU Affero
 * General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 * fiware-connectors 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 Affero General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Affero General Public License along with fiware-connectors. If not, see
 * http://www.gnu.org/licenses/.
 *
 * For those usages not covered by the GNU Affero General Public License please contact with Francisco Romero
 * francisco.romerobueno@telefonica.com
 */

package es.tid.fiware.fiwareconnectors.cygnus.utils;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author frb
 */
public final class Utils {
    
    /**
     * Constructor. It is private since utility classes should not have a public or default constructor.
     */
    private Utils() {
    } // Utils
    
    /**
     * Encodes a string replacing all the non alphanumeric characters by '_'.
     * 
     * @param in
     * @return The encoded version of the input string.
     */
    public static String encode(String in) {
        return in.replaceAll("[^a-zA-Z0-9]", "_");
    } // encode
           
    /**
     * Converts a XML node into Json.
     * @param xmlNode
     * @return
     * @throws Exception
     */
    public static JsonElement basicXml2Json(Node xmlNode) throws Exception {
        // if the XML node has not attributes, it is either an object either a string
        if (!xmlNode.hasAttributes()) {
            Node child = xmlNode.getFirstChild();

            if (child.getFirstChild() != null) {
                NodeList domObjects = ((Element) xmlNode).getChildNodes();
                JsonObject jsonObject = new JsonObject();

                for (int i = 0; i < domObjects.getLength(); i++) {
                    Node domObject = domObjects.item(i);
                    jsonObject.add(domObject.getNodeName(), basicXml2Json(domObject));
                } // for

                return jsonObject;
            } else {
                return new JsonPrimitive(xmlNode.getTextContent());
            } // if else
        } // if

        // if the "type" attribute is not among the existing ones then return error
        if (xmlNode.getAttributes().getNamedItem("type") == null) {
            throw new Exception("Attributes different than \"type\" are not allowed withing or any child tag "
                    + "according to Orion notification API");
        } // if

        String valueType = xmlNode.getAttributes().getNamedItem("type").getTextContent();

        // if the value of the "type" attribute is "vector", the proceed, return error otherwise
        if (valueType.equals("vector")) {
            NodeList domItems = ((Element) xmlNode).getElementsByTagName("item");

            if (domItems.getLength() == 0) {
                throw new Exception("No <item> tag within <contextValue type=\"vector\">");
            } // if

            JsonArray jsonArray = new JsonArray();

            for (int i = 0; i < domItems.getLength(); i++) {
                jsonArray.add(basicXml2Json(domItems.item(i)));
            } // for

            return jsonArray;
        } else {
            throw new Exception("Unknown XML node type: " + valueType);
        } // if else if else
    } // basicXml2Json
        
} // Utils

@@ -1151,16 +1151,166 @@
 mitive;%0A
+import java.io.BufferedReader;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.io.InputStreamReader;%0Aimport java.util.Properties;%0A
 import o
@@ -4043,16 +4043,1258 @@
 ml2Json%0A
+    %0A    /**%0A     * Gets the Cygnus version from the pom.xml.%0A     * @return The Cygnus version%0A     */%0A    public static String getCygnusVersion() %7B%0A        InputStream stream = Utils.class.getClassLoader().getResourceAsStream(%22pom.properties%22);%0A        %0A        if (stream == null) %7B%0A            return %22UNKNOWN%22;%0A        %7D // if%0A        %0A        Properties props = new Properties();%0A        %0A        try %7B%0A            props.load(stream);%0A            stream.close();%0A            return (String) props.get(%22version%22);%0A        %7D catch (IOException e) %7B%0A            return %22UNKNOWN%22;%0A        %7D // try catch%0A    %7D // getCygnusVersion%0A    %0A    /**%0A     * Gets the hash regarding the last Git commit.%0A     * @return The hash regarding the last Git commit.%0A     */%0A    public static String getLastCommit() %7B%0A        InputStream stream = Utils.class.getClassLoader().getResourceAsStream(%22last_git_commit.txt%22);%0A        %0A        if (stream == null) %7B%0A            return %22UNKNOWN%22;%0A        %7D // if%0A        %0A        try %7B%0A            BufferedReader reader = new BufferedReader(new InputStreamReader(stream));%0A            return reader.readLine();%0A        %7D catch (Exception e) %7B%0A            return %22UNKNOWN%22;%0A        %7D // catch%0A    %7D // getLastCommit%0A
         

Add an utility function for getting the last git commit