📄 webresponse.java
字号:
} /** * Returns all links found in the page matching the specified criteria. * @exception SAXException thrown if there is an error parsing the response. **/ public WebLink[] getMatchingLinks( HTMLElementPredicate predicate, Object criteria ) throws SAXException { return getReceivedPage().getMatchingLinks( predicate, criteria ); } /** * Returns the images found in the page in the order in which they appear. * @exception SAXException thrown if there is an error parsing the response. **/ public WebImage[] getImages() throws SAXException { return getReceivedPage().getImages(); } /** * Returns the image found in the page with the specified name attribute. * @exception SAXException thrown if there is an error parsing the response. **/ public WebImage getImageWithName( String source ) throws SAXException { return getReceivedPage().getImageWithName( source ); } /** * Returns the first image found in the page with the specified src attribute. * @exception SAXException thrown if there is an error parsing the response. **/ public WebImage getImageWithSource( String source ) throws SAXException { return getReceivedPage().getImageWithSource( source ); } /** * Returns the first image found in the page with the specified alt attribute. **/ public WebImage getImageWithAltText( String altText ) throws SAXException { return getReceivedPage().getImageWithAltText( altText ); } public WebApplet[] getApplets() throws SAXException { return getReceivedPage().getApplets(); } /** * Returns an array of text blocks found in the page. * @since 1.6 */ public TextBlock[] getTextBlocks() throws SAXException { return getReceivedPage().getTextBlocks(); } /** * Returns the text block after the specified block, if any. * @since 1.6 */ public TextBlock getNextTextBlock( TextBlock block ) throws SAXException { return getReceivedPage().getNextTextBlock( block ); } /** * Returns the first link found in the page matching the specified criteria. * @since 1.6 * @exception SAXException thrown if there is an error parsing the response. **/ public TextBlock getFirstMatchingTextBlock( HTMLElementPredicate predicate, Object criteria ) throws SAXException { return getReceivedPage().getFirstMatchingTextBlock( predicate, criteria ); } /** * Returns a copy of the domain object model tree associated with this response. * If the response is HTML, it will use a special parser which can transform HTML into an XML DOM. * @exception SAXException thrown if there is an error parsing the response. **/ public Document getDOM() throws SAXException { if (isHTML()) { return (Document) getReceivedPage().getDOM(); } else { try { return HttpUnitUtils.newParser().parse( new InputSource( new StringReader( getText() ) ) ); } catch (IOException e) { throw new SAXException( e ); } } } /** * Returns the top-level tables found in this page in the order in which * they appear. * @exception SAXException thrown if there is an error parsing the response. **/ public WebTable[] getTables() throws SAXException { return getReceivedPage().getTables(); } /** * Returns the first table in the response which matches the specified predicate and value. * Will recurse into any nested tables, as needed. * @return the selected table, or null if none is found **/ public WebTable getFirstMatchingTable( HTMLElementPredicate predicate, Object criteria ) throws SAXException { return getReceivedPage().getFirstMatchingTable( predicate, criteria ); } /** * Returns all tables found in the page matching the specified criteria. * @exception SAXException thrown if there is an error parsing the response. **/ public WebTable[] getMatchingTables( HTMLElementPredicate predicate, Object criteria ) throws SAXException { return getReceivedPage().getMatchingTables( predicate, criteria ); } /** * Returns the first table in the response which has the specified text as the full text of * its first non-blank row and non-blank column. Will recurse into any nested tables, as needed. * Case is ignored. * @exception SAXException thrown if there is an error parsing the response. * @return the selected table, or null if none is found **/ public WebTable getTableStartingWith( String text ) throws SAXException { return getReceivedPage().getTableStartingWith( text ); } /** * Returns the first table in the response which has the specified text as a prefix of the text of * its first non-blank row and non-blank column. Will recurse into any nested tables, as needed. * Case is ignored. * @exception SAXException thrown if there is an error parsing the response. * @return the selected table, or null if none is found **/ public WebTable getTableStartingWithPrefix( String text ) throws SAXException { return getReceivedPage().getTableStartingWithPrefix( text ); } /** * Returns the first table in the response which has the specified text as its summary attribute. * Will recurse into any nested tables, as needed. * Case is ignored. * @exception SAXException thrown if there is an error parsing the response. * @return the selected table, or null if none is found **/ public WebTable getTableWithSummary( String text ) throws SAXException { return getReceivedPage().getTableWithSummary( text ); } /** * Returns the first table in the response which has the specified text as its ID attribute. * Will recurse into any nested tables, as needed. * Case is ignored. * @exception SAXException thrown if there is an error parsing the response. * @return the selected table, or null if none is found **/ public WebTable getTableWithID( String text ) throws SAXException { return getReceivedPage().getTableWithID( text ); }//---------------------------------------- JavaScript methods ---------------------------------------- public Scriptable getScriptableObject() { if (_scriptable == null) _scriptable = new Scriptable(); return _scriptable; } public static ScriptableDelegate newDelegate( String delegateClassName ) { if (delegateClassName.equalsIgnoreCase( "Option" )) { return FormControl.newSelectionOption(); } else { throw new IllegalArgumentException( "No such scripting class supported: " + delegateClassName ); } } public class Scriptable extends ScriptableDelegate implements NamedDelegate { public void alert( String message ) { _client.postAlert( message ); } public boolean getConfirmationResponse( String message ) { return _client.getConfirmationResponse( message ); } public String getUserResponse( String prompt, String defaultResponse ) { return _client.getUserResponse( prompt, defaultResponse ); } public ClientProperties getClientProperties() { return _client == null ? ClientProperties.getDefaultProperties() : _client.getClientProperties(); } public HTMLPage.Scriptable getDocument() { try { if (!isHTML()) replaceText( BLANK_HTML, HTML_CONTENT ); return getReceivedPage().getScriptableObject(); } catch (SAXException e) { throw new RuntimeException( e.toString() ); } } public Scriptable[] getFrames() throws SAXException { String[] names = getFrameNames(); Scriptable[] frames = new Scriptable[ names.length ]; for (int i = 0; i < frames.length; i++) { frames[i] = getSubframeContents( names[i] ).getScriptableObject(); } return frames; } public void load() throws SAXException { if (isHTML()) { getReceivedPage().getForms(); // TODO be more explicit here - don't care about forms, after all doEvent( getReceivedPage().getOnLoadEvent() ); } } public Scriptable open( String urlString, String name, String features, boolean replace ) throws IOException, SAXException { if (urlString == null || urlString.trim().length() == 0) urlString = "about:"; GetMethodWebRequest request = new GetMethodWebRequest( getURL(), urlString, _frame, name ); WebResponse response = _window.getResponse( request ); return response == null ? null : response.getScriptableObject(); } public void close() { if (getFrameName().equals( WebRequest.TOP_FRAME )) _window.close(); } /** * Returns the value of the named property. Will return null if the property does not exist. **/ public Object get( String propertyName ) { if (propertyName.equals( "name" )) { return getName(); } else if (propertyName.equalsIgnoreCase( "top" )) { return _window.getFrameContents( WebRequest.TOP_FRAME ).getScriptableObject(); } else if (propertyName.equalsIgnoreCase( "parent" )) { return _window.getParentFrameContents( _frame ).getScriptableObject(); } else if (propertyName.equalsIgnoreCase( "opener" )) { return getFrameName().equals( WebRequest.TOP_FRAME ) ? getScriptable( _window.getOpener() ) : null; } else if (propertyName.equalsIgnoreCase( "closed" )) { return (getFrameName().equals( WebRequest.TOP_FRAME ) && _window.isClosed()) ? Boolean.TRUE : Boolean.FALSE; } else { try { return getSubframeContents( propertyName ).getScriptableObject(); } catch (NoSuchFrameException e) { return super.get( propertyName ); } } } public String getName() { String windowName = getFrameName().equals( WebRequest.TOP_FRAME ) ? _window.getName() : getFrameName(); return windowName.startsWith( WebWindow.NO_NAME ) ? "" : windowName; } private Scriptable getScriptable( WebResponse opener ) { return opener == null ? null : opener.getScriptableObject(); } /** * Sets the value of the named property. Will throw a runtime exception if the property does not exist or * cannot accept the specified value. **/ public void set( String propertyName, Object value ) { if (propertyName.equals( "name" )) { if (value == null) value = ""; if (getFrameName().equals( WebRequest.TOP_FRAME )) { _window.setName( value.toString() ); } } else { super.set( propertyName, value ); } } public void setLocation( String relativeURL ) throws IOException, SAXException { getWindow().getResponse( new GetMethodWebRequest( _pageURL, relativeURL, _frame.getName() ) ); } public URL getURL() { return WebResponse.this._pageURL; } }//---------------------------------------- Object methods -------------------------------------------- abstract public String toString();//----------------------------------------- protected members ----------------------------------------------- /** * Constructs a response object. * @param frame the frame to hold the response * @param url the url from which the response was received **/ protected WebResponse( WebClient client, FrameSelector frame, URL url ) { _client = client; _baseURL = _pageURL = url; _baseTarget = frame.getName(); _frame = frame; } /** * Constructs a response object. * @param frame the frame to hold the response * @param url the url from which the response was received **/ protected WebResponse( WebClient client, FrameSelector frame, URL url, String text ) { this( client, frame, url ); _responseText = text; } final protected void defineRawInputStream( InputStream inputStream ) throws IOException { if (_inputStream != null || _responseText != null) { throw new IllegalStateException( "Must be called before response text is defined." ); } if (encodedUsingGZIP()) { byte[] compressedData = readFromStream( inputStream, getContentLength() ); _inputStream = new GZIPInputStream( new ByteArrayInputStream( compressedData ) ); } else { _inputStream = inputStream; } } private boolean encodedUsingGZIP() { String encoding = getHeaderField( "Content-Encoding" ); return encoding != null && encoding.indexOf( "gzip" ) >= 0; } /** * Overwrites the current value (if any) of the content type header. **/ protected void setContentTypeHeader( String value ) { _contentHeader = value; }//------------------------------------------ package members ------------------------------------------------ final static String BLANK_HTML = ""; final static WebResponse createBlankResponse() { return new DefaultWebResponse(BLANK_HTML); } WebWindow getWindow() { return _window; } void setWindow( WebWindow window ) { _window = window; } boolean replaceText( String text, String contentType ) { if (_parsingPage) return false; _responseText = text; _inputStream = null; _page = null; _contentType = contentType; _baseURL = null; _baseTarget = _frame.getName(); _refreshHeader = null; try { readTags( text.getBytes() ); } catch (UnsupportedEncodingException e) { throw new RuntimeException( "Failure while attempting to reparse text: " + e ); } catch (MalformedURLException e) { throw new RuntimeException( "Failure while attempting to reparse text: " + e ); } return true; } /** * Returns the frames found in the page in the order in which they appear. **/ WebRequest[] getFrameRequests() throws SAXException { WebFrame[] frames = getFrames(); Vector requests = new Vector(); for (int i = 0; i < frames.length; i++) { if (frames[i].hasInitialRequest()) { requests.addElement( frames[i].getInitialRequest() ); } } WebRequest[] result = new WebRequest[ requests.size() ]; requests.copyInto( result ); return result; }//--------------------------------- private members -------------------------------------- private WebWindow _window; private HTMLPage _page; private String _contentHeader; private int _contentLength = UNINITIALIZED_INT; private String _contentType;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -