KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejen > ext > XMLInclude


1 //
2
// Ejen (code generation system)
3
// Copyright (C) 2001, 2002 François Wolff (ejen@noos.fr).
4
//
5
// This file is part of Ejen.
6
//
7
// Ejen is free software; you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License as published by
9
// the Free Software Foundation; either version 2 of the License, or
10
// (at your option) any later version.
11
//
12
// Ejen is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU General Public License for more details.
16
//
17
// You should have received a copy of the GNU General Public License
18
// along with Ejen; if not, write to the Free Software
19
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
//
21
package org.ejen.ext;
22
23 import org.ejen.util.DOMUtil;
24 import org.ejen.util.XSLUtil;
25 import org.w3c.dom.Node JavaDoc;
26 import org.apache.xalan.extensions.ExpressionContext;
27 import org.apache.xml.utils.WrappedRuntimeException;
28
29 /**
30  * XML file include utility (static methods).
31  * <p>
32  * <table class="usage">
33  * <tr><th class="usage">Usage (XSL stylesheet)</th></tr>
34  * <tr><td class="usage"><pre>
35  *
36  * &lt;?xml version="1.0" encoding="iso-8859-1"?&gt;
37  *
38  * &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
39  * ...
40  * <b>xmlns:xin="org.ejen.ext.XMLInclude"</b>
41  * version="1.0"&gt;
42  *
43  * &lt;xsl:output method="xml" encoding="iso-8859-1"/&gt;
44  *
45  * &lt;xsl:template match="ejen"&gt;
46  *
47  * &lt;xsl:copy-of select="xin:{@link #load(ExpressionContext,String) load}('{$name}.xml')"/&gt;
48  *
49  * &lt;/xsl:template&gt;
50  *
51  * &lt;/xsl:stylesheet&gt;
52  * </pre></td></tr></table>
53  * @author F. Wolff
54  * @version 1.0
55  */

56 public class XMLInclude {
57
58     /**
59      * Protected constructor (prevents instanciation).
60      */

61     protected XMLInclude() {}
62
63     /**
64      * Returns the root <code>Node</code> of an XML file (with all descendants).
65      * <p>
66      * <table class="usage"><tr><td class="usage"><pre>
67      *
68      * &lt;xsl:copy-of select="xin:load('{$name}.xml')"/&gt;
69      * </pre></td></tr></table>
70      * <p>
71      * <dd><dl><dt><b>XSLT parameters:</b>
72      * <dd><b>[Mandatory/AVT]</b> name of the XML file to be loaded.
73      * </dl></dd>
74      * <p>
75      * @param context automatically passed by the xalan extension mechanism.
76      * @param fileName name of the XML file.
77      * @return the root <code>Node</code> of the XML file.
78      * @throws org.apache.xml.utils.WrappedRuntimeException errors (file not found...).
79      */

80     public static Node JavaDoc load(ExpressionContext context, String JavaDoc fileName) {
81         return load(XSLUtil.evaluate(context, fileName));
82     }
83     
84     /**
85      * Returns the root <code>Node</code> of an XML file (with all descendants).
86      * <p>
87      * @param fileName name of the XML file.
88      * @return the root <code>Node</code> of the XML file.
89      * @throws org.apache.xml.utils.WrappedRuntimeException errors (file not found...).
90      */

91     protected static Node JavaDoc load(String JavaDoc fileName) {
92         try {
93             return DOMUtil.parseXMLFile(fileName).getDocumentElement();
94         } catch (WrappedRuntimeException e) {
95             throw e;
96         } catch (Exception JavaDoc e) {
97             throw new WrappedRuntimeException(e);
98         }
99     }
100 }
101
Popular Tags