Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

servlet
Class ApplyXSLT  view ApplyXSLT download ApplyXSLT.java

java.lang.Object
  extended byjavax.servlet.GenericServlet
      extended byjavax.servlet.http.HttpServlet
          extended byservlet.ApplyXSLT
All Implemented Interfaces:
java.io.Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig

public class ApplyXSLT
extends javax.servlet.http.HttpServlet

ApplyXSLT supplies the basic functions for transforming XML data using XSL stylesheets.


Field Summary
static java.lang.String CURRENTDIR
           
static java.lang.String EOL
          String representing the end of line characters for the System.
static java.lang.String FS
          String representing the file separator characters for the System.
protected static java.lang.String HEADER_NAME
          The HTTP Header used for matching the Stylesheet attribute via the media properties file selected.
protected  ApplyXSLTProperties ourDefaultParameters
          Operational parameters for this class.
protected  OrderedProps ourMediaProps
          Mapping of HTTP request's user-Agent values to stylesheet media= values.
static java.lang.String ROOT
          String representing the current directory for properties files.
protected static java.lang.String STYLESHEET_ATTRIBUTE
          The attribute name in the tag used in stylesheet selection.
 
Fields inherited from class javax.servlet.http.HttpServlet
 
Fields inherited from class javax.servlet.GenericServlet
 
Constructor Summary
ApplyXSLT()
           
 
Method Summary
protected  void displayException(javax.servlet.http.HttpServletResponse response, ApplyXSLTException xse, boolean debug)
          Invokes response.sendError setting an HTTP status code and optionally an error message as an HTML page.
 void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          HTTP Get method passed on to process().
 java.lang.String getContentType(javax.xml.transform.Templates templates)
          Returns the response content type specified by the media-type and encoding attributes of the <xsl:output> element(s) of the stylesheet.
protected  javax.xml.transform.stream.StreamSource getDocument(javax.servlet.http.HttpServletRequest request, ApplyXSLTListener listener)
          Returns an XML XSLTInputSource DOM.
 java.lang.String getMedia(javax.servlet.http.HttpServletRequest request)
           
protected  javax.xml.transform.stream.StreamSource getStylesheet(javax.xml.transform.TransformerFactory tFactory, javax.servlet.http.HttpServletRequest request, javax.xml.transform.stream.StreamSource xmlSource, ApplyXSLTListener listener)
          Returns a Templates (StylesheetRoot) object.
