[prev in list] [next in list] [prev in thread] [next in thread] 

List:       fop-user
Subject:    RE: <SOLVED - sort of> Re how to embed a font programatically
From:       "Karl Roberts" <Karl.Roberts () macquarie ! com>
Date:       2006-02-26 23:27:39
Message-ID: 82EAB0E55FC30942BCE0E0D902E9F079013B5DA8 () ntsydexm03 ! pc ! internal ! macquarie ! com
[Download RAW message or body]

Quick note,

I changed the AgentHelper.setFOUserAgent(FOUserAgent foUserAgent) method
to:

public void setFOUserAgent(FOUserAgent foUserAgent) {
	synchronized (tlAgent) {
		tlAgent.set(foUserAgent);
	}
}

So that it is set every time a new Fop is constructed, to prevent any
issues with the FOUserAgent (which contains a URIReader, that may
contain a reference to the ServletContext, if using
ServletContextURIResolver) from going stale.

Karl

-----Original Message-----
From: Karl Roberts 
Sent: Monday, 27 February 2006 9:12 AM
To: fop-users@xmlgraphics.apache.org
Subject: RE: <SOLVED - sort of> Re how to embed a font programatically

Hi Jeremias,

Thanks for the update to Trunk, Your code was a bit tidier than mine,
but did the same thing, However I still got the NullPointerException.

The crux of the problem was that The URIReader was only being used to
resolve URI's for Images in the XSLT to transform my XML to XSL-FO. It
was not being used to resolve the URI of the font information passed in
via the fopconfig.xml file.

I traced the null pointer to a line in the "createFont(String path)"
method of FontReader:-

"parser.parse(path);"

The javadoc for "XMLReader.parse(String systemID)" states that:  "If the
system identifier is a URL, it must be fully resolved by the application
before it is passed to the parser."

However because it had not gon through the ServletContextURIResolver the
String passed in was still like
"servet-context:///WEB-INF/fontinfo.xml".

What would have been nice was the ability to inject a FontReader in the
same way as URIReader can be injected into the FOUserAgent before it is
passed to the Fop constructor, but I realised that the "path" should
always be fully resolved and if a custom URIReader is injected, then we
should use the FOUserAgent.resolveURI(String path) method to do it.

I solved the problem (comments welcome) by createing a ThreadLocal
Singleton (to attempt to avoid threading issues when used within a
servlet) called AgentHelper that is initialised in the Fop Constructor:-
VVVVVVVV

/** modified Fop constructor **/
public Fop(String outputFormat, FOUserAgent ua) {
        this.outputFormat = outputFormat;

        foUserAgent = ua;
        if (foUserAgent == null) {
            foUserAgent = new FOUserAgent();
        }
        boolean setOK =
AgentHelper.getInstance().setFOUserAgent(foUserAgent);
        
    }


And edited the "FontReder.createFont(String path)" method as follows, so
that it uses the URIReader on the path first:
VVVVVVVV

/** Modified FontReader.createFont method **/ private void
createFont(String path) throws FOPException {
        XMLReader parser = null;

        try {
            final SAXParserFactory factory =
javax.xml.parsers.SAXParserFactory.newInstance();
            factory.setNamespaceAware(true);
            parser = factory.newSAXParser().getXMLReader();
        } catch (Exception e) {
            throw new FOPException(e);
        }
        if (parser == null) {
            throw new FOPException("Unable to create SAX parser");
        }

        try {
 
parser.setFeature("http://xml.org/sax/features/namespace-prefixes",
                              false);
        } catch (SAXException e) {
            throw new FOPException("You need a SAX parser which supports
SAX version 2",
                                   e);
        }

        parser.setContentHandler(this);

        try {
        	// *** TODO KARL Feature improvment to FOP once in
FOPTrunk, remove from here
        	//parser.parse(path);
        	FOUserAgent
foUserAgent=AgentHelper.getInstance().getFOUserAgent();
        	Source s =foUserAgent.resolveURI(path);
        	if(s!=null && s instanceof StreamSource)
        	{
        		InputSource is;
            	StreamSource ss = (StreamSource) s;	
			is=new InputSource(ss.getInputStream());
			parser.parse(is);
        	}
        	else
        	{
        		parser.parse(path);
        	}      	
        	// *** TODO End KARL Feature        
        } catch (SAXException e) {
            throw new FOPException(e);
        } catch (IOException e) {
            throw new FOPException(e);
        }
    }

