Docsity

Processing as a Java-based Expressive Medium: Understanding Java-mode and HTML Parsing, Assignments of Communication

Toggle sidebar
bg1
1
LCC 6310
The Computer as an
Expressive Medium
Lecture 13
Overview
Programming concepts
Processing in Java-mode
Libraries, using other classes/libraries
HTML parsing, intro to HTML parser
Assignment 3 questions?
We started with Processing in…
// any code here, no methods
line(0,0,20,20);
We started with Processing in…
// any code here, no methods
line(0,0,20,20);
// methods!
// global vars
int a;
// methods
void setup(){
}
void draw(){
}
pf3
pf4
pf5
pf8

Partial preview of the text

Download Processing as a Java-based Expressive Medium: Understanding Java-mode and HTML Parsing and more Assignments Communication in PDF only on Docsity!

LCC 6310

The Computer as anExpressive Medium

Lecture 13

Overview

Programming concepts

Processing in Java-modeLibraries, using other classes/librariesHTML parsing, intro to HTML parser

Assignment 3 questions?

We started with Processing in…

// any

code

here, no

methods

line(0,0,20,20);

We started with Processing in…

// any

code

here, no

methods

line(0,0,20,20);

// methods!// global varsint a;// methodsvoid setup(){} void draw(){}

We started with Processing in…

// any

code

here, no

methods

line(0,0,20,20);

// methods!// global varsint a;// methodsvoid setup(){} void draw(){}

// ...with classes// (all

of the above

and

then)

class Emotion

// fields// constructor// methods }

We started with Processing in…

// any

code

here, no

methods

line(0,0,20,20);

// methods!// global varsint a;// methodsvoid setup(){} void draw(){}

// ...with classes// (all

of the above

and

then)

class Emotion

// fields// constructor// methods }

// ...and subclasses!// (all

of the above, and)

class Happy

extends

Emotion {

// new fields// constructor// methods }

But in fact…

Everything you write in Processing is actually within a full-up Javaclass:

// Java

mode!

class ThatsCrazyTalk

extends PApplet {

// setup()

and draw() as

normally...

// methods// classes

and subclasses

Java Mode

Processing's Java-mode allows you to program in pure Java

You can import classes that aren't normally imported into a Processing appImporting means making a classes available to your program - the JavaAPI docs tell you what package classes are in so you know what to import

To go into Java-mode, create a class that extends PApplet

Normally all Processing applets extend PApplet behind the scenesTake a look at any compiled Processing app

So, those top-level functions that weren’t methods of any class reallyare methods of a class, a class extending PApplet

setup(), draw(), etc. are methods of the class extending PApplet

What do you mean parse HTML?

(and why would you want to do it?)

Parsing means to walk through the structure of a file (not just look atit character-by-character, word-by-word)

Look at an HTML file

The structure of an HTML file is the tag structure

So parsing means to walk through and interpret the tags

If you can parse HTML files, that means you can pull content out ofweb pages and do stuff with itProcedural manipulation of web content!

Results 1 - 20 of about 202 formatrix red … some text ==

some text

HTML parsers

There are a variety of Java HTML parsers available

proHTML is a Processing library for HTML handling

http://www.texone.org/prohtml/…easy!

but poorly documented, and chokes on any malformed HTML…

Java contains HTML parsing capabilities in the swing package

We'll look at this one today

HTML Parser is another free parser that we'll look at later

http://htmlparser.sourceforge.net/

There are many others!

There are also other kinds of web services related packages

Switchboard is a web services library for Processing written by IDTgraduate Jeff Crouse

Micah will go over the usage of this in one of the tutorialshttp://www.realtimeart.com/switchboard/

Swing HTML parsing approach

The entry point into the HTML parser is the class ParserDelegator

ParserDelegator parses an HTML document passed in as a Reader andnotifies the passed-in ParserCallback object as to the state of the parsing

ParserCallback implements the following methods

public void flush() throws BadLocationExceptionpublic void handleText(char[] data, int pos)public void handleComment(char[] data, int pos)public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos)public void handleEndTag(HTML.Tag t, int pos)public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos)public void handleError(String errorMsg, int pos)public void handleEndOfLineString(String eol)

