📄 applyxslt.java
字号:
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* $Id: ApplyXSLT.java 470245 2006-11-02 06:34:33Z minchau $
*/
package servlet;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.URL;
import java.net.MalformedURLException;
import java.net.URLConnection;
import javax.xml.transform.OutputKeys;
import org.apache.xalan.templates.Constants;
import org.apache.xalan.templates.StylesheetRoot;
// SAX2 Imports
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.Locator;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.ext.DeclHandler;
import org.xml.sax.ext.LexicalHandler;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.w3c.dom.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import org.apache.xalan.transformer.TransformerImpl;
import org.apache.xpath.objects.XObject;
import org.apache.xpath.objects.XString;
import org.apache.xalan.processor.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.helpers.XMLFilterImpl;
/*****************************************************************************************************
*
* ApplyXSLT supplies the basic
* functions for transforming XML data using XSL stylesheets.
*
* @author Spencer Shepard (sshepard@us.ibm.com)
* @author R. Adam King (rak@us.ibm.com)
* @author Tom Rowe (trowe@us.ibm.com)
* @author Don Leslie (donald_leslie@lotus.com)
*
*****************************************************************************************************/
public class ApplyXSLT extends HttpServlet
{
/**
* Operational parameters for this class.
* <p>Request-time values override init-time values which override class defaults.</p>
* @see #init
* @serial
*/
protected ApplyXSLTProperties ourDefaultParameters = null;
/**
* String representing the end of line characters for the System.
*/
public final static String EOL = System.getProperty("line.separator");
/**
* String representing the file separator characters for the System.
*/
public final static String FS = System.getProperty("file.separator");
/**
* String representing the current directory for properties files. See init().
*/
public final static String ROOT = System.getProperty("server.root");
public static String CURRENTDIR;
/**
* Initialize operational parameters from the configuration.
* @param config Configuration
* @exception ServletException Never thrown
*/
public void init(ServletConfig config)
throws ServletException
{
super.init(config);
// If the server.root property --see above-- is null, use current working directory
// as default location for media.properties.
if (ROOT != null){
CURRENTDIR= getServletContext().getRealPath("/WEB-INF/classes/servlet/") + FS;
System.out.println ( CURRENTDIR);}
else
CURRENTDIR = System.getProperty("user.dir")+ FS;
setDefaultParameters(config);
setMediaProps(config.getInitParameter("mediaURL"));
}
/**
* 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.
* @param config Configuration
*/
protected void setDefaultParameters(ServletConfig config)
{
ourDefaultParameters = new DefaultApplyXSLTProperties(config);
}
/**
* Loads the media properties file specified by the given string.
* @param mediaURLstring Location of the media properties file. Can be either a full URL or a path relative
* to the System's server.root /servlets directory. If this parameter is null,
* server.root/servlets/media.properties will be used.
* @see ApplyXSL#CURRENTDIR
*/
protected void setMediaProps(String mediaURLstring)
{
if (mediaURLstring != null)
{
URL url = null;
try
{
url = new URL(mediaURLstring);
}
catch (MalformedURLException mue1)
{
try
{
url = new URL("file", "", CURRENTDIR + mediaURLstring);
}
catch (MalformedURLException mue2)
{
writeLog("Unable to find the media properties file based on parameter 'mediaURL' = "
+ mediaURLstring, HttpServletResponse.SC_ACCEPTED, mue2);
url = null;
}
}
if (url != null)
{
try
{
ourMediaProps = new OrderedProps(url.openStream());
}
catch (IOException ioe1)
{
writeLog("Exception occurred while opening media properties file: " + mediaURLstring +
". Media table may be invalid.", HttpServletResponse.SC_ACCEPTED, ioe1);
}
}
}
else
{
String defaultProp = CURRENTDIR + "media.properties";
try
{
ourMediaProps = new OrderedProps(new FileInputStream(defaultProp));
}
catch (IOException ioe2)
{
writeLog("Default media properties file " + defaultProp + " not found.",
HttpServletResponse.SC_ACCEPTED, ioe2);
}
}
}
public String getMedia(HttpServletRequest request)
{
return ourMediaProps.getValue(request.getHeader(HEADER_NAME));
}
// doPost removed for security reasons due to the possibility of sending
// unsecure XML and XSL XSLTInputSources through the request input stream
/**
* HTTP Get method passed on to process().
* @param request The request
* @param response The response
* @see #process
* @exception ServletException Never thrown
* @exception IOException Never thrown
*/
public void doGet (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
try
{
TransformerFactory tFactory = TransformerFactory.newInstance();
process(tFactory, request, response);
}
catch (Exception e)
{
}
}
/**
* Coordinates applying an XSL stylesheet to XML data using operational parameters.
* <p>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).</p>
* <p>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.</p>
* @param processor implementation of TRaX processor
* @param request May contain information relevant to creating XML and XSL XSLTInputSource's
* @param response Where to write the transformation result
* @see #getDocument
* @see #getStylesheet
* @see #getContentType
* @see #displayException
* @see #setStylesheetParams
* @exception ServletException Never thrown
* @exception IOException Never thrown
*/
public void process(TransformerFactory tFactory,
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException, SAXException
{
boolean debug = ourDefaultParameters.isDebug(request);
long time = 0;
if (debug)
time = System.currentTimeMillis();
// Listener to be used for all reporting
ApplyXSLTListener listener = new ApplyXSLTListener();
listener.out.println("debug is " + debug);
StreamSource xmlSource = null;
StreamSource xslSource = null;
try
{
if ((xmlSource = getDocument(request, listener)) == null)
throw new ApplyXSLTException("getDocument() returned null",
new NullPointerException(),
response.SC_NOT_FOUND);
}
catch (ApplyXSLTException axe)
{
axe.appendMessage(EOL + "getDocument() resulted in ApplyXSLTException" + EOL
+ listener.getMessage());
if (debug) writeLog(axe);
displayException(response, axe, debug);
xmlSource = null;
}
// creating XSL Stylesheet
if (xmlSource != null)
{
try
{
if ((xslSource = getStylesheet(tFactory, request, xmlSource, listener)) == null)
{
throw new ApplyXSLTException("getStylesheet() returned null",
new NullPointerException(),
response.SC_NOT_FOUND);
}
// For time being, must "reset" xmlSource after extracting stylesheet PI
xmlSource = getDocument(request, listener);
}
catch (ApplyXSLTException axe)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -