Version: 5.2.1.6

3 5/15: Subversion, Redux; Moar Recursive Methods with XML

The goal of this lab is to get Subversion revision control system working. In the second part of the lab, you will practice writing more recursive data definitions and their methods.

Getting started with Subversion

Let’s try this again...

Subversion is a revision control system. It helps you keep track of the changes to a document over the course of its lifetime. It can be used for any kind of document: papers, notes, spreadsheets, web pages, etc., but it is often used for the source code of programs. We’re going to be using Subversion (svn) for the remainder of the course in order to track changes to code written for labs and homeworks and we will use it as the mechanism for turing in and receiving feedback on assignments.

Exercise 1. Skim the course notes on Subversion.

Subversion is available on all the lab machines, but you may also want to install a Subversion client (there are several to choose from) on your personal computer.

The course repository, cs2510summer2012, has been created with a directory for each pair and a directory for each student. You will only have access to access to your user directory and your pair directory. No one but the course staff has access to your user directory and no one but your partner and the course staff has access to your pair directory.

Exercise 2. Check out a copy of your user directory in the cs2510summer2012 repository. Verify that your solution to assignment 1 is in it.

You should use this directory to store lab material, notes, or anything else you’d like that shouldn’t be shared with others (beside the staff). We will use this directory to communicate (non-pair) grades back to you.

Remember that no changes are made to the repository until you successfully commit. Until then, any changes you make are only stored on the computer you’re working on. Therefore you should commit early and often (like voting).

Exercise 3. Check out a copy of your pair directory in the cs2510summer2012 repository.

You should use this directory to store assignments. We will use this directory to communicate pair grades back to you and your partner.

Exercise 4. Within your user directory, create two directories called lab1 and lab2. Add your Java source code to for each lab to these directories. Check these directories in to the repository and commit.

XML

XML, the “Extensible Markup Language”, is a ubiquitous format for exchanging data on the interwebs. It is also used extensively in modern office-productively software produced by Microsoft, Apple, and the open-source community. XML is fairly simple: it consists of content that is “marked up” with tags.

Here are some simple examples of XML:

I am XML!

The above is just plain content with no tags. We can tag certain parts of the content with a pair of open and close tags to delimit the tagged region:

I am <yell>XML</yell>!

Here the content XML is tagged with the yell tag. The tags can nest, as in:

I am <yell><italic>X</italic>ML</yell>!

Here the content XML is again tagged with yell, but the X is also tagged with the italic tag.

Tags can also carry attributes that associate data with a tag. For example, we may want to yell at a certain volume:

I am <yell volume="30db"><italic>X</italic>ML</yell>!

Here the yell tag carries a volume attribute with the value 30db. Moreover, you can add an arbitrary number of attributes to a tag, so we can specifiy both the volume and the duration of a yell:

I am <yell volume="30db" duration="5sec"><italic>X</italic>ML</yell>!

If we step back and think about how to represent XML, we arrive at the following sketch:

XML is either just plain content (no tags), or it is tagged XML, i.e. it is some tag, some list of attributes and values, and some XML.

This leads to a pretty straightforward (recursive) data definition. Unfortunately, it’s not quite adequate for the examples we’ve written. In particular, we want the following, which is valid XML, to be representable:

<first>Bake a cake.</first> <then>Eat <yell>it!</yell></then>

Here we see that XML can really be an arbitrarily long sequence of plain content and tagged content and that within a tag, there is also a sequence. To revise our sketch we have:

XML is a sequence of XML fragments. An XML fragment is either plain content (no tags), or tagged XML, i.e. some tag, some attributes and values, and some XML.

Exercise 5. Design class definitions for representing XML. Translate all of the above examples into your representation.

Now that you’ve done the hard part of designing the data for representing XML, you can write programs to operate on XML. If your data definition is good, these should be easy to write. If it’s not, well... they will probably be more challenging.

Let’s start with some simple ones:

Exercise 6. Design a contentLength method computes the length of the content in an XML document. The tags and attributes should not contribute to the length.

Exercise 7. Design a maxNest method that computes the maximum number of tag nesting in a document.

Exercise 8. Design a hasTag method that determines if an XML document contains a given tag.

Exercise 9. Design a hasAttribute method that determines if an XML document contains a given attribute.

Now for some slightly more involved methods:

Exercise 10. Design a hasAttributeInTag method that determines if an XML document contains a given attribute within a given tag.

Exercise 11. Design a mute method replaces the value of any volume attribute with a value of 0db.

Exercise 12. Design a muteYell method replaces the value of any volume attribute within a yell tag with a value of 0db.

Exercise 13. Design a renderAsString method that converts XML to plain strings by removing all tags and attributes.

Exercise 14. Design a updateAttribute method that is given an attribute name and value and updates all occurrence of that attribute to the given value.

Exercise 15. Design a renameTag method that changes all occurrences of a given tag to another given tag.

Be sure to commit your work to svn before you leave.