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  package ch.elca.el4j.services.monitoring.jmx.display;
18  
19  import java.util.Iterator;
20  import java.util.LinkedList;
21  import java.util.List;
22  import java.util.regex.Matcher;
23  import java.util.regex.Pattern;
24  
25  /**
26   * Utility for displaying HTML information and creating strings out of it.
27   *
28   * @svnLink $Revision: 3875 $;$Date: 2009-08-04 14:35:53 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$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/display/HtmlDisplayManager.java $
29   *
30   * @author David Bernhard (DBD)
31   */
32  public class HtmlDisplayManager implements DisplayManager {
33  
34  	// Checkstyle: StaticVariableNameCheck off
35  	
36  	/** Tag to wrap around title. */
37  	private static final String TITLE_TAG = "h2";
38  	
39  	/** Tag to wrap around section headings. */
40  	private static final String SECTION_TAG = "h3";
41  	
42  	// Checkstyle: StaticVariableNameCheck on
43  	
44  	/**
45  	 * Title for this page.
46  	 */
47  	private String m_title;
48  	
49  	/**
50  	 * Holds the content (sections) of this page.
51  	 */
52  	private List<Section> m_content;
53  	
54  	/**
55  	 * Default constructor.
56  	 */
57  	public HtmlDisplayManager() {
58  		m_content = new LinkedList<Section>();
59  		m_title = "";
60  	}
61  	
62  	/** {@inheritDoc} */
63  	public void setTitle(String title) {
64  		m_title = title;
65  	}
66  	
67  	/** {@inheritDoc} */
68  	public void addSection(Section section) {
69  		m_content.add(section);
70  	}
71  	
72  	/**
73  	 * Creates a hyperlink.
74  	 * @param text The link's text.
75  	 * @param target The link's target.
76  	 * @return A Html hyperlink.
77  	 */
78  	private String linkTo(String text, String target) {
79  		return "<a href=\"" + target + "\">" + text + "</a>";
80  	}
81  
82  	/**
83  	 * @param name The section name.
84  	 * @return The name modified to use as an anchor target.
85  	 */
86  	private String linkName(String name) {
87  		Pattern p = Pattern.compile("[^a-zA-Z0-9_]");
88  		Matcher m = p.matcher(name);
89  		return m.replaceAll("");
90  	}
91  	
92  	/** {@inheritDoc} */
93  	public String getPage() {
94  		String page = "";
95  		
96  		// Title
97  		if (!m_title.equals("")) {
98  			page += tag(TITLE_TAG, m_title) + "\n";
99  		}
100 		
101 		// Index
102 		Iterator<Section> i1 = m_content.iterator();
103 		String index = "";
104 		while (i1.hasNext()) {
105 			String name = i1.next().getName();
106 			index += tag("i", linkTo(name, "#" + linkName(name)) + " ");
107 		}
108 		page += index + "\n";
109 		
110 		// Sections
111 		Iterator<Section> i = m_content.iterator();
112 		while (i.hasNext()) {
113 			Section section = i.next();
114 			page += tag(SECTION_TAG, section.getName())
115 				+ "<a name=\"" + linkName(section.getName()) + "\" />\n";
116 			page += section.getContent() + "\n";
117 		}
118 			
119 		return page;
120 	}
121 	
122 	/**
123 	 * Wrap some text in a tag.
124 	 * @param tag The tag to wrap in.
125 	 * @param value The text.
126 	 * @return <code>&lt;tag&gt;</code>text<code>&lt;/tag&gt;</code>
127 	 */
128 	public static String tag(String tag, String value) {
129 		return "<" + tag + ">" + value + "</" + tag + ">";
130 	}
131 	
132 	/**
133 	 * @param value The text.
134 	 * @param tags A list of tags.
135 	 * @return the text wrapped recursively in the tags.
136 	 */
137 	public static String tagRecursive(String value, String ... tags) {
138 		String result = value;
139 		for (int i = tags.length - 1; i >= 0; i--) {
140 			result = tag(tags[i], result);
141 		}
142 		return result;
143 	}
144 
145 }