package templ;
import bum.swing.LocalProcessing;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Rectangle;
import java.awt.Window;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.print.PrinterJob;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import javax.swing.JDialog;
import javax.swing.JProgressBar;
import javax.swing.JSlider;
import javax.swing.JSpinner;
import javax.swing.SwingWorker;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
import org.apache.fop.render.awt.AWTRenderer;
import org.apache.fop.render.awt.viewer.PreviewPanel;
import org.apache.fop.render.print.PrintRenderer;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.tools.generic.ConversionTool;
import org.apache.velocity.tools.generic.DateTool;
import org.apache.velocity.tools.generic.MathTool;
import org.apache.velocity.tools.generic.NumberTool;
public class Template {
//private String xsl = "xhtml-to-xslfo.xsl";
//private String xsl = "xslt-proba.xsl";
private String xsl = "xhtml-to-xslfo.xsl";
private VelocityContext context = new VelocityContext();
public static MimeConstants Type;
private org.apache.velocity.Template tmp;
private Template(File xsl, File xml, String xmlText) throws Exception {
if(xsl != null)
this.xsl = xsl.getAbsolutePath();
if(xml != null)
xml = new File(xml.getAbsolutePath());
else
xml = new File(createXML(xmlText).getAbsolutePath());
Velocity.setProperty("file.resource.loader.path", xml.getParent());
Velocity.setProperty("file.resource.loader.cache", false);
Velocity.init();
tmp = Velocity.getTemplate(xml.getName(),"UTF-8");
context.put("convert", new ConversionTool());
context.put("dateTool", new DateTool());
context.put("mathTool", new MathTool());
context.put("numberTool", new NumberTool());
context.put("Math", new myMath());
}
public Template(File xsl, File xml) throws Exception {
this(xsl,xml,null);
}
public Template(File xml) throws Exception {
this(null,xml);
}
public Template(String xmlText) throws Exception {
this(null,null,xmlText);
}
public Template(File xsl, String xmlText) throws Exception {
this(xsl,null,xmlText);
}
private StreamSource createXSLStream() throws FileNotFoundException {
InputStream in = Template.class.getResourceAsStream(xsl);
if(in == null)
in = new FileInputStream(xsl);
StreamSource stream = new StreamSource(in);
return stream;
}
private File createXML(String xmlText) throws IOException {
File temp_xml = new File("temp_xml.xml");
temp_xml.deleteOnExit();
FileOutputStream writer = new FileOutputStream(temp_xml);
writer.write(xmlText.getBytes("UTF-8"));
writer.flush();
writer.close();
return temp_xml;
}
public void put(String paramName, Object param){
context.put(paramName, param);
}
private StreamSource createVelocityStream() throws Exception {
StringWriter writer = new StringWriter();
tmp.merge(context, writer);
writer.flush();
writer.close();
return new StreamSource(new StringReader(writer.toString()));
}
public void previewDocument() throws Exception {
previewDocument(null);
}
public synchronized void printDocument() throws Exception {
printDocument(null);
}
public synchronized File getDocument(String fileName, String type) throws Exception {
return getDocument(fileName, type, null);
}
public void previewDocument(LocalProcessing processing) throws Exception {
if(processing != null)
processing.setMinMax(0, 100);
Transformer transformer = TransformerFactory.newInstance().newTemplates(createXSLStream()).newTransformer();
if(processing != null)
processing.setValue(33);
FopFactory fopFactory = FopFactory.newInstance();
Fop fop = fopFactory.newFop(MimeConstants.MIME_FOP_AWT_PREVIEW);
if(processing != null)
processing.setValue(66);
transformer.transform(createVelocityStream(), new SAXResult(fop.getDefaultHandler()));
if(processing != null)
processing.setValue(100);
}
public PreviewPanel getPreviewPanel(JProgressBar progress) throws Exception {
Source source = createXSLStream();
if(progress != null)
progress.setValue(20);
Transformer transformer = TransformerFactory.newInstance().newTemplates(source).newTransformer();
FopFactory fopFactory = FopFactory.newInstance();
FOUserAgent agent = fopFactory.newFOUserAgent();
AWTRenderer renderer = new AWTRenderer();
agent.setRendererOverride(renderer);
renderer.setPreviewDialogDisplayed(false);
renderer.setUserAgent(agent);
renderer.clearViewportList();
Fop fop = fopFactory.newFop(agent);
if(progress != null)
progress.setValue(50);
transformer.transform(createVelocityStream(), new SAXResult(fop.getDefaultHandler()));
if(progress != null)
progress.setValue(90);
PreviewPanel previewPanel = new PreviewPanel(agent, null, renderer);
previewPanel.setScaleFactor(0.2);
previewPanel.setDisplayMode(PreviewPanel.CONTINUOUS);
previewPanel.reload();
if(progress != null)
progress.setValue(100);
return previewPanel;
}
public JDialog showInCustomDialog(Window parent) throws Exception {
final JSlider zoom = new JSlider(0, 500, 50);
zoom.setMajorTickSpacing(100);
zoom.setMinorTickSpacing(10);
zoom.setPaintTicks(true);
zoom.setPaintLabels(true);
final FopFactory fopFactory = FopFactory.newInstance();
final FOUserAgent agent = fopFactory.newFOUserAgent();
AWTRenderer renderer = new AWTRenderer();
agent.setRendererOverride(renderer);
renderer.setPreviewDialogDisplayed(false);
renderer.setUserAgent(agent);
renderer.clearViewportList();
final PreviewPanel previewPanel = new PreviewPanel(agent, null, renderer);
zoom.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
double value = zoom.getValue();
previewPanel.setScaleFactor(value/100);
}
});
final JProgressBar progress = new JProgressBar(0, 100);
JDialog dialog = new JDialog(parent);
dialog.getContentPane().setLayout(new BorderLayout());
dialog.getContentPane().add(zoom, BorderLayout.NORTH);
dialog.getContentPane().add(previewPanel, BorderLayout.CENTER);
dialog.getContentPane().add(progress, BorderLayout.SOUTH);
dialog.setSize(600, 800);
dialog.setVisible(true);
SwingWorker swingWorker = new SwingWorker() {
@Override
protected Object doInBackground() throws Exception {
Source source = createXSLStream();
progress.setValue(10);
Transformer transformer = TransformerFactory.newInstance().newTemplates(source).newTransformer();
Fop fop = fopFactory.newFop(agent);
StreamSource streamSource = createVelocityStream();
progress.setValue(50);
SAXResult saxr = new SAXResult(fop.getDefaultHandler());
progress.setValue(65);
transformer.transform(streamSource, saxr);
progress.setValue(90);
previewPanel.setScaleFactor(0.5);
previewPanel.setDisplayMode(PreviewPanel.CONTINUOUS);
previewPanel.reload();
progress.setValue(100);
return null;
}
};
swingWorker.execute();
return dialog;
}
public PrintRenderer getPrintRenderer() {
PrintRenderer printRenderer = null;
final PrinterJob printerJob = PrinterJob.getPrinterJob();
if(printerJob.printDialog())
printRenderer = new PrintRenderer(printerJob);
return printRenderer;
}
public synchronized void printDocument(PrintRenderer printRenderer, JProgressBar bar) throws Exception {
bar.setMinimum(0);
bar.setMaximum(100);
Transformer transformer = TransformerFactory.newInstance().newTemplates(createXSLStream()).newTransformer();
bar.setValue(33);
FopFactory fopFactory = FopFactory.newInstance();
if(printRenderer == null)
printRenderer = getPrintRenderer();
Fop fop;
if(printRenderer != null) {
FOUserAgent userAgent = fopFactory.newFOUserAgent();
printRenderer.setUserAgent(userAgent);
userAgent.setRendererOverride(printRenderer);
fop = fopFactory.newFop(MimeConstants.MIME_FOP_PRINT,userAgent);
}else fop = fopFactory.newFop(MimeConstants.MIME_FOP_PRINT);
bar.setValue(66);
transformer.transform(createVelocityStream(), new SAXResult(fop.getDefaultHandler()));
bar.setValue(100);
}
public synchronized void printDocument(PrintRenderer printRenderer, LocalProcessing processing) throws Exception {
if(processing != null)
processing.setMinMax(0, 100);
Transformer transformer = TransformerFactory.newInstance().newTemplates(createXSLStream()).newTransformer();
if(processing != null)
processing.setValue(33);
FopFactory fopFactory = FopFactory.newInstance();
Fop fop;
if(printRenderer != null) {
FOUserAgent userAgent = fopFactory.newFOUserAgent();
printRenderer.setUserAgent(userAgent);
userAgent.setRendererOverride(printRenderer);
fop = fopFactory.newFop(MimeConstants.MIME_FOP_PRINT,userAgent);
}else fop = fopFactory.newFop(MimeConstants.MIME_FOP_PRINT);
if(processing != null)
processing.setValue(66);
transformer.transform(createVelocityStream(), new SAXResult(fop.getDefaultHandler()));
if(processing != null)
processing.setValue(100);
}
public synchronized void printDocument(LocalProcessing processing) throws Exception {
printDocument(null, processing);
}
public synchronized File getDocument(String fileName, String type, LocalProcessing processing) throws Exception {
if(processing != null)
processing.setMinMax(0, 100);
File file = new File(new File(fileName).getAbsolutePath());
if(processing != null)
processing.setValue(10);
File dir = new File(file.getParent());
if(processing != null)
processing.setValue(20);
dir.mkdirs();
Transformer transformer = TransformerFactory.newInstance().newTemplates(createXSLStream()).newTransformer();
if(processing != null)
processing.setValue(30);
FileOutputStream out = new FileOutputStream(file);
FopFactory fopFactory = FopFactory.newInstance();
if(processing != null)
processing.setValue(50);
/**
* ???????????????????????
*/
fopFactory.setUserConfig(new File("fop.xconf"));
////////////////////////////
Fop fop = fopFactory.newFop(type,out);
if(processing != null)
processing.setValue(70);
transformer.transform(createVelocityStream(), new SAXResult(fop.getDefaultHandler()));
if(processing != null)
processing.setValue(90);
out.flush();
out.close();
if(processing != null)
processing.setValue(100);
return file;
}
public synchronized String getXSLFOString(LocalProcessing processing) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
if(processing != null)
processing.setMinMax(0, 100);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(createXSLStream());
if(processing != null)
processing.setValue(20);
Source src = createVelocityStream();
if(processing != null)
processing.setValue(50);
Result res = new StreamResult(out);
transformer.transform(src, res);
if(processing != null)
processing.setValue(90);
out.flush();
out.close();
if(processing != null)
processing.setValue(100);
return out.toString();
}
public File getDocumentFromXslFo(String fileName, String type, String xslfo) throws Exception {
File file = new File(new File(fileName).getAbsolutePath());
File dir = new File(file.getParent());
dir.mkdirs();
FileOutputStream out = new FileOutputStream(file);
FopFactory fopFactory = FopFactory.newInstance();
/**
* ???????????????????????
*/
fopFactory.setUserConfig(new File("fop.xconf"));
////////////////////////////
Fop fop = fopFactory.newFop(type,out);
Source src = new StreamSource(new StringReader(xslfo));
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.transform(src, new SAXResult(fop.getDefaultHandler()));
out.flush();
out.close();
return file;
}
public void preview(String xslfo) throws Exception {
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
FopFactory fopFactory = FopFactory.newInstance();
//fopFactory.setUserConfig(new File("fop.xconf"));
//fopFactory.setFontBaseURL("C:/WINDOWS/Fonts");
Fop fop = fopFactory.newFop(MimeConstants.MIME_FOP_AWT_PREVIEW);
Source src = new StreamSource(new StringReader(xslfo));
transformer.transform(src, new SAXResult(fop.getDefaultHandler()));
}
public void print(String xslfo) throws Exception {
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
FopFactory fopFactory = FopFactory.newInstance();
//fopFactory.setUserConfig(new File("fop.xconf"));
//fopFactory.setFontBaseURL("C:/WINDOWS/Fonts");
Fop fop = fopFactory.newFop(MimeConstants.MIME_FOP_AWT_PREVIEW);
Source src = new StreamSource(new StringReader(xslfo));
transformer.transform(src, new SAXResult(fop.getDefaultHandler()));
}
}