Java Array Normalize normalizeCharacterData(char[] ch, int start, int length)

Here you can find the source of normalizeCharacterData(char[] ch, int start, int length)

Description

Used to process the characters found between two XML elements, this method removes any leading and trailing white space, including newlines.

License

BSD License

Parameter

Parameter Description
ch the array of characters to process
start the start position in the array
length the number of characters to read from the array

Return

the resulting string

Declaration

public static String normalizeCharacterData(char[] ch, int start, int length) 

Method Source Code

//package com.java2s;
/*---------------------------------------------------------------------------*\
  This software is released under a BSD license, adapted from
  <http://opensource.org/licenses/bsd-license.php>
    /*from   ww  w .j  a  v a 2s.  c  om*/
  Copyright &copy; 2004-2012 Brian M. Clapper.
  All rights reserved.
    
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:
    
  * Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
    
  * Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
    
  * Neither the name "clapper.org", "curn", nor the names of the project's
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
    
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\*---------------------------------------------------------------------------*/

public class Main {
    /**
     * Used to process the characters found between two XML elements, this
     * method removes any leading and trailing white space, including
     * newlines. Embedded newlines are mapped to spaces.
     *
     * @param ch      the array of characters to process
     * @param start   the start position in the array
     * @param length  the number of characters to read from the array
     *
     * @return the resulting string
     *
     * @see #normalizeCharacterData(char[],int,int,StringBuffer)
     * @see #normalizeCharacterData(String)
     */
    public static String normalizeCharacterData(char[] ch, int start, int length) {
        StringBuffer buf = new StringBuffer();

        normalizeCharacterData(ch, start, length, buf);

        return buf.toString();
    }

    /**
     * Used to process the characters found between two XML elements, this
     * method removes any leading and trailing white space, including
     * newlines. Embedded newlines are mapped to spaces.
     *
     * @param s   the string to process
     *
     * @return the resulting string
     *
     * @see #normalizeCharacterData(char[],int,int,StringBuffer)
     * @see #normalizeCharacterData(String)
     */
    public static String normalizeCharacterData(String s) {
        if (s != null)
            s = normalizeCharacterData(s.toCharArray(), 0, s.length());

        return s;
    }

    /**
     * Used to process the characters found between two XML elements, this
     * method removes any leading and trailing white space, including
     * newlines. Embedded newlines are mapped to spaces.
     *
     * @param ch      the array of characters to process
     * @param start   the start position in the array
     * @param length  the number of characters to read from the array
     * @param buf     where to append the resulting characters
     *
     * @see #normalizeCharacterData(char[],int,int)
     * @see #normalizeCharacterData(String)
     */
    public static void normalizeCharacterData(char[] ch, int start, int length, StringBuffer buf) {
        boolean lastWasNewline = false;

        int end = start + length;
        while (start < end) {
            char c = ch[start++];

            // Substitute a space for each newline sequence. Be sure to
            // handle all combinations of newline sequences (e.g., \n, \r,
            // \r\n, \n\r). We want just one space to replace a given
            // newline sequence. The easiest solution is to replace all
            // consecutive newline (\n or \r) characters with one space.
            // This has the effect of nuking blank lines, but blank lines
            // are meaningless within character data anyway, at least in
            // this context.

            if ((c == '\n') || (c == '\r') || (start == 0)) {
                if (!lastWasNewline)
                    buf.append(' ');
                lastWasNewline = true;
            }

            else {
                lastWasNewline = false;
                if (!Character.isISOControl(c))
                    buf.append(c);
            }
        }
    }
}

Related

  1. normalizeAndInvertValues(double[] values, double maxValue, double minValue)
  2. normalizeArray(double[] array)
  3. normalizeArray(double[] hist)
  4. normalizeArray(String[] raw, int expectedSize)
  5. normalizeArrays(int normalizeToLength, String[]... arraysToNormalize)
  6. normalizeComplexVectorsToUnitVectors(float[] complex)
  7. normalizeComponentsOverwrite(float[][][][] in)
  8. normalizeContingencyTable(final int[][] table)
  9. normalizeCoordsSum(float[] vector, float newSum)