| 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 |
|
|
| 22 |
|
|
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
public class LeftMenuXSLTImpl implements LeftMenu { |
| 28 |
|
|
| 29 |
|
private String currentItem; |
| 30 |
|
private Document currentMenu; |
| 31 |
|
|
| 32 |
24 |
private String stylesheet = PropertyManager.getProperty("menu.xsl.path"); |
| 33 |
24 |
private String menuXML = PropertyManager.getProperty("menu.xml.path"); |
| 34 |
|
|
| 35 |
12 |
private static Log LOG = LogFactory.getLog(LeftMenuXSLTImpl.class); |
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
24 |
public LeftMenuXSLTImpl(String currentItem) throws IOException, DocumentException, TransformerException { |
| 46 |
24 |
LOG.debug("Building LeftMenuXSLTImpl with currentItem = " + currentItem); |
| 47 |
24 |
this.currentItem = currentItem; |
| 48 |
24 |
LOG.debug("Building currentMenu"); |
| 49 |
24 |
currentMenu = getCurrentMenu(getXmlMenu(), currentItem); |
| 50 |
24 |
LOG.debug("currentMenu built"); |
| 51 |
24 |
} |
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
public Document getCurrentMenu() |
| 59 |
|
{ |
| 60 |
24 |
return currentMenu; |
| 61 |
|
} |
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
|
protected Document getXmlMenu() throws MalformedURLException, DocumentException { |
| 70 |
24 |
LOG.debug("Loading input XML file for menu from " + menuXML); |
| 71 |
24 |
SAXReader reader = new SAXReader(); |
| 72 |
24 |
Document document = reader.read( FileUtils.getFile(menuXML) ); |
| 73 |
24 |
LOG.debug("Menu loaded"); |
| 74 |
24 |
return document; |
| 75 |
|
} |
| 76 |
|
|
| 77 |
|
|
| 78 |
|
|
| 79 |
|
|
| 80 |
|
|
| 81 |
|
|
| 82 |
|
|
| 83 |
|
|
| 84 |
|
|
| 85 |
|
protected Document getCurrentMenu(Document document, String currentItem) throws TransformerException { |
| 86 |
|
|
| 87 |
|
|
| 88 |
24 |
LOG.debug("starting transformation"); |
| 89 |
24 |
TransformerFactory factory = TransformerFactory.newInstance(); |
| 90 |
24 |
LOG.debug("loading stylesheet " + stylesheet); |
| 91 |
24 |
Transformer transformer = factory.newTransformer( new StreamSource( FileUtils.getFile(stylesheet) ) ); |
| 92 |
|
|
| 93 |
24 |
LOG.debug("configuring stylesheet with currentItem = " + currentItem); |
| 94 |
24 |
transformer.setParameter("selected", currentItem); |
| 95 |
|
|
| 96 |
|
|
| 97 |
24 |
DocumentSource source = new DocumentSource( document ); |
| 98 |
24 |
DocumentResult result = new DocumentResult(); |
| 99 |
24 |
transformer.transform( source, result ); |
| 100 |
|
|
| 101 |
|
|
| 102 |
24 |
Document transformedDoc = result.getDocument(); |
| 103 |
24 |
return transformedDoc; |
| 104 |
|
} |
| 105 |
|
|
| 106 |
|
|
| 107 |
|
|
| 108 |
|
} |