/**************************************************************************
@author  Chandar

@version 1.0
Development Environment        :  Oracle9i JDeveloper
Name of the File               :  PrimeNumberServiceImpl.java
Creation/Modification History  :
                                  13-Apr-2002     Created


Overview:
This class file defines methods to implement PrimeNumberWebService.
The method isPrime() is exposed by the Web Service.
**************************************************************************/


import org.w3c.dom.*;

import oracle.xml.parser.v2.*;
import java.io.*;

public class PrimeNumberServiceImpl implements PrimeNumberService
{

  public PrimeNumberServiceImpl()
  {}


  /*
  * This method is exposed by the Web Service to check if the number is
  * prime. It extracts the number for input element and checks if it is
  * prime or not and return the output in the XML Element.
  * The input element is of the form <number>23</number>
  */
  public Element isPrime(Element e)
  {

    // define an XML Element that will be returned to the client
    Element processedEl=null;
     try{

         // get the first clild of input element
         Node enode = e.getFirstChild();

         //get the value of input element and parse it
         String value=  enode.getNodeValue();
         double  number = Double.parseDouble(value);


         String answer=null;

         // check if the input number is prime
         answer = checkPrime(number);

         // create element from the answer
         processedEl  =createElement(answer);

     }catch(Exception ex){
        ex.printStackTrace();

        return null;
     }
      // return output to client
      return processedEl;
  }

 /*
  * This method creates an XML element from the input string values and return it
  * The Element is of the form <answer>value</answer>
  */
  private Element createElement(String value){


      // create a Document object
      Document xmldoc = new XMLDocument();

      // create an element with name "answer"
      Element e1 = xmldoc.createElement("answer");

      // append the element to document object
      xmldoc.appendChild(e1);

      // create a Text object from input values and append it to above element
      Text t = xmldoc.createTextNode(value);
      e1.appendChild(t);


     return (Element)xmldoc.getFirstChild();

    }

 /*
  * This method check if the input number is prime or not
  */
  private String  checkPrime(double num){

     //For a prime number mod(power(2,number),number) is equal to 2

     if ( Math.IEEEremainder( Math.pow(2.0,num) ,num)==2)
        return "The number  " + num + " is prime";
     else

       return "The number " + num + " is not prime";

    }


}

E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy