PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/eclipse_SDK-3.7.1/plugins/org.eclipse.jface.text.source_3.7.1.r371_v20110825-0800/org/eclipse/jface/internal/text/html/HTMLPrinter.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 374 lines | 311 code | 33 blank | 30 comment | 20 complexity | 1bef90002dd1c006d2956d75fb48a80a MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2000, 2010 IBM Corporation and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * IBM Corporation - initial API and implementation
  10. *******************************************************************************/
  11. package org.eclipse.jface.internal.text.html;
  12. import java.io.IOException;
  13. import java.io.Reader;
  14. import java.net.URL;
  15. import org.eclipse.swt.SWT;
  16. import org.eclipse.swt.SWTError;
  17. import org.eclipse.swt.graphics.FontData;
  18. import org.eclipse.swt.graphics.RGB;
  19. import org.eclipse.swt.widgets.Display;
  20. import org.eclipse.swt.widgets.Event;
  21. import org.eclipse.swt.widgets.Listener;
  22. import org.eclipse.jface.util.Util;
  23. import org.eclipse.jface.text.DefaultInformationControl;
  24. /**
  25. * Provides a set of convenience methods for creating HTML pages.
  26. * <p>
  27. * Moved into this package from <code>org.eclipse.jface.internal.text.revisions</code>.</p>
  28. */
  29. public class HTMLPrinter {
  30. private static RGB BG_COLOR_RGB= new RGB(255, 255, 225); // RGB value of info bg color on WindowsXP
  31. private static RGB FG_COLOR_RGB= new RGB(0, 0, 0); // RGB value of info fg color on WindowsXP
  32. private static final String UNIT; // See https://bugs.eclipse.org/bugs/show_bug.cgi?id=155993
  33. static {
  34. UNIT= Util.isMac() ? "px" : "pt"; //$NON-NLS-1$//$NON-NLS-2$
  35. }
  36. static {
  37. final Display display= Display.getDefault();
  38. if (display != null && !display.isDisposed()) {
  39. try {
  40. display.asyncExec(new Runnable() {
  41. /*
  42. * @see java.lang.Runnable#run()
  43. */
  44. public void run() {
  45. cacheColors(display);
  46. installColorUpdater(display);
  47. }
  48. });
  49. } catch (SWTError err) {
  50. // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=45294
  51. if (err.code != SWT.ERROR_DEVICE_DISPOSED)
  52. throw err;
  53. }
  54. }
  55. }
  56. private HTMLPrinter() {
  57. }
  58. private static void cacheColors(Display display) {
  59. BG_COLOR_RGB= display.getSystemColor(SWT.COLOR_INFO_BACKGROUND).getRGB();
  60. FG_COLOR_RGB= display.getSystemColor(SWT.COLOR_INFO_FOREGROUND).getRGB();
  61. }
  62. private static void installColorUpdater(final Display display) {
  63. display.addListener(SWT.Settings, new Listener() {
  64. public void handleEvent(Event event) {
  65. cacheColors(display);
  66. }
  67. });
  68. }
  69. private static String replace(String text, char c, String s) {
  70. int previous= 0;
  71. int current= text.indexOf(c, previous);
  72. if (current == -1)
  73. return text;
  74. StringBuffer buffer= new StringBuffer();
  75. while (current > -1) {
  76. buffer.append(text.substring(previous, current));
  77. buffer.append(s);
  78. previous= current + 1;
  79. current= text.indexOf(c, previous);
  80. }
  81. buffer.append(text.substring(previous));
  82. return buffer.toString();
  83. }
  84. /**
  85. * Escapes reserved HTML characters in the given string.
  86. * <p>
  87. * <b>Warning:</b> Does not preserve whitespace.
  88. *
  89. * @param content the input string
  90. * @return the string with escaped characters
  91. *
  92. * @see #convertToHTMLContentWithWhitespace(String) for use in browsers
  93. * @see #addPreFormatted(StringBuffer, String) for rendering with an {@link HTML2TextReader}
  94. */
  95. public static String convertToHTMLContent(String content) {
  96. content= replace(content, '&', "&amp;"); //$NON-NLS-1$
  97. content= replace(content, '"', "&quot;"); //$NON-NLS-1$
  98. content= replace(content, '<', "&lt;"); //$NON-NLS-1$
  99. return replace(content, '>', "&gt;"); //$NON-NLS-1$
  100. }
  101. /**
  102. * Escapes reserved HTML characters in the given string
  103. * and returns them in a way that preserves whitespace in a browser.
  104. * <p>
  105. * <b>Warning:</b> Whitespace will not be preserved when rendered with an {@link HTML2TextReader}
  106. * (e.g. in a {@link DefaultInformationControl} that renders simple HTML).
  107. * @param content the input string
  108. * @return the processed string
  109. *
  110. * @see #addPreFormatted(StringBuffer, String)
  111. * @see #convertToHTMLContent(String)
  112. * @since 3.7
  113. */
  114. public static String convertToHTMLContentWithWhitespace(String content) {
  115. content= replace(content, '&', "&amp;"); //$NON-NLS-1$
  116. content= replace(content, '"', "&quot;"); //$NON-NLS-1$
  117. content= replace(content, '<', "&lt;"); //$NON-NLS-1$
  118. content= replace(content, '>', "&gt;"); //$NON-NLS-1$
  119. return "<span style='white-space:pre'>" + content + "</span>"; //$NON-NLS-1$ //$NON-NLS-2$
  120. }
  121. public static String read(Reader rd) {
  122. StringBuffer buffer= new StringBuffer();
  123. char[] readBuffer= new char[2048];
  124. try {
  125. int n= rd.read(readBuffer);
  126. while (n > 0) {
  127. buffer.append(readBuffer, 0, n);
  128. n= rd.read(readBuffer);
  129. }
  130. return buffer.toString();
  131. } catch (IOException x) {
  132. }
  133. return null;
  134. }
  135. public static void insertPageProlog(StringBuffer buffer, int position, RGB fgRGB, RGB bgRGB, String styleSheet) {
  136. if (fgRGB == null)
  137. fgRGB= FG_COLOR_RGB;
  138. if (bgRGB == null)
  139. bgRGB= BG_COLOR_RGB;
  140. StringBuffer pageProlog= new StringBuffer(300);
  141. pageProlog.append("<html>"); //$NON-NLS-1$
  142. appendStyleSheet(pageProlog, styleSheet);
  143. appendColors(pageProlog, fgRGB, bgRGB);
  144. buffer.insert(position, pageProlog.toString());
  145. }
  146. private static void appendColors(StringBuffer pageProlog, RGB fgRGB, RGB bgRGB) {
  147. pageProlog.append("<body text=\""); //$NON-NLS-1$
  148. appendColor(pageProlog, fgRGB);
  149. pageProlog.append("\" bgcolor=\""); //$NON-NLS-1$
  150. appendColor(pageProlog, bgRGB);
  151. pageProlog.append("\">"); //$NON-NLS-1$
  152. }
  153. private static void appendColor(StringBuffer buffer, RGB rgb) {
  154. buffer.append('#');
  155. appendAsHexString(buffer, rgb.red);
  156. appendAsHexString(buffer, rgb.green);
  157. appendAsHexString(buffer, rgb.blue);
  158. }
  159. private static void appendAsHexString(StringBuffer buffer, int intValue) {
  160. String hexValue= Integer.toHexString(intValue);
  161. if (hexValue.length() == 1)
  162. buffer.append('0');
  163. buffer.append(hexValue);
  164. }
  165. public static void insertStyles(StringBuffer buffer, String[] styles) {
  166. if (styles == null || styles.length == 0)
  167. return;
  168. StringBuffer styleBuf= new StringBuffer(10 * styles.length);
  169. for (int i= 0; i < styles.length; i++) {
  170. styleBuf.append(" style=\""); //$NON-NLS-1$
  171. styleBuf.append(styles[i]);
  172. styleBuf.append('"');
  173. }
  174. // Find insertion index
  175. // a) within existing body tag with trailing space
  176. int index= buffer.indexOf("<body "); //$NON-NLS-1$
  177. if (index != -1) {
  178. buffer.insert(index+5, styleBuf);
  179. return;
  180. }
  181. // b) within existing body tag without attributes
  182. index= buffer.indexOf("<body>"); //$NON-NLS-1$
  183. if (index != -1) {
  184. buffer.insert(index+5, ' ');
  185. buffer.insert(index+6, styleBuf);
  186. return;
  187. }
  188. }
  189. private static void appendStyleSheet(StringBuffer buffer, String styleSheet) {
  190. if (styleSheet == null)
  191. return;
  192. // workaround for https://bugs.eclipse.org/318243
  193. StringBuffer fg= new StringBuffer();
  194. appendColor(fg, FG_COLOR_RGB);
  195. styleSheet= styleSheet.replaceAll("InfoText", fg.toString()); //$NON-NLS-1$
  196. StringBuffer bg= new StringBuffer();
  197. appendColor(bg, BG_COLOR_RGB);
  198. styleSheet= styleSheet.replaceAll("InfoBackground", bg.toString()); //$NON-NLS-1$
  199. buffer.append("<head><style CHARSET=\"ISO-8859-1\" TYPE=\"text/css\">"); //$NON-NLS-1$
  200. buffer.append(styleSheet);
  201. buffer.append("</style></head>"); //$NON-NLS-1$
  202. }
  203. private static void appendStyleSheetURL(StringBuffer buffer, URL styleSheetURL) {
  204. if (styleSheetURL == null)
  205. return;
  206. buffer.append("<head>"); //$NON-NLS-1$
  207. buffer.append("<LINK REL=\"stylesheet\" HREF= \""); //$NON-NLS-1$
  208. buffer.append(styleSheetURL);
  209. buffer.append("\" CHARSET=\"ISO-8859-1\" TYPE=\"text/css\">"); //$NON-NLS-1$
  210. buffer.append("</head>"); //$NON-NLS-1$
  211. }
  212. public static void insertPageProlog(StringBuffer buffer, int position) {
  213. StringBuffer pageProlog= new StringBuffer(60);
  214. pageProlog.append("<html>"); //$NON-NLS-1$
  215. appendColors(pageProlog, FG_COLOR_RGB, BG_COLOR_RGB);
  216. buffer.insert(position, pageProlog.toString());
  217. }
  218. public static void insertPageProlog(StringBuffer buffer, int position, URL styleSheetURL) {
  219. StringBuffer pageProlog= new StringBuffer(300);
  220. pageProlog.append("<html>"); //$NON-NLS-1$
  221. appendStyleSheetURL(pageProlog, styleSheetURL);
  222. appendColors(pageProlog, FG_COLOR_RGB, BG_COLOR_RGB);
  223. buffer.insert(position, pageProlog.toString());
  224. }
  225. public static void insertPageProlog(StringBuffer buffer, int position, String styleSheet) {
  226. insertPageProlog(buffer, position, null, null, styleSheet);
  227. }
  228. public static void addPageProlog(StringBuffer buffer) {
  229. insertPageProlog(buffer, buffer.length());
  230. }
  231. public static void addPageEpilog(StringBuffer buffer) {
  232. buffer.append("</body></html>"); //$NON-NLS-1$
  233. }
  234. public static void startBulletList(StringBuffer buffer) {
  235. buffer.append("<ul>"); //$NON-NLS-1$
  236. }
  237. public static void endBulletList(StringBuffer buffer) {
  238. buffer.append("</ul>"); //$NON-NLS-1$
  239. }
  240. public static void addBullet(StringBuffer buffer, String bullet) {
  241. if (bullet != null) {
  242. buffer.append("<li>"); //$NON-NLS-1$
  243. buffer.append(bullet);
  244. buffer.append("</li>"); //$NON-NLS-1$
  245. }
  246. }
  247. public static void addSmallHeader(StringBuffer buffer, String header) {
  248. if (header != null) {
  249. buffer.append("<h5>"); //$NON-NLS-1$
  250. buffer.append(header);
  251. buffer.append("</h5>"); //$NON-NLS-1$
  252. }
  253. }
  254. public static void addParagraph(StringBuffer buffer, String paragraph) {
  255. if (paragraph != null) {
  256. buffer.append("<p>"); //$NON-NLS-1$
  257. buffer.append(paragraph);
  258. }
  259. }
  260. /**
  261. * Appends a string and keeps its whitespace and newlines.
  262. * <p>
  263. * <b>Warning:</b> This starts a new paragraph when rendered in a browser, but
  264. * it doesn't starts a new paragraph when rendered with a {@link HTML2TextReader}
  265. * (e.g. in a {@link DefaultInformationControl} that renders simple HTML).
  266. *
  267. * @param buffer the output buffer
  268. * @param preFormatted the string that should be rendered with whitespace preserved
  269. *
  270. * @see #convertToHTMLContent(String)
  271. * @see #convertToHTMLContentWithWhitespace(String)
  272. * @since 3.7
  273. */
  274. public static void addPreFormatted(StringBuffer buffer, String preFormatted) {
  275. if (preFormatted != null) {
  276. buffer.append("<pre>"); //$NON-NLS-1$
  277. buffer.append(preFormatted);
  278. buffer.append("</pre>"); //$NON-NLS-1$
  279. }
  280. }
  281. public static void addParagraph(StringBuffer buffer, Reader paragraphReader) {
  282. if (paragraphReader != null)
  283. addParagraph(buffer, read(paragraphReader));
  284. }
  285. /**
  286. * Replaces the following style attributes of the font definition of the <code>html</code>
  287. * element:
  288. * <ul>
  289. * <li>font-size</li>
  290. * <li>font-weight</li>
  291. * <li>font-style</li>
  292. * <li>font-family</li>
  293. * </ul>
  294. * The font's name is used as font family, a <code>sans-serif</code> default font family is
  295. * appended for the case that the given font name is not available.
  296. * <p>
  297. * If the listed font attributes are not contained in the passed style list, nothing happens.
  298. * </p>
  299. *
  300. * @param styles CSS style definitions
  301. * @param fontData the font information to use
  302. * @return the modified style definitions
  303. * @since 3.3
  304. */
  305. public static String convertTopLevelFont(String styles, FontData fontData) {
  306. boolean bold= (fontData.getStyle() & SWT.BOLD) != 0;
  307. boolean italic= (fontData.getStyle() & SWT.ITALIC) != 0;
  308. String size= Integer.toString(fontData.getHeight()) + UNIT;
  309. String family= "'" + fontData.getName() + "',sans-serif"; //$NON-NLS-1$ //$NON-NLS-2$
  310. styles= styles.replaceFirst("(html\\s*\\{.*(?:\\s|;)font-size:\\s*)\\d+pt(\\;?.*\\})", "$1" + size + "$2"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
  311. styles= styles.replaceFirst("(html\\s*\\{.*(?:\\s|;)font-weight:\\s*)\\w+(\\;?.*\\})", "$1" + (bold ? "bold" : "normal") + "$2"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
  312. styles= styles.replaceFirst("(html\\s*\\{.*(?:\\s|;)font-style:\\s*)\\w+(\\;?.*\\})", "$1" + (italic ? "italic" : "normal") + "$2"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
  313. styles= styles.replaceFirst("(html\\s*\\{.*(?:\\s|;)font-family:\\s*).+?(;.*\\})", "$1" + family + "$2"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
  314. return styles;
  315. }
  316. }