Henning P. Schmiedehausen wrote:
Thomas Vandahl <[EMAIL PROTECTED]> writes:
I have some patches and extensions to (embedded) Turbine services
against the 2.3.1 code base. I assume from your statement that no
extensions are planned in this area and I need to convert my patches to
the Fulcrum components. Is that correct?
Give them to me! I plan to do a 2.3.2-rc RSN.
Regards
Henning
Here you are. As you can see, I created patches against the HEAD
revisions. They are identical to those of 2.3.1 anyway.
I did the following:
- Change UIManager to allow customization of directories and default values
- Get rid of File() in UIManager
- Allow UIManager to return relative links if so configured.
- Get rid of direct File() access in TurbineXSLTService and replace it
with URL access through the servlet container. The change also allows
to place the repository of XSL files elsewhere.
- Replace the cache Map() with a LRUMap() to avoid infinite memory growth
- Add transform() methods having an additional parameter to forward
a parameter set to the XSLT. The parameters can be used in the style
sheet.
If you have any questions, feel free to ask.
Bye, Thomas.
Index: java/org/apache/turbine/services/xslt/TurbineXSLT.java
===================================================================
--- java/org/apache/turbine/services/xslt/TurbineXSLT.java (revision
233475)
+++ java/org/apache/turbine/services/xslt/TurbineXSLT.java (working copy)
@@ -20,9 +20,9 @@
import java.io.Reader;
import java.io.Writer;
+import java.util.Hashtable;
import org.apache.turbine.services.TurbineServices;
-
import org.w3c.dom.Node;
/**
@@ -67,4 +67,28 @@
{
return getService().transform(xslName, in);
}
+
+ public static void transform(String xslName, Reader in, Writer out,
Hashtable params)
+ throws Exception
+ {
+ getService().transform(xslName, in, out, params);
+ }
+
+ public static String transform(String xslName, Reader in, Hashtable
params)
+ throws Exception
+ {
+ return getService().transform(xslName, in, params);
+ }
+
+ public void transform(String xslName, Node in, Writer out, Hashtable
params)
+ throws Exception
+ {
+ getService().transform(xslName, in, out, params);
+ }
+
+ public String transform(String xslName, Node in, Hashtable params)
+ throws Exception
+ {
+ return getService().transform(xslName, in, params);
+ }
}
Index: java/org/apache/turbine/services/xslt/TurbineXSLTService.java
===================================================================
--- java/org/apache/turbine/services/xslt/TurbineXSLTService.java
(revision 233475)
+++ java/org/apache/turbine/services/xslt/TurbineXSLTService.java
(working copy)
@@ -18,31 +18,30 @@
*/
-import java.io.File;
+import java.io.IOException;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
+import java.net.URL;
+import java.util.Hashtable;
+import java.util.Iterator;
-import java.util.HashMap;
-import java.util.Map;
-
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
+import org.apache.commons.collections.map.LRUMap;
import org.apache.commons.configuration.Configuration;
-
import org.apache.commons.lang.StringUtils;
-
-import org.apache.turbine.Turbine;
import org.apache.turbine.services.InitializationException;
import org.apache.turbine.services.TurbineBaseService;
-
+import org.apache.turbine.services.servlet.TurbineServlet;
import org.w3c.dom.Node;
/**
@@ -69,10 +68,15 @@
*/
protected String path;
+ /**
+ * Path separator for URLs
+ */
+ private static final String slash = "/";
+
/**
* Cache of compiled StyleSheetRoots.
*/
- protected Map cache = new HashMap();
+ private LRUMap cache = new LRUMap(20);
/**
* Factory for producing templates and null transformers
@@ -88,16 +92,7 @@
{
Configuration conf = getConfiguration();
- path = Turbine.getRealPath(conf.getString(STYLESHEET_PATH, null));
-
- if (StringUtils.isNotEmpty(path))
- {
- if (!path.endsWith("/") && !path.endsWith ("\\"))
- {
- path = path + File.separator;
- }
- }
-
+ path = conf.getString(STYLESHEET_PATH, slash);
caching = conf.getBoolean(STYLESHEET_CACHING);
tfactory = TransformerFactory.newInstance();
@@ -105,48 +100,58 @@
setInit(true);
}
- /**
- * Get a valid and existing filename from a template name.
- * The extension is removed and replaced with .xsl. If this
- * file does not exist the method attempts to find default.xsl.
- * If it fails to find default.xsl it returns null.
- */
- protected String getFileName(String templateName)
- {
- // First we chop of the existing extension
- int colon = templateName.lastIndexOf(".");
- if (colon > 0)
- {
- templateName = templateName.substring(0, colon);
- }
+ /**
+ * Try to create a valid url object from the style parameter.
+ *
+ * @param style the xsl-Style
+ * @return a <code>URL</code> object or null if the style sheet could
not be found
+ */
+ private URL getStyleURL(String style)
+ {
+ StringBuffer sb = new StringBuffer(128);
+
+ if (StringUtils.isNotEmpty(path))
+ {
+ if (!path.startsWith(slash))
+ {
+ sb.append(slash);
+ }
+
+ sb.append(path);
+
+ if (!path.endsWith(slash))
+ {
+ sb.append(slash);
+ }
+ }
+ else
+ {
+ sb.append(slash);
+ }
- // Now we try to find the file ...
- File f = new File(path + templateName + ".xsl");
- if (f.exists())
- {
- return path + templateName + ".xsl";
- }
- else
- {
- // ... or the default file
- f = new File(path + "default.xsl");
- if (f.exists())
- {
- return path + "default.xsl";
- }
- else
- {
- return null;
- }
- }
- }
+ // we chop off the existing extension
+ int colon = style.lastIndexOf(".");
+
+ if (colon > 0)
+ {
+ sb.append(style.substring(0, colon));
+ }
+ else
+ {
+ sb.append(style);
+ }
+ sb.append(".xsl");
+
+ return TurbineServlet.getResource(sb.toString());
+ }
+
/**
- * Compile Templates from an input file.
+ * Compile Templates from an input URL.
*/
- protected Templates compileTemplates(String source) throws Exception
+ protected Templates compileTemplates(URL source) throws Exception
{
- StreamSource xslin = new StreamSource(new File(source));
+ StreamSource xslin = new StreamSource(source.openStream());
Templates root = tfactory.newTemplates(xslin);
return root;
}
@@ -171,11 +176,11 @@
return (Templates) cache.get(xslName);
}
- String fn = getFileName(xslName);
+ URL url = getStyleURL(xslName);
- if (fn == null) return null;
+ if (url == null) return null;
- Templates sr = compileTemplates(fn);
+ Templates sr = compileTemplates(url);
if (caching)
{
@@ -187,26 +192,45 @@
}
- protected void transform(String xslName, Source xmlin, Result xmlout)
- throws Exception
- {
- Templates sr = getTemplates(xslName);
- Transformer transformer;
+ /**
+ * Transform the input source into the output source using the given
style
+ *
+ * @param style the stylesheet parameter
+ * @param in the input source
+ * @param out the output source
+ * @param params XSLT parameter for the style sheet
+ *
+ * @throws TransformerException
+ */
+ protected void transform(String style, Source in, Result out, Hashtable
params)
+ throws TransformerException, IOException, Exception
+ {
+ Templates styleTemplate = getTemplates(style);
+ Transformer transformer = null;
+ if (styleTemplate != null)
+ {
+ transformer = styleTemplate.newTransformer();
+ }
+ else
+ {
+ transformer = tfactory.newTransformer();
+ }
+
+ if (params != null)
+ {
+ Iterator i = params.keySet().iterator();
+ while (i.hasNext())
+ {
+ String key = (String) i.next();
+ transformer.setParameter(key, params.get(key));
+ }
+ }
+
+ // Start the transformation and rendering process
+ transformer.transform(in, out);
+ }
- // If there is no stylesheet we just echo the xml
- if (sr == null)
- {
- transformer = tfactory.newTransformer();
- }
- else
- {
- transformer = sr.newTransformer();
- }
-
- transformer.transform(xmlin, xmlout);
- }
-
/**
* Execute an xslt
*/
@@ -216,7 +240,7 @@
Source xmlin = new StreamSource(in);
Result xmlout = new StreamResult(out);
- transform(xslName, xmlin, xmlout);
+ transform(xslName, xmlin, xmlout, null);
}
/**
@@ -238,7 +262,7 @@
Source xmlin = new DOMSource(in);
Result xmlout = new StreamResult(out);
- transform(xslName, xmlin, xmlout);
+ transform(xslName, xmlin, xmlout, null);
}
/**
@@ -252,4 +276,49 @@
return sw.toString();
}
+ /**
+ * Execute an xslt
+ */
+ public void transform(String xslName, Reader in, Writer out, Hashtable
params)
+ throws Exception
+ {
+ Source xmlin = new StreamSource(in);
+ Result xmlout = new StreamResult(out);
+
+ transform(xslName, xmlin, xmlout, params);
+ }
+
+ /**
+ * Execute an xslt
+ */
+ public String transform(String xslName, Reader in, Hashtable params)
throws Exception
+ {
+ StringWriter sw = new StringWriter();
+ transform(xslName, in, sw, params);
+ return sw.toString();
+ }
+
+ /**
+ * Execute an xslt
+ */
+ public void transform (String xslName, Node in, Writer out, Hashtable
params)
+ throws Exception
+ {
+ Source xmlin = new DOMSource(in);
+ Result xmlout = new StreamResult(out);
+
+ transform(xslName, xmlin, xmlout, params);
+ }
+
+ /**
+ * Execute an xslt
+ */
+ public String transform (String xslName, Node in, Hashtable params)
+ throws Exception
+ {
+ StringWriter sw = new StringWriter();
+ transform(xslName, in, sw, params);
+ return sw.toString();
+ }
+
}
Index: java/org/apache/turbine/services/xslt/XSLTService.java
===================================================================
--- java/org/apache/turbine/services/xslt/XSLTService.java (revision
233475)
+++ java/org/apache/turbine/services/xslt/XSLTService.java (working copy)
@@ -20,9 +20,9 @@
import java.io.Reader;
import java.io.Writer;
+import java.util.Hashtable;
import org.apache.turbine.services.Service;
-
import org.w3c.dom.Node;
/**
@@ -81,4 +81,46 @@
* @param out The writer for the transformed output
*/
String transform(String xslName, Node in) throws Exception;
+
+ /**
+ * Uses an xsl file to transform xml input from a reader and writes the
+ * output to a writer.
+ *
+ * @param xslName The name of the file that contains the xsl stylesheet.
+ * @param in The reader that passes the xml to be transformed
+ * @param out The writer for the transformed output
+ * @param params A set of parameters that will be forwarded to the XSLT
+ */
+ void transform(String xslName, Reader in, Writer out, Hashtable params)
throws Exception;
+
+ /**
+ * Uses an xsl file to transform xml input from a reader and returns a
+ * string containing the transformed output.
+ *
+ * @param xslName The name of the file that contains the xsl stylesheet.
+ * @param in The reader that passes the xml to be transformed
+ * @param params A set of parameters that will be forwarded to the XSLT
+ */
+ String transform(String xslName, Reader in, Hashtable params) throws
Exception;
+
+ /**
+ * Uses an xsl file to transform xml input from a DOM note and writes
the
+ * output to a writer.
+ *
+ * @param xslName The name of the file that contains the xsl stylesheet.
+ * @param in The DOM Node to be transformed
+ * @param out The writer for the transformed output
+ * @param params A set of parameters that will be forwarded to the XSLT
+ */
+ void transform(String xslName, Node in, Writer out, Hashtable params)
throws Exception;
+
+ /**
+ * Uses an xsl file to transform xml input from a DOM note and returns a
+ * string containing the transformed output.
+ *
+ * @param xslName The name of the file that contains the xsl stylesheet.
+ * @param out The writer for the transformed output
+ * @param params A set of parameters that will be forwarded to the XSLT
+ */
+ String transform(String xslName, Node in, Hashtable params) throws
Exception;
}
Index: java/org/apache/turbine/services/pull/util/UIManager.java
===================================================================
--- java/org/apache/turbine/services/pull/util/UIManager.java (revision
233475)
+++ java/org/apache/turbine/services/pull/util/UIManager.java (working copy)
@@ -18,17 +18,17 @@
*/
-import java.io.FileInputStream;
-
+import java.io.InputStream;
import java.util.Properties;
+import org.apache.commons.configuration.Configuration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-
import org.apache.turbine.Turbine;
import org.apache.turbine.om.security.User;
import org.apache.turbine.services.pull.ApplicationTool;
import org.apache.turbine.services.pull.TurbinePull;
+import org.apache.turbine.services.servlet.TurbineServlet;
import org.apache.turbine.util.RunData;
import org.apache.turbine.util.ServerData;
import org.apache.turbine.util.uri.DataURI;
@@ -95,11 +95,40 @@
private static final String IMAGES_DIRECTORY = "/images";
/**
- * Property tag for the skin that is to be
+ * Property tag for the default skin that is to be
* used for the web application.
*/
private static final String SKIN_PROPERTY = "tool.ui.skin";
+ /**
+ * Property tag for the image directory inside the skin that is to be
+ * used for the web application.
+ */
+ private static final String IMAGEDIR_PROPERTY = "tool.ui.dir.image";
+
+ /**
+ * Property tag for the skin directory that is to be
+ * used for the web application.
+ */
+ private static final String SKINDIR_PROPERTY = "tool.ui.dir.skin";
+
+ /**
+ * Property tag for the css file that is to be
+ * used for the web application.
+ */
+ private static final String CSS_PROPERTY = "tool.ui.css";
+
+ /**
+ * Property tag for the css file that is to be
+ * used for the web application.
+ */
+ private static final String RELATIVE_PROPERTY = "tool.ui.want.relative";
+
+ /**
+ * URL separator "/"
+ */
+ private static final String PATH_SEPARATOR = "/";
+
/**
* Default skin name. This name actually represents
* a directory in the WEBAPP/resources/ui/skins
@@ -133,7 +162,7 @@
/**
* The file name for the skin style sheet.
*/
- private static final String SKIN_CSS_FILE = "skin.css";
+ private static final String DEFAULT_SKIN_CSS_FILE = "skin.css";
/**
* This the resources directory relative to the
@@ -141,7 +170,11 @@
* URIs for retrieving images in image().
*/
private String resourcesDirectory;
-
+ private String imagesDirectory;
+ private String cssFile;
+
+ private boolean want_relative = false;
+
/**
* Properties to hold the name/value pairs
* for the skin.
@@ -158,8 +191,14 @@
/**
* Store the resources directory for use in image().
*/
+ Configuration cfg = Turbine.getConfiguration();
resourcesDirectory = TurbinePull.getResourcesDirectory();
+ if (resourcesDirectory.endsWith(PATH_SEPARATOR))
+ resourcesDirectory = resourcesDirectory.substring(0,
resourcesDirectory.length()-2);
+ if (resourcesDirectory.startsWith(PATH_SEPARATOR))
+ resourcesDirectory = resourcesDirectory.substring(1);
+
if (data == null)
{
log.debug("UI Manager scope is global");
@@ -176,9 +215,22 @@
setSkin((User) data);
}
- skinsDirectory =
- TurbinePull.getAbsolutePathToResourcesDirectory() +
SKINS_DIRECTORY;
+ skinsDirectory = cfg.getString(SKINDIR_PROPERTY,
SKINS_DIRECTORY);
+ if (skinsDirectory.endsWith(PATH_SEPARATOR))
+ skinsDirectory = skinsDirectory.substring(0,
skinsDirectory.length()-2);
+ if (skinsDirectory.startsWith(PATH_SEPARATOR))
+ skinsDirectory = skinsDirectory.substring(1);
+ imagesDirectory = cfg.getString(IMAGEDIR_PROPERTY,
IMAGES_DIRECTORY);
+ if (imagesDirectory.endsWith(PATH_SEPARATOR))
+ imagesDirectory = imagesDirectory.substring(0,
imagesDirectory.length()-2);
+ if (imagesDirectory.startsWith(PATH_SEPARATOR))
+ imagesDirectory = imagesDirectory.substring(1);
+
+ cssFile = cfg.getString(CSS_PROPERTY, DEFAULT_SKIN_CSS_FILE);
+
+ want_relative = cfg.getBoolean(RELATIVE_PROPERTY, false);
+
loadSkin();
}
@@ -233,42 +285,47 @@
StringBuffer sb = new StringBuffer();
- sb.append(resourcesDirectory).
- append(SKINS_DIRECTORY).
- append("/").
- append(getSkin()).
- append(IMAGES_DIRECTORY).
- append("/").
- append(imageId);
+ sb.append(resourcesDirectory).
+ append(PATH_SEPARATOR).
+ append(skinsDirectory).
+ append(PATH_SEPARATOR).
+ append(getSkin()).
+ append(PATH_SEPARATOR).
+ append(imagesDirectory).
+ append(PATH_SEPARATOR).
+ append(imageId);
du.setScriptName(sb.toString());
- return du.getAbsoluteLink();
+
+ return want_relative ? du.getRelativeLink() : du.getAbsoluteLink();
}
- /**
- * Retrieve the URL for an image that is part
- * of a skin. The images are stored in the
- * WEBAPP/resources/ui/skins/<SKIN>/images
- * directory.
- */
- public String image(String imageId)
- {
- ServerData sd = Turbine.getDefaultServerData();
- DataURI du = new DataURI(sd);
+ /**
+ * Retrieve the URL for an image that is part
+ * of a skin. The images are stored in the
+ * WEBAPP/resources/ui/skins/<SKIN>/images
+ * directory.
+ */
+ public String image(String imageId)
+ {
+ ServerData sd = Turbine.getDefaultServerData();
+ DataURI du = new DataURI(sd);
- StringBuffer sb = new StringBuffer();
+ StringBuffer sb = new StringBuffer();
- sb.append(resourcesDirectory).
- append(SKINS_DIRECTORY).
- append("/").
- append(getSkin()).
- append(IMAGES_DIRECTORY).
- append("/").
- append(imageId);
+ sb.append(resourcesDirectory).
+ append(PATH_SEPARATOR).
+ append(skinsDirectory).
+ append(PATH_SEPARATOR).
+ append(getSkin()).
+ append(PATH_SEPARATOR).
+ append(imagesDirectory).
+ append(PATH_SEPARATOR).
+ append(imageId);
- du.setScriptName(sb.toString());
- return du.getAbsoluteLink();
- }
+ du.setScriptName(sb.toString());
+ return want_relative ? du.getRelativeLink() :
du.getAbsoluteLink();
+ }
/**
* Retrieve the URL for the style sheet that is part
@@ -286,18 +343,7 @@
*/
public String getStylecss(RunData data)
{
- DataURI du = new DataURI(data);
- StringBuffer sb = new StringBuffer();
-
- sb.append(resourcesDirectory).
- append(SKINS_DIRECTORY).
- append("/").
- append(getSkin()).
- append("/").
- append(SKIN_CSS_FILE);
-
- du.setScriptName(sb.toString());
- return du.getAbsoluteLink();
+ return getScript(cssFile, data);
}
/**
@@ -308,22 +354,56 @@
*/
public String getStylecss()
{
- ServerData sd = Turbine.getDefaultServerData();
- DataURI du = new DataURI(sd);
+ return getScript(cssFile);
+ }
- StringBuffer sb = new StringBuffer();
+ /**
+ * Retrieve the URL for a given script that is part
+ * of a skin. The script is stored in the
+ * WEBAPP/resources/ui/skins/<SKIN> directory
+ */
+ public String getScript(String filename, RunData data)
+ {
+ DataURI du = new DataURI(data);
- sb.append(resourcesDirectory).
- append(SKINS_DIRECTORY).
- append("/").
- append(getSkin()).
- append("/").
- append(SKIN_CSS_FILE);
+ StringBuffer sb = new StringBuffer();
- du.setScriptName(sb.toString());
- return du.getAbsoluteLink();
- }
+ sb.append(resourcesDirectory).
+ append(PATH_SEPARATOR).
+ append(skinsDirectory).
+ append(PATH_SEPARATOR).
+ append(getSkin()).
+ append(PATH_SEPARATOR).
+ append(filename);
+ du.setScriptName(sb.toString());
+ return want_relative ? du.getRelativeLink() :
du.getAbsoluteLink();
+ }
+
+ /**
+ * Retrieve the URL for a given script that is part
+ * of a skin. The script is stored in the
+ * WEBAPP/resources/ui/skins/<SKIN> directory
+ */
+ public String getScript(String filename)
+ {
+ ServerData sd = Turbine.getDefaultServerData();
+ DataURI du = new DataURI(sd);
+
+ StringBuffer sb = new StringBuffer();
+
+ sb.append(resourcesDirectory).
+ append(PATH_SEPARATOR).
+ append(skinsDirectory).
+ append(PATH_SEPARATOR).
+ append(getSkin()).
+ append(PATH_SEPARATOR).
+ append(filename);
+
+ du.setScriptName(sb.toString());
+ return want_relative ? du.getRelativeLink() :
du.getAbsoluteLink();
+ }
+
/**
* Load the specified skin. In development mode
* this may occur frequently as the skin properties
@@ -335,8 +415,7 @@
try
{
- FileInputStream is = new FileInputStream(
- skinsDirectory + "/" + getSkin() + "/" + SKIN_PROPS_FILE);
+ InputStream is =
TurbineServlet.getResourceAsStream(getScript(SKIN_PROPS_FILE));
skinProperties.load(is);
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]