morten 01/08/02 09:01:51
Modified: java/src/org/apache/xalan/xsltc/compiler Parser.java
XSLTC.java
java/src/org/apache/xalan/xsltc/trax
TransformerFactoryImpl.java
Log:
Added support for the TransformerFactory's getAssociatedStylesheet()
method. Added similar methods to the native XSLTC API, as they can come
in handy.
PR: n/a
Obtained from: n/a
Submitted by: [EMAIL PROTECTED]
Reviewed by: [EMAIL PROTECTED]
Revision Changes Path
1.19 +50 -8
xml-xalan/java/src/org/apache/xalan/xsltc/compiler/Parser.java
Index: Parser.java
===================================================================
RCS file:
/home/cvs/xml-xalan/java/src/org/apache/xalan/xsltc/compiler/Parser.java,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -r1.18 -r1.19
--- Parser.java 2001/07/30 13:35:40 1.18
+++ Parser.java 2001/08/02 16:01:51 1.19
@@ -1,5 +1,5 @@
/*
- * @(#)$Id: Parser.java,v 1.18 2001/07/30 13:35:40 morten Exp $
+ * @(#)$Id: Parser.java,v 1.19 2001/08/02 16:01:51 morten Exp $
*
* The Apache Software License, Version 1.1
*
@@ -419,6 +419,25 @@
return _root;
}
+ private String _PImedia = null;
+ private String _PItitle = null;
+ private String _PIcharset = null;
+
+ /**
+ * Set the parameters to use to locate the correct <?xml-stylesheet ...?>
+ * processing instruction in the case where the input document is an
+ * XML document with one or more references to a stylesheet.
+ * @param media The media attribute to be matched. May be null, in which
+ * case the prefered templates will be used (i.e. alternate = no).
+ * @param title The value of the title attribute to match. May be null.
+ * @param charset The value of the charset attribute to match. May be
null.
+ */
+ protected void setPIParameters(String media, String title, String
charset) {
+ _PImedia = media;
+ _PItitle = title;
+ _PIcharset = charset;
+ }
+
/**
* Extracts the DOM for the stylesheet. In the case of an embedded
* stylesheet, it extracts the DOM subtree corresponding to the
@@ -1075,22 +1094,45 @@
}
}
+ private String getTokenValue(String token) {
+ final int start = token.indexOf('"');
+ final int stop = token.lastIndexOf('"');
+ return token.substring(start+1, stop);
+ }
+
/**
* SAX2: Receive notification of a processing instruction.
* These require special handling for stylesheet PIs.
*/
public void processingInstruction(String name, String value) {
+ // We only handle the <?xml-stylesheet ...?> PI
if ((_target == null) && (name.equals("xml-stylesheet"))) {
+
+ String href = null; // URI of stylesheet found
+ String media = null; // Media of stylesheet found
+ String title = null; // Title of stylesheet found
+ String charset = null; // Charset of stylesheet found
+
+ // Get the attributes from the processing instruction
StringTokenizer tokens = new StringTokenizer(value);
while (tokens.hasMoreElements()) {
String token = (String)tokens.nextElement();
- if (token.startsWith("href=")) {
- _target = token.substring(5);
- final int start = _target.indexOf('"');
- final int stop = _target.lastIndexOf('"');
- _target = _target.substring(start+1,stop);
- return;
- }
+ if (token.startsWith("href"))
+ href = getTokenValue(token);
+ else if (token.startsWith("media"))
+ media = getTokenValue(token);
+ else if (token.startsWith("title"))
+ title = getTokenValue(token);
+ else if (token.startsWith("charset"))
+ charset = getTokenValue(token);
+ }
+
+ // Set the target to this PI's href if the parameters are
+ // null or match the corresponding attributes of this PI.
+ if ( ((_PImedia == null) || (_PImedia.equals(media))) &&
+ ((_PItitle == null) || (_PImedia.equals(title))) &&
+ ((_PIcharset == null) || (_PImedia.equals(charset))) ) {
+ _target = href;
}
}
}
1.23 +14 -1
xml-xalan/java/src/org/apache/xalan/xsltc/compiler/XSLTC.java
Index: XSLTC.java
===================================================================
RCS file:
/home/cvs/xml-xalan/java/src/org/apache/xalan/xsltc/compiler/XSLTC.java,v
retrieving revision 1.22
retrieving revision 1.23
diff -u -r1.22 -r1.23
--- XSLTC.java 2001/07/31 10:04:51 1.22
+++ XSLTC.java 2001/08/02 16:01:51 1.23
@@ -1,5 +1,5 @@
/*
- * @(#)$Id: XSLTC.java,v 1.22 2001/07/31 10:04:51 morten Exp $
+ * @(#)$Id: XSLTC.java,v 1.23 2001/08/02 16:01:51 morten Exp $
*
* The Apache Software License, Version 1.1
*
@@ -203,6 +203,19 @@
*/
public void setSourceLoader(SourceLoader loader) {
_loader = loader;
+ }
+
+ /**
+ * Set the parameters to use to locate the correct <?xml-stylesheet ...?>
+ * processing instruction in the case where the input document to the
+ * compiler (and parser) is an XML document.
+ * @param media The media attribute to be matched. May be null, in which
+ * case the prefered templates will be used (i.e. alternate = no).
+ * @param title The value of the title attribute to match. May be null.
+ * @param charset The value of the charset attribute to match. May be
null.
+ */
+ public void setPIParameters(String media, String title, String charset) {
+ _parser.setPIParameters(media, title, charset);
}
/**
1.18 +39 -15
xml-xalan/java/src/org/apache/xalan/xsltc/trax/TransformerFactoryImpl.java
Index: TransformerFactoryImpl.java
===================================================================
RCS file:
/home/cvs/xml-xalan/java/src/org/apache/xalan/xsltc/trax/TransformerFactoryImpl.java,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- TransformerFactoryImpl.java 2001/08/02 15:08:19 1.17
+++ TransformerFactoryImpl.java 2001/08/02 16:01:51 1.18
@@ -1,5 +1,5 @@
/*
- * @(#)$Id: TransformerFactoryImpl.java,v 1.17 2001/08/02 15:08:19 morten
Exp $
+ * @(#)$Id: TransformerFactoryImpl.java,v 1.18 2001/08/02 16:01:51 morten
Exp $
*
* The Apache Software License, Version 1.1
*
@@ -71,6 +71,7 @@
import java.net.URL;
import java.net.MalformedURLException;
import java.util.Vector;
+import java.util.Hashtable;
import javax.xml.transform.*;
import javax.xml.transform.sax.*;
@@ -124,6 +125,24 @@
private static final String COMPILE_ERR =
"Could not compile stylesheet";
+ // This Hashtable is used to store parameters for locating
+ // <?xml-stylesheet ...?> processing instructions in XML documents.
+ private Hashtable _piParams = null;
+
+ // The above hashtable stores objects of this class only:
+ private class PIParamWrapper {
+ public String _media = null;
+ public String _title = null;
+ public String _charset = null;
+
+ public PIParamWrapper(String media, String title, String charset) {
+ _media = media;
+ _title = title;
+ _charset = charset;
+ }
+ }
+
+
/**
* javax.xml.transform.sax.TransformerFactory implementation.
* Contains nothing yet
@@ -261,10 +280,12 @@
public Source getAssociatedStylesheet(Source source, String media,
String title, String charset)
throws TransformerConfigurationException {
- // The org.apache.xalan.xsltc.copiler.Parser will locate the first
- // <?xml-stylesheeet ?> PI in the Source document and use that.
- // For now we'll leave it at that.
- return(source); // TODO - pass media/title/charset to Parser
+ // First create a hashtable that maps Source refs. to parameters
+ if (_piParams == null) _piParams = new Hashtable();
+ // Store the parameters for this Source in the Hashtable
+ _piParams.put(source, new PIParamWrapper(media, title, charset));
+ // Return the same Source - we'll locate the stylesheet later
+ return(source);
}
/**
@@ -283,8 +304,6 @@
return _copyTransformer;
}
- byte[][] bytecodes = null; // The translet classes go in here
-
XSLTC xsltc = new XSLTC();
xsltc.init();
@@ -293,7 +312,7 @@
ByteArrayInputStream bytestream = new ByteArrayInputStream(bytes);
InputSource input = new InputSource(bytestream);
input.setSystemId(_defaultTransletName);
- bytecodes = xsltc.compile(_defaultTransletName, input);
+ byte[][] bytecodes = xsltc.compile(_defaultTransletName, input);
// Check that the transformation went well before returning
if (bytecodes == null) {
@@ -442,21 +461,26 @@
public Templates newTemplates(Source source)
throws TransformerConfigurationException {
- // Create a placeholder for the translet bytecodes
- byte[][] bytecodes = null;
-
// Create and initialize a stylesheet compiler
final XSLTC xsltc = new XSLTC();
xsltc.init();
// Set a document loader (for xsl:include/import) if defined
- if (_uriResolver != null)
- xsltc.setSourceLoader(this);
+ if (_uriResolver != null) xsltc.setSourceLoader(this);
+
+ // Pass parameters to the Parser to make sure it locates the correct
+ // <?xml-stylesheet ...?> PI in an XML input document
+ if ((_piParams != null) && (_piParams.get(source) != null)) {
+ // Get the parameters for this Source object
+ PIParamWrapper p = (PIParamWrapper)_piParams.get(source);
+ // Pass them on to the compiler (which will pass then to the parser)
+ if (p != null)
+ xsltc.setPIParameters(p._media, p._title, p._charset);
+ }
// Compile the stylesheet
final InputSource input = getInputSource(xsltc, source);
- bytecodes = xsltc.compile(null, input);
-
+ byte[][] bytecodes = xsltc.compile(null, input);
final String transletName = xsltc.getClassName();
// Pass compiler warnings to the error listener
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]