Created
October 25, 2012 22:22
-
-
Save iheanyi/3955843 to your computer and use it in GitHub Desktop.
XStream Errors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package edu.nd.iekechuk.JavaClassParse; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class Class { | |
private String course; | |
//private String credits; | |
private String crosslist; | |
//private ArrayList<Section> sections = new ArrayList<Section>(); | |
private List<Section> sections = new ArrayList<Section>(); | |
//private Section section; | |
public Class(String course, String crosslist, Section section) { | |
this.course = course; | |
//this.credits = credits; | |
this.crosslist = crosslist; | |
//this.section = section; | |
//sections = new ArrayList<Section>(); | |
//sections.add(section); | |
} | |
public String getCourse() { | |
return course; | |
} | |
public void setCourse(String course) { | |
this.course = course; | |
} | |
public void add(Section section) { | |
sections.add(section); | |
} | |
public List getContents() { | |
return sections; | |
} | |
public void clear() { | |
sections.clear(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package edu.nd.iekechuk.JavaClassParse; | |
import java.io.IOException; | |
import java.net.MalformedURLException; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.jsoup.Jsoup; | |
import org.jsoup.nodes.Document; | |
import org.jsoup.nodes.Element; | |
import org.jsoup.select.Elements; | |
import com.gargoylesoftware.htmlunit.ElementNotFoundException; | |
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; | |
import com.gargoylesoftware.htmlunit.WebClient; | |
import com.gargoylesoftware.htmlunit.html.HtmlForm; | |
import com.gargoylesoftware.htmlunit.html.HtmlOption; | |
import com.gargoylesoftware.htmlunit.html.HtmlPage; | |
import com.gargoylesoftware.htmlunit.html.HtmlSelect; | |
import com.thoughtworks.xstream.XStream; | |
public class Classes { | |
private String subject; | |
//private String tag; | |
private static List classes = new ArrayList(); | |
private Department department; | |
public Classes(String subject) { | |
// TODO Auto-generated constructor stub | |
//classes = new ArrayList<Class>(); | |
department = new Department(subject); | |
} | |
public String getSubject() { | |
return subject; | |
} | |
public void setSubject(String subject) { | |
this.subject = subject; | |
} | |
public void testClient() { | |
WebClient wc = new WebClient(); | |
wc.setThrowExceptionOnScriptError(false); | |
HtmlPage currentPage = null; | |
try { | |
currentPage = (HtmlPage) wc.getPage("https://was.nd.edu/reg/srch/ClassSearchServlet"); | |
} catch (FailingHttpStatusCodeException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} catch (MalformedURLException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
//("Google", currentPage.getTitleText()); | |
final HtmlForm form = currentPage.getFirstByXPath("//form[@action='ClassSearchServlet']"); | |
HtmlSelect selectSubject = (HtmlSelect) currentPage.getElementByName("SUBJ"); | |
// Accounting is selected by default on the page, do not want that | |
selectSubject.setSelectedAttribute((HtmlOption) selectSubject.getOptionByValue("ACCT"), false); // Set default choice of accounting to false | |
HtmlOption optionSubject = (HtmlOption) selectSubject.getOptionByText("Art Studio"); | |
this.setSubject("Art Studio"); | |
ArrayList<HtmlOption> optionArray = new ArrayList<HtmlOption>(); | |
//optionArray.add(selectSubject.getOptionByValue("CSE")); | |
selectSubject.setSelectedAttribute(optionSubject, true); | |
//form.getInputByName("SUBJ").setValueAttribute("ACCT"); | |
//final HtmlSubmitInput button = form.getInputByName("Submit"); | |
HtmlPage newPage = null; | |
try { | |
newPage = (HtmlPage) form.getInputByValue("Search").click(); | |
} catch (ElementNotFoundException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
String html = newPage.asXml(); | |
showResults(html); | |
/* final HtmlTable table = newPage.getHtmlElementById("resulttable"); | |
for (final HtmlTableRow row : table.getRows()) { | |
System.out.println("Found row"); | |
for (final HtmlTableCell cell : row.getCells()) { | |
System.out.println(" Found cell: " + cell.asText()); | |
} | |
}*/ | |
} | |
public void showResults(String html) { | |
String crn; | |
String title; | |
String time; | |
String instructor; | |
String op_spots; | |
String course; | |
String section; | |
Document doc = Jsoup.parse(html); | |
Element table = doc.select("table[id=resulttable]").first(); | |
Element tablebody = table.select("tbody").first(); | |
// Make external XML | |
XStream xstream = new XStream(); | |
xstream.alias("department", Department.class); | |
xstream.alias("class", Class.class); | |
xstream.alias("instructor", Instructor.class); | |
xstream.alias("section", Section.class); | |
Class entry = null; | |
int i = 0; | |
for(Element row : tablebody.select("tr")) { | |
Elements cells = row.select("td"); | |
crn = cells.get(7).text(); | |
title = cells.get(1).text(); | |
time = cells.get(10).text(); | |
instructor = cells.get(9).text(); | |
op_spots = cells.get(5).text(); | |
String max_spots = cells.get(6).text(); | |
course = cells.get(0).text(); | |
String[] values = course.split(" "); | |
course = values[0]; | |
section = values[2]; | |
Section addsect = new Section(section, "OP", max_spots, op_spots, instructor, crn, time); | |
if(entry == null) { | |
entry = new Class(course, "y", addsect); | |
classes.add(entry); | |
++i; | |
} | |
//System.out.println("Entry: " + entry.getCourse() + " " + course); | |
if(entry.getCourse() == course) { | |
int index = classes.indexOf(entry); | |
//Class second = new Class(Course); | |
//System.out.println(entry.getCourse() + " " + course); | |
//department[i].addSection(addsect); | |
//classes.add | |
} | |
else { | |
entry = new Class(course, "y", addsect); | |
++i; | |
} | |
//department.addtoList(addsect); | |
} | |
//list.add(entry); | |
//System.out.println(xml); | |
//System.out.println(course + " " + section); | |
//System.out.println(course + " - Section " + section + " : " + crn + " " + title + " " + time + " " + instructor + " " + op_spots); | |
String xml = xstream.toXML(this.department); | |
System.out.println(xml); | |
} | |
public List getClasses() { | |
return classes; | |
} | |
public void setClasses(List classes) { | |
this.classes = classes; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package edu.nd.iekechuk.JavaClassParse; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class Department { | |
private String subject; | |
private String tag; | |
private List<Class> classes = new ArrayList<Class>(); | |
//private Class entry; | |
public Department(String subject) { | |
// TODO Auto-generated constructor stub | |
this.subject = subject; | |
this.tag = tag; | |
//this.classes = new ArrayList<Class>(); | |
} | |
public void add(Class entry) { | |
this.classes.add(entry); | |
} | |
public List getContent() { | |
return classes; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" ?> | |
<classes> | |
<department subject="Accounting"> | |
<tag>ACCT</tag> | |
<classinventory> | |
<class course="ACCT20100"> | |
<name>Accountancy I</name> | |
<credits>3</credits> | |
<crosslist>true</crosslist> | |
<sections> | |
<section number = "01"> | |
<status>OP</status> | |
<maxspots>30</maxspots> | |
<openspots>30</openspots> | |
<instructor> | |
<first>Bob</first> | |
<last>Doe</last> | |
</instructor> | |
<crn>22485</crn> | |
<time> | |
<days>T R</days> | |
<start>11:00</start> | |
<stop>12:15</stop> | |
</time> | |
</section> | |
<section number = "02"> | |
<status>C</status> | |
<maxspots>30</maxspots> | |
<openspots>0</openspots> | |
<instructor> | |
<first>Plain</first> | |
<last>Jane</last> | |
</instructor> | |
<crn>123456</crn> | |
<time> | |
<days>M W F</days> | |
<start>10:40</start> | |
<stop>11:30</stop> | |
</time> | |
</section> | |
</sections> | |
</class> | |
</classinventory> | |
</department> | |
</classes> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package edu.nd.iekechuk.JavaClassParse; | |
import java.io.IOException; | |
import java.net.MalformedURLException; | |
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; | |
public class main { | |
/** | |
* @param args | |
* @throws IOException | |
* @throws MalformedURLException | |
* @throws FailingHttpStatusCodeException | |
*/ | |
public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException { | |
// TODO Auto-generated method stub | |
Classes ClassSearch = new Classes("Art Studio"); | |
ClassSearch.testClient(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package edu.nd.iekechuk.JavaClassParse; | |
public class Section { | |
private String number; | |
private String status; | |
private String openspots; | |
private String maxspots; | |
private String instructor; | |
private String crn; | |
private String time; | |
//private ArrayList entry = new ArrayList(); | |
public Section(String number, String status, String maxspots, String openspots, String instructor, String crn, String time) { | |
// TODO Auto-generated constructor stub | |
this.number = number; | |
this.status = status; | |
this.maxspots = maxspots; | |
this.openspots = openspots; | |
this.instructor = instructor; | |
this.crn = crn; | |
this.time = time; | |
//this.entry = Classes.getClasses(); | |
} | |
public String getStatus() { | |
return status; | |
} | |
public void setStatus(String status) { | |
this.status = status; | |
} | |
public String getOpenSpots() { | |
return openspots; | |
} | |
public void setOpenspots(String openspots) { | |
this.openspots = openspots; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment