001: /*
002: * Copyright (c) 2001 - 2005 ivata limited.
003: * All rights reserved.
004: * -----------------------------------------------------------------------------
005: * ivata masks may be redistributed under the GNU General Public
006: * License as published by the Free Software Foundation;
007: * version 2 of the License.
008: *
009: * These programs are free software; you can redistribute them and/or
010: * modify them under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; version 2 of the License.
012: *
013: * These programs are distributed in the hope that they will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: *
017: * See the GNU General Public License in the file LICENSE.txt for more
018: * details.
019: *
020: * If you would like a copy of the GNU General Public License write to
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place - Suite 330
024: * Boston, MA 02111-1307, USA.
025: *
026: *
027: * To arrange commercial support and licensing, contact ivata at
028: * http://www.ivata.com/contact.jsp
029: * -----------------------------------------------------------------------------
030: * $Log: NonBreakingSpaceFormat.java,v $
031: * Revision 1.6 2005/10/03 10:17:25 colinmacleod
032: * Fixed some style and javadoc issues.
033: *
034: * Revision 1.5 2005/10/02 14:06:33 colinmacleod
035: * Added/improved log4j logging.
036: *
037: * Revision 1.4 2005/04/11 14:45:39 colinmacleod
038: * Changed HTMLFormat from an abstract class
039: * into an interface.
040: *
041: * Revision 1.3 2005/04/09 18:04:18 colinmacleod
042: * Changed copyright text to GPL v2 explicitly.
043: *
044: * Revision 1.2 2005/01/19 12:55:16 colinmacleod
045: * Fixed non breaking space string.
046: *
047: * Revision 1.1 2005/01/06 22:41:01 colinmacleod
048: * Moved up a version number.
049: * Changed copyright notices to 2005.
050: * Updated the documentation:
051: * - started working on multiproject:site docu.
052: * - changed the logo.
053: * Added checkstyle and fixed LOADS of style issues.
054: * Added separate thirdparty subproject.
055: * Added struts (in web), util and webgui (in webtheme) from ivata op.
056: *
057: * Revision 1.3 2004/03/21 21:16:37 colinmacleod
058: * Shortened name to ivata op.
059: *
060: * Revision 1.2 2004/02/01 22:07:32 colinmacleod
061: * Added full names to author tags
062: *
063: * Revision 1.1.1.1 2004/01/27 20:59:48 colinmacleod
064: * Moved ivata op to SourceForge.
065: *
066: * Revision 1.2 2003/10/15 14:13:39 colin
067: * Fixes for XDoclet.
068: *
069: * Revision 1.1 2003/02/24 19:33:33 colin
070: * Moved to new subproject.
071: *
072: * Revision 1.2 2003/02/04 17:43:46 colin
073: * copyright notice
074: *
075: * Revision 1.1 2002/06/21 11:58:37 colin
076: * restructured com.ivata.mask.web into separate sub-categories:
077: * format, JavaScript, theme and tree.
078: * -----------------------------------------------------------------------------
079: */
080: package com.ivata.mask.web.format;
081:
082: import org.apache.log4j.Logger;
083:
084: /**
085: * <p>
086: * Convert all spaces in a text to non-breaking spaces. You can choose whether
087: * all spaces are converted (default), or just those at the start of the line,
088: * by changing the value of the <code>style</code> field.
089: * </p>
090: *
091: * @since ivata masks 0.4 (2002-06-19)
092: * @author Colin MacLeod
093: * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
094: * @version $Revision: 1.6 $
095: */
096: public class NonBreakingSpaceFormat implements HTMLFormat {
097: /**
098: * <p>
099: * Use this flag to indicate that all spaces should be converted to
100: * non-breaking (&nbsp;)
101: * </p>
102: *
103: * <p>
104: * This flag represents the default setting.
105: * </p>
106: *
107: * @see #NONBREAKING_NONE
108: * @see #NONBREAKING_START_LINE
109: */
110: public static final int NONBREAKING_ALL = -1;
111: /**
112: * <p>
113: * Use this flag to indicate that all spaces should be left as they are and
114: * not converted to non-breaking (&nbsp;).
115: * </p>
116: *
117: * @see #NONBREAKING_START_LINE
118: * @see #NONBREAKING_ALL
119: */
120: public static final int NONBREAKING_NONE = 0;
121: /**
122: * <p>
123: * Use this flag to indicate that spaces at the start of a line should be
124: * converted to non-breaking (&nbsp;).
125: * </p>
126: *
127: * @see #NONBREAKING_NONE
128: * @see #NONBREAKING_ALL
129: */
130: public static final int NONBREAKING_START_LINE = 1;
131: /**
132: * Used to output debugging and developer info.
133: */
134: private static final Logger logger = Logger
135: .getLogger(NonBreakingSpaceFormat.class);
136: /**
137: * <p>
138: * Stores the style of non-breaking space conversion applied. By default all
139: * spaces will be converted.
140: * </p>
141: */
142: private int style = NONBREAKING_ALL;
143: /**
144: * Just what it says - an HTML non-breaking space. :-)
145: */
146: private static final String NON_BREAKING_SPACE = " ";
147:
148: /**
149: * <p>
150: * Format the string given in <code>hTMLText</code> to the maximum length
151: * provided by calling <code>setMaxLength</code>.
152: * </p>
153: *
154: * @param hTMLTextParam
155: * the text to truncate.
156: * @return <copyDoc>Refer to {@link HTMLFormat#format}.</copyDoc>
157: */
158: public final String format(final String hTMLTextParam) {
159: if (logger.isDebugEnabled()) {
160: logger.debug("format(String hTMLTextParam = "
161: + hTMLTextParam + ") - start");
162: }
163:
164: String hTMLText = hTMLTextParam;
165: // prerequisites: should we convert spaces to non-breaking?
166: if (style == NONBREAKING_NONE) {
167: if (logger.isDebugEnabled()) {
168: logger.debug("format(String) - end - return value = "
169: + hTMLText);
170: }
171: return hTMLText;
172: }
173: int index = 0;
174: while ((index = hTMLText.indexOf(' ', index)) != -1) {
175: if (index > 1) {
176: // if the non breaking spaces are only required at the start of
177: // the line, see if the previous character is a newline; ignore
178: // if it is not
179: if ((style != NONBREAKING_START_LINE)
180: || (hTMLText.charAt(index - 1) == '\n')) {
181: try {
182: while ((hTMLText.charAt(index) == ' ')
183: && (index < hTMLText.length())) {
184: hTMLText = hTMLText.substring(0, index)
185: + NON_BREAKING_SPACE
186: + hTMLText.substring(index + 1);
187: index += NON_BREAKING_SPACE.length();
188: }
189: } catch (StringIndexOutOfBoundsException eOutOfBounds) {
190: // this can happen: don't do anything
191: if (logger.isInfoEnabled()) {
192: logger.info(
193: "String out of bounds looking for "
194: + "non-breaking string.",
195: eOutOfBounds);
196: }
197: }
198: } else {
199: ++index;
200: }
201: } else {
202: hTMLText = NON_BREAKING_SPACE
203: + hTMLText.substring(++index);
204: }
205: }
206:
207: if (logger.isDebugEnabled()) {
208: logger.debug("format(String) - end - return value = "
209: + hTMLText);
210: }
211: return hTMLText;
212: }
213:
214: /**
215: * <p>
216: * Get the style of non-breaking space conversion applied. By default all
217: * spaces will be converted.
218: * </p>
219: *
220: * @return the current value of style.
221: */
222: public int getStyle() {
223: if (logger.isDebugEnabled()) {
224: logger.debug("getStyle() - start");
225: }
226:
227: if (logger.isDebugEnabled()) {
228: logger.debug("getStyle() - end - return value = " + style);
229: }
230: return style;
231: }
232:
233: /**
234: * <p>
235: * Set the style of non-breaking space conversion applied. By default all
236: * spaces will be converted.
237: * </p>
238: *
239: * <p>
240: * You should use one of the constant <code>NONBREAKING_...</code> values
241: * set in this class, as the setting of style.
242: * </p>
243: *
244: * @param styleParam
245: * the new value of style.
246: */
247: public final void setStyle(final int styleParam) {
248: if (logger.isDebugEnabled()) {
249: logger.debug("setStyle(int styleParam = " + styleParam
250: + ") - start");
251: }
252:
253: this .style = styleParam;
254:
255: if (logger.isDebugEnabled()) {
256: logger.debug("setStyle(int) - end");
257: }
258: }
259: }
|