• No results found

Assignment I Hints for

N/A
N/A
Protected

Academic year: 2022

Share "Assignment I Hints for"

Copied!
39
0
0

Loading.... (view fulltext now)

Full text

(1)
(2)

JDOM

(3)

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

(4)

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);

(5)

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);

(6)

Same thing (Power User)

// Needs more documenting, though

Document doc = new Document(

new Element("root").setText( "This is the root") );

(7)

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);

(8)

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

(9)

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"));

(10)

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();

(11)

Element Content

§  Text content can be changed directly

element.setText("A new description");

§  Special chars are interpreted correctly element.setText("<xml> content");

(12)

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

(13)

XML

(14)

XML

If you have problems with the XML part…

Read the suggested Links on the homepage

(15)

Assignment hints

(16)

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.

(17)

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

(18)

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();

}

(19)

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

(20)

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.

(21)

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)

(22)

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

} }

}

(23)

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());

} }

(24)

AL 3

// Good!

class Foo { Foo() {

JButton b = new JButton();

b.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) { // doit

} });

} }

(25)

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);

} }

(26)

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

} });

} }

(27)

INTERFACE

(28)

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?

(29)

LayoutManagers

§  BorderLayout (!)

§  BoxLayout

§  CardLayout

§  FlowLayout

§  GridBagLayout (!)

§  GridLayout

§  (GroupLayout)

§  SpringLayout

Learn how to use them!

(30)

A simple interface structure

Window Frame

[Jframe]

Panel

Jpanel Layout- manager

Widgets

Jbutton

Jlabel

Jslider

Etc.

(31)

Versionings systems (CVS, SVN)

(32)

Subversion and CVS

§  A system for versioning in software projects

§  IDE supported

§  One server per group

•  Common file structure

•  Password protected

(33)

DIARY

(34)

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

(35)

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

(36)

START

(37)

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…

(38)

Problems

§  Assignment, Java: Simon, (Lars)

§  Diary: Mikael

§  SVN: Simon

§  Other problems: Lars

§  Simon: room 1157

§  Mikael: room 1254

§  Lars: room 11257

(39)

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

References

Related documents

Through a field research in Lebanon, focusing on the Lebanese Red Cross and their methods used for communication, it provides a scrutiny of the theoretical insights

The storing of the food can be divided in three parts, make food last longer, plan the meals and shopping and keep track on the food we have.. The final result is the smart

Keywords: design, methodology, fashion, packaging, garments, structure, construction patterns, packaging nets, construction system, menswear, body, product... To understand

pedagogue should therefore not be seen as a representative for their native tongue, but just as any other pedagogue but with a special competence. The advantage that these two bi-

Establishing a language for innovation to institutionalise a definition, use corporate culture as a control system, standardising internal processes, fostering a learning

The second phase of the data analyses has to do with the sub question, “How can parcel (package) delivery firms minimize high send – agains (returns) in the Supply Chain

But even though the playing can feel like a form of therapy for me in these situations, I don't necessarily think the quality of the music I make is any better.. An emotion

According to Merchant and Van der Stede (2007) companies and other organizations have four management control alternatives: results controls, action controls,