Download presentation
Presentation is loading. Please wait.
Published byGiles Crawford Modified over 8 years ago
1
Flexible Data-binding With RelaxNGCC Kohsuke Kawaguchi kk@kohsuke.org
2
2 What ’ s RelaxNGCC? RelaxNGCC is: Parser generator (e.g., YACC, JavaCC, or ANTLR) Data-binding tool (e.g., JAXB, Castor, or Relaxer) Purpose To simplify XML parsing development
3
3 Before RelaxNGCC But writing SAX handler... Is hard and tiring Takes time Is routine and not fun XML Parser XML document Hand-written SAX Handler So people turn their eyes to data-binding
4
4 Problems With Data-binding Tools Impedance mismatch b/w XML and ideal OM What does (A|B|C)* mean? Customization is limited Generated code is low in quality Expose a lot of unnecessary methods
5
5 Problems With Data-binding Tools Unable to bridge existing code and existing schema Take time to get used to the generated code Need to know how schemas are mapped
6
6 After RelaxNGCC Reduces development time XML Parser XML document Generated SAX Handler Annotated RELAX NG Schema
7
7 How RelaxNGCC Works? By associating code and schema number= name= System.out.println("start"); number= System.out.print(number+":"); name= System.out.println(name); System.out.println("end");
8
8 Key Concepts Anchoring data to variables Values are copied to specified variables as document gets parsed number=
9
9 Key Concepts Code will be also executed at the "right" moment me you start 1:me 2:you end
10
10 Key Concepts Pattern blocks work like function calls passing data down and up across boundaries result= j= result= (3); j= i+=Integer.parseInt(j);
11
11 Code Generation Each pattern block gets its own class At runtime, new object is allocated to process new block...... Class Foo Class Bar
12
12 Code Generation Aliases become fields Additional methods can be defined *** 1 *** *** 2 *** abc = import x.y.z; *** 1 *** class Foo { *** 2 *** String abc;... }
13
13 Runtime Code used to help generated code Just 3 classes No runtime version dependency Runtime receives SAX events and coordinate handlers SAX events Generated Runtime Generated SAX Handler Generated SAX Handler Generated Handlers
14
14 Runtime Provides services to user-specified code Retrieve Locator object Resolve namespace prefix Redirect sub-tree to another SAX ContentHandler
15
15 Runtime User-defined code can be added Added methods/fields available to handlers Useful to keep global info SAX events Default Runtime Customized Runtime extend Generated SAX Handler Generated SAX Handler Generated Handlers access
16
16 Runtime (example) runtime.myFunction();... class MyRuntime extends NGCCRuntime { public void myFunction() {... }
17
17 Put in Practice Reading XML configuration file Extend runtime to hold Options class Fill in the structure as you go through document name = value= runtime.opt.properties.put( name,value); runtime.opt.paramX = class Options { Properties properties; String paramX; } class MyRuntime : NGCCRuntime { public Options opt; }
18
18 Put in Practice Quickly build Abstract Syntax Tree Just use generated class hierarchy and their fields Use cc:class to throw in extra classes public Set params; p= name = value= params.add(p); paramX =
19
19 Put in Practice Build full-blown object model RelaxNGCC uses itself to parse RELAX NG Design OM without worrying about syntax Then use RelaxNGCC to build a parser for that Good efficient parser in short time
20
20 Why RELAX NG? Cannot write annotation like this How can I anchor 10 values to 10 different variables? No way!...
21
21 Why RELAX NG? Formal model makes RelaxNGCC simple Simpler state management Simpler schema parsing Uniform treatment of attributes/elements
22
22 Why RELAX NG? Some XML Schema features don't work well Nillable Type substitution Substitution group... ironic because all those features are supposed to be for data-oriented XMLs
23
23 Loosely-coupled Systems Type sharing makes systems tightly-coupled Some says that's what XML is trying to avoid Better to share the syntax w/o sharing data model RelaxNGCC allows you to do this!
24
24 License Compiler GPL Generated code, including runtime All yours!
25
25 To Get More Information Project web-site http://relaxngcc.sourceforge.net/ Contact developers http://groups.yahoo.com/group/reldeve/ RELAX NG Info http://relaxng.org/ This presentation http://www.kohsuke.org/
26
26 End Acknowledgement Daisuke Okajima, the inventor of RelaxNGCC Sun Microsystems, for allowing me to work on RELAX NG Any Question?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.