package tutorial.sample31.client;

import java.io.IOException;

import java.net.URL;

import java.util.Iterator;

import javax.xml.namespace.QName;

import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;
import javax.xml.rpc.ServiceException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import javax.xml.soap.SOAPElement;
import javax.xml.soap.Name;


public class Main{

  public static void main( String[] args ){

    if( args.length == 1 ){
      new Main( args[0] );
    }else{
      throw new IllegalArgumentException( "URL of the service not specified" );
    }
  }

  private void invokeStatic( String wsdlUrl ) 
    throws IOException, ServiceException, ParserConfigurationException {

    HelloWorldService service = new HelloWorldService_Impl( wsdlUrl );
    HelloWorldServicePort port = service.getHelloWorldServicePort();
    
    SOAPElement dom = (SOAPElement)port.helloDom();
    System.out.println( dom );

    Element element = getDomElement( dom );
  }

  private Element getDomElement( SOAPElement soapElement ) 
    throws ParserConfigurationException{

    DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = fact.newDocumentBuilder();
    Document doc = db.newDocument();

    return getDomElement( soapElement, doc );
  }

  private Element getDomElement( SOAPElement soapElement, Document doc ){
    Name name = soapElement.getElementName();

    Element domElement = doc.createElementNS( name.getURI(), 
        name.getQualifiedName() );

    addAttributes( soapElement, domElement );

    return domElement;
  }

  private void addAttributes( SOAPElement soapElement, Element domElement ){

    for( Iterator it = soapElement.getAllAttributes(); it.hasNext(); ){
      Name name = (Name) it.next();
      String value = soapElement.getAttributeValue( name );

      domElement.setAttributeNS( name.getURI(), 
          name.getQualifiedName(), value );
    }
  }

  private void invokeDynamicProxy( String wsdlUrl ) 
    throws ServiceException, IOException{

    try{
      HelloWorldService service = new HelloWorldService_Impl( wsdlUrl );
      HelloWorld port = (HelloWorld)service.getPort( HelloWorld.class );

      Element element = port.helloDom();
      weblogic.xml.babel.stream.DOMInputStream.printNode( (Node)element );

    }catch( java.rmi.RemoteException e ){
      e.printStackTrace();

      if( e.getCause() != null ){
        e.getCause().printStackTrace();
      }
    }
  }

  public Main( String wsdlUrl ){
    try{
      invokeDynamicProxy( wsdlUrl );
    }catch( IOException e ){
      System.out.println( "Failed to create web service client:" + e );
    }catch( ServiceException e ){
      System.out.println( "Failed to create web service client:" + e );
    }catch( Throwable e ){
      e.printStackTrace();
    }
  }
}

