Hints for Assignment I Department of Information Technology Uppsala University
JDOM
JDOM JDOM is an open source library for Javaoptimized 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") );
Output is very flexible Exporting to XML 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
The List is live! Active List 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
Abstraction Iterators 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 Layoutmanager 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