March 28, 2001 XSP SessionO’Reilly Enterprise Java Conference
1
XSP SessionXSP Session
Sue SpielmanSue Spielman
President/Consulting President/Consulting EngineerEngineer
www.switchbacksoftware.cowww.switchbacksoftware.comm
sspielman@[email protected]
March 28, 2001 XSP SessionO’Reilly Enterprise Java Conference
2
Session OverviewSession Overview
• XSP OverviewXSP Overview• XSP vs. JSPXSP vs. JSP• Technologies InvolvedTechnologies Involved• Object ModelObject Model• Implementation DetailsImplementation Details• SamplesSamples• Questions Questions
March 28, 2001 XSP SessionO’Reilly Enterprise Java Conference
3
XSP – What Is It?XSP – What Is It?
• eXtensible Server PageseXtensible Server Pages• Component of Cocoon used for Component of Cocoon used for
building web apps with building web apps with dynamic XML contentdynamic XML content
• Dynamic content is based onDynamic content is based on– Request ParametersRequest Parameters– DatabasesDatabases– Remote Server ProcessesRemote Server Processes
March 28, 2001 XSP SessionO’Reilly Enterprise Java Conference
4
XSP – PurposeXSP – Purpose
• Created to allow web authors to Created to allow web authors to generate dynamic content generate dynamic content without forcing them to learn a without forcing them to learn a programming languageprogramming language
• Promotes a clean division of Promotes a clean division of labor:labor:– Application Developers Application Developers – Content authors Content authors – Presentation designers Presentation designers
March 28, 2001 XSP SessionO’Reilly Enterprise Java Conference
5
XSP vs. JSP SimilaritiesXSP vs. JSP Similarities
• Follows the compiled server Follows the compiled server pages modelpages model
• Can be parsed and validated by Can be parsed and validated by regular XML parsers regular XML parsers
• Can be transformed by XSLT Can be transformed by XSLT processors processors
• Aim to programming language Aim to programming language abstraction abstraction
March 28, 2001 XSP SessionO’Reilly Enterprise Java Conference
6
XSP vs. JSP DifferencesXSP vs. JSP Differences
• Output Exposure Output Exposure – XSP doesn’t have access to the XSP doesn’t have access to the
output streamoutput stream– JSP has access to output streamJSP has access to output stream
• Page ReadabilityPage Readability– XSP uses the XML features to allows XSP uses the XML features to allows
code to be easily distinguishable code to be easily distinguishable between markup elementsbetween markup elements
– JSP needs to enclose programming JSP needs to enclose programming code by scriptlet tagscode by scriptlet tags
March 28, 2001 XSP SessionO’Reilly Enterprise Java Conference
7
X* Technologies InvolvedX* Technologies Involved
• XSP – eXtensible Server PagesXSP – eXtensible Server Pages• XML – eXtensible Markup LanguageXML – eXtensible Markup Language
– Portable DataPortable Data• XSL - Stylesheet language for XMLXSL - Stylesheet language for XML
– Includes an XML vocabulary for Includes an XML vocabulary for specifying formattingspecifying formatting
• XSLT – XSL TransformationsXSLT – XSL Transformations– Language for transforming XML Language for transforming XML
documents into other XML documents into other XML documents.documents.
March 28, 2001 XSP SessionO’Reilly Enterprise Java Conference
8
Object ModelObject Model
• Processors – take a DOM tree as Processors – take a DOM tree as input and produce another DOM input and produce another DOM tree as output (i.e. XSP engine in tree as output (i.e. XSP engine in Cocoon)Cocoon)
• Producers – what XSP pages are Producers – what XSP pages are compiled into (Cocoon Java compiled into (Cocoon Java type). type). – Similar to the fact that JSPs are Similar to the fact that JSPs are
compiled into Servletscompiled into Servlets
March 28, 2001 XSP SessionO’Reilly Enterprise Java Conference
9
Object ModelObject Model
Model from xml.apache.org
March 28, 2001 XSP SessionO’Reilly Enterprise Java Conference
10
Built-in ObjectsBuilt-in Objects
requestrequest sessionsession
responseresponse servletContexservletContextt
documentdocument xspGlobalxspGlobal
xspNodeStacxspNodeStackk
xspCurrentNoxspCurrentNodede
xspParentNodxspParentNodee
xspParserxspParser
Should look familiar to JSP programmers…
March 28, 2001 XSP SessionO’Reilly Enterprise Java Conference
11
XSPUtil ClassXSPUtil Class• DOM utility methodsDOM utility methods
– cloneNode, toMarkupcloneNode, toMarkup• HTTP HTTP
– encodeMarkup, formEncode, encodeMarkup, formEncode, formDecodeformDecode
• FileFile– pathComponent, fileComponent, pathComponent, fileComponent,
baseName, normalizeBaseName, baseName, normalizeBaseName, relativeFileNamerelativeFileName
• StringString– split, isAlphaNumericsplit, isAlphaNumeric
March 28, 2001 XSP SessionO’Reilly Enterprise Java Conference
12
What is an XSP PageWhat is an XSP Page• An XSP page is an XML page with the An XSP page is an XML page with the
following requirements:following requirements:– A Cocoon processing instruction that invokes A Cocoon processing instruction that invokes
the the XSP processor <?cocoon processtype="xsp"?>XSP processor <?cocoon processtype="xsp"?>
– Document root element must be <xsp:page>Document root element must be <xsp:page>– Any language and Taglib declarations as Any language and Taglib declarations as
attributes in the <xsp:page> tag .attributes in the <xsp:page> tag .Example: <xsp:page language="java“ Example: <xsp:page language="java“ xmlns:xsp="http://www.apache.org/1999/XSP/Cxmlns:xsp="http://www.apache.org/1999/XSP/Core">ore">
– An <xsp:logic> element and an <xsp:exp> An <xsp:logic> element and an <xsp:exp> element. element.
March 28, 2001 XSP SessionO’Reilly Enterprise Java Conference
13
Sample Minimal XSPSample Minimal XSP<?xml version="1.0"?><?xml version="1.0"?><?cocoon-process type="xsp"?><?cocoon-process type="xsp"?><?cocoon-process type="xslt"?><?cocoon-process type="xslt"?><?xml-stylesheet href="page-html.xsl" type="text/xsl"?><?xml-stylesheet href="page-html.xsl" type="text/xsl"?><xsp:page language="java" <xsp:page language="java"
xmlns:xsp="http://www.apache.org/1999/XSP/Core">xmlns:xsp="http://www.apache.org/1999/XSP/Core"><xsp:logic><xsp:logic> static private int counter = 0; static private int counter = 0; private synchronized int count() {private synchronized int count() {
return counter++;return counter++; }} </xsp:logic></xsp:logic><page><page> <p>This page has been requested <xsp:expr>count()</xsp:expr> <p>This page has been requested <xsp:expr>count()</xsp:expr>
times.</p>times.</p> </page></page> </xsp:page></xsp:page>
March 28, 2001 XSP SessionO’Reilly Enterprise Java Conference
14
Processing InstructionProcessing Instruction
• XML PIs used to forward the document to XML PIs used to forward the document to the right processor or formatter. the right processor or formatter.
• <?cocoon-process type="xxx"?> for <?cocoon-process type="xxx"?> for processingprocessing
• <?cocoon-format type="yyy"?> for <?cocoon-format type="yyy"?> for formattingformatting
• Examples:Examples:– <?cocoon-process type="xsp"?> <?cocoon-process type="xsp"?> – <?cocoon-process type="xslt"?><?cocoon-process type="xslt"?>
<?xml-stylesheet href="sample.xsl” <?xml-stylesheet href="sample.xsl” type="text/xsl"?>type="text/xsl"?>
March 28, 2001 XSP SessionO’Reilly Enterprise Java Conference
15
Logic TagsLogic Tags
• <xsp:logic> encloses developer-<xsp:logic> encloses developer-supplied program logic. supplied program logic. Transcribed verbatim into the Transcribed verbatim into the generated Cocoon producer generated Cocoon producer
• <xsp:expr> evaluates a program <xsp:expr> evaluates a program expression and substitutes its expression and substitutes its value as a Text DOM node in the value as a Text DOM node in the resulting document resulting document
March 28, 2001 XSP SessionO’Reilly Enterprise Java Conference
16
TagsetTagset
<xsp:page><xsp:page> <xsp:structur<xsp:structure>e>
<xsp:include<xsp:include> >
<xsp:content<xsp:content>>
<xsp:logic><xsp:logic> <xsp:expr><xsp:expr>
<xsp:element<xsp:element>>
<xsp:attribut<xsp:attribute>e>
<xsp:pi><xsp:pi> <xsp:comme<xsp:comment>nt>
March 28, 2001 XSP SessionO’Reilly Enterprise Java Conference
17
Scripting LanguagesScripting Languages
• XSP designed to support XSP designed to support scripting languages other than scripting languages other than JavaJava
• In theory, any programming In theory, any programming language that has a Java-based language that has a Java-based interpreter could be used to interpreter could be used to script XSPscript XSP
• Right now, only JavaRight now, only Java
March 28, 2001 XSP SessionO’Reilly Enterprise Java Conference
18
Sample - Embedded Logic Sample - Embedded Logic
sample.xml
Well formed
XSP page
XSP Stylesheet
sample.xsl
HTML
sample.html
March 28, 2001 XSP SessionO’Reilly Enterprise Java Conference
19
Sample - Included LogicsheetSample - Included Logicsheet
HTML
sample.html
LogicSheet
sample-xsp.xslsample.xml
Stylesheet
sample.xsl
XSP
TransformedXML
XML
March 28, 2001 XSP SessionO’Reilly Enterprise Java Conference
20
Sample - Taglib LogicsheetSample - Taglib Logicsheet
HTML
sample.html
LogicSheet
taglib.xslsample.xml
Stylesheet
sample.xsl
XSP
TransformedXML
XSP That Includes
A Reference To ATaglib
PropertiesFile
Cocoon properties
March 28, 2001 XSP SessionO’Reilly Enterprise Java Conference
21
Additional ResourcesAdditional Resources
• xml.apache.orgxml.apache.org• xml.apache.org/cocoon/wd-xml.apache.org/cocoon/wd-
xsp.htmlxsp.html• www.w3.org/TR/xsltwww.w3.org/TR/xslt• www.w3.org/TR/REC-xmlwww.w3.org/TR/REC-xml
March 28, 2001 XSP SessionO’Reilly Enterprise Java Conference
22
QuestionsQuestions
<xsp:logic><xsp:logic>
? ? ?? ? ?</xsp:logic></xsp:logic>
<xsp:logic><xsp:logic>
? ? ?? ? ?</xsp:logic></xsp:logic>