📄 fopadapter.java
字号:
package de.spieleck.helper;
import java.io.OutputStream;
import java.util.Set;
import javax.xml.transform.Result;
import javax.xml.transform.sax.SAXResult;
import org.apache.fop.apps.Driver;
import gnu.trove.TObjectIntHashMap;
import gnu.trove.decorator.TObjectIntHashMapDecorator;
/**
* Utility class to provide JAXP Results which route XML into Apache-FOP.
* @author fsn
*/
public class FopAdapter
{
/** A map of mime types which can be handled by Apache-FOP */
final private static TObjectIntHashMap mime4fop;
private static boolean isFopAvailable;
static
{
mime4fop = new TObjectIntHashMap();
// Try to check weather fop is available in classpath.
try
{
new Driver();
isFopAvailable = true;
}
catch ( Throwable e )
{
isFopAvailable = false;
}
if ( isFopAvailable )
{
mime4fop.put("application/pdf", Driver.RENDER_PDF);
mime4fop.put("application/ps", Driver.RENDER_PS);
mime4fop.put("application/mif", Driver.RENDER_MIF); // mime ok?
mime4fop.put("application/svg", Driver.RENDER_SVG); // mime ok?
mime4fop.put("experimental/pcl", Driver.RENDER_PCL); // is there a mime?
mime4fop.put("experimental/txt", Driver.RENDER_TXT); // is there a mime?
mime4fop.put("experimental/xml", Driver.RENDER_XML); // is there a mime?
}
}
/** Do not construct me! */
private FopAdapter() { }
/** Check the availabilty of fop at load time. */
public static boolean isFopAvailable()
{
return isFopAvailable;
}
/** Check weather Fop will handle this mime type. */
public static boolean isFopEnabled(String mime)
{
return isFopAvailable && mime != null && mime4fop.contains(mime);
}
/** Get a Set of available Fop mime types. */
public static Set getFopMimes()
{
return new TObjectIntHashMapDecorator(mime4fop).keySet();
}
/**
* Return a Result when Fop can handle that particular mimetype
*/
public static Result getFopResult(String mime, OutputStream out)
{
if ( !isFopEnabled(mime) )
return null;
Driver fop = new Driver();
fop.setRenderer(mime4fop.get(mime));
fop.addElementMapping("org.apache.fop.fo.StandardElementMapping");
fop.addElementMapping("org.apache.fop.svg.SVGElementMapping");
fop.setOutputStream(out);
return new SAXResult(fop.getContentHandler());
}
}
//
// Jacson - Text Filtering with Java.
// Copyright (C) 2002 Frank S. Nestel (nestefan -at- users.sourceforge.net)
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -