Java XML Node Value Check isWhitespace(Node node)

Here you can find the source of isWhitespace(Node node)

Description

test if a node is a text node that contains just whitespace.

License

Open Source License

Parameter

Parameter Description
node a parameter

Return

true if the node contains all whitespace, false otherwise.

Declaration

public static boolean isWhitespace(Node node) 

Method Source Code


//package com.java2s;
/*//from  w  w  w . ja v  a  2s  .  c o m
 * ? Copyright IBM Corp. 2011
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at:
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing 
 * permissions and limitations under the License.
 */

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.w3c.dom.Node;

public class Main {
    private static Pattern _whitespacePattern;

    /**
     * test if a node is a text node that contains just whitespace.
     * 
     * @param node
     * @return true if the node contains all whitespace, false otherwise.
     */
    public static boolean isWhitespace(Node node) {

        if (node != null && node.getNodeType() == Node.TEXT_NODE) {
            String text = node.getNodeValue();
            Matcher m = _whitespacePattern.matcher(text);
            if (m.matches()) {
                return true;
            }
        }

        return false;
    }

    /**
     * test if a string to see if it contains just whitespace.
     * 
     * @param String
     * @return true if the string contains all whitespace, false otherwise.
     */
    public static boolean isWhitespace(String text) {

        if (text != null) {
            Matcher m = _whitespacePattern.matcher(text);
            if (m.matches()) {
                return true;
            }
        }

        return false;
    }
}

Related

  1. isText(Node node)
  2. isText(Node node)
  3. isText(Node node)
  4. isValidNode(Node node, String name)
  5. isWhiteSpace(Node node)
  6. isWhitespaceNode(Node n)
  7. isWhitespaceNode(Node t)