001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * 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, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.xml.client;
017:
018: import com.google.gwt.xml.client.impl.XMLParserImpl;
019:
020: import java.util.ArrayList;
021: import java.util.List;
022:
023: /**
024: * This class represents the client interface to XML parsing.
025: */
026: public class XMLParser {
027:
028: private static final XMLParserImpl impl = XMLParserImpl
029: .getInstance();
030:
031: /**
032: * This method creates a new document, to be manipulated by the DOM API.
033: *
034: * @return the newly created document
035: */
036: public static Document createDocument() {
037: return impl.createDocument();
038: }
039:
040: /**
041: * This method parses a new document from the supplied string, throwing a
042: * <code>DOMParseException</code> if the parse fails.
043: *
044: * @param contents the String to be parsed into a <code>Document</code>
045: * @return the newly created <code>Document</code>
046: */
047: public static Document parse(String contents) {
048: return impl.parse(contents);
049: }
050:
051: /**
052: * This method removes all <code>Text</code> nodes which are made up of only
053: * white space.
054: *
055: * @param n the node which is to have all of its whitespace descendents
056: * removed.
057: */
058: public static void removeWhitespace(Node n) {
059: removeWhitespaceInner(n, null);
060: }
061:
062: /**
063: * This method determines whether the browser supports {@link CDATASection}
064: * as distinct entities from <code>Text</code> nodes.
065: *
066: * @return true if the browser supports {@link CDATASection}, otherwise
067: * <code>false</code>.
068: */
069: public static boolean supportsCDATASection() {
070: return impl.supportsCDATASection();
071: }
072:
073: /*
074: * The inner recursive method for removeWhitespace
075: */
076: private static void removeWhitespaceInner(Node n, Node parent) {
077: // This n is removed from the parent if n is a whitespace node
078: if (parent != null && n instanceof Text
079: && (!(n instanceof CDATASection))) {
080: Text t = (Text) n;
081: if (t.getData().matches("[ \t\n]*")) {
082: parent.removeChild(t);
083: }
084: }
085: if (n.hasChildNodes()) {
086: int length = n.getChildNodes().getLength();
087: List<Node> toBeProcessed = new ArrayList<Node>();
088: // We collect all the nodes to iterate as the child nodes will change
089: // upon removal
090: for (int i = 0; i < length; i++) {
091: toBeProcessed.add(n.getChildNodes().item(i));
092: }
093: // This changes the child nodes, but the iterator of nodes never changes
094: // meaning that this is safe
095: for (Node childNode : toBeProcessed) {
096: removeWhitespaceInner(childNode, n);
097: }
098: }
099: }
100:
101: /**
102: * Not instantiable.
103: */
104: private XMLParser() {
105: }
106:
107: }
|