Linguaggi Speciali di Programmazione 1 - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

Linguaggi Speciali di Programmazione 1

Description:

An automated conversion between SOAP messages and IIOP messages is required ... DOM provides a representation of an XML document as a tree ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 33
Provided by: albertos2
Category:

less

Transcript and Presenter's Notes

Title: Linguaggi Speciali di Programmazione 1


1
Linguaggi Speciali di Programmazione 1
  • Ing. Marco Scotto (scotto_at_dist.unige.it)

2
Outline
  • Web Services vs. CORBA
  • Web Services or CORBA?
  • WS-CORBA interoperation
  • Java XML

3
Web Services vs. CORBA (1/4)
4
Web Services vs. CORBA (2/4)
Server Side Usage
  • Web Services
  • define the service as interface in WSDL
  • compile the WSDL to generate the stub and the
    skeleton
  • implement the service and associate it with the
    skeleton (implementation dependent)
  • publish the service with UDDI for use by clients
  • CORBA
  • define the service as interface in IDL
  • compile the IDL to generate the stub and the
    skeleton
  • implement the service and associate it with the
    skeleton via the portable object adapter (POA)
  • publish the service with a Naming Service for use
    by clients

5
Web Services vs. CORBA (3/4)
Client Side Usage
  • Web Services
  • contact UDDI for the desired service and retrieve
    the URL
  • invoke operations on the URL using the generated
    stub from the WSDL
  • process reply
  • CORBA
  • contact the Naming Service for the desired
    service and retrieve the object reference
  • invoke operations on the object reference using
    the IDL-compiler generated stub
  • process reply

6
Web Services vs. CORBA (4/4)
7
Web Services or CORBA? (1/3)
  • Web Interface
  • WS are the best XSLT can easily convert SOAP
    messages into HTML (CORBA requires more work)
  • Firewalls
  • WS are the best SOAP is designed to work in such
    environments (CORBA IIOP does not have a specific
    port for the communication)
  • Legacy systems
  • CORBA is the best a lot of legacy systems
    (including EJBs) can easily communicate with
    CORBA components (DCOM components can easily
    communicate to Web Services using MS.NET)

8
Web Services or CORBA? (2/3)
  • Stateful applications
  • CORBA is the best the state is managed by the
    object (WS require more work)
  • Mobile users
  • WS are the best HTTP support easily IP changing
    and unreliable networks (CORBA wireless is under
    development)
  • Thin clients
  • WS are the best SOAP is light and XML parsers
    can easily discard non suited tags (CORBA
    requires the entire ORB implemented)
  • Transparent proxies
  • WS are the best SOAP is easy to interface with
    new components that process or change the content
    of the message (CORBA has proxies for specific
    applications such as load balancing, caching,
    etc.)

9
Web Services or CORBA? (3/3)
  • Real-time performances
  • To experiment at present there are several
    real-time applications in CORBA. Its technology
    is more mature and this is a the crucial factor
    to choose the implementation technology. Its too
    early to compare with WS.

10
WS-CORBA interoperation
  • An automated conversion between SOAP messages and
    IIOP messages is required
  • Management of different paradigms OO,
    message-oriented
  • XML Schema IDL mapping
  • SOAP-CORBA bridge are available but suffer of
    several limitations

11
Java XML
12
XML Parsers
  • There are three fundamental parser type
  • A model parser reads an entire document and
    creates a representation of the document in
    memory
  • Model parsers use significantly more memory than
    other types of parsers
  • A push parser reads through an entire document.
    As it encounters various parts of the document,
    it notifies a listener object
  • This is how the popular SAX API operates
  • A pull parser reads a little bit of a document at
    once. The application drives the parser through
    the document by repeatedly requesting the next
    piece

13
XML Parsers
  • Which type you choose depends on how you want
    your application to behave and what types of
    documents you are expecting to parse

14
SAX (Simple API for XML)
  • SAX provides an event-based framework for parsing
    XML data
  • SAX defines events that can occur during the XML
    parsing process
  • Example
  • SAX defines an org.xml.sax.ContentHandler
    interface that defines methods such as
    startDocument() and endElement()
  • Implementing this interface allows complete
    control over these portions of the XML parsing
    process

15
DOM (Document Object Model)
  • While SAX only provides access to the data within
    an XML document, DOM is designed to provide a
    means of manipulating that data
  • DOM provides a representation of an XML document
    as a tree
  • DOM also reads an entire XML document into
    memory, storing all the data in nodes, so the
    entire document is very fast to access
  • It is all in memory for the length of its
    existence in the DOM tree

16
JAXP (Java API for XML Parsing)
  • JAXP provides a vendor neutral-interface to the
    underlying DOM or SAX parser
  • It adds some convenient methods to make the XML
    APIs easier to use for Java developers

javax.xml.parsers
DocumentBuilderFactory DocumentBuilder
SAXParserFactory SAXParser
ParserConfiguratonException FactoryConfigurationEr
ror
17
SAX Example (1/4)
  • import org.xml.sax.
  • import org.xml.sax.helpers.
  • import java.io.
  • import javax.xml.parsers.
  • public class SAXDemo extends DefaultHandler
  • public static void main(String args)
  • if (args.length ! 1)
  • System.err.println("Usage cmd filename")
  • System.exit(1)
  • DefaultHandler handler new SAXDemo()
  • SAXParserFactory factory SAXParserFactory.newI
    nstance()

18
SAX Example (2/4)
  • try
  • // Parse the input
  • SAXParser saxParser factory.newSAXParser()
  • saxParser.parse( new File(args0),
    handler)
  • catch (Throwable t)
  • t.printStackTrace()
  • System.exit(0)
  • //
  • // SAX DocumentHandler methods
  • //
  • public void startDocument() throws
    SAXException
  • System.out.println("START DOCUMENT")