static java.lang.String getXSLURLfromDoc(javax.xml.transform.stream.StreamSource xmlSource, java.lang.String attributeName, java.lang.String attributeValue, javax.xml.transform.TransformerFactory tFactory)
          Returns the XSL stylesheet URL associated with the specified XML document.
 void init(javax.servlet.ServletConfig config)
          Initialize operational parameters from the configuration.
 void process(javax.xml.transform.TransformerFactory tFactory, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Coordinates applying an XSL stylesheet to XML data using operational parameters.
protected  void setDefaultParameters(javax.servlet.ServletConfig config)
          Sets the default parameters for the servlet from the configuration.
protected  void setMediaProps(java.lang.String mediaURLstring)
          Loads the media properties file specified by the given string.
 void setStylesheetParams(javax.xml.transform.Transformer transformer, javax.servlet.http.HttpServletRequest request)
          Defines and sets select top-level XSL stylesheet variables from the HTTP request, which can be evaluated using <xsl:param-variable>.
protected  java.net.URLConnection toAcceptLanguageConnection(java.net.URL url, javax.servlet.http.HttpServletRequest request)
          Returns a connection which respects the Accept-Language header of the HTTP request.
protected  void writeLog(ApplyXSLTException axe)
          Writes the following information to the servlet log: HTTP status code Message Stack trace
protected  void writeLog(java.lang.String msg, int statusCode)
          Writes the following information to the servlet log: HTTP status code Message
protected  void writeLog(java.lang.String msg, int statusCode, java.lang.Throwable t)
          Writes the following information to the servlet log: HTTP status code Message Stack trace
 
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service, service
 
Methods inherited from class javax.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ourDefaultParameters

protected ApplyXSLTProperties ourDefaultParameters
Operational parameters for this class.

Request-time values override init-time values which override class defaults.

See Also:
init(javax.servlet.ServletConfig) 55

EOL

public static final java.lang.String EOL
String representing the end of line characters for the System.


FS

public static final java.lang.String FS
String representing the file separator characters for the System.


ROOT

public static final java.lang.String ROOT
String representing the current directory for properties files. See init().


CURRENTDIR

public static java.lang.String CURRENTDIR

ourMediaProps

protected OrderedProps ourMediaProps
Mapping of HTTP request's user-Agent values to stylesheet media= values.

This mapping is defined by a file pointed to by the operational parameter "mediaURL" which can either contain a full URL or a path relative to the System's server.root /servlets directory.

See Also:
setMediaProps(java.lang.String) 55 , getMedia(javax.servlet.http.HttpServletRequest) 55

STYLESHEET_ATTRIBUTE

protected static final java.lang.String STYLESHEET_ATTRIBUTE
The attribute name in the tag used in stylesheet selection.

See Also:
Constant Field Values

HEADER_NAME

protected static final java.lang.String HEADER_NAME
The HTTP Header used for matching the Stylesheet attribute via the media properties file selected.

See Also:
Constant Field Values
Constructor Detail

ApplyXSLT

public ApplyXSLT()
Method Detail

init

public void init(javax.servlet.ServletConfig config)
          throws javax.servlet.ServletException
Initialize operational parameters from the configuration.


setDefaultParameters

protected void setDefaultParameters(javax.servlet.ServletConfig config)
Sets the default parameters for the servlet from the configuration. Also sets required system properties until we figure out why servlet sometimess fails to read properties from properties files.


setMediaProps

protected void setMediaProps(java.lang.String mediaURLstring)
Loads the media properties file specified by the given string.


getMedia

public java.lang.String getMedia(javax.servlet.http.HttpServletRequest request)

doGet

public void doGet(javax.servlet.http.HttpServletRequest request,
                  javax.servlet.http.HttpServletResponse response)
           throws javax.servlet.ServletException,
                  java.io.IOException
HTTP Get method passed on to process().


process

public void process(javax.xml.transform.TransformerFactory tFactory,
                    javax.servlet.http.HttpServletRequest request,
                    javax.servlet.http.HttpServletResponse response)
             throws javax.servlet.ServletException,
                    java.io.IOException,
                    org.xml.sax.SAXException
Coordinates applying an XSL stylesheet to XML data using operational parameters.

If successfully applied, the result tree will be streamed to the response object and the content type set according to the XSL stylesheet's <xsl:output> element(s).

If there is a problem in parsing the XML/XSL or if there is a problem in applying the XSL to the XML, an exception will be streamed to the response object. The detail of the information returned in the response object will depend on whether we're running in debug mode or not.


getDocument

protected javax.xml.transform.stream.StreamSource getDocument(javax.servlet.http.HttpServletRequest request,
                                                              ApplyXSLTListener listener)
                                                       throws ApplyXSLTException
Returns an XML XSLTInputSource DOM. Attempts will be make to create the DOM from the following sources:
  1. A relative URL specified in the HTTP request's path information. This capability is intended for use by servlet engines that map some or all XML data to be processed at the server.
  2. A URL specified in the HTTP request's URL= parameter. This capability is intended for clients wishing to selectively process XML data at the server. For security reasons, this URL will be forced to the local IP host.
  3. The HTTP request's XML input stream. This capability is intended for use by chained servlets.


getStylesheet

protected javax.xml.transform.stream.StreamSource getStylesheet(javax.xml.transform.TransformerFactory tFactory,
                                                                javax.servlet.http.HttpServletRequest request,
                                                                javax.xml.transform.stream.StreamSource xmlSource,
                                                                ApplyXSLTListener listener)
                                                         throws ApplyXSLTException
Returns a Templates (StylesheetRoot) object. Attempts will be make to create the Stylesheet from the followingsources:
  1. A URL specified in the HTTP request's xslURL= parameter. This capability is intended for clients wishing to selectively override the server algorithm for applying XSL stylesheets. For security reasons, this URL will be forced to the local IP host.
  2. XML association. XML documents may contain references to one or more stylesheets using this W3C proposed recommendation. If the XML document does contain such references, a best match will be chosen based on the browser type making the request and the default association. This capability enables relationships to be defined between client capabilities and stylesheets capable of acting on these capabilities.
  3. A configured default stylesheet URL


getContentType

public java.lang.String getContentType(javax.xml.transform.Templates templates)
Returns the response content type specified by the media-type and encoding attributes of the <xsl:output> element(s) of the stylesheet.


setStylesheetParams

public void setStylesheetParams(javax.xml.transform.Transformer transformer,
                                javax.servlet.http.HttpServletRequest request)
Defines and sets select top-level XSL stylesheet variables from the HTTP request, which can be evaluated using <xsl:param-variable>. The following variables will be automatically set:
ParameterName
Each non-reserved request parameter returned from request.getParameterNames(). If a parameter contains more than a single value, only the first value is available.
servlet-RemoteAddr
Contains String output from request.getRemoteAddr(), which is the IP address of the client machine.
servlet-RemoteHost
Contains String output from request.getRemoteHost(), which is the host name of the client machine.
servlet-RemoteUser
Contains String output from request.getRemoteUser(), which was the user name accepted by the server to grant access to this servlet.
servlet-Request
Contains the request object.


writeLog

protected void writeLog(ApplyXSLTException axe)
Writes the following information to the servlet log:
  1. HTTP status code
  2. Message
  3. Stack trace


writeLog

protected void writeLog(java.lang.String msg,
                        int statusCode,
                        java.lang.Throwable t)
Writes the following information to the servlet log:
  1. HTTP status code
  2. Message
  3. Stack trace


writeLog

protected void writeLog(java.lang.String msg,
                        int statusCode)
Writes the following information to the servlet log:
  1. HTTP status code
  2. Message


displayException

protected void displayException(javax.servlet.http.HttpServletResponse response,
                                ApplyXSLTException xse,
                                boolean debug)
Invokes response.sendError setting an HTTP status code and optionally an error message as an HTML page.

If running in debug mode, also try to return a stack trace of the exception and and xml/xsl processor messages.


toAcceptLanguageConnection

protected java.net.URLConnection toAcceptLanguageConnection(java.net.URL url,
                                                            javax.servlet.http.HttpServletRequest request)
                                                     throws java.lang.Exception
Returns a connection which respects the Accept-Language header of the HTTP request. This is useful when XSL files are internationalized for use with Web servers which respect this header.

For example, Apache 1.3.6 may be configured for multiviews. Under this configuration, requests for http://myhost/index.html would return http://myhost/index.html.fr to French browsers and http://myhost/index.html.en to English browsers.


getXSLURLfromDoc

public static java.lang.String getXSLURLfromDoc(javax.xml.transform.stream.StreamSource xmlSource,
                                                java.lang.String attributeName,
                                                java.lang.String attributeValue,
                                                javax.xml.transform.TransformerFactory tFactory)
Returns the XSL stylesheet URL associated with the specified XML document. If multiple XSL stylesheets are associated with the XML document, preference will be given to the stylesheet which contains an attribute name/value pair that corresponds to the specified attributeName and attributeValue.