?? jsommapoutput.java
字號:
package fi.javasom.jsom;
/**
* This is JSomMapOutput class that outputs the map in wanted format.
*
* Copyright (C) 2001 Tomi Suuronen
*
* @version 1.0
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
import fi.javasom.jsom.*;
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import javax.xml.parsers.*;
import org.apache.xerces.dom.*;
import org.w3c.dom.*;
import org.apache.fop.apps.*;
import org.apache.fop.render.pdf.*;
import org.xml.sax.*;
public class JSomMapOutput
{
private File folder;
private File cache;
private JSomCreateDomTree dom; //DOM tree.
private String fileName; //name of the file to be created.
private String xslfile; //XSL file to be used.
private String paper; //the size of a paper for pdf.
private String orientation; //orientation of a paper for pdf.
private StringWriter embedSVG; //embedded SVG code.
private JsomMap map; //information about the map.
/**
* Constructor.
*
* @param File folder - Output folder.
* @param DocuemntImpl dom - Document DOM tree.
* @param String paper - the size of the paper :: a4 | letter
* @param String orientation - the orientation of the paper :: portrait || landscape
* @param JsomMap map - the information about the map
*/
public JSomMapOutput(File folder,JSomCreateDomTree dom,String paper,String orientation, JsomMap map)
{
this.folder = folder;
this.dom = dom;
this.paper = paper;
this.orientation = orientation;
this.map = map;
}
/**
* Outputs the map in the specified format with a specific name.
*
* @param String type = Type of the map:: xml | svg | pdf.
* @param String fileName = name of the file (without a suffix).
*/
public void outputMap(String type,String fileName)
{
this.fileName = fileName;
type = type.toLowerCase();
if(type.equals("pdf"))
{
try
{
cache = new File(folder,fileName+"."+type);
xslfile = "jsom_svg_pdf.xsl";
embedSVG = new StringWriter(); // Creates an empy StringWriter object for the output.
transform("pdf");
XMLReader parser = (XMLReader)new org.apache.xerces.parsers.SAXParser();
FopConstructor fc = new FopConstructor(paper,orientation,embedSVG.toString());
Driver driver = new Driver();
driver.setRenderer("org.apache.fop.render.pdf.PDFRenderer",Version.getVersion());
driver.addElementMapping("org.apache.fop.fo.StandardElementMapping");
driver.addElementMapping("org.apache.fop.svg.SVGElementMapping");
driver.addPropertyList("org.apache.fop.fo.StandardPropertyListMapping");
driver.addPropertyList("org.apache.fop.svg.SVGPropertyListMapping");
driver.setOutputStream(new FileOutputStream(cache));
driver.buildFOTree(parser,new org.xml.sax.InputSource(new StringReader(fc.getFopString())));
driver.format();
driver.render();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
else //XML and SVG
{
if(type.equals("xml"))
{
xslfile = "jsom_copier.xsl";
}
else //svg
{
xslfile = "jsom_svg.xsl";
}
cache = new File(folder,fileName+"."+type);
transform("save");
}
}
/*
* Makes the transformation.
*
* @param String function - type of transformation. save (saves into a file) | pdf.
*/
private void transform(String function)
{
/* Uses TRAX API to transform the data */
try
{
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(new StreamSource(xslfile));
if(function.equals("save"))
{
transformer.transform(new DOMSource(dom.getDomTreeDocument()),new StreamResult(cache));
}
else //pdf
{
transformer.transform(new DOMSource(dom.getDomTreeDocument()),new javax.xml.transform.stream.StreamResult(embedSVG));
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
/*********************************************************************************/
/*
* Constructs the XSL Formatting Objects as a DocumentImpl.
*/
private class FopConstructor
{
private String row;
/**
* Constructor.
*
* @param String paper - the size of the paper :: A4 | Letter
* @param String orientation - the orientation of the paper :: Portrait || Landscape
*/
public FopConstructor(String paper, String orientation, String svg)
{
String height = "", width = "", papert = "";
row = "";
if(paper.toLowerCase().equals("a4"))
{
papert = "A4";
if(orientation.toLowerCase().equals("portrait"))
{
height = "297mm";
width = "210mm";
}
else //landscape
{
height = "210mm";
width = "297mm";
}
}
else //letter
{
papert = "Letter";
if(orientation.toLowerCase().equals("portrait"))
{
height = "279mm";
width = "216mm";
}
else //landscape
{
height = "216mm";
width = "279mm";
}
}
row += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
row += "<fo:root xmlns:fo=\"http://www.w3.org/1999/XSL/Format\">";
row += "<fo:layout-master-set>";
row += "<fo:simple-page-master master-name=\""+papert+"\" page-height=\""+height+"\" page-width=\""+width+"\" margin-top=\"10mm\" margin-bottom=\"10mm\" margin-left=\"10mm\" margin-right=\"10mm\">";
row += "<fo:region-body margin-top=\"0mm\"/>";
row += "<fo:region-after extent=\"20mm\"/>";
row += "</fo:simple-page-master>";
row += "</fo:layout-master-set>";
row += "<fo:page-sequence master-name=\""+papert+"\" >";
row += "<fo:static-content flow-name=\"xsl-region-after\">";
row += "<fo:block text-align=\"start\" font-size=\"10pt\" font-family=\"Helvetica\" line-height=\"12pt\" border-top-width=\"thin\">";
row += "<fo:inline font-weight=\"bold\">Project name:</fo:inline> "+map.getProjectName();
row += "</fo:block>";
row += "<fo:block text-align=\"start\" font-size=\"10pt\" font-family=\"Helvetica\" line-height=\"12pt\" >";
row += "<fo:inline font-weight=\"bold\">Project code:</fo:inline> "+map.getProjectCode();
row += "</fo:block>";
row += "<fo:block text-align=\"start\" font-size=\"10pt\" font-family=\"Helvetica\" line-height=\"12pt\" >";
row += "<fo:inline font-weight=\"bold\">Identifier:</fo:inline> "+fileName;
row += "</fo:block>";
row += "<fo:block text-align=\"end\" font-size=\"10pt\" font-family=\"Helvetica\" line-height=\"12pt\" >";
row += "p. <fo:page-number/>";
row += "</fo:block>";
row += "</fo:static-content>";
row += "<fo:flow flow-name=\"xsl-region-body\">";
row += "<fo:block>";
row += "<fo:instream-foreign-object>";
/* start of SVG data addition */
row += svg;
/* end of SVG data addition */
row += "</fo:instream-foreign-object>";
row += "</fo:block>";
row += "</fo:flow>";
row += "</fo:page-sequence>";
row += "</fo:root>";
}
/**
* Builds the XSL Formatting Objects as a string.
*
* @return String - the XSL FO document.
*/
public String getFopString()
{
return row;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -