Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

XStream Errors

View Class.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
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();
}
}
View Class.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
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;
}
 
}
View Class.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
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;
}
 
 
}
View Class.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
<?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>
View Class.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
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();
 
}
 
}
View Class.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
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
Something went wrong with that request. Please try again.