Java tutorial
//package com.java2s; /*-------------------------------------------------------------------------- * Copyright (c) 2004, 2006 OpenMethods, LLC * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Trip Gilman (OpenMethods), Lonnie G. Pryor (OpenMethods), * Vincent Pruitt (OpenMethods) * * T.D. Barnes (OpenMethods) - initial API and implementation -------------------------------------------------------------------------*/ import org.w3c.dom.CharacterData; import org.w3c.dom.Element; import org.w3c.dom.Node; public class Main { public static String getElementTextDataNoEx(Element element) { String ret = null; try { ret = getElementTextData(element); } catch (Exception ex) { } return ret; } public static String getElementTextDataNoEx(Element element, boolean unindent) { String result = null; try { result = getElementTextData(element); if (unindent) result = unindentTextData(result); } catch (Exception e) { } return result; } /** * Returns the text content of the provided element as is. If multiple text * DOM nodes are present, they are concatenated together. * * @param element The element that contains the desired text content. * @return The text content of the given element. * @throws Exception If an exception occurs during the traversal of the DOM * objects. */ public static String getElementTextData(Element element) throws Exception { if (!element.hasChildNodes()) { throw new Exception("Element has no children."); } Node n = element.getFirstChild(); if ((n.getNodeType() != Node.TEXT_NODE) && (n.getNodeType() != Node.CDATA_SECTION_NODE)) { // must be a textual node // for now throw an exception, but later need to just skip this module and // resume initialization throw new Exception("Element child node is not textual."); } CharacterData value = (CharacterData) n; return value.getData(); } /** * Returns the text content of the provided element. If unindent is false, * the contents are returned as-is. Otherwise, indention performed for * formatting sake will be removed. * * @param element The element that contains the text content. * @param unindent Whether or not to unindent the text content. * @return The text content of the provided element. * @throws Exception If an exception occurs during the traversal of the DOM * elements. */ public static String getElementTextData(Element element, boolean unindent) throws Exception { String result = getElementTextData(element); if (unindent) result = unindentTextData(result); return result; } /** * Removes excess indention from the provided text. Also trims blank lines * from the beginning and end of the text. * * @param text The text to unindent. * @return The reformatted text. */ public static String unindentTextData(String text) { // Exclude blank lines at the beginning. int start = 0; while (start < text.length()) { if (!Character.isWhitespace(text.charAt(start))) break; else ++start; } if (start == text.length()) return ""; //$NON-NLS-1$ while (true) { if (text.charAt(start) == '\n') { ++start; break; } else if (start == 0) break; else --start; } // Exclude blank lines at the end. int end = text.length(); while (Character.isWhitespace(text.charAt(end - 1))) --end; while (true) { if (text.charAt(end - 1) == '\n') { --end; if (text.charAt(end - 1) == '\r') --end; break; } else if (end == text.length()) break; else ++end; } // Determine the indent that was used for the first line. int indentLength = 0; while (Character.isWhitespace(text.charAt(start + indentLength))) ++indentLength; // Validate the indent against all other lines. int index = start + indentLength, lineCount = 1; while (true) { // Check to see if any indent exists. if (indentLength == 0) return text.substring(start, end); // Move to the next line. while (index < end && text.charAt(index) != '\n') ++index; ++index; if (index >= end) break; ++lineCount; // Validate the indent. for (int i = 0; i < indentLength; ++i) if (text.charAt(start + i) != text.charAt(index + i)) indentLength = i; index += indentLength; } // Remove the indent and return the results. StringBuffer result = new StringBuffer((end - start) - (indentLength * lineCount)); for (int i = start, j = 0; i < end; ++i) { while (j < indentLength) { ++i; ++j; } char c = text.charAt(i); result.append(c); if (c == '\n') j = 0; } return result.toString(); } }