1 /*
2 * Copyright (c) 2001 - 2005 ivata limited.
3 * All rights reserved.
4 * -----------------------------------------------------------------------------
5 * ivata masks may be redistributed under the GNU General Public
6 * License as published by the Free Software Foundation;
7 * version 2 of the License.
8 *
9 * These programs are free software; you can redistribute them and/or
10 * modify them under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; version 2 of the License.
12 *
13 * These programs are distributed in the hope that they will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 *
17 * See the GNU General Public License in the file LICENSE.txt for more
18 * details.
19 *
20 * If you would like a copy of the GNU General Public License write to
21 *
22 * Free Software Foundation, Inc.
23 * 59 Temple Place - Suite 330
24 * Boston, MA 02111-1307, USA.
25 *
26 *
27 * To arrange commercial support and licensing, contact ivata at
28 * http://www.ivata.com/contact.jsp
29 * -----------------------------------------------------------------------------
30 * $Log: NonBreakingSpaceFormat.java,v $
31 * Revision 1.4 2005/04/11 14:45:39 colinmacleod
32 * Changed HTMLFormat from an abstract class
33 * into an interface.
34 *
35 * Revision 1.3 2005/04/09 18:04:18 colinmacleod
36 * Changed copyright text to GPL v2 explicitly.
37 *
38 * Revision 1.2 2005/01/19 12:55:16 colinmacleod
39 * Fixed non breaking space string.
40 *
41 * Revision 1.1 2005/01/06 22:41:01 colinmacleod
42 * Moved up a version number.
43 * Changed copyright notices to 2005.
44 * Updated the documentation:
45 * - started working on multiproject:site docu.
46 * - changed the logo.
47 * Added checkstyle and fixed LOADS of style issues.
48 * Added separate thirdparty subproject.
49 * Added struts (in web), util and webgui (in webtheme) from ivata op.
50 *
51 * Revision 1.3 2004/03/21 21:16:37 colinmacleod
52 * Shortened name to ivata op.
53 *
54 * Revision 1.2 2004/02/01 22:07:32 colinmacleod
55 * Added full names to author tags
56 *
57 * Revision 1.1.1.1 2004/01/27 20:59:48 colinmacleod
58 * Moved ivata op to SourceForge.
59 *
60 * Revision 1.2 2003/10/15 14:13:39 colin
61 * Fixes for XDoclet.
62 *
63 * Revision 1.1 2003/02/24 19:33:33 colin
64 * Moved to new subproject.
65 *
66 * Revision 1.2 2003/02/04 17:43:46 colin
67 * copyright notice
68 *
69 * Revision 1.1 2002/06/21 11:58:37 colin
70 * restructured com.ivata.mask.web into separate sub-categories:
71 * format, JavaScript, theme and tree.
72 * -----------------------------------------------------------------------------
73 */
74 package com.ivata.mask.web.format;
75 import org.apache.log4j.Logger;
76 /***
77 * <p>
78 * Convert all spaces in a text to non-breaking spaces. You can choose whether
79 * all spaces are converted (default), or just those at the start of the line,
80 * by changing the value of the <code>style</code> field.
81 * </p>
82 *
83 * @since ivata masks 0.4 (2002-06-19)
84 * @author Colin MacLeod
85 * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
86 * @version $Revision: 1.4 $
87 */
88 public class NonBreakingSpaceFormat implements HTMLFormat {
89 /***
90 * <p>
91 * Use this flag to indicate that all spaces should be converted to
92 * non-breaking (&nbsp;)
93 * </p>
94 *
95 * <p>
96 * This flag represents the default setting.
97 * </p>
98 *
99 * @see #NONBREAKING_NONE
100 * @see #NONBREAKING_START_LINE
101 */
102 public static final int NONBREAKING_ALL = -1;
103 /***
104 * <p>
105 * Use this flag to indicate that all spaces should be left as they are and
106 * not converted to non-breaking (&nbsp;).
107 * </p>
108 *
109 * @see #NONBREAKING_START_LINE
110 * @see #NONBREAKING_ALL
111 */
112 public static final int NONBREAKING_NONE = 0;
113 /***
114 * <p>
115 * Use this flag to indicate that spaces at the start of a line should be
116 * converted to non-breaking (&nbsp;).
117 * </p>
118 *
119 * @see #NONBREAKING_NONE
120 * @see #NONBREAKING_ALL
121 */
122 public static final int NONBREAKING_START_LINE = 1;
123 /***
124 * Used to output debugging and developer info.
125 */
126 private static final Logger LOG = Logger
127 .getLogger(NonBreakingSpaceFormat.class);
128 /***
129 * <p>
130 * Stores the style of non-breaking space conversion applied. By default all
131 * spaces will be converted.
132 * </p>
133 */
134 private int style = NONBREAKING_ALL;
135 /***
136 * Just what it says - an HTML non-breaking space. :-)
137 */
138 private static final String NON_BREAKING_SPACE = " ";
139 /***
140 * <p>
141 * Format the string given in <code>hTMLText</code> to the maximum length
142 * provided by calling <code>setMaxLength</code>.
143 * </p>
144 *
145 * @param hTMLTextParam
146 * the text to truncate.
147 * @return Refer to {@link HTMLFormat#format}.
148 */
149 public final String format(final String hTMLTextParam) {
150 String hTMLText = hTMLTextParam;
151 // prerequisites: should we convert spaces to non-breaking?
152 if (style == NONBREAKING_NONE) {
153 return hTMLText;
154 }
155 int index = 0;
156 while ((index = hTMLText.indexOf(' ', index)) != -1) {
157 if (index > 1) {
158 // if the non breaking spaces are only required at the start of
159 // the line, see if the previous character is a newline; ignore
160 // if it is not
161 if ((style != NONBREAKING_START_LINE)
162 || (hTMLText.charAt(index - 1) == '\n')) {
163 try {
164 while ((hTMLText.charAt(index) == ' ')
165 && (index < hTMLText.length())) {
166 hTMLText = hTMLText.substring(0, index)
167 + NON_BREAKING_SPACE
168 + hTMLText.substring(index + 1);
169 index += NON_BREAKING_SPACE.length();
170 }
171 } catch (StringIndexOutOfBoundsException eOutOfBounds) {
172 // this can happen: don't do anything
173 if (LOG.isInfoEnabled()) {
174 LOG.info("String out of bounds looking for "
175 + "non-breaking string.", eOutOfBounds);
176 }
177 }
178 } else {
179 ++index;
180 }
181 } else {
182 hTMLText = NON_BREAKING_SPACE + hTMLText.substring(++index);
183 }
184 }
185 return hTMLText;
186 }
187 /***
188 * <p>
189 * Get the style of non-breaking space conversion applied. By default all
190 * spaces will be converted.
191 * </p>
192 *
193 * @return the current value of style.
194 */
195 public int getStyle() {
196 return style;
197 }
198 /***
199 * <p>
200 * Set the style of non-breaking space conversion applied. By default all
201 * spaces will be converted.
202 * </p>
203 *
204 * <p>
205 * You should use one of the constant <code>NONBREAKING_...</code> values
206 * set in this class, as the setting of style.
207 * </p>
208 *
209 * @param styleParam
210 * the new value of style.
211 */
212 public final void setStyle(final int styleParam) {
213 this.style = styleParam;
214 }
215 }
216