Skip to content

Commit 8a6fef0

Browse files
committed
adding simpleML as a library just for chapter 18
1 parent abfebc8 commit 8a6fef0

File tree

5 files changed

+436
-0
lines changed

5 files changed

+436
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* Daniel Shiffman */
2+
/* Programming from A to Z */
3+
/* Spring 2006 */
4+
/* http://www.shiffman.net */
5+
/* daniel.shiffman@nyu.edu */
6+
7+
/* Class to read an input file */
8+
/* and return a String */
9+
10+
package simpleML;
11+
import java.net.*;
12+
import java.io.*;
13+
14+
public class A2ZUrlReader
15+
{
16+
private String urlPath;
17+
private String content;
18+
19+
public A2ZUrlReader(String name) throws IOException {
20+
urlPath = name;
21+
readContent();
22+
}
23+
24+
25+
public static InputStream openStream(String urlpath) {
26+
InputStream stream = null;
27+
try {
28+
URL url = new URL(urlpath);
29+
stream = url.openStream();
30+
return stream;
31+
} catch (MalformedURLException e) {
32+
System.out.println("Something's wrong with the URL: "+ urlpath);
33+
e.printStackTrace();
34+
} catch (IOException e) {
35+
System.out.println("there's a problem downloading from: "+ urlpath);
36+
e.printStackTrace();
37+
}
38+
return stream;
39+
}
40+
41+
42+
public void readContent() throws IOException {
43+
StringBuffer stuff = new StringBuffer();
44+
try {
45+
InputStream stream = openStream(urlPath);
46+
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
47+
String line;
48+
while (( line = reader.readLine()) != null) {
49+
stuff.append(line + "\n");
50+
}
51+
reader.close();
52+
} catch (IOException e) {
53+
e.printStackTrace();
54+
}
55+
content = stuff.toString();
56+
}
57+
58+
public String getContent() {
59+
return content;
60+
}
61+
}
62+
63+
64+
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/* Daniel Shiffman */
2+
/* Programming from A to Z */
3+
/* Spring 2006 */
4+
/* http://www.shiffman.net */
5+
/* daniel.shiffman@nyu.edu */
6+
7+
/* Class to search / traverse an */
8+
/* XML file */
9+
10+
11+
package simpleML;
12+
import java.net.*;
13+
import java.util.ArrayList;
14+
import java.io.*;
15+
16+
import javax.xml.parsers.DocumentBuilder;
17+
import javax.xml.parsers.DocumentBuilderFactory;
18+
import javax.xml.parsers.ParserConfigurationException;
19+
20+
import org.w3c.dom.Document;
21+
import org.w3c.dom.Element;
22+
import org.w3c.dom.NamedNodeMap;
23+
import org.w3c.dom.Node;
24+
import org.w3c.dom.NodeList;
25+
import org.xml.sax.SAXException;
26+
27+
public class A2ZXmlReader
28+
{
29+
private String urlPath;
30+
private Document doc;
31+
private Element root;
32+
33+
public A2ZXmlReader(String name) throws IOException {
34+
urlPath = name;
35+
try {
36+
createDocument();
37+
} catch (ParserConfigurationException e) {
38+
e.printStackTrace();
39+
} catch (SAXException e) {
40+
e.printStackTrace();
41+
} catch (IOException e) {
42+
e.printStackTrace();
43+
}
44+
}
45+
46+
public void createDocument() throws ParserConfigurationException, SAXException, IOException {
47+
InputStream is = openStream(urlPath);
48+
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
49+
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
50+
doc = docBuilder.parse(is);
51+
doc.getDocumentElement().normalize(); // // strips out empty "text" nodes?
52+
53+
root = doc.getDocumentElement();
54+
}
55+
56+
public Element getRoot() {
57+
return root;
58+
}
59+
60+
// This method recursively traverses the XML Tree (starting from currElement)
61+
public void traverseXML (Node currNode)
62+
{
63+
// If it's an Element, spit out the Name
64+
if (currNode.getNodeType() == Node.ELEMENT_NODE) {
65+
System.out.print(currNode.getNodeName() + ": ");
66+
// If it's a "Text" node, take the value
67+
// These will come one after another and therefore appear in the right order
68+
} else if (currNode.getNodeType() == Node.TEXT_NODE) {
69+
System.out.println(currNode.getNodeValue().trim());
70+
}
71+
72+
// Display any attributes
73+
if (currNode.hasAttributes()) {
74+
NamedNodeMap attributes = currNode.getAttributes();
75+
for (int i = 0; i < attributes.getLength(); i++) {
76+
Node attr = attributes.item(i);
77+
System.out.println(" " + attr.getNodeName() + ": " + attr.getNodeValue());
78+
}
79+
}
80+
81+
// Check any children
82+
if (currNode.hasChildNodes()) {
83+
// Get the list of children
84+
NodeList children = currNode.getChildNodes();
85+
// Go through all the chilrden
86+
for (int i = 0; i < children.getLength(); i++) {
87+
// Search each Node
88+
Node n = children.item(i);
89+
traverseXML(n);
90+
}
91+
}
92+
}
93+
94+
// This method recursively searches for an Element (starting from currElement) with the name elementName
95+
public Element findElement(Element currElement, String elementName)
96+
{
97+
// So far we've found nothing, i.e. null
98+
Element found = null;
99+
// If the current element is what we want, hooray we are done!
100+
if (currElement.getTagName().equals(elementName)) {
101+
found = currElement;
102+
// Otherwise, if there are children, let's check those
103+
} else if (currElement.hasChildNodes()) {
104+
// Get the list of children
105+
NodeList children = currElement.getChildNodes();
106+
// As long as we haven't found it (i.e. found is still null)
107+
for (int i = 0; i < children.getLength() && found == null; i++) {
108+
// Search each Element type node
109+
Node n = children.item(i);
110+
if (n.getNodeType() == Node.ELEMENT_NODE) {
111+
Element e = (Element) n;
112+
found = findElement(e, elementName);
113+
}
114+
}
115+
}
116+
return found;
117+
}
118+
119+
120+
// Find all elements
121+
public void fillArrayList(Element currElement, String elementName, ArrayList a) {
122+
// So far we've found nothing, i.e. null
123+
Element found = null;
124+
// If the current element is what we want, hooray we are done!
125+
if (currElement.getTagName().equals(elementName)) {
126+
found = currElement;
127+
// Otherwise, if there are children, let's check those
128+
} else if (currElement.hasChildNodes()) {
129+
// Get the list of children
130+
NodeList children = currElement.getChildNodes();
131+
// As long as we haven't found it (i.e. found is still null)
132+
for (int i = 0; i < children.getLength(); i++) {
133+
// Search each Element type node
134+
Node n = children.item(i);
135+
if (n.getNodeType() == Node.ELEMENT_NODE) {
136+
Element e = (Element) n;
137+
fillArrayList(e, elementName,a);
138+
}
139+
}
140+
}
141+
if (found != null) a.add(found);
142+
}
143+
144+
145+
146+
147+
148+
149+
private InputStream openStream(String urlpath) {
150+
InputStream stream = null;
151+
try {
152+
URL url = new URL(urlpath);
153+
stream = url.openStream();
154+
return stream;
155+
} catch (MalformedURLException e) {
156+
System.out.println("Something's wrong with the URL: "+ urlpath);
157+
e.printStackTrace();
158+
} catch (IOException e) {
159+
System.out.println("there's a problem downloading from: "+ urlpath);
160+
e.printStackTrace();
161+
}
162+
return stream;
163+
}
164+
165+
166+
}
167+
168+
169+
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package simpleML;
2+
3+
import processing.core.*;
4+
import java.lang.reflect.*;
5+
6+
public class HTMLRequest implements Runnable {
7+
8+
PApplet parent;
9+
Method eventMethod;
10+
Thread runner;
11+
boolean available = false;
12+
String text;
13+
String url;
14+
15+
public HTMLRequest(PApplet parent, String url_) {
16+
this.parent = parent;
17+
this.text = "";
18+
this.url = url_;
19+
parent.registerDispose(this);
20+
try {
21+
eventMethod = parent.getClass().getMethod("netEvent", new Class[] {
22+
HTMLRequest.class }
23+
);
24+
}
25+
catch (Exception e) {
26+
//System.out.println("Hmmm, event method no go?");
27+
}
28+
}
29+
30+
public void makeRequest() {
31+
runner = new Thread(this);
32+
runner.start();
33+
}
34+
35+
public boolean available() {
36+
return available;
37+
}
38+
39+
40+
public String readRawSource() {
41+
available = false;
42+
return text;
43+
}
44+
45+
46+
public void run() {
47+
try {
48+
A2ZUrlReader urlReader = new A2ZUrlReader(url);
49+
text = urlReader.getContent();
50+
available = true;
51+
if (eventMethod != null) {
52+
try {
53+
eventMethod.invoke(parent, new Object[] {
54+
this }
55+
);
56+
}
57+
catch (Exception e) {
58+
System.err.println("Oopsies.");
59+
e.printStackTrace();
60+
eventMethod = null;
61+
}
62+
}
63+
} catch (Exception e) {
64+
65+
}
66+
}
67+
68+
/**
69+
* Called by applets to stop.
70+
*/
71+
public void stop() {
72+
runner = null; // unwind the thread
73+
}
74+
75+
/**
76+
* Called by PApplet to shut down
77+
*/
78+
public void dispose() {
79+
stop();
80+
System.out.println("calling dispose");
81+
}
82+
83+
}

0 commit comments

Comments
 (0)