KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > xml > client > XMLParser


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.xml.client;
17
18 import com.google.gwt.xml.client.impl.XMLParserImpl;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23
24 /**
25  * This class represents the client interface to XML parsing.
26  */

27 public class XMLParser {
28
29   private static final XMLParserImpl impl = XMLParserImpl.getInstance();
30
31   /**
32    * This method creates a new document, to be manipulated by the DOM API.
33    *
34    * @return the newly created document
35    */

36   public static Document createDocument() {
37     return impl.createDocument();
38   }
39
40   /**
41    * This method parses a new document from the supplied string, throwing a
42    * <code>DOMParseException</code> if the parse fails.
43    *
44    * @param contents the String to be parsed into a <code>Document</code>
45    * @return the newly created <code>Document</code>
46    */

47   public static Document parse(String JavaDoc contents) {
48     return impl.parse(contents);
49   }
50
51   /**
52    * This method removes all <code>Text</code> nodes which are made up of only
53    * white space.
54    *
55    * @param n the node which is to have all of its whitespace descendents
56    * removed.
57    */

58   public static void removeWhitespace(Node n) {
59     removeWhitespaceInner(n, null);
60   }
61
62   /**
63    * This method determines whether the browser supports {@link CDATASection}
64    * as distinct entities from <code>Text</code> nodes.
65    *
66    * @return true if the browser supports {@link CDATASection}, otherwise
67    * <code>false</code>.
68    */

69   public static boolean supportsCDATASection() {
70     return impl.supportsCDATASection();
71   }
72
73   /*
74    * The inner recursive method for removeWhitespace
75    */

76   private static void removeWhitespaceInner(Node n, Node parent) {
77     // This n is removed from the parent if n is a whitespace node
78
if (parent != null && n instanceof Text && (!(n instanceof CDATASection))) {
79       Text t = (Text) n;
80       if (t.getData().matches("[ \t\n]*")) {
81         parent.removeChild(t);
82       }
83     }
84     if (n.hasChildNodes()) {
85       int length = n.getChildNodes().getLength();
86       List JavaDoc toBeProcessed = new ArrayList JavaDoc();
87       // We collect all the nodes to iterate as the child nodes will change
88
// upon removal
89
for (int i = 0; i < length; i++) {
90         toBeProcessed.add(n.getChildNodes().item(i));
91       }
92       // This changes the child nodes, but the iterator of nodes never changes
93
// meaning that this is safe
94
for (Iterator JavaDoc iter = toBeProcessed.iterator(); iter.hasNext();) {
95         Node childNode = (Node) iter.next();
96         removeWhitespaceInner(childNode, n);
97       }
98     }
99   }
100
101   /**
102    * Not instantiable.
103    */

104   private XMLParser() {
105   }
106
107 }
108
Popular Tags