Users Guide
Users Guide
Table of Contents
What it is and How it Works ......................................................................................... 1 What it is ............................................................................................................ 1 What it does ........................................................................................................ 2 What you can do with it ........................................................................................ 2 What it does not do .............................................................................................. 2 License and Dependencies ..................................................................................... 3 Requirements for Running and Using Flying Saucer ................................................... 3 Setting your Classpath ........................................................................................... 3 Using Flying Saucer ..................................................................................................... 4 Basic Usage .........................................................................................................4 Sample Applications ............................................................................................. 6 Configuration ............................................................................................................... 6 The Flying Saucer Configuration File ...................................................................... 6 Logging .............................................................................................................. 7 What it is and How it Works What it is What it does What you can do with it What it does not do License and Dependencies Requirements for Running and Using Flying Saucer Setting your Classpath Using Flying Saucer Basic Usage Sample Applications Configuration The Flying Saucer Configuration File Logging
there are many good books available, and tutorials on the web. Check out the W3Schools CSS Tutorial [http://www.w3schools.com/css/default.asp] for a starting point.
What it does
Flying Saucer takes XML and CSS as input (where the CSS might be embedded in the document, or linked from it) and generates rendered content. Our current major output formats are in a GUI interface (a panel) and in PDF; we can also render to image formats, e.g. render the page and save as an image. If rendering to a GUI, hyperlinks work so you can navigate between pages. As with HTML, you can also render forms, capture output, and create applications that way. In a GUI, Flying Saucer provides a read-only view of the output; we cannot replace Swing's JEditorPane or JTextPane. However, for static content, or content created by you, Flying Saucer can be used for help documents, tutorials, books, splash screens, presentations, and much more. We can also render to PDF. For PDF, the layout rules come from the CSS. The difference is the rendered output uses the iText [http://www.lowagie.com/iText/] library to generate PDF. Last, we have utility classes to render output to an image file. With this, you could use XML and CSS to layout printable contentfor example, a flyer, a poster, business cards, etc.and save them as images you can print out or email. It's also a nice way to create thumbnail or reduced-size images of pages.
the best. But without a bunch of work, you won't be able to use Flying Saucer for a real web browser component. However, note that's not a technical limitation, just a lack of time and resources. HTML plugins, like applets, Flash programs, etc. are not supported. Scripting (e.g. JavaScript) is not supported. We ignore script tags. Dynamic changes to the content requires a re-layout (quick, but noticeable), that is, you can't dynamically change the DOM and see results live. Most DOM callbacks used in JavaScript are not yet implemented (onLoad, onClick, onBlur, etc.).
These limitations all have a pragmatic origin. Josh Marinacci, the founder of and original lead developer for the Flying Saucer project, realized that writing a fully capable HTML browser component (like Firefox's Gecko engine) could take many man-years of development. But if one focused on well-formed XHTML only, and stuck to the CSS spec, you could cover most of the useful stuff you want to do with a rendering engine, and do it in a reasonable amount of time. So it's not impossible to add scripting, DHTML, plugins to Flying Saucer, we've just deferred this until someone has the time and energy to get it to workthat way, we stay focused on the goal, which is pure CSS 2.1 support for well-formed XML. Of course, you can help fix any of these things. Contributors welcome!
iText [http://www.lowagie.com/iText/] is not necessary at runtime if you are not generating PDFs, but is necessary for the build to satisfy compile-time dependencies. In theory, you could substitute another CSS parser that is SAC [http://www.w3.org/Style/CSS/SAC] (Simple API for CSS) compliant, but we've had little success with this due to variations in implementation.
Minium toolkit, add minium.jar. That is all you need for your own programs. You also need an XML parser to be in your classpath, but this already included in recent versions of the JRE. To run the browser or use any of it's support classes you will need the browser.jar file. To summarize, the easiest CLASSPATH to set is: core-renderer.jar (required) cssparser-0-9-4-fs.jar (required) itext-paulo-155.jar minium.jar
To make life easier for our end-users, we have created a special Java package, org.xhtmlrenderer.simple, which contains classes you can use to get up and running without any hassle. In addition, in a separate branch of our source tree, we created some sample single-class Java programs to show different uses of Flying Saucer. To understand where to start, you have to look at how Flying Saucer works. The input is a document, identifed by a Uniform Resource Identifier (URL) or Uniform Resource Locator (URI). That document must be well-formed XML. The document is loaded and elements are matched against the CSS provided for the document. For XHTML, we have specifications for how CSS is specifiedas linked stylesheets, as inline styles, and as style attributes. For XML, we support linked stylesheets via the xml-stylesheet processing instruction. Once we've matched CSS, we run through a layout phase, where we calculate the size and position, as well as display attributes, of all visible elements. The layout is then used to render to some output sink. The standard output sink is a Swing JPanel subclass we call org.xhtmlrenderer.swing.BasicPanel, or its extended (and more powerful) child, XHTMLPanel.
XHTMLPanel panel = new XHTMLPanel(); // // Set the XHTML document to render. We use the simplest form // of the API call, which uses a File reference. There // are a variety of overloads for setDocument(). // panel.setDocument(new File(fileName)); // // Put our panel in a scrolling pane. You can use // a regular JScrollPane here, or our FSScrollPane. // FSScrollPane is already set up to move the correct // amount when scrolling 1 line or 1 page // FSScrollPane scroll = new FSScrollPane(panel); JFrame frame = new JFrame("Flying Saucer Single Page Demo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(scroll); frame.pack(); frame.setSize(1024, 768); frame.setVisible(true); } The basic process is: create a BasicPanel or XHTMLPanel instance add it to a Swing JScrollPane or an FSScrollPane (unless your pages will fit without scrolling add the panel to a containera JFrame, another panel, etc. call setDocument() to load and render your document
That's it! You can now display XHTML and CSS in your Swing applications. What about AWT or alternate GUI toolkits for Java? Our basic rendering routine writes to a "renderer". Right now we support the concept of rendering to an output device. We have two output device implementations: one for Java2D (essentially a canvas, or Graphics2D instance) and the other for PDF (using iText). When we render to a Swing panel, we are still painting on a Java2D canvas, so in principle, you should be able to port this rendering to other 2D output surfaces. If you do, we'd like to hear from you! Just note there is no explicit technical limitation that forces you to use Swingit's just easy, and easy to make it look good.
JMenuItem about = new JMenuItem("About..."); about.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { AboutBox ab = new AboutBox("About Flying Saucer", "demos/about/index.xhtml"); ab.setVisible(true); } });
Sample Applications
The Browser The About Box Eeze
Configuration
The Flying Saucer Configuration File
The renderer works with a simple, java.util.Properties -based configuration systemno XML! Our org.xhtmlrenderer.util.Confuration class loads properties on first access and makes them available at runtime. When you are using the renderer, Configuration needs to know where to find the properties file. If you are running from the renderer JAR file, our default properties will be read from there. If you have unpacked, or re-packed, the JAR, the location of the file is currently hard-coded as /resources/conf/ xhtmlrenderer.conf. This path must be on the CLASSPATH as it is loaded as a system resource using a ClassLoader. You need to add the parent directory for /resources to your classpath, or include /resources in your JAR with no parent directory. You can change the default properties for the application right in the .conf file. However, this is not a good idea, as you will have to merge your changes back on new releases. Plus, it will make reporting bugs more complicated. Instead, you can use one of two override mechanisms for changing the value of individual properties.
$user.home/.flyingsaucer/local.xhtmlrenderer.conf The user.home variable is a system property. If you call the System.getProperty("user.home") from within any Java program on your machine, you will find out where this is. The location is usually c:\Documents And Settings\{username} and under the /usr directory on UNIX systems. Try that method call to see where it is on your machine.
Logging
The renderer uses the java.util.logging [http://java.sun.com/j2se/1.4.2/docs/api/java/util/logging/packagesummary.html] package for logging information and exceptions at runtime. Logging behavior (output) is 7
controlled via the main configuration file. The defaults may be overridden just like any other configuration properties. Please review the java.util.logging [http://java.sun.com/j2se/1.4.2/docs/api/java/util/logging/packagesummary.html] package docs before proceeding. We log to a set of hierarchies. The internal codeeverything between a request to load a page and the page renderingis logged to a subhierarchy of "plumbing", e.g. plumbing.load. Our convention is that WARNING and SEVERE levels are very important and should always be logged. INFO messages are useful and but can be excluded if you want a quiet ride. Anything below INFO (FINE, FINER, FINEST) is generally only interesting for core renderer developers. We don't guarrantee that anything below INFO will be useful, correct, practical or informative. You can usually leave log levels at INFO for most purposes. If you are modifying the renderer core code and want to add log messages, we recommend you always use the org.xhtmlrenderer.XRLog class. Using this class ensures that our log configuration is read properly before we write anything out to the log system. The class is pretty self-explanatory, and all logging methods in it are static. If for some reason you need to use the java.util.logging.Logger class directly, please use XRLog.getLogger() to retrieve the instance to use.