LCC 6310 Computation as an Expressive Medium - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

LCC 6310 Computation as an Expressive Medium

Description:

Processing Java-mode (programming in raw java) Introducing Parsing HTML ... To go into Java mode, create a class that extends BApplet ... – PowerPoint PPT presentation

Number of Views:56
Avg rating:3.0/5.0
Slides: 14
Provided by: michael74
Category:

less

Transcript and Presenter's Notes

Title: LCC 6310 Computation as an Expressive Medium


1
LCC 6310Computation as an Expressive Medium
  • Lecture 8

2
Outline
  • Programming concepts
  • Processing Java-mode (programming in raw java)
  • Introducing Parsing HTML
  • HTMLEditorKit.ParserCallback
  • HTMLReader
  • Discuss reading
  • Mythinformation

3
Java Mode
  • Allows you to program in pure Java
  • Lets you import classes that arent 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
    BApplet
  • Normally all Processing applets extend BApplet
    behind the scenes
  • Take at any compiled Processing app
  • So, those top-level functions that werent
    methods of any class really are methods of a
    class, a class extending BApplet
  • setup(), loop(), etc. are methods of the class
    extending BApplet

4
Template of a Java-mode program
  • class MyProgram extends BApplet
  • void setup()
  • void loop()
  • void myTopLevelMethod()
  • class Text // Text is just an example
  • int xPos, yPos
  • String word
  • Notice that any classes you define are inside the
    top class

5
Why use Java-mode again?
  • Java-mode gives you access to the entire Java SDK
  • We need access to some SDK classes for HTML
    parsing that Processing doesnt make visible by
    default
  • Java-mode helps you to understand how Processing
    is built on-top of Java
  • All those magic functions and variables are
    just methods and fields of BApplet that your
    program inherits

6
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 at it 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 of web pages and do stuff with
    it
  • Procedural manipulation of web content

7
Basic approach
  • Use two classes to parse
  • One class reads info from a URL HTMLParser
  • The other class is used by HTMLParser to process
    tags child of HTMLEditorKit.ParserCallback
  • HTMLParser recognizes when a tag appears (ltTAGgt)
    and calls appropriate methods on the
    ParserCallback class (start-tags, end-tags,
    simple-tags, text, etc.)
  • The programmer (ie. you), fill in the
    ParserCallback methods to do whatever you want
    when you see different kinds of tags

8
Running the example
  • Weve written HTMLParser for you
  • To access it, it must be in the data directory of
    your project
  • Simplest thing will be just to copy the code from
    the website and put the directory in your default
    sketchbook directory

9
handleSimpleTag
  • public void handleSimpleTag(HTML.Tag tag,
    MutableAttributeSet attrib, int pos)
  • Called for tags like IMG
  • tag stores the name of the tag
  • attrib stores any attributes
  • pos is the position in the file
  • Example ltimg srcimage.gif alttext
    description of image alignright width10gt
  • The tag is img
  • The attributes are src, alt, align, width (with
    their respective values)

10
handleStartTag
  • public void handleStartTag(HTML.Tag tag,
    MutableAttributeSet attrib, int pos)
  • Called for tags like BODY
  • tag stores the name of the tag
  • attrib stores any attributes
  • pos is the position in the file
  • Example ltbody bgcolorFFFFFF topmargin0
    leftmargin0 marginheight0 marginwidth0gt
  • The tag is body
  • The attributes are bgcolor, topmargin,
    leftmargin, marginheight (with their respective
    values)

11
handleEndTag
  • public void handleEndTag(HTML.Tag tag, int pos)
  • Called for tags like lt/agt
  • tag stores the name of the tag
  • pos is the position in the file

12
handleText
  • public void handleText(char data, int pos)
  • Handles anything thats not a tag (the text
    between tags)
  • data is an array of characters containing the
    text
  • pos is the position

13
Filling in these methods
  • You fill in these methods to do whatever
    processing you want
  • In the image collage example
  • handleSimpleTag is looking for images
  • handleStartTag is looking for the start of
    anchors and follows links
Write a Comment
User Comments (0)
About PowerShow.com