HTML Object Model Obiettivo
Transcript
HTML Object Model Obiettivo
02CBI Programmazione a Oggetti HTML Object Model Esercizio Obiettivo Costruire un insieme di classi che consentano la generazione (semplificata) di codice HTML. Tali classi permettono ad un programma di generare del codice HTML senza manipolare direttamente delle enormi stringhe 1 02CBI Programmazione a Oggetti Esempio <HTML> <HEAD> <TITLE>Test</TITLE> </HEAD> <BODY bgcolor=white> <H1>This is a test</H1> This is plain text <A HREF=“http://www.w3c.org”> Click here... </A> </BODY> </HTML> Esempio Page Head <HTML> <HEAD><TITLE>Test</TITLE></HEAD> Body <BODY bgcolor=white> <H1> This is a test </H1> This is plain text <A HREF=“http://www.w3c.org”> Click here... Heading Text Link Text </A> </BODY> </HTML> 2 02CBI Programmazione a Oggetti Struttura della pagina sample : Page test : Head a title : Heading body : Body plain text : Text title text : Text a link : Link link text : Text Analisi del problema Heading Page ordered 0..n Head 0..n 1 1 containment associations are ordered 0..n Body Text 0..n 0..n 0..n ordered 0..n ordered Link 3 02CBI Programmazione a Oggetti Considerazioni L’obiettivo delle classi è fornire una struttura facilmente attraversabile e stampabile Quasi tutti gli elementi posseggono una stringa che rappresenta il contenuto dell’elemento Alcuni elementi devono fornire un meccanismo per iterare sugli elementi contenuti Tutti gli elementi devono restituire il tag di apertura e (eventualmente) quello di chiusura Progetto Element Element() getOpeningTag() getClosingTag() firstChild() nextSibling() isComposed() Heading (from Analysis) ) 0..n 0..n 0..n Text ) 0..n (from Analysis) Page (from Analysis) 1 Body (from Analysis) ) 0..n serialize_all() 0..n 1 Head (from Analysis) ) Link 0..n (from Analysis) ) 4 02CBI Programmazione a Oggetti Esempio di stampa class Page { public void serialize(Element current ){ print( current.getOpeningTag() ); if( current.isComposed() ){ for(Element child=current.firstChild(); child!=null; child=child.nextSibling();){ serialize(child); } } print( current.getClosingTag() ) } //… } Collaborazione title text : Text the head : Head L L = variabile locale link text : L Text the page : Page L a title : Heading L plain text : Text L a link : Link the body : Body 5 02CBI Programmazione a Oggetti Stampa con delega class Element{ serialize(){ print( this.getOpeningTag() ); if( this.isComposed() ){ for(Element child=firstChild(); child!=null; child=child.nextSibling();){ child.serialize(); } } println( this.getClosingTag() ); } } Collaborazione delega the page : Page the head : Head the body : Body a title : Heading title text : Text plain text : Text a link : Link link text : Text 6 02CBI Programmazione a Oggetti Considerazioni L’aggiunta di una classe al diagramma delle classi comporta l’aggiunta di molte relazioni Alcuni elementi sono composti, altri semplici Il comportamento in termini di stampa è diverso Si possono identificare due classi astratte che rappresentano queste due categorie Composite {ordered} +children Composite Page can contain one Head and one Body Element Text Body cannot contain Page or Head Head Heading Link cannot contain Page or Head or Body 7 02CBI Programmazione a Oggetti Composite La struttura prevede un elemento semplice e di un elemento composto che deriva dal primo e con esso ha una relazione di composizione È una struttura ricorrente in strutture composte: Espressioni aritmetiche Figure geometriche... 8