You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cocoon.apache.org by Hussayn Dabbous <da...@saxess.com> on 2006/09/25 08:37:52 UTC
[PATCH] enable EntityResolver in XSL-Stylesheets ...
Hy;
This is a reminder for an email i sent a few weeks ago.
I'd like to know in first place, whether the patch is
relevant, or not ;-)
thanks in advance
Hussayn
The original message was:
Has anybody already checked out my patch concerning
entity resolving in XSL style sheets ?
Is there a chance to get this patch eventually into the
cocoon-core ? Maybe some enhancements are needed before this
can be done?
Some feedback would be great.
thanks, hussayn
Hussayn dabbous wrote:
> Hi;
>
> I tried to use external entities like ü etc. in xsl stylesheets.
> For this purpose in first place i added a DOCTYPE definition on top of
> my stylesheet, i.e.:
>
>
> <?xml version="1.0"?>
> <!DOCTYPE mydoc [
> <!ENTITY % ISOlat1 PUBLIC "ISO 8879:1986//ENTITIES Added Latin
> 1//EN//XML" "ISOlat1.pen"> %ISOlat1;
> ]><xsl:stylesheet version="1.0">
> ...
>
> I assumed, the default entity resolver would resolve the ISOlat1.pen,
> but it did not!
> I tracked this down and found, that entity resolution only works
> correctly when i
> set the SYSTEM identifier to the absolute location of the external
> entity file. I did
> not like this "hack", hence i tracked the problem further down and
> eventually found
> one location in TraxProcessor.java which apparently needs a modification
> in order to inject
> the correct EntityResolver to work as expected.
>
> I finally created a patch, which adds Entity resolving within XSLT
> processing wherever
> a document(), import or include is performed. i did not check whether
> this patch works
> recursively (follows an import within an imported xsl), but it looks
> like it would do ...
>
> The patch has been created against cocoon/branches/BRANCH_2_1_X
> It works for me. Hopefully it is usefull for others and does not break
> code at other
> places. If anyone would like to review the patch and give some feedback,
> That would be great ;-)
>
> best regards,
> Hussayn
>
>
> ------------------------------------------------------------------------
>
> Index: src/java/org/apache/cocoon/components/xslt/TraxProcessor.java
> ===================================================================
> --- src/java/org/apache/cocoon/components/xslt/TraxProcessor.java (revision 437809)
> +++ src/java/org/apache/cocoon/components/xslt/TraxProcessor.java (working copy)
> @@ -30,6 +30,7 @@
> import javax.xml.transform.TransformerException;
> import javax.xml.transform.TransformerFactory;
> import javax.xml.transform.URIResolver;
> +import javax.xml.transform.sax.SAXSource;
> import javax.xml.transform.sax.SAXTransformerFactory;
> import javax.xml.transform.sax.TemplatesHandler;
> import javax.xml.transform.sax.TransformerHandler;
> @@ -52,14 +53,17 @@
> import org.apache.excalibur.source.SourceValidity;
> import org.apache.excalibur.source.impl.validity.AggregatedValidity;
> import org.apache.excalibur.store.Store;
> +import org.apache.excalibur.xml.EntityResolver;
> import org.apache.excalibur.xml.sax.XMLizable;
> import org.apache.excalibur.xml.xslt.XSLTProcessor;
> import org.apache.excalibur.xml.xslt.XSLTProcessorException;
> import org.apache.excalibur.xmlizer.XMLizer;
> +import org.apache.xml.utils.XMLReaderManager;
> import org.xml.sax.ContentHandler;
> import org.xml.sax.InputSource;
> import org.xml.sax.SAXException;
> import org.xml.sax.XMLFilter;
> +import org.xml.sax.XMLReader;
>
> /**
> * Adaptation of Excalibur's XSLTProcessor implementation to allow for better
> @@ -91,6 +95,9 @@
>
> /** Resolver used to resolve XSLT document() calls, imports and includes */
> protected SourceResolver m_resolver;
> +
> + /** EntityResolver used to resolve Entity references within document(), import and include targets */
> + protected EntityResolver m_entityResolver;
>
> /** Check included stylesheets */
> protected boolean m_checkIncludes;
> @@ -118,6 +125,7 @@
> if (m_manager.hasService(Store.TRANSIENT_STORE)) {
> m_store = (Store) m_manager.lookup(Store.TRANSIENT_STORE);
> }
> + m_entityResolver = (EntityResolver) m_manager.lookup(EntityResolver.ROLE);
> }
>
> /**
> @@ -560,7 +568,7 @@
> }
> }
>
> - InputSource is = getInputSource(xslSource);
> + SAXSource saxSource = getInputSource(xslSource);
>
> if (getLogger().isDebugEnabled()) {
> getLogger().debug("xslSource = " + xslSource + ", system id = " + xslSource.getURI());
> @@ -580,7 +588,7 @@
> }
> }
>
> - return new StreamSource(is.getByteStream(), is.getSystemId());
> + return saxSource;
> } catch (SourceException e) {
> if (getLogger().isDebugEnabled()) {
> getLogger().debug("Failed to resolve " + href + "(base = " + base + "), return null", e);
> @@ -614,10 +622,22 @@
> * @throws IOException
> * if I/O error occured.
> */
> - protected InputSource getInputSource(final Source source) throws IOException, SourceException {
> - final InputSource newObject = new InputSource(source.getInputStream());
> + protected SAXSource getInputSource(final Source source) throws IOException, SourceException {
> + InputSource newObject = new InputSource(source.getInputStream());
> newObject.setSystemId(source.getURI());
> - return newObject;
> + SAXSource saxSource = new SAXSource(newObject);
> + try{
> + if(m_entityResolver != null){
> + XMLReader reader = XMLReaderManager.getInstance().getXMLReader();
> + reader.setEntityResolver(m_entityResolver);
> + saxSource.setXMLReader(reader);
> + }
> + }
> + catch( Exception e) {
> + getLogger().error(
> + "Error while attaching the default EntityResolver to an XMLReader. Using no EntityResolver",e);
> + }
> + return saxSource;
> }
>
> /**
Re: [PATCH] enable EntityResolver in XSL-Stylesheets ...
Posted by Jean-Baptiste Quenot <jb...@apache.org>.
* Hussayn Dabbous:
> This is a reminder for an email i sent a few weeks ago.
> I'd like to know in first place, whether the patch is
> relevant, or not ;-)
Can you please file an issue at
https://issues.apache.org/jira/secure/Dashboard.jspa
Thanks in advance,
--
Jean-Baptiste Quenot
aka John Banana Qwerty
http://caraldi.com/jbq/