xinclude - can't use include from within userInputSpec.xml (in installation time)
Piotr Skowronek <
piotr@...>
2008-02-03 22:38:13 GMT
Hi All,
There is a problem with using xinclude feature when used
in userInputSpec.xml. The problem appears in installation-time,
when XIncludeXMLBuilder is unable to find fragments. They would
have to be placed outside of install.jar, to be accessible
in installation-time.
However, xinclude feature works fine for install.xml as fragments
are included/resolved during compilation time. UserInputSpec.xml
is not parsed in compile-time, but simply embedded into resources.
I have worked out a quick solution for it. It can be found
in the attachment. Tell me what do you think about it.
The solution is to modify XIncludeXMLBuilder, to be able
to check resources whether they contain referenced fragment.
Fragments have to be put in resources, and referenced using
/res/ prefix, as I didn't want to hard code this prefix into
XIncludeXMLBuilder.
One more time, tell me what do you think about this patch.
Regards,
Piotr Skowronek
Index: src/lib/net/n3/nanoxml/XIncludeXMLBuilder.java
===================================================================
--- src/lib/net/n3/nanoxml/XIncludeXMLBuilder.java (revision 2031)
+++ src/lib/net/n3/nanoxml/XIncludeXMLBuilder.java (working copy)
@@ -125,11 +125,18 @@
IXMLReader reader = null;
try {
reader = getReader(element);
- } catch (Exception e) { // yes really catch all exceptions
- // ok failed to read from the location for some reason.
- // see if we have a fallback
- reader = handleFallback(element);
- usingFallback = true;
+ } catch (Exception ex1) { // yes really catch all exceptions
+
+ try {
+ // ok failed to read from the location for some reason.
+ // see if we have a file in resource
+ reader = getResourceReader(element);
+ } catch (Exception ex2) {
+ // ok failed to read from the location and resource for some reason.
+ // see if we have a fallback
+ reader = handleFallback(element);
+ usingFallback = true;
+ }
}
String parse = element.getAttribute(PARSE_ATTRIB, "xml");
// process as text if we are not using our fallback and the parse
@@ -340,7 +347,45 @@
}
InputStream is = connection.getInputStream();
+ return getIXMLReader(element, is, url);
+ }
+ /**
+ * Return a reader for the specified resource {@link #INCLUDE_ELEMENT}.
+ * The caller is responsible for closing the reader produced.
+ *
+ * @param element the include element to obtain a reader for
+ * @return a reader for the include element
+ * @throws XMLParseException if a problem occurs parsing the
+ * {@link #INCLUDE_ELEMENT}
+ * @throws IOException if the resource location cannot be read
+ */
+ private IXMLReader getResourceReader(XMLElement element) throws XMLParseException, IOException {
+ String location = element.getAttribute(HREF_ATTRIB);
+
+ URL url = XIncludeXMLBuilder.class.getResource(location);
+ InputStream is = XIncludeXMLBuilder.class.getResourceAsStream(location);
+
+ if (is == null) {
+ throw new IOException("Resource not found: "+location);
+ }
+
+ return getIXMLReader(element, is, url);
+ }
+
+ /**
+ * Return a IXMLReader based on element's attributes, input stream, and url.
+ *
+ * @param element the include element to obtain a reader for
+ * @param is the input stream
+ * @param url the url
+ * @return the IXMLReader
+ * @throws XMLParseException
+ * @throws IOException
+ */
+ private IXMLReader getIXMLReader(XMLElement element, InputStream is, URL url)
+ throws XMLParseException, IOException {
+
InputStreamReader reader = null;
// Only pay attention to the {@link #ENCODING_ATTRIB} if parse='text'
if (element.getAttribute(PARSE_ATTRIB, "xml").equals("text") &&
_______________________________________________
izpack-devel mailing list
izpack-devel@...
https://lists.berlios.de/mailman/listinfo/izpack-devel