Source Code Cross Referenced for XNumber.java in  » XML » xalan » org » apache » xpath » objects » 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 » XML » xalan » org.apache.xpath.objects 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 1999-2004 The Apache Software Foundation.
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *     http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:        /*
017:         * $Id: XNumber.java,v 1.21 2005/01/23 01:08:21 mcnamara Exp $
018:         */
019:        package org.apache.xpath.objects;
020:
021:        import org.apache.xpath.ExpressionOwner;
022:        import org.apache.xpath.XPathContext;
023:        import org.apache.xpath.XPathVisitor;
024:
025:        /**
026:         * This class represents an XPath number, and is capable of
027:         * converting the number to other types, such as a string.
028:         * @xsl.usage general
029:         */
030:        public class XNumber extends XObject {
031:            static final long serialVersionUID = -2720400709619020193L;
032:
033:            /** Value of the XNumber object.
034:             *  @serial         */
035:            double m_val;
036:
037:            /**
038:             * Construct a XNodeSet object.
039:             *
040:             * @param d Value of the object
041:             */
042:            public XNumber(double d) {
043:                super ();
044:
045:                m_val = d;
046:            }
047:
048:            /**
049:             * Construct a XNodeSet object.
050:             *
051:             * @param num Value of the object
052:             */
053:            public XNumber(Number num) {
054:
055:                super ();
056:
057:                m_val = num.doubleValue();
058:                m_obj = num;
059:            }
060:
061:            /**
062:             * Tell that this is a CLASS_NUMBER.
063:             *
064:             * @return node type CLASS_NUMBER 
065:             */
066:            public int getType() {
067:                return CLASS_NUMBER;
068:            }
069:
070:            /**
071:             * Given a request type, return the equivalent string.
072:             * For diagnostic purposes.
073:             *
074:             * @return type string "#NUMBER" 
075:             */
076:            public String getTypeString() {
077:                return "#NUMBER";
078:            }
079:
080:            /**
081:             * Cast result object to a number.
082:             *
083:             * @return the value of the XNumber object
084:             */
085:            public double num() {
086:                return m_val;
087:            }
088:
089:            /**
090:             * Evaluate expression to a number.
091:             *
092:             * @return 0.0
093:             *
094:             * @throws javax.xml.transform.TransformerException
095:             */
096:            public double num(XPathContext xctxt)
097:                    throws javax.xml.transform.TransformerException {
098:
099:                return m_val;
100:            }
101:
102:            /**
103:             * Cast result object to a boolean.
104:             *
105:             * @return false if the value is NaN or equal to 0.0
106:             */
107:            public boolean bool() {
108:                return (Double.isNaN(m_val) || (m_val == 0.0)) ? false : true;
109:            }
110:
111:            //  /**
112:            //   * Cast result object to a string.
113:            //   *
114:            //   * @return "NaN" if the number is NaN, Infinity or -Infinity if
115:            //   * the number is infinite or the string value of the number.
116:            //   */
117:            //  private static final int PRECISION = 16;
118:            //  public String str()
119:            //  {
120:            //
121:            //    if (Double.isNaN(m_val))
122:            //    {
123:            //      return "NaN";
124:            //    }
125:            //    else if (Double.isInfinite(m_val))
126:            //    {
127:            //      if (m_val > 0)
128:            //        return "Infinity";
129:            //      else
130:            //        return "-Infinity";
131:            //    }
132:            //
133:            //    long longVal = (long)m_val;
134:            //    if ((double)longVal == m_val)
135:            //      return Long.toString(longVal);
136:            //
137:            //
138:            //    String s = Double.toString(m_val);
139:            //    int len = s.length();
140:            //
141:            //    if (s.charAt(len - 2) == '.' && s.charAt(len - 1) == '0')
142:            //    {
143:            //      return s.substring(0, len - 2);
144:            //    }
145:            //
146:            //    int exp = 0;
147:            //    int e = s.indexOf('E');
148:            //    if (e != -1)
149:            //    {
150:            //      exp = Integer.parseInt(s.substring(e + 1));
151:            //      s = s.substring(0,e);
152:            //      len = e;
153:            //    }
154:            //
155:            //    // Calculate Significant Digits:
156:            //    // look from start of string for first digit
157:            //    // look from end for last digit
158:            //    // significant digits = end - start + (0 or 1 depending on decimal location)
159:            //
160:            //    int decimalPos = -1;
161:            //    int start = (s.charAt(0) == '-') ? 1 : 0;
162:            //    findStart: for( ; start < len; start++ )
163:            //    {
164:            //      switch (s.charAt(start))
165:            //      {
166:            //      case '0':
167:            //        break;
168:            //      case '.':
169:            //        decimalPos = start;
170:            //        break;
171:            //      default:
172:            //        break findStart;
173:            //      }
174:            //    }
175:            //    int end = s.length() - 1;
176:            //    findEnd: for( ; end > start; end-- )
177:            //    {
178:            //      switch (s.charAt(end))
179:            //      {
180:            //      case '0':
181:            //        break;
182:            //      case '.':
183:            //        decimalPos = end;
184:            //        break;
185:            //      default:
186:            //        break findEnd;
187:            //      }
188:            //    }
189:            //
190:            //    int sigDig = end - start;
191:            //
192:            //    // clarify decimal location if it has not yet been found
193:            //    if (decimalPos == -1)
194:            //      decimalPos = s.indexOf('.');
195:            //
196:            //    // if decimal is not between start and end, add one to sigDig
197:            //    if (decimalPos < start || decimalPos > end)
198:            //      ++sigDig;
199:            //
200:            //    // reduce significant digits to PRECISION if necessary
201:            //    if (sigDig > PRECISION)
202:            //    {
203:            //      // re-scale BigDecimal in order to get significant digits = PRECISION
204:            //      BigDecimal num = new BigDecimal(s);
205:            //      int newScale = num.scale() - (sigDig - PRECISION);
206:            //      if (newScale < 0)
207:            //        newScale = 0;
208:            //      s = num.setScale(newScale, BigDecimal.ROUND_HALF_UP).toString();
209:            //
210:            //      // remove trailing '0's; keep track of decimalPos
211:            //      int truncatePoint = s.length();
212:            //      while (s.charAt(--truncatePoint) == '0')
213:            //        ;
214:            //
215:            //      if (s.charAt(truncatePoint) == '.')
216:            //      {
217:            //        decimalPos = truncatePoint;
218:            //      }
219:            //      else
220:            //      {
221:            //        decimalPos = s.indexOf('.');
222:            //        truncatePoint += 1;
223:            //      }
224:            //
225:            //      s = s.substring(0, truncatePoint);
226:            //      len = s.length();
227:            //    }
228:            //
229:            //    // Account for exponent by adding zeros as needed 
230:            //    // and moving the decimal place
231:            //
232:            //    if (exp == 0)
233:            //       return s;
234:            //
235:            //    start = 0;
236:            //    String sign;
237:            //    if (s.charAt(0) == '-')
238:            //    {
239:            //      sign = "-";
240:            //      start++;
241:            //    }
242:            //    else
243:            //      sign = "";
244:            //
245:            //    String wholePart = s.substring(start, decimalPos);
246:            //    String decimalPart = s.substring(decimalPos + 1);
247:            //
248:            //    // get the number of digits right of the decimal
249:            //    int decimalLen = decimalPart.length();
250:            //
251:            //    if (exp >= decimalLen)
252:            //      return sign + wholePart + decimalPart + zeros(exp - decimalLen);
253:            //
254:            //    if (exp > 0)
255:            //      return sign + wholePart + decimalPart.substring(0, exp) + "."
256:            //             + decimalPart.substring(exp);
257:            //
258:            //    return sign + "0." + zeros(-1 - exp) + wholePart + decimalPart;
259:            //  }
260:
261:            /**
262:             * Cast result object to a string.
263:             *
264:             * @return "NaN" if the number is NaN, Infinity or -Infinity if
265:             * the number is infinite or the string value of the number.
266:             */
267:            public String str() {
268:
269:                if (Double.isNaN(m_val)) {
270:                    return "NaN";
271:                } else if (Double.isInfinite(m_val)) {
272:                    if (m_val > 0)
273:                        return "Infinity";
274:                    else
275:                        return "-Infinity";
276:                }
277:
278:                double num = m_val;
279:                String s = Double.toString(num);
280:                int len = s.length();
281:
282:                if (s.charAt(len - 2) == '.' && s.charAt(len - 1) == '0') {
283:                    s = s.substring(0, len - 2);
284:
285:                    if (s.equals("-0"))
286:                        return "0";
287:
288:                    return s;
289:                }
290:
291:                int e = s.indexOf('E');
292:
293:                if (e < 0) {
294:                    if (s.charAt(len - 1) == '0')
295:                        return s.substring(0, len - 1);
296:                    else
297:                        return s;
298:                }
299:
300:                int exp = Integer.parseInt(s.substring(e + 1));
301:                String sign;
302:
303:                if (s.charAt(0) == '-') {
304:                    sign = "-";
305:                    s = s.substring(1);
306:
307:                    --e;
308:                } else
309:                    sign = "";
310:
311:                int nDigits = e - 2;
312:
313:                if (exp >= nDigits)
314:                    return sign + s.substring(0, 1) + s.substring(2, e)
315:                            + zeros(exp - nDigits);
316:
317:                // Eliminate trailing 0's - bugzilla 14241
318:                while (s.charAt(e - 1) == '0')
319:                    e--;
320:
321:                if (exp > 0)
322:                    return sign + s.substring(0, 1) + s.substring(2, 2 + exp)
323:                            + "." + s.substring(2 + exp, e);
324:
325:                return sign + "0." + zeros(-1 - exp) + s.substring(0, 1)
326:                        + s.substring(2, e);
327:            }
328:
329:            /**
330:             * Return a string of '0' of the given length
331:             *
332:             *
333:             * @param n Length of the string to be returned
334:             *
335:             * @return a string of '0' with the given length
336:             */
337:            static private String zeros(int n) {
338:                if (n < 1)
339:                    return "";
340:
341:                char[] buf = new char[n];
342:
343:                for (int i = 0; i < n; i++) {
344:                    buf[i] = '0';
345:                }
346:
347:                return new String(buf);
348:            }
349:
350:            /**
351:             * Return a java object that's closest to the representation
352:             * that should be handed to an extension.
353:             *
354:             * @return The value of this XNumber as a Double object
355:             */
356:            public Object object() {
357:                if (null == m_obj)
358:                    m_obj = new Double(m_val);
359:                return m_obj;
360:            }
361:
362:            /**
363:             * Tell if two objects are functionally equal.
364:             *
365:             * @param obj2 Object to compare this to
366:             *
367:             * @return true if the two objects are equal 
368:             *
369:             * @throws javax.xml.transform.TransformerException
370:             */
371:            public boolean equals(XObject obj2) {
372:
373:                // In order to handle the 'all' semantics of 
374:                // nodeset comparisons, we always call the 
375:                // nodeset function.
376:                int t = obj2.getType();
377:                try {
378:                    if (t == XObject.CLASS_NODESET)
379:                        return obj2.equals(this );
380:                    else if (t == XObject.CLASS_BOOLEAN)
381:                        return obj2.bool() == bool();
382:                    else
383:                        return m_val == obj2.num();
384:                } catch (javax.xml.transform.TransformerException te) {
385:                    throw new org.apache.xml.utils.WrappedRuntimeException(te);
386:                }
387:            }
388:
389:            /**
390:             * Tell if this expression returns a stable number that will not change during 
391:             * iterations within the expression.  This is used to determine if a proximity 
392:             * position predicate can indicate that no more searching has to occur.
393:             * 
394:             *
395:             * @return true if the expression represents a stable number.
396:             */
397:            public boolean isStableNumber() {
398:                return true;
399:            }
400:
401:            /**
402:             * @see org.apache.xpath.XPathVisitable#callVisitors(ExpressionOwner, XPathVisitor)
403:             */
404:            public void callVisitors(ExpressionOwner owner, XPathVisitor visitor) {
405:                visitor.visitNumberLiteral(owner, this);
406:            }
407:
408:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.