[izpack-devel] parsexml attribute for resources
Matthew Fudge
matfud at yahoo.com
Fri Jan 11 14:39:28 CET 2008
Hi all,
Please find included a patch that adds a "parsexml" attribute to <res>
elements. This can be used in conjunction with the "parse" element but
forces the resource to be parsed using an xml parser (rather then only
performing substitutions). The reason for this is that using the nanoXML
parser to parse the file allows xinclude elements in the resource to be
evaluated. If the "parse" attribute and the "parsexml" attribute are
both specified then the resource will be parsed by nanoxml first (all
xincludes will be evaluated) and then have variable substitution
performed on the result.
Cheers
Matfud
Index: src/lib/net/n3/nanoxml/XIncludeXMLBuilder.java
===================================================================
--- src/lib/net/n3/nanoxml/XIncludeXMLBuilder.java (revision 1977)
+++ src/lib/net/n3/nanoxml/XIncludeXMLBuilder.java (working copy)
@@ -31,9 +31,9 @@
*/
public class XIncludeXMLBuilder extends StdXMLBuilder {
/**
- * Namespace to for XInclude (NOTE that this is not used
+ * Namespace for XInclude (NOTE that this is not used
* at the moment). The specification can be found
- * here'>http://www.w3.org/TR/xinclude/">here.
+ * <a href="http://www.w3.org/TR/xinclude/">here</a>.
*/
public static final String INCLUDE_NS = "http://www.w3.org/2001/XInclude";
/**
@@ -82,7 +82,7 @@
public static final String FRAGMENT_NS = "https://izpack.github.io/izpack/fragment";
/**
- * The name of the fragment element is a root node element that can be
+ * The fragment element is a root node element that can be
* used to wrap xml fragments for inclusion. It is removed during the
* include operation. This should be called "fragment" and be in the
* {@link #FRAGMENT_NS} but namespaces are not supported.
Index: src/lib/com/izforge/izpack/compiler/CompilerConfig.java
===================================================================
--- src/lib/com/izforge/izpack/compiler/CompilerConfig.java (revision 1977)
+++ src/lib/com/izforge/izpack/compiler/CompilerConfig.java (working copy)
@@ -26,14 +26,44 @@
package com.izforge.izpack.compiler;
+import com.izforge.izpack.CustomData;
+import com.izforge.izpack.ExecutableFile;
+import com.izforge.izpack.GUIPrefs;
+import com.izforge.izpack.Info;
+import com.izforge.izpack.PackFile;
+import com.izforge.izpack.Panel;
+import com.izforge.izpack.ParsableFile;
+import com.izforge.izpack.UpdateCheck;
+import com.izforge.izpack.compiler.Compiler.CmdlinePackagerListener;
+import com.izforge.izpack.event.CompilerListener;
+import com.izforge.izpack.rules.Condition;
+import com.izforge.izpack.rules.RulesEngine;
+import com.izforge.izpack.util.Debug;
+import com.izforge.izpack.util.OsConstraint;
+import com.izforge.izpack.util.VariableSubstitutor;
+import net.n3.nanoxml.IXMLParser;
+import net.n3.nanoxml.IXMLReader;
+import net.n3.nanoxml.NonValidator;
+import net.n3.nanoxml.StdXMLParser;
+import net.n3.nanoxml.StdXMLReader;
+import net.n3.nanoxml.XMLBuilderFactory;
+import net.n3.nanoxml.XMLElement;
+import net.n3.nanoxml.XMLException;
+import net.n3.nanoxml.XMLParserFactory;
+import net.n3.nanoxml.XMLWriter;
+import org.apache.tools.ant.DirectoryScanner;
+
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.File;
+import java.io.FileInputStream;
import java.io.FileNotFoundException;
-import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
@@ -55,32 +85,6 @@
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
-import org.apache.tools.ant.DirectoryScanner;
-
-import com.izforge.izpack.CustomData;
-import com.izforge.izpack.ExecutableFile;
-import com.izforge.izpack.GUIPrefs;
-import com.izforge.izpack.Info;
-import com.izforge.izpack.PackFile;
-import com.izforge.izpack.Panel;
-import com.izforge.izpack.ParsableFile;
-import com.izforge.izpack.UpdateCheck;
-import com.izforge.izpack.compiler.Compiler.CmdlinePackagerListener;
-import com.izforge.izpack.event.CompilerListener;
-import com.izforge.izpack.rules.Condition;
-import com.izforge.izpack.rules.RulesEngine;
-import com.izforge.izpack.util.Debug;
-import com.izforge.izpack.util.OsConstraint;
-import com.izforge.izpack.util.VariableSubstitutor;
-
-import net.n3.nanoxml.IXMLReader;
-import net.n3.nanoxml.NonValidator;
-import net.n3.nanoxml.StdXMLParser;
-import net.n3.nanoxml.StdXMLReader;
-import net.n3.nanoxml.XMLElement;
-import net.n3.nanoxml.XMLException;
-import net.n3.nanoxml.XMLBuilderFactory;
-
/**
* A parser for the installer xml configuration. This parses a document
* conforming to the installation.dtd and populates a Compiler instance to
@@ -1294,46 +1298,96 @@
XMLElement res = (XMLElement) iter.next();
String id = requireAttribute(res, "id");
String src = requireAttribute(res, "src");
- boolean parse = validateYesNoAttribute(res, "parse", NO);
+ boolean substitute = validateYesNoAttribute(res, "substitute", NO);
+ boolean parsexml = validateYesNoAttribute(res, "parsexml", NO);
// basedir is not prepended if src is already an absolute path
- URL url = findProjectResource(src, "Resource", res);
+ URL originalUrl = findProjectResource(src, "Resource", res);
+ URL url = originalUrl;
- // substitute variable values in the resource if parsed
- if (parse)
+ InputStream is = null;
+ OutputStream os = null;
+ try
{
- if (compiler.getVariables().isEmpty())
+ if (parsexml ||
+ (substitute && !compiler.getVariables().isEmpty()))
{
- parseWarn(res, "No variables defined. " + url.getPath() + " not parsed.");
+ // make the substitutions into a temp file
+ File parsedFile = File.createTempFile("izpp", null);
+ parsedFile.deleteOnExit();
+ FileOutputStream outFile = new FileOutputStream(parsedFile);
+ os = new BufferedOutputStream(outFile);
+ // and specify the substituted file to be added to the
+ // packager
+ url = parsedFile.toURL();
}
- else
+
+ if (parsexml)
+ {
+ IXMLParser parser = XMLParserFactory.createDefaultXMLParser();
+ // this constructor will open the specified url (this is
+ // why the InputStream is not handled in a similar manner
+ // to the OutputStream)
+ IXMLReader reader = new StdXMLReader(null, url.toExternalForm());
+ parser.setReader(reader);
+ XMLElement xml = (XMLElement) parser.parse();
+
+ if (substitute && !compiler.getVariables().isEmpty()) {
+ // if we are also performing substitutions on the file
+ // then create an in-memory copy to pass to the
+ // substitutor
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ XMLWriter xmlWriter = new XMLWriter(baos);
+ xmlWriter.write(xml);
+ is = new ByteArrayInputStream(baos.toByteArray());
+ } else {
+ // otherwise write direct to the temp file
+ XMLWriter xmlWriter = new XMLWriter(os);
+ xmlWriter.write(xml);
+ }
+ }
+
+ // substitute variable values in the resource if parsed
+ if (substitute)
{
- String type = res.getAttribute("type");
- String encoding = res.getAttribute("encoding");
- File parsedFile = null;
-
- try
+ if (compiler.getVariables().isEmpty())
{
- // make the substitutions into a temp file
- InputStream bin = new BufferedInputStream(url.openStream());
+ // reset url to original.
+ url = originalUrl;
+ parseWarn(res, "No variables defined. " + url.getPath() + " not parsed.");
+ }
+ else
+ {
+ String type = res.getAttribute("type");
+ String encoding = res.getAttribute("encoding");
- parsedFile = File.createTempFile("izpp", null);
- parsedFile.deleteOnExit();
- FileOutputStream outFile = new FileOutputStream(parsedFile);
- BufferedOutputStream bout = new BufferedOutputStream(outFile);
-
+ // if the xml parser did not open the url
+ // ('parsexml' was not enabled)
+ if (null == is) {
+ is = new BufferedInputStream(url.openStream());
+ }
VariableSubstitutor vs = new VariableSubstitutor(compiler.getVariables());
- vs.substitute(bin, bout, type, encoding);
- bin.close();
- bout.close();
+ vs.substitute(is, os, type, encoding);
+ }
+ }
- // and specify the substituted file to be added to the
- // packager
- url = parsedFile.toURL();
+ } catch (Exception e)
+ {
+ parseError(res, e.getMessage(), e);
+ } finally {
+ if (null != os) {
+ try {
+ os.close();
+ } catch (IOException e) {
+ // ignore as there is nothing we can realistically do
+ // so lets at least try to close the input stream
}
- catch (IOException x)
- {
- parseError(res, x.getMessage(), x);
+ }
+ if (null != is) {
+ try {
+ is.close();
+ } catch (IOException e) {
+ // ignore as there is nothing we can realistically do
}
}
}
__________________________________________________________
Sent from Yahoo! Mail - a smarter inbox http://uk.mail.yahoo.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://lists.berlios.de/pipermail/izpack-devel/attachments/20080111/164ef174/attachment.html
More information about the izpack-devel
mailing list