Java tutorial
/* * Copyright 2012 Sazneo. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.sazneo.export.formatter.html; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.w3c.dom.Document; import org.xml.sax.SAXException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.text.MessageFormat; /** * @author Jamie Atkinson */ public class HtmlFormatter { //-------------------------------------------------------------------------------------- Private fields private Log logger = LogFactory.getLog(HtmlFormatter.class); private Document document = null; private File styleSheet = null; private File dataFile = null; private OutputStream outputStream = null; //-------------------------------------------------------------------------------------- Constructor public HtmlFormatter(File styleSheet, File dataFile, OutputStream outputStream) { logger.debug(MessageFormat.format("Using stylesheet: {0}", styleSheet.getAbsolutePath())); this.styleSheet = styleSheet; logger.debug(MessageFormat.format("Using export XML from: {0}", dataFile.getAbsolutePath())); this.dataFile = dataFile; this.outputStream = outputStream; } //-------------------------------------------------------------------------------------- Public Methods public File getStyleSheet() { return styleSheet; } public void setStyleSheet(File styleSheet) { this.styleSheet = styleSheet; } public File getDataFile() { return dataFile; } public void setDataFile(File dataFile) { this.dataFile = dataFile; } public OutputStream getOutputStream() { return outputStream; } public void setOutputStream(OutputStream outputStream) { this.outputStream = outputStream; } /** * Transform the data file and output to the chosen output dir */ public void transform() { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder builder = factory.newDocumentBuilder(); document = builder.parse(dataFile); TransformerFactory transformerFactory = TransformerFactory.newInstance(); StreamSource styleSource = new StreamSource(styleSheet); Transformer transformer = transformerFactory.newTransformer(styleSource); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(outputStream); transformer.transform(source, result); } catch (ParserConfigurationException e) { logger.error(e); } catch (SAXException e) { logger.error(e); } catch (IOException e) { logger.error(e); } catch (TransformerConfigurationException e) { logger.error(e); } catch (TransformerException e) { logger.error(e); } } }