~testplan-team/testplan/source-collection

5 by edA-qa mort-ora-y
updating hmltunit src
1
/*
2
 * Copyright (c) 2002-2009 Gargoyle Software Inc.
3
 *
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
8
 *
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.
14
 */
15
package com.gargoylesoftware.htmlunit;
16
17
import java.io.IOException;
18
19
import org.w3c.dom.Document;
20
import org.w3c.dom.DocumentType;
21
import org.w3c.dom.Element;
22
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;
27
28
/**
29
 * A basic class to be implemented by {@link com.gargoylesoftware.htmlunit.html.HtmlPage} and
30
 * {@link com.gargoylesoftware.htmlunit.xml.XmlPage}.
31
 *
32
 * @version $Revision: 4773 $
33
 * @author Ahmed Ashour
34
 */
35
public abstract class SgmlPage extends DomNode implements Page, Document {
36
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_;
43
44
    /**
45
     * Creates an instance of SgmlPage.
46
     *
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
49
     */
50
    public SgmlPage(final WebResponse webResponse, final WebWindow webWindow) {
51
        super(null);
52
        webResponse_ = webResponse;
53
        enclosingWindow_ = webWindow;
54
        webClient_ = webWindow.getWebClient();
55
    }
56
57
    /**
58
     * {@inheritDoc}
59
     */
60
    public void cleanUp() throws IOException {
61
    }
62
63
    /**
64
     * {@inheritDoc}
65
     */
66
    public WebResponse getWebResponse() {
67
        return webResponse_;
68
    }
69
70
    /**
71
     * {@inheritDoc}
72
     */
73
    public void initialize() throws IOException {
74
    }
75
76
    /**
77
     * Gets the name for the current node.
78
     * @return the node name
79
     */
80
    @Override
81
    public String getNodeName() {
82
        return "#document";
83
    }
84
85
    /**
86
     * Gets the type of the current node.
87
     * @return the node type
88
     */
89
    @Override
90
    public short getNodeType() {
91
        return org.w3c.dom.Node.DOCUMENT_NODE;
92
    }
93
94
    /**
95
     * Returns the window that this page is sitting inside.
96
     *
97
     * @return the enclosing frame or null if this page isn't inside a frame
98
     */
99
    public WebWindow getEnclosingWindow() {
100
        return enclosingWindow_;
101
    }
102
103
    /**
104
     * Sets the window that contains this page.
105
     *
106
     * @param window the new frame or null if this page is being removed from a frame
107
     */
108
    public void setEnclosingWindow(final WebWindow window) {
109
        enclosingWindow_ = window;
110
    }
111
112
    /**
113
     * Returns the WebClient that originally loaded this page.
114
     *
115
     * @return the WebClient that originally loaded this page
116
     */
117
    public WebClient getWebClient() {
118
        return webClient_;
119
    }
120
121
    /**
122
     * Creates an empty {@link DomDocumentFragment} object.
123
     * @return a newly created {@link DomDocumentFragment}
124
     */
125
    public DomDocumentFragment createDomDocumentFragment() {
126
        return new DomDocumentFragment(this);
127
    }
128
129
    /**
130
     * Returns the document type.
131
     * @return the document type
132
     */
133
    public final DocumentType getDoctype() {
134
        return documentType_;
135
    }
136
137
    /**
138
     * Sets the document type.
139
     * @param type the document type
140
     */
141
    protected void setDocumentType(final DomDocumentType type) {
142
        documentType_ = type;
143
    }
144
145
    /**
146
     * {@inheritDoc}
147
     */
148
    @Override
149
    public SgmlPage getPage() {
150
        return this;
151
    }
152
153
    /**
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
157
     */
158
    public abstract Element createElement(final String tagName);
159
160
    /**
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
165
     */
166
    public abstract Element createElementNS(final String namespaceURI, final String qualifiedName);
167
168
    /**
169
     * Returns the page encoding.
170
     * @return the page encoding
171
     */
172
    public abstract String getPageEncoding();
173
174
    /**
175
     * Returns the document element.
176
     * @return the document element
177
     */
178
    public DomElement getDocumentElement() {
179
        if (documentElement_ == null) {
180
            DomNode childNode = getFirstChild();
181
            while (childNode != null && !(childNode instanceof DomElement)) {
182
                childNode = childNode.getNextSibling();
183
            }
184
            documentElement_ = (DomElement) childNode;
185
        }
186
        return documentElement_;
187
    }
188
189
    /**
190
     * Creates a clone of this instance, and clears cached state to be not shared with the original.
191
     *
192
     * @return a clone of this instance
193
     */
194
    @Override
195
    protected SgmlPage clone() {
196
        try {
197
            final SgmlPage result = (SgmlPage) super.clone();
198
            result.documentElement_ = null;
199
            return result;
200
        }
201
        catch (final CloneNotSupportedException e) {
202
            throw new IllegalStateException("Clone not supported");
203
        }
204
    }
205
206
    /**
207
     * {@inheritDoc}
208
     */
209
    @Override
210
    public String asXml() {
211
        return getDocumentElement().asXml();
212
    }
213
214
    /**
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
218
     */
219
    public abstract boolean hasCaseSensitiveTagNames();
220
221
    /**
222
     * {@inheritDoc}
223
     * The current implementation just {@link DomNode#normalize()}s the document element.
224
     */
225
    public void normalizeDocument() {
226
        getDocumentElement().normalize();
227
    }
228
229
}