JDOM
JDOM
§ JDOM is an open source library for Java- optimized XML data manipulations
§ JDOM was created to...
• Be straightforward for Java programmers
• Use the power of the Java language (method overloading, collections, reflection)
• Hide the complexities of XML wherever possible
Documents
§ Documents are represented by org.jdom.Document
§ They may be constructed from scratch:
Document doc = new Document(new Element("root"));
§ Or built from a file, stream, systemID, URL:
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(url);
JDOM
§ To create a simple document in JDOM:
Document doc = new Document();
Element e = new Element("root");
e.setText("This is the root");
doc.addContent(e);
Same thing (Power User)
// Needs more documenting, though
Document doc = new Document(
new Element("root").setText( "This is the root") );
Exporting to XML
§ Output is very flexible
Document doc = new Document(...);
XMLOutputter outp = new XMLOutputter();
// Raw output
outp.output(doc, fileOutputStream);
// Compressed output outp.setTextTrim(true);
outp.output(doc, socket.getOutputStream());
// Pretty output outp.setIndent(" ");
outp.setNewlines(true);
outp.output(doc, System.out);
Element Class
§ The XML tree consists of Elements
// Get the root element
Element root = doc.getRootElement();
// Get a list of all child elements List allChildren = root.getChildren();
// Get only elements with a given name
List namedChildren = root.getChildren("name");
// Get the first element with a given name Element child = root.getChild("name");
NOTE! The List stuctures are java.util.List
Active List
§ The List is live!
List allChildren = root.getChildren(); // !!!!
// Remove the fourth child allChildren.remove(3);
// Remove children named "jack”
allChildren.removeAll(root.getChildren("jack"));
root.removeChildren("jack"); // convenience // Add a new child
allChildren.add(new Element("jane"));
root.addContent(new Element("jane")); // conv.
allChildren.add(0, new Element("first"));
Element Content
§ An Element may have text content
<description>
A cool demo </description>
// The text is directly available // Returns "\n A cool demo\n"
String desc = element.getText();
// There's a convenient shortcut // Returns "A cool demo"
String desc = element.getTextTrim();
Element Content
§ Text content can be changed directly
element.setText("A new description");
§ Special chars are interpreted correctly element.setText("<xml> content");
Unnecessary knowledge
§ Orange (UK Telecom) used JDOM to support their B2B system "Orange API”
• A "Where is my nearest" WAP service handles 1,000 requests per minute
• That's just 1 of 40 services built on XML
• All their XML processing uses JDOM
• JDOM supports an XML-RPC/SOAP framework now deployed globally
• Source: Jools Enticknap, Orange
XML
XML
If you have problems with the XML part…
Read the suggested Links on the homepage
Assignment hints
Other things to consider
§ Cohesion – One class performs a set of closely related actions
§ Decoupling – classes should not depend on the structure of each other
• Communication through accessors and methods
§ Flexibility – awareness of the need for change in the solutions.
Collections
§ TIJ/Eckel: chapter 11, Collections of Objects
§ Read about containers, skip arrays
§ List – stores objects in sequence
• Important for the use of JDOM
§ Set – stores unique objects in a space
§ Map – manages key/value pairs
Iterators
§ Abstraction
• you don’t care about the storage structure
§ Iterators are “lightweight” and do not consume a lot of resources
Iterator it = collection.iterator();
while (it.hasNext()) {
Object o = it.next();
}
Swing
§ Lightweight components
• Are not created by the operating system but by the jvm
• Reacts in the same way in different environments or operating systems
§ Highly extendible Java library
Java Beans
§ JavaBeans are reusable software components for Java that can be manipulated visually in a builder tool.
• The class must have a public default constructor.
• The class properties must be accessible using getters, setters, and other methods (so-called accessor methods and mutator methods), following a standard naming
convention.
• The class should be serializable.
Access control!
§ Public: can be accessed from anywhere.
§ Private: can be accessed only from within the class in which the field or method is declared.
§ Protected: can be accessed within the package, or from outside the package by a subclass of the class in which the members declared.
§ Package: can be accessed within the package (it is the default)
ActionListener (AL)
// This is not OOP
class Foo implements ActionListener { Foo() {
JButton b = new JButton();
b.addActionListener(this); // ugly!
public void actionPerformed(ActionEvent e) { // do the stuff
} }
}
AL 2
// Sometimes ok, mostly not class Foo {
class Bar implements ActionListener {
public void actionPerformed(ActionEvent e) { // doit
} }
Foo() {
JButton b = new JButton();
b.addActionListener(new Bar());
} }
AL 3
// Good!
class Foo { Foo() {
JButton b = new JButton();
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { // doit
} });
} }
AL 2.1
class Foo {
class Bar implements ActionListener {
public void actionPerformed(ActionEvent e) { // doit
} }
Foo() {
Bar bar = new Bar();
JButton b1 = new JButton();
JButton b2 = new JButton();
b1.addActionListener(bar); // smart, shares action b2.addActionListener(bar);
} }
AL 4 – scope?
class Foo { private int i;
Foo() {
int k = 0;
final m = 0;
JButton b = new JButton();
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { // can see i and m, not k
} });
} }
INTERFACE
Thinking about the interface
§ First attempt - keep it simple
§ Get it working
§ Think of alternative designs
• Different in principle, not detail
• What are the effects on the differences?
• What are the benefits of each alternative?
• What are the difficulties with each alternative?
LayoutManagers
§ BorderLayout (!)
§ BoxLayout
§ CardLayout
§ FlowLayout
§ GridBagLayout (!)
§ GridLayout
§ (GroupLayout)
§ SpringLayout
Learn how to use them!
A simple interface structure
Window Frame
[Jframe]
Panel
Jpanel Layout- manager
Widgets
Jbutton
Jlabel
Jslider
Etc.
Versionings systems (CVS, SVN)
Subversion and CVS
§ A system for versioning in software projects
§ IDE supported
§ One server per group
• Common file structure
• Password protected
DIARY
Project Diary
§ Individual diaries
§ Automatic time reporting
§ Can be read by all in group
§ Should be used daily
• Whenever you do something on the project
§ Part of the examination
§ Details will be sent via email
Group work
§ Make sure that all group members have access to the SVN system
§ Check that you can access the project diary
§ Exchange email addresses (!)
§ Make a plan for the work
• Remember that project groups can work on distance:
• SVN, MSN, Skype, Diary notes
START
Assignment 1
§ Create the application with a simple interface
§ Make sure that it runs properly
§ Document the code (!)
§ Pack in a jar-file and send in…
Problems
§ Assignment, Java: Simon, (Lars)
§ Diary: Mikael
§ SVN: Simon
§ Other problems: Lars
§ Simon: room 1157
§ Mikael: room 1254
§ Lars: room 11257
Check your diary and get in contact via mail(hint: use Studentportalen)
If you have problems finding your group members get in touch with us as soon as
possible