Source Code Cross Referenced for ParserUtil.java in  » Graphic-Library » jcommon-components » org » jfree » xml » 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 » Graphic Library » jcommon components » org.jfree.xml 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* ========================================================================
002:         * JCommon : a free general purpose class library for the Java(tm) platform
003:         * ========================================================================
004:         *
005:         * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006:         * 
007:         * Project Info:  http://www.jfree.org/jcommon/index.html
008:         *
009:         * This library is free software; you can redistribute it and/or modify it 
010:         * under the terms of the GNU Lesser General Public License as published by 
011:         * the Free Software Foundation; either version 2.1 of the License, or 
012:         * (at your option) any later version.
013:         *
014:         * This library is distributed in the hope that it will be useful, but 
015:         * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
016:         * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
017:         * License for more details.
018:         *
019:         * You should have received a copy of the GNU Lesser General Public
020:         * License along with this library; if not, write to the Free Software
021:         * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
022:         * USA.  
023:         *
024:         * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
025:         * in the United States and other countries.]
026:         * 
027:         * ---------------
028:         * ParserUtil.java
029:         * ---------------
030:         * (C)opyright 2002-2005, by Thomas Morgner and Contributors.
031:         *
032:         * Original Author:  Thomas Morgner (taquera@sherito.org);
033:         * Contributor(s):   David Gilbert (for Object Refinery Limited);
034:         *
035:         * $Id: ParserUtil.java,v 1.3 2005/10/18 13:25:44 mungady Exp $
036:         *
037:         * Changes
038:         * -------
039:         * 21-May-2002 : Contains utility functions to make parsing easier.
040:         * 10-Dec-2002 : Fixed issues reported by Checkstyle (DG);
041:         * 29-Apr-2003 : Distilled from the JFreeReport project and moved into JCommon;
042:         * 23-Sep-2003 : Minor Javadoc updates (DG);
043:         *
044:         */
045:        package org.jfree.xml;
046:
047:        import java.awt.BasicStroke;
048:        import java.awt.Color;
049:        import java.awt.Stroke;
050:        import java.awt.geom.Rectangle2D;
051:        import java.lang.reflect.Field;
052:
053:        import org.xml.sax.Attributes;
054:        import org.xml.sax.SAXException;
055:
056:        /**
057:         * Basic helper functions to ease up the process of parsing.
058:         *
059:         * @author Thomas Morgner
060:         */
061:        public class ParserUtil {
062:
063:            /**
064:             * Parses the string <code>text</code> into an int. If text is null or does not
065:             * contain a parsable value, the message given in <code>message</code> is used to
066:             * throw a SAXException.
067:             *
068:             * @param text  the text to parse.
069:             * @param message  the error message if parsing fails.
070:             *
071:             * @return the int value.
072:             *
073:             * @throws SAXException if there is a problem with the parsing.
074:             */
075:            public static int parseInt(final String text, final String message)
076:                    throws SAXException {
077:                if (text == null) {
078:                    throw new SAXException(message);
079:                }
080:
081:                try {
082:                    return Integer.parseInt(text);
083:                } catch (NumberFormatException nfe) {
084:                    throw new SAXException("NumberFormatError: " + message);
085:                }
086:            }
087:
088:            /**
089:             * Parses an integer.
090:             *
091:             * @param text  the text to parse.
092:             * @param defaultVal  the default value.
093:             *
094:             * @return the integer.
095:             */
096:            public static int parseInt(final String text, final int defaultVal) {
097:                if (text == null) {
098:                    return defaultVal;
099:                }
100:
101:                try {
102:                    return Integer.parseInt(text);
103:                } catch (NumberFormatException nfe) {
104:                    return defaultVal;
105:                }
106:            }
107:
108:            /**
109:             * Parses the string <code>text</code> into an float. If text is null or does not
110:             * contain a parsable value, the message given in <code>message</code> is used to
111:             * throw a SAXException.
112:             *
113:             * @param text  the text to parse.
114:             * @param message  the error message if parsing fails.
115:             *
116:             * @return the float value.
117:             *
118:             * @throws SAXException if there is a problem with the parsing.
119:             */
120:            public static float parseFloat(final String text,
121:                    final String message) throws SAXException {
122:                if (text == null) {
123:                    throw new SAXException(message);
124:                }
125:                try {
126:                    return Float.parseFloat(text);
127:                } catch (NumberFormatException nfe) {
128:                    throw new SAXException("NumberFormatError: " + message);
129:                }
130:            }
131:
132:            /**
133:             * Parses the string <code>text</code> into an float. If text is null or does not
134:             * contain a parsable value, the message given in <code>message</code> is used to
135:             * throw a SAXException.
136:             *
137:             * @param text  the text to parse.
138:             * @param defaultVal the defaultValue returned if parsing fails.
139:             *
140:             * @return the float value.
141:             */
142:            public static float parseFloat(final String text,
143:                    final float defaultVal) {
144:                if (text == null) {
145:                    return defaultVal;
146:                }
147:                try {
148:                    return Float.parseFloat(text);
149:                } catch (NumberFormatException nfe) {
150:                    return defaultVal;
151:                }
152:            }
153:
154:            /**
155:             * Parses a boolean. If the string <code>text</code> contains the value of "true", the
156:             * true value is returned, else false is returned.
157:             *
158:             * @param text  the text to parse.
159:             * @param defaultVal  the default value.
160:             *
161:             * @return a boolean.
162:             */
163:            public static boolean parseBoolean(final String text,
164:                    final boolean defaultVal) {
165:                if (text == null) {
166:                    return defaultVal;
167:                }
168:                return text.equalsIgnoreCase("true");
169:            }
170:
171:            /**
172:             * Parses a string. If the <code>text</code> is null, defaultval is returned.
173:             *
174:             * @param text  the text to parse.
175:             * @param defaultVal  the default value.
176:             *
177:             * @return a string.
178:             */
179:            public static String parseString(final String text,
180:                    final String defaultVal) {
181:                if (text == null) {
182:                    return defaultVal;
183:                }
184:                return text;
185:            }
186:
187:            /**
188:             * Creates a basic stroke given the width contained as float in the given string.
189:             * If the string could not be parsed into a float, a basic stroke with the width of
190:             * 1 is returned.
191:             *
192:             * @param weight  a string containing a number (the stroke weight).
193:             *
194:             * @return the stroke.
195:             */
196:            public static Stroke parseStroke(final String weight) {
197:                try {
198:                    if (weight != null) {
199:                        final Float w = new Float(weight);
200:                        return new BasicStroke(w.floatValue());
201:                    }
202:                } catch (NumberFormatException nfe) {
203:                    //Log.warn("Invalid weight for stroke", nfe);
204:                }
205:                return new BasicStroke(1);
206:            }
207:
208:            /**
209:             * Parses a color entry. If the entry is in hexadecimal or ocal notation, the color is
210:             * created using Color.decode(). If the string denotes a constant name of on of the color
211:             * constants defined in java.awt.Color, this constant is used.
212:             * <p>
213:             * As fallback the color black is returned if no color can be parsed.
214:             *
215:             * @param color  the color (as a string).
216:             *
217:             * @return the paint.
218:             */
219:            public static Color parseColor(final String color) {
220:                return parseColor(color, Color.black);
221:            }
222:
223:            /**
224:             * Parses a color entry. If the entry is in hexadecimal or octal notation, the color is
225:             * created using Color.decode(). If the string denotes a constant name of one of the color
226:             * constants defined in java.awt.Color, this constant is used.
227:             * <p>
228:             * As fallback the supplied default value is returned if no color can be parsed.
229:             *
230:             * @param color  the color (as a string).
231:             * @param defaultValue  the default value (returned if no color can be parsed).
232:             *
233:             * @return the paint.
234:             */
235:            public static Color parseColor(final String color,
236:                    final Color defaultValue) {
237:                if (color == null) {
238:                    return defaultValue;
239:                }
240:                try {
241:                    // get color by hex or octal value
242:                    return Color.decode(color);
243:                } catch (NumberFormatException nfe) {
244:                    // if we can't decode lets try to get it by name
245:                    try {
246:                        // try to get a color by name using reflection
247:                        // black is used for an instance and not for the color itselfs
248:                        final Field f = Color.class.getField(color);
249:
250:                        return (Color) f.get(null);
251:                    } catch (Exception ce) {
252:                        //Log.warn("No such Color : " + color);
253:                        // if we can't get any color return black
254:                        return defaultValue;
255:                    }
256:                }
257:            }
258:
259:            /**
260:             * Parses a position of an element. If a relative postion is given, the returnvalue
261:             * is a negative number between 0 and -100.
262:             *
263:             * @param value  the value.
264:             * @param exceptionMessage  the exception message.
265:             *
266:             * @return the float value.
267:             *
268:             * @throws SAXException if there is a problem parsing the string.
269:             */
270:            public static float parseRelativeFloat(final String value,
271:                    final String exceptionMessage) throws SAXException {
272:                if (value == null) {
273:                    throw new SAXException(exceptionMessage);
274:                }
275:                final String tvalue = value.trim();
276:                if (tvalue.endsWith("%")) {
277:                    final String number = tvalue.substring(0, tvalue
278:                            .indexOf("%"));
279:                    final float f = parseFloat(number, exceptionMessage)
280:                            * -1.0f;
281:                    return f;
282:                } else {
283:                    return parseFloat(tvalue, exceptionMessage);
284:                }
285:            }
286:
287:            /**
288:             * Parses an element position. The position is stored in the attributes "x", "y", "width" and
289:             * "height". The attributes are allowed to have relative notion.
290:             *
291:             * @param atts  the attributes.
292:             *
293:             * @return the element position.
294:             *
295:             * @throws SAXException if there is a problem getting the element position.
296:             */
297:            public static Rectangle2D getElementPosition(final Attributes atts)
298:                    throws SAXException {
299:                final float x = ParserUtil.parseRelativeFloat(atts
300:                        .getValue("x"), "Element x not specified");
301:                final float y = ParserUtil.parseRelativeFloat(atts
302:                        .getValue("y"), "Element y not specified");
303:                final float w = ParserUtil.parseRelativeFloat(atts
304:                        .getValue("width"), "Element width not specified");
305:                final float h = ParserUtil.parseRelativeFloat(atts
306:                        .getValue("height"), "Element height not specified");
307:                final Rectangle2D.Float retval = new Rectangle2D.Float(x, y, w,
308:                        h);
309:                return retval;
310:            }
311:
312:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.