  | |  | XInclude... is it the way to make it working ? | XInclude... is it the way to make it working ? 2003-10-07 - By Christophe Grosjean
Hi,
I tried the XInclude feature. I had no problem in setting the XIncludeConfiguration and the parsing includes well
relative and absolute URI.
But...
I want to specify files in may classpath instead of URI like :
<xi:include href="/com/mypacakge/file.xml"/>
instead of
<xi:include href="c://src/com/mypacakge/file.xml"/>
Setting an EntityResolver on the parser does'nt seem to work. The parser never asks the EntityResolver
to do its job.
Here follows the code of the EntityResolverWrapper that wraps the user-defined EntityResolver in Xerces code.
It seems that the pubId and sysId are always null, so the EntityResolver job is skipped. In my case, it is
The XMLResourceIdentifier. getLiteralSystemId() that contains the href String.
Is it a bug in the EntityResolverWrapper ? Do I sth wrong ?
I found a workaround by extending the XIncludeParserConfiguration and setting an custom XMLEntityResolver.
Christophe.
public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier)
throws XNIException, IOException {
// When both pubId and sysId are null, the user's entity resolver
// can do nothing about it. We'd better not bother calling it.
// This happens when the resourceIdentifier is a GrammarDescription,
// which describes a schema grammar of some namespace, but without
// any schema location hint. -Sg
String pubId = resourceIdentifier.getPublicId();
String sysId = resourceIdentifier.getExpandedSystemId();
if (pubId == null && sysId == null)
return null;
// resolve entity using SAX entity resolver
if (fEntityResolver != null && resourceIdentifier != null) {
try {
InputSource inputSource = fEntityResolver.resolveEntity(pubId, sysId);
if (inputSource != null) {
String publicId = inputSource.getPublicId();
String systemId = inputSource.getSystemId();
String baseSystemId = resourceIdentifier.getBaseSystemId();
InputStream byteStream = inputSource.getByteStream();
Reader charStream = inputSource.getCharacterStream();
String encoding = inputSource.getEncoding();
XMLInputSource xmlInputSource =
new XMLInputSource(publicId, systemId, baseSystemId);
xmlInputSource.setByteStream(byteStream);
xmlInputSource.setCharacterStream(charStream);
xmlInputSource.setEncoding(encoding);
return xmlInputSource;
}
}
// error resolving entity
catch (SAXException e) {
Exception ex = e.getException();
if (ex == null) {
ex = e;
}
throw new XNIException(ex);
}
}
// unable to resolve entity
return null;
}
<html>
<head> <meta http-equiv=Content-Type content="text/html; charset=us-ascii"> <meta name=Generator content="Microsoft Word 10 (filtered)">
<style> <!-- /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {margin:0cm; margin-bottom:.0001pt; font-size:12.0pt; font-family:"Times New Roman";} a:link, span.MsoHyperlink {color:blue; text-decoration:underline;} a:visited, span.MsoHyperlinkFollowed {color:purple; text-decoration:underline;} span.EmailStyle17 {font-family:Arial; color:windowtext;} @(protected) Section1 {size:595.3pt 841.9pt; margin:70.85pt 70.85pt 70.85pt 70.85pt;} div.Section1 {page:Section1;} --> </style>
</head>
<body lang=FR-BE link=blue vlink=purple>
<div class=Section1>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt; font-family:Arial'>Hi,</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt; font-family:Arial'> </span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'>I tried the XInclude feature. I had no problem in setting the XIncludeConfiguration and the parsing includes well</span></font>< /p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'>relative and absolute URI.</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'> </span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'>But…</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'> </span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'>I want to specify files in may classpath instead of URI like :</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'> </span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'><xi:include href=”/com/mypacakge/file.xml& #8221;/></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'> </span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'>instead of </span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'> </span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'><xi:include href=”c://src/com/mypacakge/file .xml”/></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'> </span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'>Setting an EntityResolver on the parser does’nt seem to work. The parser never asks the EntityResolver</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'>to do its job.</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'> </span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'>Here follows the code of the EntityResolverWrapper that wraps the user-defined EntityResolver in Xerces code.</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'>It seems that the pubId and sysId are always null, so the EntityResolver job is skipped. In my case, it is</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'>The XMLResourceIdentifier.</span></font><font size=2 color=blue face="Courier New"><span lang=EN-GB style='font-size:10.0pt; font-family:"Courier New";color:blue;background:white'> getLiteralSystemId< /span></font><font size=2 color=blue face="Courier New"><span lang=EN-GB style='font-size:10.0pt; font-family:"Courier New";color:blue'>() </span></font><font size=2 face=Arial> <span lang=EN-GB style='font-size:10.0pt;font-family:Arial'>that contains the href String.</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'> </span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'>Is it a bug in the EntityResolverWrapper ? Do I sth wrong ?</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'> </span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'>I found a workaround by extending the XIncludeParserConfiguration and setting an custom XMLEntityResolver.</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'> </span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'>Christophe.</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'> </span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'> </span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'>public XMLInputSource resolveEntity (XMLResourceIdentifier resourceIdentifier)</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'> throws XNIException, IOException {</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'> </span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'> // When both pubId and sysId are null, the user's entity resolver</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'> // can do nothing about it. We'd better not bother calling it.</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'> // This happens when the resourceIdentifier is a GrammarDescription,</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'> // which describes a schema grammar of some namespace, but without</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'> // any schema location hint. -Sg</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'> String pubId = resourceIdentifier.getPublicId();</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'> String sysId = resourceIdentifier.getExpandedSystemId();</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'> if (pubId == null && sysId == null)</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'>   ; return null;</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'> </span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'> // resolve entity using SAX entity resolver</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'> if (fEntityResolver != null && resourceIdentifier != null) {</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'>   ; try {</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'>   ; InputSource inputSource = fEntityResolver.resolveEntity(pubId, sysId);</span>< /font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'>   ; if (inputSource != null) {</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'>   ; String publicId = inputSource.getPublicId();</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'>   ; String systemId = inputSource.getSystemId();</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'>   ; String baseSystemId = resourceIdentifier.getBaseSystemId();</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'>   ; InputStream byteStream = inputSource.getByteStream();</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'>   ; Reader charStream = inputSource.getCharacterStream();</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'>   ; String encoding = inputSource.getEncoding();</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'>   ; XMLInputSource xmlInputSource =</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'>   ; new XMLInputSource(publicId, systemId, baseSystemId);</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'>   ; xmlInputSource.setByteStream(byteStream);</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'>   ; xmlInputSource.setCharacterStream(charStream);</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'>   ; xmlInputSource.setEncoding(encoding);</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'>   ; return xmlInputSource;</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'>   ; }</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'> }</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'> </span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'>   ; // error resolving entity</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'>   ; catch (SAXException e) {</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'>   ; </span></font><font size=2 face=Arial><span style='font-size:10.0pt;font-family: Arial'>Exception ex = e.getException();</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt; font-family:Arial'> if (ex == null) {</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt; font-family:Arial'> ex = e;</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt; font-family:Arial'> }</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt; font-family:Arial'> throw new XNIException(ex);</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt; font-family:Arial'> </span></font><font size=2 face=Arial><span lang=EN-GB style='font-size:10.0pt; font-family:Arial'>}</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'> }</span>< /font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'> </span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'> // unable to resolve entity</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'> return null;</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'> </span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span lang=EN-GB style='font-size: 10.0pt;font-family:Arial'> }</span></font></p>
</div>
</body>
</html>
|
|
|