📄 pdfbuilderimpl.java
字号:
public class PDFBuilderImpl implements PDFBuilder {
private String xslFileName = null;
/**
*
* @param xslFileName xslFileName and xslFilePath
*/
public PDFBuilderImpl(String xslFileName) {
this.xslFileName = xslFileName;
System.out.println("[PDFBuilderImpl:PDFBuilderImpl]xslFileName:" + xslFileName);
}
/**
* return XSLTFile Name
* @return
*/
public String getXSLTFileName() {
return this.xslFileName;
}
/**
*
* @param data
* @return
*/
public Source getDataSource(ResultSet data,Hashtable criterialHash,String reportType){
Source source = new SAXSource(new ResultSetXMLReader(), new ResultSetInputSource(data,criterialHash,reportType));
return source;
}
/**
*
* @param data
* @return
*/
public Source getDataSource(DataContainer data) {
try {
String strXMLContent = data.serialize();
StreamSource xmlInput = new StreamSource(new StringReader(strXMLContent));
return xmlInput;
} catch (ExDataContainer e) {
e.printStackTrace();
}
return null;
}
/**
*
* @param str
* @return
*/
public Source generate(String str){
StreamSource xmlInput = new StreamSource(new StringReader(str));
return xmlInput;
}
/**
*
* @param data
* @return
*/
public Source getDatasource(InputStream data) {
try{
InputStreamReader inputStreamReader = new InputStreamReader(data);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String returnStr = "";
String redStr = "";
while(redStr !=null){
redStr = bufferedReader.readLine();
returnStr = returnStr + redStr;
}
StreamSource xmlInput = new StreamSource(new StringReader(returnStr));
return xmlInput;
}catch(IOException ie){
ie.printStackTrace();
}
return null;
}
/**
*
* @param data
* @return
*/
public Source getDataSource(ReportData data,Hashtable criterialHash,String reportType) throws Exception {
if(data instanceof TrendAnalysisData){
Source source = new SAXSource(new TrendAnalysisDataXMLReader(), new TrendAnalysisDataInputSource((TrendAnalysisData)data,criterialHash,reportType));
return source;
}
throw new Exception("[PDFBuilderImpl:getDataSource]The report type not support ");
}
/**
*
* @param out
* @return
*/
private Driver initDriver(ByteArrayOutputStream out)
{
Driver driver;
driver = new Driver();
driver.setRenderer(Driver.RENDER_PDF);
driver.setOutputStream(out);
return driver;
}
/**
*
* @param xslPath
* @return
* @throws TransformerConfigurationException
*/
private Transformer getXSLTransformer(String xslPath)
throws TransformerConfigurationException {
//Transformer
// System.out.println("=====,,,,,,");
// xslPath = "/transactionList.xsl";
Source xsltSrc = new StreamSource(xslPath);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer(xsltSrc);
return transformer;
}
/**
*
*/
public OutputStream generate(ResultSet resultset, ReportCriteria filter,Hashtable criterialHash,String reportType) {
Source source = getDataSource(resultset,criterialHash,reportType);
return generate(source);
}
/**
* Only for Test
* @param source
* @param res
* @param transformer
* @throws TransformerFactoryConfigurationError
*/
public static void writeToDisk(Source source) throws TransformerFactoryConfigurationError {
//---------------------------- DEMO code ----------------------------------------
try {
System.out.println("FOP ExampleObj2XML\n");
System.out.println("Preparing...");
//Setup directories
File baseDir = new File("c:\\");
File outDir = new File(baseDir, "abcdefg");
outDir.mkdirs();
//Setup input and output
File xmlfile = new File("C:\\", "ResultObj2XMLsss.xml");
System.out.println("Input: a ProjectTeam object");
System.out.println("Output: XML (" + xmlfile + ")");
System.out.println();
System.out.println("Serializing...");
//Setup XSLT
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
Result res = new StreamResult(xmlfile);
transformer.transform(source, res);
System.out.println("Success!");
} catch (Exception e) {
System.out.println(e.toString());
}
}
/**
*
*/
public OutputStream generate(DataContainer dataContainer) {
Source source = getDataSource(dataContainer);
return generate(source);
}
/**
*
*/
public OutputStream generate(InputStream inputStream) {
Source source = getDatasource(inputStream);
return generate(source);
}
/**
*
*/
public OutputStream generate(Source data) {
// writeToDisk(data);
ByteArrayOutputStream out;
out = new ByteArrayOutputStream();
Driver driver;
Result res;
driver = initDriver(out);
res = new SAXResult(driver.getContentHandler());
//render output
Transformer transformer = null;
try {
transformer = getXSLTransformer(this.xslFileName);
transformer.transform(data, res);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e1) {
e1.printStackTrace();
}
return out;
}
/**
*
*/
public Source generate(ReportData reportData,Hashtable criterialHash,String reportType){
try {
return getDataSource(reportData,criterialHash,reportType);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -