1 package be.dvw.administration.mvc.navigation;
2
3 import be.dvw.administration.util.FileUtils;
4 import be.dvw.administration.util.PropertyManager;
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
7 import org.dom4j.Document;
8 import org.dom4j.DocumentException;
9 import org.dom4j.io.DocumentResult;
10 import org.dom4j.io.DocumentSource;
11 import org.dom4j.io.SAXReader;
12
13 import javax.xml.transform.Transformer;
14 import javax.xml.transform.TransformerException;
15 import javax.xml.transform.TransformerFactory;
16 import javax.xml.transform.stream.StreamSource;
17 import java.io.IOException;
18 import java.net.MalformedURLException;
19
20 /***
21 * Created by IntelliJ IDEA.
22 * User: Toon.Timbermont
23 * Date: Mar 25, 2005
24 * Time: 6:19:27 PM
25 * To change this template use File | Settings | File Templates.
26 */
27 public class LeftMenuXSLTImpl implements LeftMenu {
28
29 private String currentItem;
30 private Document currentMenu;
31
32 private String stylesheet = PropertyManager.getProperty("menu.xsl.path");
33 private String menuXML = PropertyManager.getProperty("menu.xml.path");
34
35 private static Log LOG = LogFactory.getLog(LeftMenuXSLTImpl.class);
36
37
38 /***
39 * Constructor based on the currently selected item, defining the currentMenu
40 * @param currentItem
41 * @throws IOException
42 * @throws DocumentException
43 * @throws TransformerException
44 */
45 public LeftMenuXSLTImpl(String currentItem) throws IOException, DocumentException, TransformerException {
46 LOG.debug("Building LeftMenuXSLTImpl with currentItem = " + currentItem);
47 this.currentItem = currentItem;
48 LOG.debug("Building currentMenu");
49 currentMenu = getCurrentMenu(getXmlMenu(), currentItem);
50 LOG.debug("currentMenu built");
51 }
52
53 /***
54 * Method returning the unformatted HTML menu
55 * based on the XSL transformation
56 * @return currrentMenu
57 */
58 public Document getCurrentMenu()
59 {
60 return currentMenu;
61 }
62
63 /***
64 * Helper method that reads the XML menu file
65 * @return
66 * @throws java.net.MalformedURLException
67 * @throws org.dom4j.DocumentException
68 */
69 protected Document getXmlMenu() throws MalformedURLException, DocumentException {
70 LOG.debug("Loading input XML file for menu from " + menuXML);
71 SAXReader reader = new SAXReader();
72 Document document = reader.read( FileUtils.getFile(menuXML) );
73 LOG.debug("Menu loaded");
74 return document;
75 }
76
77
78 /***
79 * Helper method that transforms the menu XML to the output HTML
80 * @param document
81 * @param currentItem
82 * @return
83 * @throws TransformerException
84 */
85 protected Document getCurrentMenu(Document document, String currentItem) throws TransformerException {
86
87
88 LOG.debug("starting transformation");
89 TransformerFactory factory = TransformerFactory.newInstance();
90 LOG.debug("loading stylesheet " + stylesheet);
91 Transformer transformer = factory.newTransformer( new StreamSource( FileUtils.getFile(stylesheet) ) );
92
93 LOG.debug("configuring stylesheet with currentItem = " + currentItem);
94 transformer.setParameter("selected", currentItem);
95
96
97 DocumentSource source = new DocumentSource( document );
98 DocumentResult result = new DocumentResult();
99 transformer.transform( source, result );
100
101
102 Document transformedDoc = result.getDocument();
103 return transformedDoc;
104 }
105
106
107
108 }