2
* Copyright (c) 2002-2009 Gargoyle Software Inc.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
* http://www.apache.org/licenses/LICENSE-2.0
9
* Unless required by applicable law or agreed to in writing, software
10
* distributed under the License is distributed on an "AS IS" BASIS,
11
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
* See the License for the specific language governing permissions and
13
* limitations under the License.
15
package com.gargoylesoftware.htmlunit;
17
import java.io.IOException;
19
import org.w3c.dom.Document;
20
import org.w3c.dom.DocumentType;
21
import org.w3c.dom.Element;
23
import com.gargoylesoftware.htmlunit.html.DomDocumentFragment;
24
import com.gargoylesoftware.htmlunit.html.DomDocumentType;
25
import com.gargoylesoftware.htmlunit.html.DomElement;
26
import com.gargoylesoftware.htmlunit.html.DomNode;
29
* A basic class to be implemented by {@link com.gargoylesoftware.htmlunit.html.HtmlPage} and
30
* {@link com.gargoylesoftware.htmlunit.xml.XmlPage}.
32
* @version $Revision: 4773 $
33
* @author Ahmed Ashour
35
public abstract class SgmlPage extends DomNode implements Page, Document {
37
private static final long serialVersionUID = -8803248938782701094L;
38
private DomDocumentType documentType_;
39
private final WebResponse webResponse_;
40
private WebWindow enclosingWindow_;
41
private final WebClient webClient_;
42
private DomElement documentElement_;
45
* Creates an instance of SgmlPage.
47
* @param webResponse the web response that was used to create this page
48
* @param webWindow the window that this page is being loaded into
50
public SgmlPage(final WebResponse webResponse, final WebWindow webWindow) {
52
webResponse_ = webResponse;
53
enclosingWindow_ = webWindow;
54
webClient_ = webWindow.getWebClient();
60
public void cleanUp() throws IOException {
66
public WebResponse getWebResponse() {
73
public void initialize() throws IOException {
77
* Gets the name for the current node.
78
* @return the node name
81
public String getNodeName() {
86
* Gets the type of the current node.
87
* @return the node type
90
public short getNodeType() {
91
return org.w3c.dom.Node.DOCUMENT_NODE;
95
* Returns the window that this page is sitting inside.
97
* @return the enclosing frame or null if this page isn't inside a frame
99
public WebWindow getEnclosingWindow() {
100
return enclosingWindow_;
104
* Sets the window that contains this page.
106
* @param window the new frame or null if this page is being removed from a frame
108
public void setEnclosingWindow(final WebWindow window) {
109
enclosingWindow_ = window;
113
* Returns the WebClient that originally loaded this page.
115
* @return the WebClient that originally loaded this page
117
public WebClient getWebClient() {
122
* Creates an empty {@link DomDocumentFragment} object.
123
* @return a newly created {@link DomDocumentFragment}
125
public DomDocumentFragment createDomDocumentFragment() {
126
return new DomDocumentFragment(this);
130
* Returns the document type.
131
* @return the document type
133
public final DocumentType getDoctype() {
134
return documentType_;
138
* Sets the document type.
139
* @param type the document type
141
protected void setDocumentType(final DomDocumentType type) {
142
documentType_ = type;
149
public SgmlPage getPage() {
154
* Creates an element, the type of which depends on the specified tag name.
155
* @param tagName the tag name which determines the type of element to be created
156
* @return an element, the type of which depends on the specified tag name
158
public abstract Element createElement(final String tagName);
161
* Create a new Element with the given namespace and qualified name.
162
* @param namespaceURI the URI that identifies an XML namespace
163
* @param qualifiedName the qualified name of the element type to instantiate
164
* @return the new element
166
public abstract Element createElementNS(final String namespaceURI, final String qualifiedName);
169
* Returns the page encoding.
170
* @return the page encoding
172
public abstract String getPageEncoding();
175
* Returns the document element.
176
* @return the document element
178
public DomElement getDocumentElement() {
179
if (documentElement_ == null) {
180
DomNode childNode = getFirstChild();
181
while (childNode != null && !(childNode instanceof DomElement)) {
182
childNode = childNode.getNextSibling();
184
documentElement_ = (DomElement) childNode;
186
return documentElement_;
190
* Creates a clone of this instance, and clears cached state to be not shared with the original.
192
* @return a clone of this instance
195
protected SgmlPage clone() {
197
final SgmlPage result = (SgmlPage) super.clone();
198
result.documentElement_ = null;
201
catch (final CloneNotSupportedException e) {
202
throw new IllegalStateException("Clone not supported");
210
public String asXml() {
211
return getDocumentElement().asXml();
215
* Returns <tt>true</tt> if this page has case-sensitive tag names, <tt>false</tt> otherwise. In general,
216
* XML has case-sensitive tag names, and HTML doesn't. This is especially important during XPath matching.
217
* @return <tt>true</tt> if this page has case-sensitive tag names, <tt>false</tt> otherwise
219
public abstract boolean hasCaseSensitiveTagNames();
223
* The current implementation just {@link DomNode#normalize()}s the document element.
225
public void normalizeDocument() {
226
getDocumentElement().normalize();