##### Coverage Report - org.jaxen.function.NormalizeSpaceFunction

Classes in this File | Line Coverage | Branch Coverage | Complexity
NormalizeSpaceFunction |
100% |
100% |
| 3.25;3.25

1 |  |
     /*
2 |  |
      * $Header: /home/projects/jaxen/scm/jaxen/src/java/main/org/jaxen/function/NormalizeSpaceFunction.java,v 1.16 2005/06/28 13:44:45 elharo Exp $
3 |  |
      * $Revision: 1.16 $
4 |  |
      * $Date: 2005/06/28 13:44:45 $
5 |  |
      *
6 |  |
      * ====================================================================
7 |  |
      *
8 |  |
      * Copyright (C) 2000-2002 bob mcwhirter & James Strachan.
9 |  |
      * All rights reserved.
10 |  |
      *
11 |  |
      * Redistribution and use in source and binary forms, with or without
12 |  |
      * modification, are permitted provided that the following conditions
13 |  |
      * are met:
14 |  |
      *
15 |  |
      * 1. Redistributions of source code must retain the above copyright
16 |  |
      *    notice, this list of conditions, and the following disclaimer.
17 |  |
      *
18 |  |
      * 2. Redistributions in binary form must reproduce the above copyright
19 |  |
      *    notice, this list of conditions, and the disclaimer that follows
20 |  |
      *    these conditions in the documentation and/or other materials
21 |  |
      *    provided with the distribution.
22 |  |
      *
23 |  |
      * 3. The name "Jaxen" must not be used to endorse or promote products
24 |  |
      *    derived from this software without prior written permission.  For
25 |  |
      *    written permission, please contact license@jaxen.org.
26 |  |
      *
27 |  |
      * 4. Products derived from this software may not be called "Jaxen", nor
28 |  |
      *    may "Jaxen" appear in their name, without prior written permission
29 |  |
      *    from the Jaxen Project Management (pm@jaxen.org).
30 |  |
      *
31 |  |
      * In addition, we request (but do not require) that you include in the
32 |  |
      * end-user documentation provided with the redistribution and/or in the
33 |  |
      * software itself an acknowledgement equivalent to the following:
34 |  |
      *     "This product includes software developed by the
35 |  |
      *      Jaxen Project <http://www.jaxen.org/>."
36 |  |
      * Alternatively, the acknowledgment may be graphical using the logos
37 |  |
      * available at http://www.jaxen.org/
38 |  |
      *
39 |  |
      * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40 |  |
      * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41 |  |
      * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42 |  |
      * DISCLAIMED.  IN NO EVENT SHALL THE Jaxen AUTHORS OR THE PROJECT
43 |  |
      * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 |  |
      * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45 |  |
      * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46 |  |
      * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47 |  |
      * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48 |  |
      * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49 |  |
      * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 |  |
      * SUCH DAMAGE.
51 |  |
      *
52 |  |
      * ====================================================================
53 |  |
      * This software consists of voluntary contributions made by many
54 |  |
      * individuals on behalf of the Jaxen Project and was originally
55 |  |
      * created by bob mcwhirter <bob@werken.com> and
56 |  |
      * James Strachan <jstrachan@apache.org>.  For more information on the
57 |  |
      * Jaxen Project, please see <http://www.jaxen.org/>.
58 |  |
      *
59 |  |
      * $Id: NormalizeSpaceFunction.java,v 1.16 2005/06/28 13:44:45 elharo Exp $
60 |  |
      */
61 |  |
62 |  |
63 |  |
     package org.jaxen.function;
64 |  |
65 |  |
     import java.util.List;
66 |  |
67 |  |
     import org.jaxen.Context;
68 |  |
     import org.jaxen.Function;
69 |  |
     import org.jaxen.FunctionCallException;
70 |  |
     import org.jaxen.Navigator;
71 |  |
72 |  |
     /**
73 |  |
      * <p>
74 |  |
      * <b>4.2</b> <code><i>string</i> normalize-space(<i>string</i>)</code>
75 |  |
      * </p>
76 |  |
      *
77 |  |
      * <blockquote src="http://www.w3.org/TR/xpath#function-normalize-space">
78 |  |
      * The <b>normalize-space</b> function
79 |  |
      * returns the argument string with whitespace normalized by stripping
80 |  |
      * leading and trailing whitespace and replacing sequences of whitespace
81 |  |
      * characters by a single space. Whitespace characters are the same as
82 |  |
      * those allowed by the <a href="http://www.w3.org/TR/REC-xml#NT-S" target="_top">S</a>
83 |  |
      * production in XML. If the argument is omitted, it defaults to the
84 |  |
      * context node converted to a string, in other words the <a
85 |  |
      * href="http://www.w3.org/TR/xpath#dt-string-value"
86 |  |
      * target="_top">string-value</a> of the context node.
87 |  |
      * </blockquote>
88 |  |
      *
89 |  |
      * @author James Strachan (james@metastuff.com)
90 |  |
      * @see <a href="http://www.w3.org/TR/xpath#function-normalize-space"
91 |  |
      *      target="_top">Section 4.2 of the XPath Specification</a>
92 |  |
      */
