View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
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   * This MBean adds css information to a given html page.
32   *
33   * @svnLink $Revision: 4010 $;$Date: 2009-12-01 10:59:54 +0100 (Di, 01. Dez 2009) $;$Author: jonasha $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/jmx/src/main/java/ch/elca/el4j/services/monitoring/jmx/CssHtmlParser.java $
34   *
35   * @author Martin Zeltner (MZE)
36   */
37  public class CssHtmlParser implements CssHtmlParserMBean {
38  	/**
39  	 * Contains the stylesheet content to add to html page.
40  	 */
41  	private String m_stylesheetContent;
42  	
43  	/**
44  	 * Constructor.
45  	 *
46  	 * @param stylesheetPath
47  	 *            Is the path to the needed stylesheet.
48  	 * @throws IOException
49  	 *             If reading stylesheet makes trouble.
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  	 * {@inheritDoc}
95  	 *
96  	 * Asks which action should be done for the given request uri string. If
97  	 * <code>null</code> is returned the underlying parser takes action.
98  	 */
99  	public String parseRequest(String s) {
100 		return null;
101 	}
102 
103 	/**
104 	 * {@inheritDoc}
105 	 *
106 	 * Adds css content directly before the head end tag in html page. If no
107 	 * head end tag exists, no css will be added. The lookup of the head end tag
108 	 * is case insensitive.
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 }