Question: Hello, I need this to be done in java eclipse, I got some of the code correct, but I still cannot get it compile. Note
Hello, I need this to be done in java eclipse, I got some of the code correct, but I still cannot get it compile.
Note that the XmlUtility is in xml subpackage in week12 package.
Thank you!

I have error regarding this class
package week12.xml;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import week12.LoginRequest;
import week12.LoginResponse;
import week12.Week12Object;
import week12.Week12Exception;
public class XmlUtility {
private static Document generateLoginRequestXml(LoginRequest request)
{
Document dom=null;
//build the root element
Element root = new Element("login-request");
Attribute version = new Attribute("version", "1.0");
root.setAttribute(version);
String pinFat =String.format("%d", request.getPin());
Attribute pin = new Attribute("pin_number", pinFat);
root.setAttribute(pin);
String accountFat =String.format("%d",request.getAccountId());
Attribute session = new Attribute("session Id", accountFat);
root.setAttribute(session);
dom = new Document(root);
return dom;
}
private static Document generateLoginResponseXml(LoginResponse response) {
Document dom=null;
//build the root element
Element root = new Element("login-response");
Attribute version = new Attribute("version", "1.0");
root.setAttribute(version);
String loggedInFat = response.getLoggedIn().toString();
Attribute loggedIn = new Attribute("logged-in", loggedInFat);
root.setAttribute(loggedIn);
String sessionFat =String.format("%d",response.getSessionId());
Attribute account = new Attribute("session Id", sessionFat);
root.setAttribute(account);
dom = new Document(root);
return dom;
}
private static Week12Object getLoginObject(Element root)
throws NumberFormatException
{
String AccountIdString = root.getAttributeValue("Account-Id");
String PinString = root.getAttributeValue("Pin");
int pin = Integer.parseInt(PinString);
long sessionId =Integer.parseInt(AccountIdString);
return new LoginRequest(pin , sessionId);
}
private static Week12Object getLoginResponseObject(Element root)
throws NumberFormatException
{
String loggedInString = root.getAttributeValue("logged-in");
String sessionIdString = root.getAttributeValue("session-in");
boolean loggedIn = Boolean.parseBoolean(loggedInString);
int accountId =Integer.parseInt(sessionIdString);
return new LoginResponse(loggedIn , accountId);
}
public static Document objectToXml(Week12Object obj) throws Week12Exception
{
Document dom = null;
if(obj instanceof LoginRequest){
dom=generateLoginRequestXml((LoginRequest)obj);
}
else if(obj instanceof LoginResponse){
dom = generateLoginResponseXml((LoginResponse)obj);
}
else
{
throw new Week12Exception("unknown Week12Object");
}
return dom;
}
public static Week12Object xmlToObject(Document dom) throws Week12Exception
{
Week12Object atmObject =null;
Element root = dom.getRootElement();
String elementName = root.getName();
try{
if(elementName.equals("login-request"))
{
atmObject = getLoginObject(root);
}
else if(elementName.equals("login-response"))
{
atmObject = getLoginResponseObject(root);
}
}
catch(NumberFormatException ex){
throw new Week12Exception(ex);
}
return atmObject;
}
}
/*******************
Provided xml hard code
/**********************************************************************
These classes are correct*************
******************************
package week12;
public class LoginResponse extends Week12Object
{
public LoginResponse(boolean loggedIn, long sessionId)
{
m_loggedIn = loggedIn;
m_sessionId = sessionId;
}
public Boolean getLoggedIn()
{
return m_loggedIn;
}
public long getSessionId()
{
return m_sessionId;
}
@Override
public boolean equals(Object obj)
{
boolean result = false;
if( obj instanceof LoginResponse)
{
LoginResponse rhs = (LoginResponse)obj;
if( this.getLoggedIn() == rhs.getLoggedIn() &&
this.getSessionId() == rhs.getSessionId())
{
result = true;
}
}
return result;
}
private Boolean m_loggedIn;
private long m_sessionId;
/******************************
package week12;
import week12.xml.XmlUtility;
import static org.junit.Assert.*;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import org.junit.Test;
import org.jdom2.Document;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
public class Week12JUnitTest
{
/**
* Pass in invalid guesses and get an InvalidArgumentException
*/
@Test
public void testXmlResponseProcessing()
{
trace("testXmlResponseProcessing");
try
{
Document dom = generateResponseXmlDocument();
Week12Object obj = XmlUtility.xmlToObject(dom);
boolean isResponseObj = obj instanceof LoginResponse;
assertTrue("Expected a LoginResponse Object", isResponseObj);
// Now try to serialize the obj
Document actual = XmlUtility.objectToXml(obj);
XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
StringWriter swriter = new StringWriter();
xout.output(actual, swriter);
String actualXml = swriter.getBuffer().toString();
analyzeXML(TEST_RESPONSE_XML, actualXml);
assertTrue("Expected XML strings to be the same", TEST_RESPONSE_XML.equals(actualXml));
}
catch(Week12Exception ex)
{
fail("Error processing XML " + ex.getMessage());
}
catch(JDOMException ex)
{
fail("Error processing XML " + ex.getMessage());
}
catch(IOException ex)
{
fail("Error processing XML " + ex.getMessage());
}
}
/**
* Pass in invalid guesses and get an InvalidArgumentException
*/
@Test
public void testXmlRequestProcessing()
{
trace("testXmlRequestProcessing");
try
{
Document dom = generateRequestXmlDocument();
Week12Object obj = XmlUtility.xmlToObject(dom);
boolean isRequestObj = obj instanceof LoginRequest;
assertTrue("Expected a LoginRequest Object", isRequestObj);
// Now try to serialize the obj
Document actual = XmlUtility.objectToXml(obj);
XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
StringWriter swriter = new StringWriter();
xout.output(actual, swriter);
String actualXml = swriter.getBuffer().toString();
analyzeXML(TEST_REQUEST_XML, actualXml);
assertTrue("Expected XML strings to be the same", TEST_REQUEST_XML.equals(actualXml));
}
catch(Week12Exception ex)
{
fail("Error processing XML " + ex.getMessage());
}
catch(JDOMException ex)
{
fail("Error processing XML " + ex.getMessage());
}
catch(IOException ex)
{
fail("Error processing XML " + ex.getMessage());
}
}
/**
* Generates a string of random printable characters
* @return A random string of characters
*/
private Document generateResponseXmlDocument()
throws JDOMException, IOException
{
String myxml = TEST_RESPONSE_XML;
SAXBuilder sax = new SAXBuilder();
Document doc;
doc = sax.build(new StringReader(myxml));
return doc;
}
/**
* Generates a string of random printable characters
* @return A random string of characters
*/
private Document generateRequestXmlDocument()
throws JDOMException, IOException
{
String myxml = TEST_REQUEST_XML;
SAXBuilder sax = new SAXBuilder();
Document dom;
dom = sax.build(new StringReader(myxml));
return dom;
}
private void analyzeXML(String expected, String actual)
{
int expectedLength = expected.length();
int actualLength = actual.length();
trace("Expected XML");
trace(expected);
trace("Actual XML");
trace(actual);
String msg = String.format("Lengths (expected/actual) %d/%d", expectedLength, actualLength);
trace(msg);
boolean areEqual = expected.equals(actual);
trace(areEqual ? "XML equal" : "XML not equal");
}
private void trace(String msg)
{
System.out.println(msg);
}
private static String TEST_RESPONSE_XML =
" "
+ " ";
private static String TEST_REQUEST_XML =
" "
+ " ";
}
/***************************************
package week12;
public class LoginRequest extends Week12Object{
private long m_accountId;
private int m_pin;
public LoginRequest(int pin, long accountId)
{
m_pin = pin;
m_accountId = accountId;
}
public long getAccountId(){
return m_accountId;
}
public int getPin(){
return m_pin;
}
public void setPin(int m_pin){
}
public void setAccountId(long m_accountId ){
}
@Override
public boolean equals(Object obj)
{
boolean result = false;
if( obj instanceof LoginRequest)
{
LoginRequest lrst = (LoginRequest)obj;
if( this.getAccountId() == lrst.getAccountId() &&
this.getPin() == lrst.getPin())
{
result = true;
}
}
return result;
}
}
/******************************************
package week12;
public class Week12Object
{
public Week12Object()
{
// TODO Auto-generated constructor stub
}
}
/**********************************
package week12;
public class Week12Exception extends Exception
{
private static final long serialVersionUID = 1893714798006544957L;
public Week12Exception()
{
}
public Week12Exception(String msg)
{
super(msg);
}
public Week12Exception(Throwable ex)
{
super(ex);
}
public Week12Exception(String msg, Throwable ex)
{
super(msg, ex);
}
public Week12Exception(String msg, Throwable ex, boolean enableSuppression,
boolean writableStackTrace)
{
super(msg, ex, enableSuppression, writableStackTrace);
}
}
/******************************************
package week12;
import java.util.List;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestHarness
{
public static void main(String[] args)
{
trace("TestHarness");
try
{
Result result = org.junit.runner.JUnitCore
.runClasses(Week12JUnitTest.class);
int runs = result.getRunCount();
int ignores = result.getIgnoreCount();
trace(String.format("Runs: %d", runs));
trace(String.format("Ingores: %d", ignores));
int failCount = result.getFailureCount();
if(failCount > 0)
{
List failures = result.getFailures();
for(Failure fail : failures)
{
trace("FAILED: " + fail.getMessage());
}
}
else
{
trace("SUCCESS");
}
}
catch(Exception ex)
{
trace("Unhandled exception: " + ex.getMessage());
}
}
private static void trace(String msg)
{
System.out.println(msg);
}
}
Login Request m accountid long m oin int equals object) :boolean getAccountld0 long get Pin0 :int LoginRequest int, long) set Accountld(long) :void setPin(int) :void xml::xmlUtility nRequestXm Document ner at Week 120bject TestHarness generate LoginResponsexml Login Response) Document etL nobject (Element) :Week12Object main(Strin void Week120bject0 nResponse Object Element) :Week 12Object etLo trace (Strin void objectToXml(Week 12Object Document xmlToObject Document Week 12Object Exception Week 12Exception LoginResponse serialVersionUID :lol 1893714798008544957 L readOn m logged in Boolean Week 12Exception0 m sessionid :long Week12Exception(String) Week 12Exception(Throwable) equals object) :boolean Week 12Exception String, Throwable) get Loggedln0 :Boolean Week 12Exception String, Throwable, boolean, boolean) getSessionld0 :long Login Response(boolean, long) Week 12 JUnitTest TEST REQUEST XML Strin xml version TEST RESPONSE XML String xml version analyzeXML(String. String) :void generate RequestxmlDocument0 :Document generate Response XmlDocumento Document test KmlRequestProcessing0 :void test Xml ResponseProoessing0 void trace (String) :void Login Request m accountid long m oin int equals object) :boolean getAccountld0 long get Pin0 :int LoginRequest int, long) set Accountld(long) :void setPin(int) :void xml::xmlUtility nRequestXm Document ner at Week 120bject TestHarness generate LoginResponsexml Login Response) Document etL nobject (Element) :Week12Object main(Strin void Week120bject0 nResponse Object Element) :Week 12Object etLo trace (Strin void objectToXml(Week 12Object Document xmlToObject Document Week 12Object Exception Week 12Exception LoginResponse serialVersionUID :lol 1893714798008544957 L readOn m logged in Boolean Week 12Exception0 m sessionid :long Week12Exception(String) Week 12Exception(Throwable) equals object) :boolean Week 12Exception String, Throwable) get Loggedln0 :Boolean Week 12Exception String, Throwable, boolean, boolean) getSessionld0 :long Login Response(boolean, long) Week 12 JUnitTest TEST REQUEST XML Strin xml version TEST RESPONSE XML String xml version analyzeXML(String. String) :void generate RequestxmlDocument0 :Document generate Response XmlDocumento Document test KmlRequestProcessing0 :void test Xml ResponseProoessing0 void trace (String) :void
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