The programmer (you!) then creates a ParserCallback subclass and fillsin these methods to make the parser do what they wantWe'll try this out, but first a method overview and some new concepts…

handleText

public void handleText(char[] data, int pos)

Handles anything that’s not a tag (the text between tags)data is an array of characters containing the textpos is the position in the file

handleSimpleTag

public void handleSimpleTag(HTML.Tag tag,

MutableAttributeSet attrib,int pos)

Called for tags like IMGtag stores the name of the tagattrib stores any attributespos is the position in the file

Example:

The tag is imgThe attributes are src, alt, align, width (with their respective values)

handleStartTag

public void handleStartTag(HTML.Tag tag,

MutableAttributeSet attrib,int pos)

Called for tags like BODYtag stores the name of the tagattrib stores any attributespos is the position in the file

Example:

The tag is bodyThe attributes are bgcolor, topmargin, leftmargin, marginheight,marginwidth (with their respective values)

handleEndTag

public void handleEndTag(HTML.Tag tag, int pos)

Called for tags like tag stores the name of the tagpos is the position in the file

Exceptions

An exception is an event that occurs during the execution of aprogram that disrupts the normal flow of instructionsCode that might cause certain kinds of exceptions must be enclosedby one of the following:

A try statement that catches the exception

try {

// some code that throws an exception } catch (Exception e) {

// what to do in case the exception happens }

A method that specifies that it can throw the exception

void myNastyMethod() throws Exception {

// some code that throws an exception }

We'll see these in the upcoming examples…

Subclassing ParserCallback

Check the API docs:

javax.swing.text.html.parser.ParserDelegatorjavax.swing.text.html.HTMLEditorKit.ParserCallback

A compact way to do this:

HTMLEditorKit.ParserCallback

callback

new

HTMLEditorKit.ParserCallback

override

the

handleText

method

public

void

handleText(char[]

data,

int

pos)

System.out.println(data); } };

We've subclassed ParserCallback inline with declaring a variablenamed callback and creating an instance of it!

Let's test it out

Let's try this on a sample HTML file as follows:

try

BufferedReader reader =

new

BufferedReader(new

InputStreamReader(openStream("test.html"))); new

ParserDelegator().parse(reader,

callback,

true);

} catch

(IOException

e) {

e.printStackTrace(); }

Let's see this in Processing…

How about parsing something online?

All we need to do is open a URL instead of a local File… try {

URL url = new URL(href);InputStream instream = url.openStream();Reader reader = new BufferedReader(new InputStreamReader(instream));new ParserDelegator().parse(reader, callback, true);instream.close(); } catch (IOException e) {

e.printStackTrace();

} Let's see this in Processing…

And a little more complexity

A few things to do:

Move our ParserCallback subclass into a separate Java class calledMyParserCallback.javaParse based on tags and attributes

Let's look at these changes in Processing…

Assignment 3

Posted online, due FridayA3-01:

Create a subclass of PImage that implements a mosaic(int blockSize) method. TheblockSize parameter specifies how big the mosaic block is (e.g. blockSize = 4 wouldmean the mosaic block size is 4 pixels by 4 pixels). The mosaic method shouldreplace each block of pixels in the image (e.g. if blockSize = 4, each block of 4 by 4pixels) with the average color value of the pixels in that block. Look at thePixelate->Mosaic filter in photoshop for an example of what this image operationdoes. Demonstrate your new class by drawing an image with several different blocksizes.

A3-02:

Write a small app that demonstrates kinetic text. Your app should allow the user totype something and move the text around in some way while they type. Forexample, the user might type text on a line, but slowly the words or letters startdrifting apart, or perhaps the line starts bending, or the words and letters flutter tothe bottom of the screen, etc. Of course you shouldn't exactly copy any of thetypographic examples in Processing or that you find on the web (though using suchexamples for inspiration, as a place to start modifying code, etc. is fine).

Remember…

For Thursday this week: Theory Readings

Two presenters (you know who you are!)Everyone else: prepare one discussion question for each reading

As We May Think - Vannevar Bush, NMR pp. 35-47Mythinformation - Langdon Winner, NMR pp. 587-