Source Code Cross Referenced for NonBreakingSpaceFormat.java in  » Development » ivatamasks » com » ivata » mask » web » format » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Development » ivatamasks » com.ivata.mask.web.format 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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 (&amp;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 (&amp;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 (&amp;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 = "&nbsp;";
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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.