93 |  |
     public class NormalizeSpaceFunction implements Function
94 |  |
     {
95 |  |
96 |  |
97 |  |
         /**
98 |  |
          * Create a new <code>NormalizeSpaceFunction</code> object.
99 |  |
          */
100 | 294 |
         public NormalizeSpaceFunction() {}
101 |  |
102 |  |
         /**
103 |  |
          * Returns the string-value of the first item in <code>args</code>
104 |  |
          * after removing all leading and trailing white space, and
105 |  |
          * replacing each other sequence of whitespace by a single space.
106 |  |
          * Whitespace consists of the characters space (0x32), carriage return (0x0D),
107 |  |
          * linefeed (0x0A), and tab (0x09).
108 |  |
          *
109 |  |
          * @param context the context at the point in the
110 |  |
          *         expression when the function is called
111 |  |
          * @param args a list that contains exactly one item
112 |  |
          *
113 |  |
          * @return a normalized <code>String</code>
114 |  |
          *
115 |  |
          * @throws FunctionCallException if <code>args</code> does not have length one
116 |  |
          */
117 |  |
         public Object call(Context context,
118 |  |
                            List args) throws FunctionCallException
119 |  |
         {
120 |  |
121 | 168 |
             if (args.size() == 0) {
122 | 6 |
                 return evaluate( context.getNodeSet(),
123 |  |
                                  context.getNavigator() );
124 |  |
             }
125 | 162 |
             else if (args.size() == 1)
126 |  |
             {
127 | 156 |
                 return evaluate( args.get(0),
128 |  |
                                  context.getNavigator() );
129 |  |
             }
130 |  |
131 | 6 |
             throw new FunctionCallException( "normalize-space() cannot have more than one argument" );
132 |  |
         }
133 |  |
134 |  |
         /**
135 |  |
          * Returns the string-value of <code>strArg</code> after removing
136 |  |
          * all leading and trailing white space, and
137 |  |
          * replacing each other sequence of whitespace by a single space.
138 |  |
          * Whitespace consists of the characters space (0x32), carriage return (0x0D),
139 |  |
          * linefeed (0x0A), and tab (0x09).
140 |  |
          *
141 |  |
          * @param strArg the object whose string-value is normalized
142 |  |
          * @param nav the context at the point in the
143 |  |
          *         expression when the function is called
144 |  |
          *
145 |  |
          * @return the normalized string-value
146 |  |
          */
147 |  |
         public static String evaluate(Object strArg,
148 |  |
                                       Navigator nav)
149 |  |
         {
150 | 162 |
             String str = StringFunction.evaluate( strArg,
151 |  |
                                                   nav );
152 |  |
153 | 162 |
             char[] buffer = str.toCharArray();
154 | 162 |
             int write = 0;
155 | 162 |
             int lastWrite = 0;
156 | 162 |
             boolean wroteOne = false;
157 | 162 |
             int read = 0;
158 | 786 |
             while (read < buffer.length)
159 |  |
             {
160 | 624 |
                 if (isXMLSpace(buffer[read]))
161 |  |
                 {
162 | 300 |
                     if (wroteOne)
163 |  |
                     {
164 | 174 |
                         buffer[write++] = ' ';
165 |  |
                     }
166 |  |
                     do
167 |  |
                     {
168 | 732 |
                         read++;
169 |  |
                     }
170 | 732 |
                     while(read < buffer.length && isXMLSpace(buffer[read]));
171 |  |
                 }
172 |  |
                 else
173 |  |
                 {
174 | 324 |
                     buffer[write++] = buffer[read++];
175 | 324 |
                     wroteOne = true;
176 | 324 |
                     lastWrite = write;
177 |  |
                 }
178 |  |
             }
179 |  |
180 | 162 |
             return new String(buffer, 0, lastWrite);
181 |  |
         }
182 |  |
183 |  |
184 |  |
         private static boolean isXMLSpace(char c) {
185 | 1254 |
             return c == ' ' || c == '\n' || c == '\r' || c == '\t';
186 |  |
         }
187 |  |
188 |  |
     }