001 /*
002 * Copyright 2009 - 2009 JEuclid, http://jeuclid.sf.net
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: TextContent.java,v 8d5443ff84fe 2009/09/01 11:21:57 max $ */
018
019 package net.sourceforge.jeuclid.elements.support.text;
020
021 import net.sourceforge.jeuclid.elements.AbstractJEuclidElement;
022
023 import org.w3c.dom.Node;
024
025 /**
026 * Helper class for handling Text Content.
027 *
028 * @version $Revision: 8d5443ff84fe $
029 */
030 public final class TextContent {
031
032 private TextContent() {
033 // do not instantiate.
034 }
035
036 /**
037 * Retrieve the text content for a given node according to MathML standards.
038 *
039 * @param node
040 * Node
041 * @return Text content. Always a valid String, never null.
042 */
043 public static String getText(final Node node) {
044 final String theText = node.getTextContent();
045 if (theText == null) {
046 return "";
047 } else {
048
049 final StringBuilder newText = new StringBuilder();
050
051 // As seen in 2.4.6
052 newText.append(theText.trim());
053
054 for (int i = 0; i < newText.length() - 1; i++) {
055 if ((newText.charAt(i) <= AbstractJEuclidElement.TRIVIAL_SPACE_MAX)
056 && (newText.charAt(i + 1) <= AbstractJEuclidElement.TRIVIAL_SPACE_MAX)) {
057 newText.deleteCharAt(i);
058 // CHECKSTYLE:OFF
059 // This is intentional
060 i--;
061 // CHECKSTYLE:ON
062 }
063 }
064 return CharConverter.convertEarly(newText.toString());
065 }
066
067 }
068 }