162 lines
5.1 KiB
Java
162 lines
5.1 KiB
Java
package org.gcube.application.framework.core.util;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.ByteArrayInputStream;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.InputStreamReader;
|
|
import java.io.StringReader;
|
|
import java.io.StringWriter;
|
|
import java.util.ArrayList;
|
|
|
|
import javax.xml.parsers.DocumentBuilder;
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
import javax.xml.parsers.ParserConfigurationException;
|
|
import javax.xml.transform.OutputKeys;
|
|
import javax.xml.transform.Templates;
|
|
import javax.xml.transform.Transformer;
|
|
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 org.gcube.application.framework.core.session.ASLSession;
|
|
import org.w3c.dom.Document;
|
|
import org.w3c.dom.Element;
|
|
import org.w3c.dom.NodeList;
|
|
import org.xml.sax.InputSource;
|
|
import org.xml.sax.SAXException;
|
|
|
|
import com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl;
|
|
|
|
public class LayoutPortlets {
|
|
|
|
protected static final String PORTLETS_XSL = "/etc/layoutPortlets.xsl";
|
|
|
|
|
|
public static void addPortletsToSession(ASLSession session, Document layout) throws IOException, TransformerException, ParserConfigurationException, SAXException {
|
|
String layoutString = getStringFromDocument(layout);
|
|
String layoutXSL = getPortletsXSLT();
|
|
String portlets = transform(layoutString, layoutXSL);
|
|
|
|
Document portletsDoc = getDocumentFromString(portlets);
|
|
|
|
ArrayList<String> availablePortlets = new ArrayList<String>();
|
|
|
|
Element root = portletsDoc.getDocumentElement();
|
|
NodeList portletNodes = root.getChildNodes();
|
|
|
|
for (int i = 0; i < portletNodes.getLength(); i++) {
|
|
String portletString = portletNodes.item(i).getTextContent();
|
|
String[] split = portletString.split("#");
|
|
String portletName = split[1];
|
|
availablePortlets.add(portletName);
|
|
System.out.println("Adding Portlet name to session: " + split[1]);
|
|
}
|
|
|
|
session.setAttribute("availablePortlets", availablePortlets);
|
|
}
|
|
|
|
private static String convertStreamToString(InputStream is) throws IOException {
|
|
if (is != null) {
|
|
StringBuilder sb = new StringBuilder();
|
|
String line;
|
|
|
|
try {
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
|
|
while ((line = reader.readLine()) != null) {
|
|
sb.append(line).append("\n");
|
|
}
|
|
} finally {
|
|
is.close();
|
|
}
|
|
return sb.toString();
|
|
} else
|
|
return "";
|
|
}
|
|
|
|
public static String getPortletsXSLT() throws IOException {
|
|
InputStream is = LayoutPortlets.class.getResourceAsStream(PORTLETS_XSL); //??
|
|
|
|
if (is == null){
|
|
|
|
System.out.println("Default XSLT resource not found on "+PORTLETS_XSL);
|
|
return null;
|
|
}
|
|
|
|
InputStreamReader isr = new InputStreamReader(is);
|
|
|
|
BufferedReader filebuf = null;
|
|
String nextStr = null;
|
|
String toReturn = new String();
|
|
try {
|
|
filebuf = new BufferedReader(isr);
|
|
nextStr = filebuf.readLine();
|
|
while (nextStr != null) {
|
|
toReturn += nextStr ;
|
|
nextStr = filebuf.readLine();
|
|
}
|
|
filebuf.close(); // chiude il file
|
|
} catch (FileNotFoundException e) {
|
|
e.printStackTrace();
|
|
} catch (IOException e1) {
|
|
e1.printStackTrace();
|
|
}
|
|
|
|
return toReturn;
|
|
}
|
|
|
|
/**
|
|
* @param xml the XML to convert.
|
|
* @param xslt the XML used for the conversion.
|
|
* @return the HTML.
|
|
* @throws TransformerException if an error occurs.
|
|
*/
|
|
public static String transform(String xml, String xslt) throws TransformerException
|
|
{
|
|
|
|
TransformerFactoryImpl factory = new TransformerFactoryImpl();
|
|
StreamSource sourceInput = new StreamSource(new ByteArrayInputStream(xslt.getBytes()));
|
|
Templates sheet = factory.newTemplates(sourceInput);
|
|
|
|
Transformer instance = sheet.newTransformer();
|
|
instance.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "true");
|
|
StringWriter w = new StringWriter();
|
|
instance.transform(new StreamSource(new StringReader(xml)), new StreamResult(w));
|
|
|
|
return w.toString();
|
|
}
|
|
|
|
//method to convert Document to String
|
|
private static String getStringFromDocument(Document doc)
|
|
{
|
|
try
|
|
{
|
|
DOMSource domSource = new DOMSource(doc);
|
|
StringWriter writer = new StringWriter();
|
|
StreamResult result = new StreamResult(writer);
|
|
TransformerFactory tf = TransformerFactory.newInstance();
|
|
Transformer transformer = tf.newTransformer();
|
|
transformer.transform(domSource, result);
|
|
return writer.toString();
|
|
}
|
|
catch(TransformerException ex)
|
|
{
|
|
ex.printStackTrace();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
private static Document getDocumentFromString(String str) throws ParserConfigurationException, SAXException, IOException {
|
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
DocumentBuilder builder = factory.newDocumentBuilder();
|
|
Document document = builder.parse(new InputSource(new StringReader(str)));
|
|
|
|
return document;
|
|
}
|
|
|
|
}
|