Source Code Cross Referenced for XStringForFSB.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: XStringForFSB.java,v 1.24 2005/01/23 01:08:21 mcnamara Exp $
018:         */
019:        package org.apache.xpath.objects;
020:
021:        import org.apache.xalan.res.XSLMessages;
022:        import org.apache.xml.utils.FastStringBuffer;
023:        import org.apache.xml.utils.XMLCharacterRecognizer;
024:        import org.apache.xml.utils.XMLString;
025:        import org.apache.xml.utils.XMLStringFactory;
026:        import org.apache.xpath.res.XPATHErrorResources;
027:
028:        /**
029:         * This class will wrap a FastStringBuffer and allow for
030:         */
031:        public class XStringForFSB extends XString {
032:            static final long serialVersionUID = -1533039186550674548L;
033:
034:            /** The start position in the fsb. */
035:            int m_start;
036:
037:            /** The length of the string. */
038:            int m_length;
039:
040:            /** If the str() function is called, the string will be cached here. */
041:            protected String m_strCache = null;
042:
043:            /** cached hash code */
044:            protected int m_hash = 0;
045:
046:            /**
047:             * Construct a XNodeSet object.
048:             *
049:             * @param val FastStringBuffer object this will wrap, must be non-null.
050:             * @param start The start position in the array.
051:             * @param length The number of characters to read from the array.
052:             */
053:            public XStringForFSB(FastStringBuffer val, int start, int length) {
054:
055:                super (val);
056:
057:                m_start = start;
058:                m_length = length;
059:
060:                if (null == val)
061:                    throw new IllegalArgumentException(
062:                            XSLMessages
063:                                    .createXPATHMessage(
064:                                            XPATHErrorResources.ER_FASTSTRINGBUFFER_CANNOT_BE_NULL,
065:                                            null));
066:            }
067:
068:            /**
069:             * Construct a XNodeSet object.
070:             *
071:             * @param val String object this will wrap.
072:             */
073:            private XStringForFSB(String val) {
074:
075:                super (val);
076:
077:                throw new IllegalArgumentException(XSLMessages
078:                        .createXPATHMessage(
079:                                XPATHErrorResources.ER_FSB_CANNOT_TAKE_STRING,
080:                                null)); // "XStringForFSB can not take a string for an argument!");
081:            }
082:
083:            /**
084:             * Cast result object to a string.
085:             *
086:             * @return The string this wraps or the empty string if null
087:             */
088:            public FastStringBuffer fsb() {
089:                return ((FastStringBuffer) m_obj);
090:            }
091:
092:            /**
093:             * Cast result object to a string.
094:             *
095:             * @return The string this wraps or the empty string if null
096:             */
097:            public void appendToFsb(org.apache.xml.utils.FastStringBuffer fsb) {
098:                // %OPT% !!! FSB has to be updated to take partial fsb's for append.
099:                fsb.append(str());
100:            }
101:
102:            /**
103:             * Tell if this object contains a java String object.
104:             *
105:             * @return true if this XMLString can return a string without creating one.
106:             */
107:            public boolean hasString() {
108:                return (null != m_strCache);
109:            }
110:
111:            //  /** NEEDSDOC Field strCount */
112:            //  public static int strCount = 0;
113:            //
114:            //  /** NEEDSDOC Field xtable */
115:            //  static java.util.Hashtable xtable = new java.util.Hashtable();
116:
117:            /**
118:             * Since this object is incomplete without the length and the offset, we 
119:             * have to convert to a string when this function is called.
120:             *
121:             * @return The java String representation of this object.
122:             */
123:            public Object object() {
124:                return str();
125:            }
126:
127:            /**
128:             * Cast result object to a string.
129:             *
130:             * @return The string this wraps or the empty string if null
131:             */
132:            public String str() {
133:
134:                if (null == m_strCache) {
135:                    m_strCache = fsb().getString(m_start, m_length);
136:
137:                    //      strCount++;
138:                    //
139:                    //      RuntimeException e = new RuntimeException("Bad!  Bad!");
140:                    //      java.io.CharArrayWriter writer = new java.io.CharArrayWriter();
141:                    //      java.io.PrintWriter pw = new java.io.PrintWriter(writer);
142:                    //
143:                    //      e.printStackTrace(pw);
144:                    //
145:                    //      String str = writer.toString();
146:                    //
147:                    //      str = str.substring(0, 600);
148:                    //
149:                    //      if (null == xtable.get(str))
150:                    //      {
151:                    //        xtable.put(str, str);
152:                    //        System.out.println(str);
153:                    //      }
154:                    //      System.out.println("strCount: " + strCount);
155:
156:                    //      throw e;
157:                    //      e.printStackTrace();
158:                    // System.exit(-1);
159:                }
160:
161:                return m_strCache;
162:            }
163:
164:            /**
165:             * Directly call the
166:             * characters method on the passed ContentHandler for the
167:             * string-value. Multiple calls to the
168:             * ContentHandler's characters methods may well occur for a single call to
169:             * this method.
170:             *
171:             * @param ch A non-null reference to a ContentHandler.
172:             *
173:             * @throws org.xml.sax.SAXException
174:             */
175:            public void dispatchCharactersEvents(org.xml.sax.ContentHandler ch)
176:                    throws org.xml.sax.SAXException {
177:                fsb().sendSAXcharacters(ch, m_start, m_length);
178:            }
179:
180:            /**
181:             * Directly call the
182:             * comment method on the passed LexicalHandler for the
183:             * string-value.
184:             *
185:             * @param lh A non-null reference to a LexicalHandler.
186:             *
187:             * @throws org.xml.sax.SAXException
188:             */
189:            public void dispatchAsComment(org.xml.sax.ext.LexicalHandler lh)
190:                    throws org.xml.sax.SAXException {
191:                fsb().sendSAXComment(lh, m_start, m_length);
192:            }
193:
194:            /**
195:             * Returns the length of this string.
196:             *
197:             * @return  the length of the sequence of characters represented by this
198:             *          object.
199:             */
200:            public int length() {
201:                return m_length;
202:            }
203:
204:            /**
205:             * Returns the character at the specified index. An index ranges
206:             * from <code>0</code> to <code>length() - 1</code>. The first character
207:             * of the sequence is at index <code>0</code>, the next at index
208:             * <code>1</code>, and so on, as for array indexing.
209:             *
210:             * @param      index   the index of the character.
211:             * @return     the character at the specified index of this string.
212:             *             The first character is at index <code>0</code>.
213:             * @exception  IndexOutOfBoundsException  if the <code>index</code>
214:             *             argument is negative or not less than the length of this
215:             *             string.
216:             */
217:            public char charAt(int index) {
218:                return fsb().charAt(m_start + index);
219:            }
220:
221:            /**
222:             * Copies characters from this string into the destination character
223:             * array.
224:             *
225:             * @param      srcBegin   index of the first character in the string
226:             *                        to copy.
227:             * @param      srcEnd     index after the last character in the string
228:             *                        to copy.
229:             * @param      dst        the destination array.
230:             * @param      dstBegin   the start offset in the destination array.
231:             * @exception IndexOutOfBoundsException If any of the following
232:             *            is true:
233:             *            <ul><li><code>srcBegin</code> is negative.
234:             *            <li><code>srcBegin</code> is greater than <code>srcEnd</code>
235:             *            <li><code>srcEnd</code> is greater than the length of this
236:             *                string
237:             *            <li><code>dstBegin</code> is negative
238:             *            <li><code>dstBegin+(srcEnd-srcBegin)</code> is larger than
239:             *                <code>dst.length</code></ul>
240:             * @exception NullPointerException if <code>dst</code> is <code>null</code>
241:             */
242:            public void getChars(int srcBegin, int srcEnd, char dst[],
243:                    int dstBegin) {
244:
245:                // %OPT% Need to call this on FSB when it is implemented.
246:                // %UNTESTED% (I don't think anyone calls this yet?)
247:                int n = srcEnd - srcBegin;
248:
249:                if (n > m_length)
250:                    n = m_length;
251:
252:                if (n > (dst.length - dstBegin))
253:                    n = (dst.length - dstBegin);
254:
255:                int end = srcBegin + m_start + n;
256:                int d = dstBegin;
257:                FastStringBuffer fsb = fsb();
258:
259:                for (int i = srcBegin + m_start; i < end; i++) {
260:                    dst[d++] = fsb.charAt(i);
261:                }
262:            }
263:
264:            /**
265:             * Compares this string to the specified object.
266:             * The result is <code>true</code> if and only if the argument is not
267:             * <code>null</code> and is a <code>String</code> object that represents
268:             * the same sequence of characters as this object.
269:             *
270:             * @param   obj2       the object to compare this <code>String</code>
271:             *                     against.
272:             *
273:             * @return  <code>true</code> if the <code>String </code>are equal;
274:             *          <code>false</code> otherwise.
275:             * @see     java.lang.String#compareTo(java.lang.String)
276:             * @see     java.lang.String#equalsIgnoreCase(java.lang.String)
277:             */
278:            public boolean equals(XMLString obj2) {
279:
280:                if (this  == obj2) {
281:                    return true;
282:                }
283:
284:                int n = m_length;
285:
286:                if (n == obj2.length()) {
287:                    FastStringBuffer fsb = fsb();
288:                    int i = m_start;
289:                    int j = 0;
290:
291:                    while (n-- != 0) {
292:                        if (fsb.charAt(i) != obj2.charAt(j)) {
293:                            return false;
294:                        }
295:
296:                        i++;
297:                        j++;
298:                    }
299:
300:                    return true;
301:                }
302:
303:                return false;
304:            }
305:
306:            /**
307:             * Tell if two objects are functionally equal.
308:             *
309:             * @param obj2 Object to compare this to
310:             *
311:             * @return true if the two objects are equal
312:             *
313:             * @throws javax.xml.transform.TransformerException
314:             */
315:            public boolean equals(XObject obj2) {
316:
317:                if (this  == obj2) {
318:                    return true;
319:                }
320:                if (obj2.getType() == XObject.CLASS_NUMBER)
321:                    return obj2.equals(this );
322:
323:                String str = obj2.str();
324:                int n = m_length;
325:
326:                if (n == str.length()) {
327:                    FastStringBuffer fsb = fsb();
328:                    int i = m_start;
329:                    int j = 0;
330:
331:                    while (n-- != 0) {
332:                        if (fsb.charAt(i) != str.charAt(j)) {
333:                            return false;
334:                        }
335:
336:                        i++;
337:                        j++;
338:                    }
339:
340:                    return true;
341:                }
342:
343:                return false;
344:            }
345:
346:            /**
347:             * Tell if two objects are functionally equal.
348:             *
349:             * @param anotherString Object to compare this to
350:             *
351:             * @return true if the two objects are equal
352:             *
353:             * @throws javax.xml.transform.TransformerException
354:             */
355:            public boolean equals(String anotherString) {
356:
357:                int n = m_length;
358:
359:                if (n == anotherString.length()) {
360:                    FastStringBuffer fsb = fsb();
361:                    int i = m_start;
362:                    int j = 0;
363:
364:                    while (n-- != 0) {
365:                        if (fsb.charAt(i) != anotherString.charAt(j)) {
366:                            return false;
367:                        }
368:
369:                        i++;
370:                        j++;
371:                    }
372:
373:                    return true;
374:                }
375:
376:                return false;
377:            }
378:
379:            /**
380:             * Compares this string to the specified object.
381:             * The result is <code>true</code> if and only if the argument is not
382:             * <code>null</code> and is a <code>String</code> object that represents
383:             * the same sequence of characters as this object.
384:             *
385:             * @param   obj2       the object to compare this <code>String</code>
386:             *                     against.
387:             *
388:             * @return  <code>true</code> if the <code>String </code>are equal;
389:             *          <code>false</code> otherwise.
390:             * @see     java.lang.String#compareTo(java.lang.String)
391:             * @see     java.lang.String#equalsIgnoreCase(java.lang.String)
392:             */
393:            public boolean equals(Object obj2) {
394:
395:                if (null == obj2)
396:                    return false;
397:
398:                if (obj2 instanceof  XNumber)
399:                    return obj2.equals(this );
400:
401:                // In order to handle the 'all' semantics of 
402:                // nodeset comparisons, we always call the 
403:                // nodeset function.
404:                else if (obj2 instanceof  XNodeSet)
405:                    return obj2.equals(this );
406:                else if (obj2 instanceof  XStringForFSB)
407:                    return equals((XMLString) obj2);
408:                else
409:                    return equals(obj2.toString());
410:            }
411:
412:            /**
413:             * Compares this <code>String</code> to another <code>String</code>,
414:             * ignoring case considerations.  Two strings are considered equal
415:             * ignoring case if they are of the same length, and corresponding
416:             * characters in the two strings are equal ignoring case.
417:             *
418:             * @param   anotherString   the <code>String</code> to compare this
419:             *                          <code>String</code> against.
420:             * @return  <code>true</code> if the argument is not <code>null</code>
421:             *          and the <code>String</code>s are equal,
422:             *          ignoring case; <code>false</code> otherwise.
423:             * @see     #equals(Object)
424:             * @see     java.lang.Character#toLowerCase(char)
425:             * @see java.lang.Character#toUpperCase(char)
426:             */
427:            public boolean equalsIgnoreCase(String anotherString) {
428:                return (m_length == anotherString.length()) ? str()
429:                        .equalsIgnoreCase(anotherString) : false;
430:            }
431:
432:            /**
433:             * Compares two strings lexicographically.
434:             *
435:             * @param   xstr   the <code>String</code> to be compared.
436:             *
437:             * @return  the value <code>0</code> if the argument string is equal to
438:             *          this string; a value less than <code>0</code> if this string
439:             *          is lexicographically less than the string argument; and a
440:             *          value greater than <code>0</code> if this string is
441:             *          lexicographically greater than the string argument.
442:             * @exception java.lang.NullPointerException if <code>anotherString</code>
443:             *          is <code>null</code>.
444:             */
445:            public int compareTo(XMLString xstr) {
446:
447:                int len1 = m_length;
448:                int len2 = xstr.length();
449:                int n = Math.min(len1, len2);
450:                FastStringBuffer fsb = fsb();
451:                int i = m_start;
452:                int j = 0;
453:
454:                while (n-- != 0) {
455:                    char c1 = fsb.charAt(i);
456:                    char c2 = xstr.charAt(j);
457:
458:                    if (c1 != c2) {
459:                        return c1 - c2;
460:                    }
461:
462:                    i++;
463:                    j++;
464:                }
465:
466:                return len1 - len2;
467:            }
468:
469:            /**
470:             * Compares two strings lexicographically, ignoring case considerations.
471:             * This method returns an integer whose sign is that of
472:             * <code>this.toUpperCase().toLowerCase().compareTo(
473:             * str.toUpperCase().toLowerCase())</code>.
474:             * <p>
475:             * Note that this method does <em>not</em> take locale into account,
476:             * and will result in an unsatisfactory ordering for certain locales.
477:             * The java.text package provides <em>collators</em> to allow
478:             * locale-sensitive ordering.
479:             *
480:             * @param   xstr   the <code>String</code> to be compared.
481:             *
482:             * @return  a negative integer, zero, or a positive integer as the
483:             *          the specified String is greater than, equal to, or less
484:             *          than this String, ignoring case considerations.
485:             * @see     java.text.Collator#compare(String, String)
486:             * @since   1.2
487:             */
488:            public int compareToIgnoreCase(XMLString xstr) {
489:
490:                int len1 = m_length;
491:                int len2 = xstr.length();
492:                int n = Math.min(len1, len2);
493:                FastStringBuffer fsb = fsb();
494:                int i = m_start;
495:                int j = 0;
496:
497:                while (n-- != 0) {
498:                    char c1 = Character.toLowerCase(fsb.charAt(i));
499:                    char c2 = Character.toLowerCase(xstr.charAt(j));
500:
501:                    if (c1 != c2) {
502:                        return c1 - c2;
503:                    }
504:
505:                    i++;
506:                    j++;
507:                }
508:
509:                return len1 - len2;
510:            }
511:
512:            /**
513:             * Returns a hashcode for this string. The hashcode for a
514:             * <code>String</code> object is computed as
515:             * <blockquote><pre>
516:             * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
517:             * </pre></blockquote>
518:             * using <code>int</code> arithmetic, where <code>s[i]</code> is the
519:             * <i>i</i>th character of the string, <code>n</code> is the length of
520:             * the string, and <code>^</code> indicates exponentiation.
521:             * (The hash value of the empty string is zero.)
522:             *
523:             * @return  a hash code value for this object.
524:             */
525:            public int hashCode() {
526:                // Commenting this out because in JDK1.1.8 and VJ++
527:                // we don't match XMLStrings. Defaulting to the super
528:                // causes us to create a string, but at this point
529:                // this only seems to get called in key processing.
530:                // Maybe we can live with it?
531:
532:                /*
533:                 int h = m_hash;
534:
535:                 if (h == 0)
536:                 {
537:                 int off = m_start;
538:                 int len = m_length;
539:                 FastStringBuffer fsb = fsb();
540:
541:                 for (int i = 0; i < len; i++)
542:                 {
543:                 h = 31 * h + fsb.charAt(off);
544:
545:                 off++;
546:                 }
547:
548:                 m_hash = h;
549:                 }
550:                 */
551:
552:                return super .hashCode(); // h;
553:            }
554:
555:            /**
556:             * Tests if this string starts with the specified prefix beginning
557:             * a specified index.
558:             *
559:             * @param   prefix    the prefix.
560:             * @param   toffset   where to begin looking in the string.
561:             * @return  <code>true</code> if the character sequence represented by the
562:             *          argument is a prefix of the substring of this object starting
563:             *          at index <code>toffset</code>; <code>false</code> otherwise.
564:             *          The result is <code>false</code> if <code>toffset</code> is
565:             *          negative or greater than the length of this
566:             *          <code>String</code> object; otherwise the result is the same
567:             *          as the result of the expression
568:             *          <pre>
569:             *          this.subString(toffset).startsWith(prefix)
570:             *          </pre>
571:             * @exception java.lang.NullPointerException if <code>prefix</code> is
572:             *          <code>null</code>.
573:             */
574:            public boolean startsWith(XMLString prefix, int toffset) {
575:
576:                FastStringBuffer fsb = fsb();
577:                int to = m_start + toffset;
578:                int tlim = m_start + m_length;
579:                int po = 0;
580:                int pc = prefix.length();
581:
582:                // Note: toffset might be near -1>>>1.
583:                if ((toffset < 0) || (toffset > m_length - pc)) {
584:                    return false;
585:                }
586:
587:                while (--pc >= 0) {
588:                    if (fsb.charAt(to) != prefix.charAt(po)) {
589:                        return false;
590:                    }
591:
592:                    to++;
593:                    po++;
594:                }
595:
596:                return true;
597:            }
598:
599:            /**
600:             * Tests if this string starts with the specified prefix.
601:             *
602:             * @param   prefix   the prefix.
603:             * @return  <code>true</code> if the character sequence represented by the
604:             *          argument is a prefix of the character sequence represented by
605:             *          this string; <code>false</code> otherwise.
606:             *          Note also that <code>true</code> will be returned if the
607:             *          argument is an empty string or is equal to this
608:             *          <code>String</code> object as determined by the
609:             *          {@link #equals(Object)} method.
610:             * @exception java.lang.NullPointerException if <code>prefix</code> is
611:             *          <code>null</code>.
612:             * @since   JDK1. 0
613:             */
614:            public boolean startsWith(XMLString prefix) {
615:                return startsWith(prefix, 0);
616:            }
617:
618:            /**
619:             * Returns the index within this string of the first occurrence of the
620:             * specified character. If a character with value <code>ch</code> occurs
621:             * in the character sequence represented by this <code>String</code>
622:             * object, then the index of the first such occurrence is returned --
623:             * that is, the smallest value <i>k</i> such that:
624:             * <blockquote><pre>
625:             * this.charAt(<i>k</i>) == ch
626:             * </pre></blockquote>
627:             * is <code>true</code>. If no such character occurs in this string,
628:             * then <code>-1</code> is returned.
629:             *
630:             * @param   ch   a character.
631:             * @return  the index of the first occurrence of the character in the
632:             *          character sequence represented by this object, or
633:             *          <code>-1</code> if the character does not occur.
634:             */
635:            public int indexOf(int ch) {
636:                return indexOf(ch, 0);
637:            }
638:
639:            /**
640:             * Returns the index within this string of the first occurrence of the
641:             * specified character, starting the search at the specified index.
642:             * <p>
643:             * If a character with value <code>ch</code> occurs in the character
644:             * sequence represented by this <code>String</code> object at an index
645:             * no smaller than <code>fromIndex</code>, then the index of the first
646:             * such occurrence is returned--that is, the smallest value <i>k</i>
647:             * such that:
648:             * <blockquote><pre>
649:             * (this.charAt(<i>k</i>) == ch) && (<i>k</i> >= fromIndex)
650:             * </pre></blockquote>
651:             * is true. If no such character occurs in this string at or after
652:             * position <code>fromIndex</code>, then <code>-1</code> is returned.
653:             * <p>
654:             * There is no restriction on the value of <code>fromIndex</code>. If it
655:             * is negative, it has the same effect as if it were zero: this entire
656:             * string may be searched. If it is greater than the length of this
657:             * string, it has the same effect as if it were equal to the length of
658:             * this string: <code>-1</code> is returned.
659:             *
660:             * @param   ch          a character.
661:             * @param   fromIndex   the index to start the search from.
662:             * @return  the index of the first occurrence of the character in the
663:             *          character sequence represented by this object that is greater
664:             *          than or equal to <code>fromIndex</code>, or <code>-1</code>
665:             *          if the character does not occur.
666:             */
667:            public int indexOf(int ch, int fromIndex) {
668:
669:                int max = m_start + m_length;
670:                FastStringBuffer fsb = fsb();
671:
672:                if (fromIndex < 0) {
673:                    fromIndex = 0;
674:                } else if (fromIndex >= m_length) {
675:
676:                    // Note: fromIndex might be near -1>>>1.
677:                    return -1;
678:                }
679:
680:                for (int i = m_start + fromIndex; i < max; i++) {
681:                    if (fsb.charAt(i) == ch) {
682:                        return i - m_start;
683:                    }
684:                }
685:
686:                return -1;
687:            }
688:
689:            /**
690:             * Returns a new string that is a substring of this string. The
691:             * substring begins with the character at the specified index and
692:             * extends to the end of this string. <p>
693:             * Examples:
694:             * <blockquote><pre>
695:             * "unhappy".substring(2) returns "happy"
696:             * "Harbison".substring(3) returns "bison"
697:             * "emptiness".substring(9) returns "" (an empty string)
698:             * </pre></blockquote>
699:             *
700:             * @param      beginIndex   the beginning index, inclusive.
701:             * @return     the specified substring.
702:             * @exception  IndexOutOfBoundsException  if
703:             *             <code>beginIndex</code> is negative or larger than the
704:             *             length of this <code>String</code> object.
705:             */
706:            public XMLString substring(int beginIndex) {
707:
708:                int len = m_length - beginIndex;
709:
710:                if (len <= 0)
711:                    return XString.EMPTYSTRING;
712:                else {
713:                    int start = m_start + beginIndex;
714:
715:                    return new XStringForFSB(fsb(), start, len);
716:                }
717:            }
718:
719:            /**
720:             * Returns a new string that is a substring of this string. The
721:             * substring begins at the specified <code>beginIndex</code> and
722:             * extends to the character at index <code>endIndex - 1</code>.
723:             * Thus the length of the substring is <code>endIndex-beginIndex</code>.
724:             *
725:             * @param      beginIndex   the beginning index, inclusive.
726:             * @param      endIndex     the ending index, exclusive.
727:             * @return     the specified substring.
728:             * @exception  IndexOutOfBoundsException  if the
729:             *             <code>beginIndex</code> is negative, or
730:             *             <code>endIndex</code> is larger than the length of
731:             *             this <code>String</code> object, or
732:             *             <code>beginIndex</code> is larger than
733:             *             <code>endIndex</code>.
734:             */
735:            public XMLString substring(int beginIndex, int endIndex) {
736:
737:                int len = endIndex - beginIndex;
738:
739:                if (len > m_length)
740:                    len = m_length;
741:
742:                if (len <= 0)
743:                    return XString.EMPTYSTRING;
744:                else {
745:                    int start = m_start + beginIndex;
746:
747:                    return new XStringForFSB(fsb(), start, len);
748:                }
749:            }
750:
751:            /**
752:             * Concatenates the specified string to the end of this string.
753:             *
754:             * @param   str   the <code>String</code> that is concatenated to the end
755:             *                of this <code>String</code>.
756:             * @return  a string that represents the concatenation of this object's
757:             *          characters followed by the string argument's characters.
758:             * @exception java.lang.NullPointerException if <code>str</code> is
759:             *          <code>null</code>.
760:             */
761:            public XMLString concat(String str) {
762:
763:                // %OPT% Make an FSB here?
764:                return new XString(str().concat(str));
765:            }
766:
767:            /**
768:             * Removes white space from both ends of this string.
769:             *
770:             * @return  this string, with white space removed from the front and end.
771:             */
772:            public XMLString trim() {
773:                return fixWhiteSpace(true, true, false);
774:            }
775:
776:            /**
777:             * Returns whether the specified <var>ch</var> conforms to the XML 1.0 definition
778:             * of whitespace.  Refer to <A href="http://www.w3.org/TR/1998/REC-xml-19980210#NT-S">
779:             * the definition of <CODE>S</CODE></A> for details.
780:             * @param   ch      Character to check as XML whitespace.
781:             * @return          =true if <var>ch</var> is XML whitespace; otherwise =false.
782:             */
783:            private static boolean isSpace(char ch) {
784:                return XMLCharacterRecognizer.isWhiteSpace(ch); // Take the easy way out for now.
785:            }
786:
787:            /**
788:             * Conditionally trim all leading and trailing whitespace in the specified String.
789:             * All strings of white space are
790:             * replaced by a single space character (#x20), except spaces after punctuation which
791:             * receive double spaces if doublePunctuationSpaces is true.
792:             * This function may be useful to a formatter, but to get first class
793:             * results, the formatter should probably do it's own white space handling
794:             * based on the semantics of the formatting object.
795:             *
796:             * @param   trimHead    Trim leading whitespace?
797:             * @param   trimTail    Trim trailing whitespace?
798:             * @param   doublePunctuationSpaces    Use double spaces for punctuation?
799:             * @return              The trimmed string.
800:             */
801:            public XMLString fixWhiteSpace(boolean trimHead, boolean trimTail,
802:                    boolean doublePunctuationSpaces) {
803:
804:                int end = m_length + m_start;
805:                char[] buf = new char[m_length];
806:                FastStringBuffer fsb = fsb();
807:                boolean edit = false;
808:
809:                /* replace S to ' '. and ' '+ -> single ' '. */
810:                int d = 0;
811:                boolean pres = false;
812:
813:                for (int s = m_start; s < end; s++) {
814:                    char c = fsb.charAt(s);
815:
816:                    if (isSpace(c)) {
817:                        if (!pres) {
818:                            if (' ' != c) {
819:                                edit = true;
820:                            }
821:
822:                            buf[d++] = ' ';
823:
824:                            if (doublePunctuationSpaces && (d != 0)) {
825:                                char prevChar = buf[d - 1];
826:
827:                                if (!((prevChar == '.') || (prevChar == '!') || (prevChar == '?'))) {
828:                                    pres = true;
829:                                }
830:                            } else {
831:                                pres = true;
832:                            }
833:                        } else {
834:                            edit = true;
835:                            pres = true;
836:                        }
837:                    } else {
838:                        buf[d++] = c;
839:                        pres = false;
840:                    }
841:                }
842:
843:                if (trimTail && 1 <= d && ' ' == buf[d - 1]) {
844:                    edit = true;
845:
846:                    d--;
847:                }
848:
849:                int start = 0;
850:
851:                if (trimHead && 0 < d && ' ' == buf[0]) {
852:                    edit = true;
853:
854:                    start++;
855:                }
856:
857:                XMLStringFactory xsf = XMLStringFactoryImpl.getFactory();
858:
859:                return edit ? xsf.newstr(buf, start, d - start) : this ;
860:            }
861:
862:            /**
863:             * Convert a string to a double -- Allowed input is in fixed
864:             * notation ddd.fff.
865:             *
866:             * %OPT% CHECK PERFORMANCE against generating a Java String and
867:             * converting it to double. The advantage of running in native
868:             * machine code -- perhaps even microcode, on some systems -- may
869:             * more than make up for the cost of allocating and discarding the
870:             * additional object. We need to benchmark this. 
871:             *
872:             * %OPT% More importantly, we need to decide whether we _care_ about
873:             * the performance of this operation. Does XString.toDouble constitute
874:             * any measurable percentage of our typical runtime? I suspect not!
875:             *
876:             * @return A double value representation of the string, or return Double.NaN 
877:             * if the string can not be converted.  */
878:            public double toDouble() {
879:                if (m_length == 0)
880:                    return Double.NaN;
881:                int i;
882:                char c;
883:                String valueString = fsb().getString(m_start, m_length);
884:
885:                // The following are permitted in the Double.valueOf, but not by the XPath spec:
886:                // - a plus sign
887:                // - The use of e or E to indicate exponents
888:                // - trailing f, F, d, or D
889:                // See function comments; not sure if this is slower than actually doing the
890:                // conversion ourselves (as was before).
891:
892:                for (i = 0; i < m_length; i++)
893:                    if (!XMLCharacterRecognizer.isWhiteSpace(valueString
894:                            .charAt(i)))
895:                        break;
896:                if (i == m_length)
897:                    return Double.NaN;
898:                if (valueString.charAt(i) == '-')
899:                    i++;
900:                for (; i < m_length; i++) {
901:                    c = valueString.charAt(i);
902:                    if (c != '.' && (c < '0' || c > '9'))
903:                        break;
904:                }
905:                for (; i < m_length; i++)
906:                    if (!XMLCharacterRecognizer.isWhiteSpace(valueString
907:                            .charAt(i)))
908:                        break;
909:                if (i != m_length)
910:                    return Double.NaN;
911:
912:                try {
913:                    return new Double(valueString).doubleValue();
914:                } catch (NumberFormatException nfe) {
915:                    // This should catch double periods, empty strings.
916:                    return Double.NaN;
917:                }
918:            }
919:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.