XML-Free Programming

3,180 views
3,051 views

Published on

Talk on using JavaEE 6 and JavaFX 2 together to write applications without XML. Given at JavaOne 2011 with Arun Gupta.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
3,180
On SlideShare
0
From Embeds
0
Number of Embeds
53
Actions
Shares
0
Downloads
15
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

XML-Free Programming

  1. 1. XML-Free Programming : Java Server and Client Development Without <><br />Stephen Chin<br />Chief Agile Methodologist, GXS<br />steveonjava@gmail.com<br />tweet: @steveonjava<br />Arun Gupta<br />Oracle Corporation<br />arun.p.gupta@oracle.com<br />tweet: @arungupta<br />
  2. 2. Meet the Presenters<br />Stephen Chin<br />Arun Gupta<br />Community Guy<br />Family Man<br />Motorcyclist<br />Marathoner<br />
  3. 3. Our Plan<br />Quick (Humorous) History of Angle Brackets<br />XML-Free Programming<br />Configuration Lives with the Code<br />Data Transfer Models the Domain<br />Design Programming Languages for Humans<br />JavaOne Speakers Application <Demo><br />3<br />
  4. 4. Exhibit A – Angle Bracket Sighting in Virginia, 1922<br />Source: Library of Congress, Prints and Photographs Collection – Public Domain<br />http://www.flickr.com/photos/pingnews/434444310/<br />4<br />><br />><br />><br />
  5. 5. Exhibit B - Bermuda Tri-Angle Brackets<br />Source: NOAA National Ocean Service – CC licensed<br />http://www.flickr.com/photos/usoceangov/4276194691/sizes/o/in/photostream/<br />5<br />><br />><br />><br />
  6. 6. Exhibit C – Tim Bray, Co-Founder of XML<br />Source: Linux.com<br />http://www.linux.com/archive/feature/133149<br />6<br />><br />><br />><br />><br />
  7. 7. History of XML<br />Based on Standard Generalized Markup Language (SGML)<br />Created by a W3C working group of eleven members<br />Version History:<br />XML 1.0 (1998) – Widely adopted with 5 subsequent revisions<br />XML 1.1 (2004) – Limited adoption<br />7<br />
  8. 8. XML Design Goals (a.k.a. problems with SGML)<br />Usable Over the Internet<br />Support a Wide Variety of Applications<br />Compatible with SGML<br />Easy to Write Programs to Process XML Documents<br />Minimum Number of Optional Features<br />Documents Should be Human-Legible and Reasonably Clear<br />Design Should be Prepared Quickly<br />Design Should be Formal and Concise<br />Documents Should be Easy to Create<br />Terseness in Markup is of Minimal Importance<br />8<br />
  9. 9. Design Goals Per Application<br />9<br />
  10. 10. Tenet 1<br />Configuration Lives with the Code<br />10<br />
  11. 11. Letting Go of XML is Hard!<br />11<br />This is not intended as a replacement for Spring's XML format.<br />Rod Johnson on Spring’s Annotations-based Configuration<br />“A Java configuration option for Spring,” 11/28/06<br />
  12. 12. Java EE 6 Annotations<br />@Stateless<br />@Path<br />@WebServlet<br />@Inject<br />@Named<br />@Entity<br />12<br />
  13. 13. But There is Hope!<br />13<br />You can have a Groovy DSL … it is as short as can be.<br />Dierk Koenig on Canoo Web Test<br />“Interview with Dierk Koenig,” ThirstyHead.com 6/3/2009<br />
  14. 14. Canoo Web Test Comparison<br />XML<br />Groovy Builder<br /><project default="test"><br /> <target name="test"><br /> <webtest        name="Google WebTest Search">    <invoke url="http://www.google.com/ncr" />    <verifyTitle text="Google" />    <setInputField name="q" value="WebTest" />    <clickButton label="I'm Feeling Lucky" />    <verifyTitle text="Canoo WebTest" />  </webtest> </target></project><br />class SimpleTest extends WebtestCase { void testWebtestOnGoogle() {  webtest("Google WebTestSearch") {   invoke "http://www.google.com/ncr"   verifyTitle "Google"   setInputField name: "q", value: "WebTest"   clickButton "I'm Feeling Lucky"   verifyTitle "CanooWebTest"  } }}<br />14<br />
  15. 15. Tenet 2<br />Data Transfer Models the Domain<br />15<br />
  16. 16. JavaScript Object Notation (JSON)<br />Matches Relational/Object-Oriented Structures<br />Easy to Read and Write<br />Simple to Parse and Generate<br />Familiar to Programmers of the C-family of languages:<br />C, C++, C#, Java, JavaScript, Perl, Python, etc.<br />Very Simple Specification<br />16<br />
  17. 17. JSON Syntax in a Slide<br />17<br />Images courtesy: http://www.json.org/<br />
  18. 18. JAX-RS Sample<br />18<br />@GET<br /> @Produces({"application/json", "application/xml"})<br /> public List<Sezzion> findAll() {<br /> return super.findAll();<br /> }<br /> @GET<br />@Path("{from}/{to}")<br /> @Produces({"application/xml", "application/json"})<br /> public List<Sezzion> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {<br /> return super.findRange(new int[]{from, to});<br /> }<br />@Stateless<br />@Path("sezzion")<br />public class SezzionFacadeREST extends AbstractFacade<Sezzion> {<br />@PersistenceContext<br /> private EntityManagerem;<br />@POST<br />@Consumes({"application/json", "application/xml"})<br /> public void create(Sezzion entity) {<br />super.create(entity);<br /> }<br />
  19. 19. Tenet 3<br />Design Programming Languages for Humans<br />19<br />
  20. 20. Counter Example – o:XML<br />Created By Martin Klang in 2002<br />Object Oriented Language<br />Features:<br />Poymorphism<br />Function Overloading<br />Exception Handling<br />Threads<br />20<br />Diagram from: http://www.o-xml.org/documentation/o-xml-tool-chain.html<br />
  21. 21. String Replacement in o:XML vs. Java<br /><?xml-stylesheethref="../xsl/default.xsl" type="text/xsl"?><br /><program><br /> <o:function name="ex:replace"><br /> <o:param name="input" type="String"/><br /> <o:param name="from" type="String"/><br /> <o:param name="to" type="String"/><br /> <o:do><br /> <o:variable name="result"/><br /> <o:while test="contains($input, $from)"><br /> <o:set result="concat($result, substring-before($input, $from), $to)"/><br /> <o:set input="substring-after($input, $from)"/><br /> </o:while><br /> <o:return select="concat($result, $input)"/><br /> </o:do><br /> </o:function><br /></program><br />class Replace {<br /> public String replace(String input, String from, String to) {<br />StringBuilder result = new StringBuilder();<br />int last = 0;<br />int index = 0;<br /> while ((index = input.indexOf(from, last)) != -1) {<br />result.append(input.substring(last, index));<br />result.append(to);<br /> last = index + from.length()<br /> }<br />result.append(input.substring(last));<br /> return result.toString();<br /> }<br />}<br />21<br />16 Lines<br />461 Characters<br />14 Lines<br />319 Characters<br />
  22. 22. String Replacement in o:XML<br /><?xml-stylesheethref="../xsl/default.xsl" type="text/xsl"?><br /><program><br /> <o:function name="ex:replace"><br /> <o:param name="input" type="String"/><br /> <o:param name="from" type="String"/><br /> <o:param name="to" type="String"/><br /> <o:do><br /> <o:variable name="result"/><br /> <o:while test="contains($input, $from)"><br /> <o:set result="concat($result, substring-before($input, $from), $to)"/><br /> <o:set input="substring-after($input, $from)"/><br /> </o:while><br /> <o:return select="concat($result, $input)"/><br /> </o:do><br /> </o:function><br /></program><br />22<br />
  23. 23. Equivalent Java<br />class Replace {<br /> public String replace(String input, String from, String to) {<br />StringBuilder result = new StringBuilder();<br />int last = 0;<br />int index = 0;<br /> while ((index = input.indexOf(from, last)) != -1) {<br />result.append(input.substring(last, index));<br />result.append(to);<br /> last = index + from.length()<br /> }<br />result.append(input.substring(last));<br /> return result.toString();<br /> }<br />}<br />23<br />
  24. 24. Simple Java<br />class Replace {<br /> public String replace(String input, String from, String to) {<br /> return input.replaceAll(from, to)<br /> }<br />}<br />24<br />
  25. 25. JavaFX 2.0<br />Powerful graphics, animation, and media capabilities<br />Deploys in the browser or on desktop<br />Includes builders for declarative construction<br />Alternative languages can also be used for simpler UI creation<br />GroovyFX<br />ScalaFX<br />Visage<br />25<br />
  26. 26. 26<br />Hello JavaOne (Java Version)<br />public class HelloJavaOne extends Application {<br /> public static void main(String[] args) {<br /> launch(HelloJavaOne.class, args);<br /> }<br /> @Override<br /> public void start(Stage primaryStage) {<br />primaryStage.setTitle("Hello JavaOne");<br /> Group root = new Group();<br /> Scene scene = new Scene(root, 400, 250, Color.ALICEBLUE);<br /> Text text = new Text();<br />text.setX(105);<br />text.setY(120);<br />text.setFont(new Font(30));<br />text.setText("Hello JavaOne");<br />root.getChildren().add(text); <br />primaryStage.setScene(scene);<br />primaryStage.show();<br /> }<br />}<br />
  27. 27. 27<br />Hello JavaOne(Builder Version)<br />public void start(Stage primaryStage) {<br />primaryStage.setTitle("Hello JavaOne");<br />primaryStage.setScene(SceneBuilder.create()<br /> .width(400)<br /> .height(250)<br /> .fill(Color.ALICEBLUE)<br /> .root(<br />GroupBuilder.create().children(<br />TextBuilder.create()<br /> .x(105)<br />.y(120)<br /> .text("Hello JavaOne")<br /> .font(new Font(30))<br /> .build()<br /> ).build()<br /> )<br /> .build());<br />primaryStage.show();<br />}<br />
  28. 28. 28<br />Hello JavaOne (GroovyFX Version)<br />GroovyFX.start { primaryStage -><br />defsg = new SceneGraphBuilder()<br />sg.stage(<br /> title: 'Hello JavaOne',<br /> show: true) {<br /> scene(<br /> fill: aliceblue,<br /> width: 400,<br /> height: 250) {<br /> text(<br /> x: 105,<br /> y: 120,<br /> text: "Hello JavaOne"<br /> font: "30pt")<br /> }<br /> }<br />}<br />
  29. 29. 29<br />Hello JavaOne (ScalaFX Version)<br />object HelloJavaOne extends JFXApp {<br /> stage = new Stage {<br /> title = "Hello JavaFX"<br /> width = 400<br /> height = 250<br /> scene = new Scene {<br /> fill = BLUE<br /> Text {<br /> x = 105<br /> y = 120<br /> text = "Hello JavaOne"<br /> font = Font(size: 30)<br /> }<br /> }<br /> }<br />}<br />
  30. 30. 30<br />Hello JavaOne (Visage Version)<br />Stage {<br />title: "Hello JavaOne"<br />width: 400<br />height: 250<br />scene: Scene {<br /> fill: BLUE<br /> content: Text {<br /> x: 105<br /> y: 120<br /> text: "Hello JavaOne"<br /> font: Font {size: 30pt}<br /> }<br /> }<br />}<br />
  31. 31. JavaOne Speakers Application<br />End-to-end application with no XML coding<br />Server written using JavaEE 6 annotations<br />Data transfer uses JSON<br />Client written in JavaFX 2.0<br />31<br />
  32. 32. Finished Application<br />32<br />
  33. 33. Support the Freedom From XML Petition<br />http://steveonjava.com/freedom-from-xml/<br />Provide Non-XML Alternatives For:<br />Declarative Programming<br />Configuration<br />Data Transfer<br />33<br /></><br />Sign the Petition Today!<br />
  34. 34. 34<br />Stephen Chin<br />steveonjava@gmail.com<br />tweet: @steveonjava<br />Arun Gupta<br />arun.p.gupta@oracle.com<br />tweet: @arungupta<br />

×