34
XML-Free Programming : Java Server and Client Development Without <> Stephen Chin Chief Agile Methodologist, GXS [email protected] tweet: @steveonjava Arun Gupta Oracle Corporation [email protected] m tweet: @arungupta

XML-Free Programming

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: XML-Free Programming

XML-Free Programming : Java Server and Client Development Without <>Stephen ChinChief Agile Methodologist, [email protected]: @steveonjava

Arun GuptaOracle [email protected]: @arungupta

Page 2: XML-Free Programming

Meet the Presenters

Stephen Chin

Motorcyclist

Family Man

Arun Gupta

Marathoner

Community Guy

Page 3: XML-Free Programming

Our Plan

> Quick (Humorous) History of Angle Brackets> XML-Free Programming

1. Configuration Lives with the Code2. Data Transfer Models the Domain3. Design Programming Languages for Humans

> JavaOne Speakers Application <Demo>

3

Page 4: XML-Free Programming

Exhibit A – Angle Bracket Sighting in Virginia, 1922Source: Library of Congress, Prints and Photographs Collection – Public Domainhttp://www.flickr.com/photos/pingnews/434444310/

4

> > >

Page 5: XML-Free Programming

Exhibit B - Bermuda Tri-Angle BracketsSource: NOAA National Ocean Service – CC licensedhttp://www.flickr.com/photos/usoceangov/4276194691/sizes/o/in/photostream/

5

>>>

Page 6: XML-Free Programming

Exhibit C – Tim Bray, Co-Founder of XMLSource: Linux.comhttp://www.linux.com/archive/feature/133149

6

>>>>

Page 7: XML-Free Programming

History of XML

> Based on Standard Generalized Markup Language (SGML)> Created by a W3C working group of eleven members

> Version History: XML 1.0 (1998) – Widely adopted with 5 subsequent revisions XML 1.1 (2004) – Limited adoption

7

Page 8: XML-Free Programming

XML Design Goals (a.k.a. problems with SGML)

1. Usable Over the Internet2. Support a Wide Variety of Applications3. Compatible with SGML4. Easy to Write Programs to Process XML Documents5. Minimum Number of Optional Features6. Documents Should be Human-Legible and Reasonably Clear7. Design Should be Prepared Quickly8. Design Should be Formal and Concise9. Documents Should be Easy to Create10. Terseness in Markup is of Minimal Importance

8

Page 9: XML-Free Programming

Design Goals Per Application

Publishing Configuration Data Transfer Programming

Usable Over Internet Important N/A Important N/A

Wide Variety of Applications Acceptable Negative N/A N/A

Compatible With SGML Important Negative Negative Negative

Computer Processable Important Important Important Important

No Optional Features Important Important Important Important

Human-Legible Important Important Acceptable Important

Design Completed Quickly Important N/A N/A N/A

Formal and Concise Spec Important Important Important N/A

Easy to Create Documents Important Important N/A Important

Markup Can be Verbose Negative Negative Negative Negative

9

Page 10: XML-Free Programming

10

Tenet 1

Configuration Lives with the Code

Page 11: XML-Free Programming

Letting Go of XML is Hard!

11

This is not intended as a replacement for Spring's XML format.

Rod Johnson on Spring’s Annotations-based Configuration“A Java configuration option for Spring,” 11/28/06

Page 12: XML-Free Programming

Java EE 6 Annotations

> @Stateless> @Path> @WebServlet> @Inject> @Named> @Entity

12

Page 13: XML-Free Programming

But There is Hope!

13

You can have a Groovy DSL … it is as short as can be.

Dierk Koenig on Canoo Web Test“Interview with Dierk Koenig,” ThirstyHead.com 6/3/2009

Page 14: XML-Free Programming

Canoo Web Test Comparison

XML

<project default="test"> <target name="test"> <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>

Groovy Builder

class SimpleTest extends WebtestCase { void testWebtestOnGoogle() {  webtest("Google WebTest Search") {   invoke "http://www.google.com/ncr"   verifyTitle "Google"   setInputField name: "q", value: "WebTest"   clickButton "I'm Feeling Lucky"   verifyTitle "Canoo WebTest"  } }}

14

Page 15: XML-Free Programming

15

Tenet 2

Data Transfer Models the Domain

Page 16: XML-Free Programming

JavaScript Object Notation (JSON)

> Matches Relational/Object-Oriented Structures> Easy to Read and Write> Simple to Parse and Generate> Familiar to Programmers of the C-family of languages:

C, C++, C#, Java, JavaScript, Perl, Python, etc.> Very Simple Specification

16

Page 17: XML-Free Programming

JSON Syntax in a Slide

17Images courtesy: http://www.json.org/

Page 18: XML-Free Programming

JAX-RS Sample

18

@Stateless@Path("sezzion")public class SezzionFacadeREST extends AbstractFacade<Sezzion> { @PersistenceContext private EntityManager em;

@POST@Consumes({"application/json", "application/xml"}) public void create(Sezzion entity) { super.create(entity); }

@GET @Produces({"application/json", "application/xml"}) public List<Sezzion> findAll() { return super.findAll(); }

@GET @Path("{from}/{to}") @Produces({"application/xml", "application/json"}) public List<Sezzion> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) { return super.findRange(new int[]{from, to}); }

Page 19: XML-Free Programming

19

Tenet 3

Design Programming Languages for Humans

Page 20: XML-Free Programming

20

Counter Example – o:XML

> Created By Martin Klang in 2002> Object Oriented Language> Features:

Poymorphism Function Overloading Exception Handling Threads

Diagram from: http://www.o-xml.org/documentation/o-xml-tool-chain.html

Page 21: XML-Free Programming

String Replacement in o:XML vs. Java<?xml-stylesheethref="../xsl/default.xsl" type="text/xsl"?><program> <o:function name="ex:replace"> <o:param name="input" type="String"/> <o:param name="from" type="String"/> <o:param name="to" type="String"/> <o:do> <o:variable name="result"/> <o:while test="contains($input, $from)"> <o:set result="concat($result, substring-before($input, $from), $to)"/> <o:set input="substring-after($input, $from)"/> </o:while> <o:return select="concat($result, $input)"/> </o:do> </o:function></program>

class Replace { public String replace(String input, String from, String to) { StringBuilder result = new StringBuilder(); int last = 0; int index = 0; while ((index = input.indexOf(from, last)) != -1) { result.append(input.substring(last, index)); result.append(to); last = index + from.length() } result.append(input.substring(last)); return result.toString(); }}

21

16 Lines461 Characters

14 Lines319 Characters

Page 22: XML-Free Programming

22

String Replacement in o:XML

<?xml-stylesheethref="../xsl/default.xsl" type="text/xsl"?><program> <o:function name="ex:replace"> <o:param name="input" type="String"/> <o:param name="from" type="String"/> <o:param name="to" type="String"/> <o:do> <o:variable name="result"/> <o:while test="contains($input, $from)"> <o:set result="concat($result, substring-before($input, $from), $to)"/> <o:set input="substring-after($input, $from)"/> </o:while> <o:return select="concat($result, $input)"/> </o:do> </o:function></program>

Page 23: XML-Free Programming

Equivalent Java

class Replace { public String replace(String input, String from, String to) { StringBuilder result = new StringBuilder(); int last = 0; int index = 0; while ((index = input.indexOf(from, last)) != -1) { result.append(input.substring(last, index)); result.append(to); last = index + from.length() } result.append(input.substring(last)); return result.toString(); }}

23

Page 24: XML-Free Programming

Simple Java

class Replace { public String replace(String input, String from, String to) { return input.replaceAll(from, to) }}

24

Page 25: XML-Free Programming

JavaFX 2.0

> Powerful graphics, animation, and media capabilities> Deploys in the browser or on desktop> Includes builders for declarative construction> Alternative languages can also be used for simpler UI creation

GroovyFX ScalaFX Visage

25

Page 26: XML-Free Programming

26

Hello JavaOne (Java Version)public class HelloJavaOne extends Application { public static void main(String[] args) { launch(HelloJavaOne.class, args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("Hello JavaOne"); Group root = new Group(); Scene scene = new Scene(root, 400, 250, Color.ALICEBLUE); Text text = new Text(); text.setX(105); text.setY(120); text.setFont(new Font(30)); text.setText("Hello JavaOne"); root.getChildren().add(text); primaryStage.setScene(scene); primaryStage.show(); }}

Page 27: XML-Free Programming

27

Hello JavaOne (Builder Version)public void start(Stage primaryStage) { primaryStage.setTitle("Hello JavaOne"); primaryStage.setScene(SceneBuilder.create() .width(400) .height(250) .fill(Color.ALICEBLUE) .root( GroupBuilder.create().children( TextBuilder.create() .x(105) .y(120) .text("Hello JavaOne") .font(new Font(30)) .build() ).build() ) .build()); primaryStage.show();}

Page 28: XML-Free Programming

28

Hello JavaOne (GroovyFX Version)GroovyFX.start { primaryStage -> def sg = new SceneGraphBuilder() sg.stage( title: 'Hello JavaOne', show: true) { scene( fill: aliceblue, width: 400, height: 250) { text( x: 105, y: 120, text: "Hello JavaOne" font: "30pt") } }}

Page 29: XML-Free Programming

29

Hello JavaOne (ScalaFX Version)object HelloJavaOne extends JFXApp { stage = new Stage { title = "Hello JavaFX" width = 400 height = 250 scene = new Scene { fill = BLUE Text { x = 105 y = 120 text = "Hello JavaOne" font = Font(size: 30) } } }}

Page 30: XML-Free Programming

30

Hello JavaOne (Visage Version)Stage { title: "Hello JavaOne" width: 400 height: 250 scene: Scene { fill: BLUE content: Text { x: 105 y: 120 text: "Hello JavaOne" font: Font {size: 30pt} } }}

Page 31: XML-Free Programming

JavaOne Speakers Application

> End-to-end application with no XML coding> Server written using JavaEE 6 annotations> Data transfer uses JSON> Client written in JavaFX 2.0

31

Page 32: XML-Free Programming

32

Finished Application

Page 33: XML-Free Programming

Support the Freedom From XML Petition

http://steveonjava.com/freedom-from-xml/

Provide Non-XML Alternatives For:> Declarative Programming> Configuration> Data Transfer

33

</>

Sign the Petition Today!