19
SAX Example (3/4)
  • public void endDocument() throws SAXException
  • System.out.println("END DOCUMENT")
  • public void startElement(String namespaceURI,
  • String lName, // local name
  • String qName, // qualified name
  • Attributes attrs)
  • throws SAXException
  • System.out.println("ELEMENT " qName)
  • if (attrs ! null)
  • for (int i 0 i lt attrs.getLength() i)
  • String aName attrs.getQName(i) // Attr
    name
  • System.out.println("ATTR " aName " "
    attrs.getValue(i))

20
SAX Example (4/4)
  • public void characters(char ch, int start, int
    length) throws SAXException
  • String s new String(ch, start, length)
  • System.out.println("CHARS " s)
  • public void endElement(String namespaceURI,
    String sName, // simple name
  • String qName // qualified name)
  • throws SAXException
  • System.out.println("END ELEMENT " qName)

21
DOM Example 1 (1/3)
  • import java.io.
  • import javax.xml.parsers.
  • import org.xml.sax.
  • import org.w3c.dom.
  • public class DOMDemo
  • public static void main(String args)
  • if (args.length ! 1)
  • System.err.println("Usage cmd filename")
  • System.exit(1)
  • Document doc null
  • DocumentBuilder db null
  • DocumentBuilderFactory dbf DocumentBuilderFacto
    ry.newInstance()

22
DOM Example 1 (2/3)
  • try
  • db dbf.newDocumentBuilder()
  • doc db.parse(new File(args0))
  • doc.normalize()
  • catch (ParserConfigurationException pce)
  • pce.printStackTrace()
  • catch (SAXException se)
  • se.printStackTrace()
  • catch (IOException ioe)
  • ioe.printStackTrace()
  • NodeList nl doc.getChildNodes()
  • queryDocument(nl)

23
DOM Example 1 (3/3)
  • public static void queryDocument(NodeList nl)
  • for (int i0 iltnl.getLength() i)
  • Node n nl.item(i)
  • if (n instanceof Element)
  • Element e (Element) n
  • System.out.println(e.getNodeName())
  • queryDocument(e.getChildNodes())
  • else if (n instanceof Text)
  • Text t (Text) n
  • System.out.println(t.getNodeValue())

24
DOM Example 2 (1/4)
  • import java.io.
  • import javax.xml.parsers.
  • import javax.xml.transform.
  • import javax.xml.transform.stream.
  • import javax.xml.transform.dom.
  • import org.w3c.dom.
  • public class DomDemo2
  • public static void main(String args)
  • Document doc null
  • DocumentBuilder db null
  • if (args.length ! 1)
  • System.err.println("Usage cmd filename")
  • System.exit(1)

25
DOM Example 2 (2/4)
  • DocumentBuilderFactory dbf DocumentBuilderFactor
    y.newInstance()
  • try
  • db dbf.newDocumentBuilder()
  • doc db.newDocument()
  • catch (ParserConfigurationException pce)
  • System.err.println("Parser configuratione
    exception.")
  • createDocument(doc)
  • serialize(doc, new File(args0))

26
DOM Example 2 (3/4)
  • public static void createDocument(Document doc)
  • Element root doc.createElement("Agenda")
  • doc.appendChild(root)
  • Element person doc.createElement("Person")
  • Person.setAttribute("id", "1")
  • root.appendChild(person)
  • Element name doc.createElement("Name")
  • name.appendChild(doc.createTextNode("Marco"))
  • person.appendChild(name)
  • Element surname doc.createElement("Surname")
  • surname.appendChild(doc.createTextNode("Scotto"))
  • person.appendChild(surname)
  • Element email doc.createElement("Email")
  • email.appendChild(doc.createTextNode("scotto_at_dist
    .unige.it"))
  • person.appendChild(email)

27
DOM Example 2 (4/4)
  • public static void serialize(Document doc, File
    file )
  • Transformer serializer null
  • StreamResult streamResult null
  • DOMSource domSource new DOMSource(doc)
  • try
  • serializer TransformerFactory.newInstance().
    newTransformer()
  • serializer.setOutputProperty(OutputKeys.INDENT
    , "yes")
  • streamResult new StreamResult(new
    FileOutputStream(file))
  • serializer.transform(domSource,
    streamResult)
  • catch (Exception e)
  • e.printStackTrace()

28
DOM or SAX?
  • DOM
  • Suitable for small documents
  • Easily modify document
  • Memory intensive load the complete XML document
  • SAX
  • Suitable for large documents saves significant
    amounts of memory
  • Event driven
  • Limited standard functions

29
XML Parsers for J2ME
  • The following table summarizes the current
    offering of small XML parsers that are
    appropriate for J2ME

30
XPath
  • Language for addressing and matching parts of an
    XML document
  • Designed to be used standalone, but also by
    XPointer, XSLT, XQuery.
  • Uses a compact, non-XML format to facilitate use
    within URIs and attribute values
  • Provides basic facilities for string, number and
    boolean manipulation
  • Supports namespaces

31
XPath Example (1/2)
  • Sample XML document
  • ltAgendagt
  • ltPerson NameMarco SurnameScottogt
  • ltemailgtscotto_at_dist.unige.itlt/emailgt
  • lt/Persongt
  • ltPerson NameMario SurnameRossigt
  • ltemailgtMario.Rossi_at_email.itlt/emailgt
  • lt/Persongt
  • lt/Agendagt

32
XPath Example (2/2)
  • Selects the name attribute of the first person
    element
  • /Agenda/Person1/_at_Name
  • Selects the surname attribute of the first person
    element
  • /Agenda/Person1/_at_Surname
  • Selects the email of the first person element
  • /Agenda/Person1/email/text()
Write a Comment
User Comments (0)
About PowerShow.com