And here is the AgentHelper Singleton:-
VVVVVVVV

import org.apache.fop.apps.FOUserAgent;

public class AgentHelper {

	private static AgentHelper singleton = new AgentHelper();	
	private AgentHelper() {
        // Exists only to defeat instantiation.
      }
	private static class ThreadLocalFOUserAgent extends ThreadLocal
{}
	private ThreadLocalFOUserAgent tlAgent = new
ThreadLocalFOUserAgent();

	public static AgentHelper getInstance()
	{
		return singleton; //no syncronization needed as
statically initialised
	}

	public FOUserAgent getFOUserAgent() {
		synchronized (tlAgent) {
			return (FOUserAgent) tlAgent.get();
		}
	}

	/**
	 * Only allows foUserAgent to be set once, I would have used a
final,  
	 * except it is placed in the ThreadLocal object.
	 * 
	 * @param foUserAgent
	 */
	public boolean setFOUserAgent(FOUserAgent foUserAgent) {
		boolean ret = false;
		synchronized (tlAgent) {
			if (tlAgent.get() == null) {
				tlAgent.set(foUserAgent);
				ret = true;
			}
		}
		return ret;
	}
}

Any thoughts on how this could be improoved and added to the Trunk so
that I don't have to maintain a separate branch of FOP?

Cheers

Karl Roberts 

-----Original Message-----
From: Jeremias Maerki [mailto:dev@jeremias-maerki.ch]
Sent: Thursday, 23 February 2006 9:56 PM
To: fop-users@xmlgraphics.apache.org
Subject: Re: Re how to embed a font programatically

Have you seen my other post? I've implemented exactly what I proposed to
you and put it in FOP Trunk:
http://svn.apache.org/viewcvs?rev=379810&view=rev

More comments inline...

On 23.02.2006 07:02:33 Karl Roberts wrote:
> Hi I built a  URIResolver that reads URL's like:
> 
> servlet-context:/WEB-INF/metric.xml
> 
> And it works fine, however when it encounters a URL that doesn't match

> the servlet-context scheme I return null.

That's correct IMO. It's what I did.

> However I then get an NullPointerException Caused by: 
> javax.xml.transform.TransformerException:
> java.lang.NullPointerException
> 	at
> com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform
> (T
> ransformerImpl.java:647)
> 	at
> com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform
> (T
> ransformerImpl.java:279)
> 	....

Hmm, I don't get these with my implementation. TransformerException is
not the real exception. Look further down in the stack trace where you
should find the right place where the exception occurred. The above
doesn't really help. Setting an exception breakpoint for
NullPointerException will also be useful.

> I do return null from my URIResolver in the cases where the scheme 
> doesn't match the one I'm after, and FOURIResolver kicks in, but it 
> never seems to come back to my URIResolver.
> Should I instead delegate to FOURIResolver rather than return null?

No, I don't think so. There must be something else that's wrong. Can you
try my implementation instead?
http://svn.apache.org/repos/asf/xmlgraphics/fop/trunk/src/java/org/apach
e/fop/servlet/ServletContextURIResolver.java

<snip/>

Jeremias Maerki


---------------------------------------------------------------------
To unsubscribe, e-mail: fop-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: fop-users-help@xmlgraphics.apache.org



NOTICE
This e-mail and any attachments are confidential and may contain
copyright material of Macquarie Bank or third parties. If you are not
the intended recipient of this email you should not read, print,
re-transmit, store or act in reliance on this e-mail or any attachments,
and should destroy all copies of them. Macquarie Bank does not guarantee
the integrity of any emails or any attached files. The views or opinions
expressed are the author's own and may not reflect the views or opinions
of Macquarie Bank.


---------------------------------------------------------------------
To unsubscribe, e-mail: fop-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: fop-users-help@xmlgraphics.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: fop-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: fop-users-help@xmlgraphics.apache.org


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic