LCC 6310 Computation as an Expressive Medium Lecture 8.

13
LCC 6310 Computation as an Expressive Medium Lecture 8 Lecture 8

Transcript of LCC 6310 Computation as an Expressive Medium Lecture 8.

Page 1: LCC 6310 Computation as an Expressive Medium Lecture 8.

LCC 6310Computation as an Expressive

Medium

LCC 6310Computation as an Expressive

Medium

Lecture 8Lecture 8

Page 2: LCC 6310 Computation as an Expressive Medium Lecture 8.

OutlineOutline

• Programming conceptsProgramming concepts• Processing Java-mode (programming in raw java)

• Introducing Parsing HTML

• HTMLEditorKit.ParserCallback

• HTMLReader

• Discuss readingDiscuss reading• Mythinformation

Page 3: LCC 6310 Computation as an Expressive Medium Lecture 8.

Java ModeJava Mode

• Allows you to program in pure JavaAllows you to program in pure Java• Let’s you import classes that aren’t normally imported into a Processing app

• Importing means making a classes available to your program – the Java API docs tell you what package classes are in so you know what to import

• To go into Java mode, create a class that extends BAppletTo go into Java mode, create a class that extends BApplet• Normally all Processing applets extend BApplet behind the scenes

• Take at any compiled Processing app

• So, those top-level functions that weren’t methods of any So, those top-level functions that weren’t methods of any class really are methods of a class, a class extending BApplet class really are methods of a class, a class extending BApplet • setup(), loop(), etc. are methods of the class extending BApplet

Page 4: LCC 6310 Computation as an Expressive Medium Lecture 8.

Template of a Java-mode programTemplate of a Java-mode program

class MyProgram extends BApplet {class MyProgram extends BApplet {void setup() { … }void setup() { … }void loop() { … }void loop() { … }

void myTopLevelMethod() { … }void myTopLevelMethod() { … }

class Text { // Text is just an example class Text { // Text is just an example int xPos, yPos;int xPos, yPos;String word;String word;……

}}}}

Notice that any classes you define are Notice that any classes you define are insideinside the top class the top class

Page 5: LCC 6310 Computation as an Expressive Medium Lecture 8.

Why use Java-mode again?Why use Java-mode again?

• Java-mode gives you access to the entire Java-mode gives you access to the entire Java SDKJava SDK• We need access to some SDK classes for HTML parsing

that Processing doesn’t make visible by default

• Java-mode helps you to understand how Java-mode helps you to understand how Processing is built on-top of JavaProcessing is built on-top of Java• All those “magic” functions and variables are just

methods and fields of BApplet that your program inherits

Page 6: LCC 6310 Computation as an Expressive Medium Lecture 8.

What do you mean parse HTML?(and why would you want to do it?)

What do you mean parse HTML?(and why would you want to do it?)• ““Parsing” means to walk through the structure of a Parsing” means to walk through the structure of a

file (not just look at it character-by-character, word-file (not just look at it character-by-character, word-by-word)by-word)• Look at an HTML file

• The structure of an HTML file is the tag structure 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 If you can parse HTML files, that means you can pull content out of web pages and content out of web pages and do do stuff with itstuff with it

• Procedural manipulation of web contentProcedural manipulation of web content

Page 7: LCC 6310 Computation as an Expressive Medium Lecture 8.

Basic approachBasic approach

• Use two classes to parseUse two classes to parse

• One class reads info from a URL – HTMLParserOne class reads info from a URL – HTMLParser• The other class is used by HTMLParser to process tags The other class is used by HTMLParser to process tags

– child of HTMLEditorKit.ParserCallback– child of HTMLEditorKit.ParserCallback

• HTMLParser recognizes when a tag appears (<TAG>) HTMLParser recognizes when a tag appears (<TAG>) and calls appropriate methods on the ParserCallback and calls appropriate methods on the ParserCallback class (start-tags, end-tags, simple-tags, text, etc.)class (start-tags, end-tags, simple-tags, text, etc.)

• The programmer (ie. The programmer (ie. youyou), fill in the ParserCallback ), fill in the ParserCallback methods to do whatever you want when you see methods to do whatever you want when you see different kinds of tagsdifferent kinds of tags

Page 8: LCC 6310 Computation as an Expressive Medium Lecture 8.

Running the exampleRunning the example

• We’ve written HTMLParser for youWe’ve written HTMLParser for you

• To access it, it must be in the data To access it, it must be in the data directory of your projectdirectory of your project

• Simplest thing will be just to copy the Simplest thing will be just to copy the code from the website and put the code from the website and put the directory in your default sketchbook directory in your default sketchbook directory directory

Page 9: LCC 6310 Computation as an Expressive Medium Lecture 8.

handleSimpleTaghandleSimpleTag

• public void handleSimpleTag(HTML.Tag public void handleSimpleTag(HTML.Tag tagtag, , MutableAttributeSet MutableAttributeSet attribattrib, int , int pospos))• Called for tags like IMG

• tag stores the name of the tag

• attrib stores any attributes

• pos is the position in the file

• Example: Example: <img src=“image.gif” alt=“text description <img src=“image.gif” alt=“text description of image” align=“right” width=“10”>of image” align=“right” width=“10”>• The tag is img

• The attributes are src, alt, align, width (with their respective values)

Page 10: LCC 6310 Computation as an Expressive Medium Lecture 8.

handleStartTaghandleStartTag

• public void handleStartTag(HTML.Tag public void handleStartTag(HTML.Tag tagtag, , MutableAttributeSet MutableAttributeSet attribattrib, int , int pospos))• Called for tags like BODY

• tag stores the name of the tag

• attrib stores any attributes

• pos is the position in the file

• Example: Example: <body bgcolor=“#FFFFFF” topmargin=“0” <body bgcolor=“#FFFFFF” topmargin=“0” leftmargin=“0” marginheight=“0” marginwidth=“0”>leftmargin=“0” marginheight=“0” marginwidth=“0”>• The tag is body

• The attributes are bgcolor, topmargin, leftmargin, marginheight (with their respective values)

Page 11: LCC 6310 Computation as an Expressive Medium Lecture 8.

handleEndTaghandleEndTag

• public void public void handleEndTag(HTML.Tag tag, int handleEndTag(HTML.Tag tag, int pos)pos)•Called for tags like </a>

• tag stores the name of the tag

•pos is the position in the file

Page 12: LCC 6310 Computation as an Expressive Medium Lecture 8.

handleTexthandleText

• public void handleText(char[] data, int public void handleText(char[] data, int pos)pos)• Handles anything that’s not a tag (the text between

tags)

• data is an array of characters containing the text

• pos is the position

Page 13: LCC 6310 Computation as an Expressive Medium Lecture 8.

Filling in these methodsFilling in these methods

• You fill in these methods to do You fill in these methods to do whatever processing you wantwhatever processing you want

• In the image collage exampleIn the image collage example•handleSimpleTag is looking for images

•handleStartTag is looking for the start of anchors and follows links