1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package ch.elca.el4j.services.monitoring.jmx;
19
20 import java.io.BufferedReader;
21 import java.io.File;
22 import java.io.FileReader;
23 import java.io.IOException;
24 import java.io.InputStream;
25
26 import org.springframework.util.StringUtils;
27
28 import ch.elca.el4j.services.monitoring.notification.CoreNotificationHelper;
29
30
31
32
33
34
35
36
37 public class CssHtmlParser implements CssHtmlParserMBean {
38
39
40
41 private String m_stylesheetContent;
42
43
44
45
46
47
48
49
50
51 public CssHtmlParser(String stylesheetPath) throws IOException {
52 StringBuffer sb = new StringBuffer();
53
54 File stylesheetFile = new File(stylesheetPath);
55 if (stylesheetFile.exists()) {
56 BufferedReader br
57 = new BufferedReader(new FileReader(stylesheetFile));
58 String line = br.readLine();
59 while (StringUtils.hasLength(line)) {
60 sb.append(line);
61 line = br.readLine();
62 }
63 if (br != null) {
64 br.close();
65 }
66
67 } else {
68 ClassLoader cl = Thread.currentThread().getContextClassLoader();
69 InputStream is = cl.getResourceAsStream(stylesheetPath);
70 if (is != null && is.available() > 0) {
71 int character;
72 while ((character = is.read()) != -1) {
73 sb.append((char) character);
74 }
75 } else {
76 CoreNotificationHelper.notifyMisconfiguration(
77 "Stylesheet '" + stylesheetPath + "' not found.");
78 }
79 }
80
81 if (!StringUtils.hasText(sb.toString())) {
82 CoreNotificationHelper.notifyMisconfiguration(
83 "Stylesheet '" + stylesheetPath + "' has no content.");
84 }
85
86 StringBuffer stylesheetContent = new StringBuffer();
87 stylesheetContent.append("<style type=\"text/css\"><!-- ");
88 stylesheetContent.append(sb.toString());
89 stylesheetContent.append(" --></style>");
90 m_stylesheetContent = stylesheetContent.toString();
91 }
92
93
94
95
96
97
98
99 public String parseRequest(String s) {
100 return null;
101 }
102
103
104
105
106
107
108
109
110 public String parsePage(String s) {
111 String answer = s;
112 if (StringUtils.hasText(s)) {
113 int insertionPoint = s.toLowerCase().indexOf("</head>");
114 if (insertionPoint > 0) {
115 StringBuffer sb = new StringBuffer();
116 sb.append(s.substring(0, insertionPoint));
117 sb.append(m_stylesheetContent);
118 sb.append(s.substring(insertionPoint));
119 answer = sb.toString();
120 }
121 }
122 return answer;
123 }
124 }