1 |
3018
|
vassilis.s
|
/**
|
2 |
|
|
*
|
3 |
|
|
*/
|
4 |
|
|
package eu.dnetlib.data.search.web;
|
5 |
|
|
|
6 |
56601
|
katerina.i
|
import eu.dnetlib.api.data.SearchService;
|
7 |
|
|
import gr.uoa.di.driver.enabling.resultset.ResultSetFactory;
|
8 |
|
|
import org.springframework.context.ApplicationContext;
|
9 |
|
|
import org.springframework.web.context.support.WebApplicationContextUtils;
|
10 |
|
|
import org.w3c.dom.Document;
|
11 |
|
|
import org.w3c.dom.Element;
|
12 |
|
|
import org.xml.sax.InputSource;
|
13 |
|
|
import org.xml.sax.SAXException;
|
14 |
3018
|
vassilis.s
|
|
15 |
|
|
import javax.servlet.ServletContext;
|
16 |
|
|
import javax.servlet.http.HttpServletRequest;
|
17 |
|
|
import javax.xml.parsers.DocumentBuilder;
|
18 |
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
19 |
|
|
import javax.xml.parsers.ParserConfigurationException;
|
20 |
56601
|
katerina.i
|
import javax.xml.transform.*;
|
21 |
3018
|
vassilis.s
|
import javax.xml.transform.dom.DOMSource;
|
22 |
|
|
import javax.xml.transform.stream.StreamResult;
|
23 |
56601
|
katerina.i
|
import java.io.IOException;
|
24 |
|
|
import java.io.StringReader;
|
25 |
|
|
import java.io.StringWriter;
|
26 |
3018
|
vassilis.s
|
|
27 |
|
|
/**
|
28 |
|
|
* @author stoumpos
|
29 |
|
|
*
|
30 |
|
|
*/
|
31 |
|
|
public class SearchServiceContextUtils {
|
32 |
|
|
|
33 |
|
|
public static SearchService getSearchService(HttpServletRequest request) {
|
34 |
|
|
ServletContext session = request.getSession().getServletContext();
|
35 |
|
|
ApplicationContext context = WebApplicationContextUtils
|
36 |
|
|
.getWebApplicationContext(session);
|
37 |
|
|
SearchService search = (SearchService) context.getBean("searchService");
|
38 |
6673
|
antonis.le
|
|
39 |
3018
|
vassilis.s
|
return search;
|
40 |
|
|
}
|
41 |
6673
|
antonis.le
|
|
42 |
|
|
public static ResultSetFactory getResultSetFactory(
|
43 |
|
|
HttpServletRequest request) {
|
44 |
|
|
ServletContext session = request.getSession().getServletContext();
|
45 |
|
|
ApplicationContext context = WebApplicationContextUtils
|
46 |
|
|
.getWebApplicationContext(session);
|
47 |
|
|
ResultSetFactory rsFactory = (ResultSetFactory) context
|
48 |
|
|
.getBean("resultSetFactory");
|
49 |
|
|
|
50 |
|
|
return rsFactory;
|
51 |
|
|
}
|
52 |
|
|
|
53 |
3018
|
vassilis.s
|
public static final String prettyPrint(String xml)
|
54 |
|
|
throws TransformerConfigurationException,
|
55 |
|
|
TransformerFactoryConfigurationError, TransformerException,
|
56 |
|
|
SAXException, IOException, ParserConfigurationException {
|
57 |
6673
|
antonis.le
|
|
58 |
3018
|
vassilis.s
|
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
59 |
6673
|
antonis.le
|
dbf.setValidating(true);
|
60 |
|
|
DocumentBuilder db = dbf.newDocumentBuilder();
|
61 |
|
|
InputSource sourceXML = new InputSource(new StringReader(xml));
|
62 |
|
|
Document xmlDoc = db.parse(sourceXML);
|
63 |
|
|
Element source = xmlDoc.getDocumentElement();
|
64 |
|
|
source.normalize();
|
65 |
|
|
|
66 |
|
|
StreamResult target = new StreamResult(new StringWriter());
|
67 |
|
|
|
68 |
3018
|
vassilis.s
|
Transformer tf = TransformerFactory.newInstance().newTransformer();
|
69 |
|
|
tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
|
70 |
|
|
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
|
71 |
|
|
tf.setOutputProperty(OutputKeys.INDENT, "yes");
|
72 |
|
|
tf.transform(new DOMSource(source), target);
|
73 |
6673
|
antonis.le
|
|
74 |
3018
|
vassilis.s
|
return target.getWriter().toString();
|
75 |
|
|
}
|
76 |
|
|
|
77 |
|
|
}
|