package it.cnr.isti.driver.utils;

import java.io.IOException;
import java.io.StringReader;

import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.*;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class ODL_EPR {
	
	private String Address;
	private String ResourceIdentifier;
	private String wsdlLocation;
	private String ServiceName;
	
	private DOMParser parser;
	private Document doc;
	/*
	<wsa:EndpointReference 
	 xmlns:wsa="http://www.driver.org/schema" 
	 xmlns:driver="http://www.driver.org" 
	 xmlns:wsaw="http://www.w3.org/2006/02/addressing/wsdl" 
	 xmlns:wsdl="http://www.w3.org/2005/08/wsdl-instance">
		<wsa:Address>http://212.87.15.122:8002/SOAP/ResultSet</wsa:Address>
		<wsa:ReferenceParameters>
			<driver:ResourceIdentifier>RS-68b5b4e5-de48-4184-863c-1d5041d943eb-3889</driver:ResourceIdentifier>
		</wsa:ReferenceParameters>
		<wsa:Metadata wsdl:wsdlLocation="http://212.87.15.122:8002/SOAP/ResultSet?WSDL">
			<wsaw:ServiceName>theResultSet_for_ISStore</wsaw:ServiceName>
		</wsa:Metadata>
	</wsa:EndpointReference>
	*/
	public ODL_EPR() {
	}
	
	public ODL_EPR(String docXML) {
		try {
			parser = new DOMParser();
			parser.parse(new InputSource(new StringReader(docXML)));
			
			doc = parser.getDocument();
			
			Address = findNodeValue(doc.getDocumentElement(), "http://www.driver.org/schema", "Address");
			ResourceIdentifier = findNodeValue(doc.getDocumentElement(), "http://www.driver.org", "ResourceIdentifier");
			ServiceName = findNodeValue(doc.getDocumentElement(), "http://www.w3.org/2006/02/addressing/wsdl", "ServiceName");
			wsdlLocation = findAttributeNodeValue(doc.getDocumentElement(), "http://www.driver.org/schema", "Metadata", "http://www.w3.org/2005/08/wsdl-instance", "wsdlLocation");

		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public String getAddress() {
		return Address;
	}
	public String getResourceIdentifier() {
		return ResourceIdentifier;
	}
	public String getServiceName() {
		return ServiceName;
	}
	public String getWsdlLocation() {
		return wsdlLocation;
	}
	private String findNodeValue(Element root, String nsp, String tagName) {
		NodeList nodeList = root.getElementsByTagNameNS(nsp, tagName);
		int nodeListLength = nodeList.getLength();
        for (int j = 0; j < nodeListLength; ++j) {
            Node node = nodeList.item(j);
            return node.getTextContent();
        }
		return "";
	}
	private String findAttributeNodeValue(Element root, String nsp, String tagName, String nspAttr, String nameAttr) {
		NodeList nodeList = root.getElementsByTagNameNS(nsp, tagName);
		int nodeListLength = nodeList.getLength();
        for (int j = 0; j < nodeListLength; ++j) {
            Node node = nodeList.item(j);
            if (node.hasAttributes()) {
            	NamedNodeMap map = node.getAttributes();
            	Node attr = map.getNamedItemNS(nspAttr, nameAttr);
            	return attr.getTextContent();
            }
        }
		return "";
	}

	public void setAddress(String address) {
		Address = address;
	}

	public void setResourceIdentifier(String resourceIdentifier) {
		ResourceIdentifier = resourceIdentifier;
	}
}