Java tutorial
/* Copyright 2009 - 2010 Under Dusken 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 no.dusken.barweb.view; import no.dusken.barweb.model.BarPerson; import no.dusken.barweb.model.Gjeng; import no.dusken.barweb.model.Vare; import org.apache.fop.apps.Fop; import org.apache.fop.apps.FopFactory; import org.apache.fop.apps.MimeConstants; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.servlet.view.AbstractView; import org.w3c.dom.Document; import org.w3c.dom.Element; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.sax.SAXResult; import javax.xml.transform.stream.StreamSource; import java.text.SimpleDateFormat; import java.util.*; /** * @author Marvin B. Lillehaug <lillehau@underdusken.no> */ public class XListView extends AbstractView { private Logger log = LoggerFactory.getLogger(getClass()); private String styleSheet = "x-list-fo.xsl"; private FopFactory fopFactory = FopFactory.newInstance(); private TransformerFactory tFactory = TransformerFactory.newInstance(); protected void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception { response.setContentType("application/pdf"); response.setCharacterEncoding("UTF-8"); response.setHeader("Content-Disposition", "attachment; filename=xlist.pdf"); List<BarPerson> barPersons = (List<BarPerson>) model.get("barPersons"); Gjeng g = (Gjeng) model.get("gjeng"); List<Vare> varer = (List<Vare>) model.get("varer"); Integer lowlimit = (Integer) model.get("lowLimit"); Boolean panger = (Boolean) model.get("panger"); //Setup FOP Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, response.getOutputStream()); //Setup Transformer Source xsltSrc = new StreamSource( this.getClass().getResourceAsStream("/no/dusken/barweb/stylesheets/" + styleSheet)); Transformer transformer = tFactory.newTransformer(xsltSrc); //Make sure the XSL transformation's result is piped through to FOP Result res = new SAXResult(fop.getDefaultHandler()); //Setup input DOMSource src = new DOMSource(generateXml(barPersons, g, varer, lowlimit, panger)); //Start the transformation and rendering process transformer.transform(src, res); response.getOutputStream().flush(); } private Document generateXml(List<BarPerson> barPersons, Gjeng gjeng, List<Vare> varer, int lowlimit, Boolean panger) { Document dom = null; //get an instance of factory DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { //get an instance of builder DocumentBuilder db = dbf.newDocumentBuilder(); //create an instance of DOM dom = db.newDocument(); } catch (ParserConfigurationException pce) { log.error("Error when generating x-list"); } Element root = dom.createElement("xlist"); root.setAttribute("gjeng", gjeng.getName()); SimpleDateFormat dateformat = new SimpleDateFormat("d. MMMMMMMMM yyyy - HH:mm", new Locale("no")); root.setAttribute("generated", dateformat.format((new GregorianCalendar()).getTime())); root.setAttribute("magicNumber", String.valueOf(((new Random()).nextDouble() * 1000))); root.setAttribute("panger", panger.toString()); dom.appendChild(root); Element personsEle = dom.createElement("barPersons"); for (BarPerson p : barPersons) { Element personEle = createPersonElement(p, dom, lowlimit); personsEle.appendChild(personEle); } root.appendChild(personsEle); Element vareEle = dom.createElement("varer"); for (Vare v : varer) { Element vare = createVareElement(v, dom); vareEle.appendChild(vare); } root.appendChild(vareEle); return dom; } private Element createPersonElement(BarPerson p, Document dom, Integer lowLimit) { Element personEle = dom.createElement("barPerson"); Boolean enoughMoney = p.getMoney() >= lowLimit; personEle.setAttribute("enoughmoney", enoughMoney.toString()); personEle.setTextContent(p.getName()); return personEle; } private Element createVareElement(Vare v, Document dom) { Element vareEle = dom.createElement("vare"); vareEle.setTextContent(v.getName()); return vareEle; } }