📄 page.java
字号:
public String getEncoding () { return (getSource ().getEncoding ()); } /** * Begins reading from the source with the given character set. * If the current encoding is the same as the requested encoding, * this method is a no-op. Otherwise any subsequent characters read from * this page will have been decoded using the given character set.<p> * Some magic happens here to obtain this result if characters have already * been consumed from this page. * Since a Reader cannot be dynamically altered to use a different character * set, the underlying stream is reset, a new Source is constructed * and a comparison made of the characters read so far with the newly * read characters up to the current position. * If a difference is encountered, or some other problem occurs, * an exception is thrown. * @param character_set The character set to use to convert bytes into * characters. * @exception ParserException If a character mismatch occurs between * characters already provided and those that would have been returned * had the new character set been in effect from the beginning. An * exception is also thrown if the underlying stream won't put up with * these shenanigans. */ public void setEncoding (String character_set) throws ParserException { getSource ().setEncoding (character_set); } /** * Build a URL from the link and base provided using non-strict rules. * @param link The (relative) URI. * @param base The base URL of the page, either from the <BASE> tag * or, if none, the URL the page is being fetched from. * @return An absolute URL. * @exception MalformedURLException If creating the URL fails. * @see #constructUrl(String, String, boolean) */ public URL constructUrl (String link, String base) throws MalformedURLException { return (constructUrl (link, base, false)); } /** * Build a URL from the link and base provided. * @param link The (relative) URI. * @param base The base URL of the page, either from the <BASE> tag * or, if none, the URL the page is being fetched from. * @param strict If <code>true</code> a link starting with '?' is handled * according to <a href="http://www.ietf.org/rfc/rfc2396.txt">RFC 2396</a>, * otherwise the common interpretation of a query appended to the base * is used instead. * @return An absolute URL. * @exception MalformedURLException If creating the URL fails. */ public URL constructUrl (String link, String base, boolean strict) throws MalformedURLException { String path; boolean modified; boolean absolute; int index; URL url; // constructed URL combining relative link and base // Bug #1461473 Relative links starting with ? if (!strict && ('?' == link.charAt (0))) { // remove query part of base if any if (-1 != (index = base.lastIndexOf ('?'))) base = base.substring (0, index); url = new URL (base + link); } else url = new URL (new URL (base), link); path = url.getFile (); modified = false; absolute = link.startsWith ("/"); if (!absolute) { // we prefer to fix incorrect relative links // this doesn't fix them all, just the ones at the start while (path.startsWith ("/.")) { if (path.startsWith ("/../")) { path = path.substring (3); modified = true; } else if (path.startsWith ("/./") || path.startsWith("/.")) { path = path.substring (2); modified = true; } else break; } } // fix backslashes while (-1 != (index = path.indexOf ("/\\"))) { path = path.substring (0, index + 1) + path.substring (index + 2); modified = true; } if (modified) url = new URL (url, path); return (url); } /** * Create an absolute URL from a relative link. * @param link The reslative portion of a URL. * @return The fully qualified URL or the original link if it was absolute * already or a failure occured. */ public String getAbsoluteURL (String link) { return (getAbsoluteURL (link, false)); } /** * Create an absolute URL from a relative link. * @param link The reslative portion of a URL. * @param strict If <code>true</code> a link starting with '?' is handled * according to <a href="http://www.ietf.org/rfc/rfc2396.txt">RFC 2396</a>, * otherwise the common interpretation of a query appended to the base * is used instead. * @return The fully qualified URL or the original link if it was absolute * already or a failure occured. */ public String getAbsoluteURL (String link, boolean strict) { String base; URL url; String ret; if ((null == link) || ("".equals (link))) ret = ""; else try { base = getBaseUrl (); if (null == base) base = getUrl (); if (null == base) ret = link; else { url = constructUrl (link, base, strict); ret = url.toExternalForm (); } } catch (MalformedURLException murle) { ret = link; } return (ret); } /** * Get the line number for a cursor. * @param cursor The character offset into the page. * @return The line number the character is in. */ public int row (Cursor cursor) { return (mIndex.row (cursor)); } /** * Get the line number for a cursor. * @param position The character offset into the page. * @return The line number the character is in. */ public int row (int position) { return (mIndex.row (position)); } /** * Get the column number for a cursor. * @param cursor The character offset into the page. * @return The character offset into the line this cursor is on. */ public int column (Cursor cursor) { return (mIndex.column (cursor)); } /** * Get the column number for a cursor. * @param position The character offset into the page. * @return The character offset into the line this cursor is on. */ public int column (int position) { return (mIndex.column (position)); } /** * Get the text identified by the given limits. * @param start The starting position, zero based. * @param end The ending position * (exclusive, i.e. the character at the ending position is not included), * zero based. * @return The text from <code>start</code> to <code>end</code>. * @see #getText(StringBuffer, int, int) * @exception IllegalArgumentException If an attempt is made to get * characters ahead of the current source offset (character position). */ public String getText (int start, int end) throws IllegalArgumentException { String ret; try { ret = mSource.getString (start, end - start); } catch (IOException ioe) { throw new IllegalArgumentException ( "can't get the " + (end - start) + "characters at position " + start + " - " + ioe.getMessage ()); } return (ret); } /** * Put the text identified by the given limits into the given buffer. * @param buffer The accumulator for the characters. * @param start The starting position, zero based. * @param end The ending position * (exclusive, i.e. the character at the ending position is not included), * zero based. * @exception IllegalArgumentException If an attempt is made to get * characters ahead of the current source offset (character position). */ public void getText (StringBuffer buffer, int start, int end) throws IllegalArgumentException { int length; if ((mSource.offset () < start) || (mSource.offset () < end)) throw new IllegalArgumentException ( "attempt to extract future characters from source" + start + "|" + end + " > " + mSource.offset ()); if (end < start) { length = end; end = start; start = length; } length = end - start; try { mSource.getCharacters (buffer, start, length); } catch (IOException ioe) { throw new IllegalArgumentException ( "can't get the " + (end - start) + "characters at position " + start + " - " + ioe.getMessage ()); } } /** * Get all text read so far from the source. * @return The text from the source. * @see #getText(StringBuffer) */ public String getText () { return (getText (0, mSource.offset ())); } /** * Put all text read so far from the source into the given buffer. * @param buffer The accumulator for the characters. * @see #getText(StringBuffer,int,int) */ public void getText (StringBuffer buffer) { getText (buffer, 0, mSource.offset ()); } /** * Put the text identified by the given limits into the given array at the specified offset. * @param array The array of characters. * @param offset The starting position in the array where characters are to be placed. * @param start The starting position, zero based. * @param end The ending position * (exclusive, i.e. the character at the ending position is not included), * zero based. * @exception IllegalArgumentException If an attempt is made to get * characters ahead of the current source offset (character position). */ public void getText (char[] array, int offset, int start, int end) throws IllegalArgumentException { int length; if ((mSource.offset () < start) || (mSource.offset () < end)) throw new IllegalArgumentException ("attempt to extract future characters from source"); if (end < start) { // swap length = end; end = start; start = length; } length = end - start; try { mSource.getCharacters (array, offset, start, end); } catch (IOException ioe) { throw new IllegalArgumentException ( "can't get the " + (end - start) + "characters at position " + start + " - " + ioe.getMessage ()); } } /** * Get the text line the position of the cursor lies on. * @param cursor The position to calculate for. * @return The contents of the URL or file corresponding to the line number * containing the cursor position. */ public String getLine (Cursor cursor) { int line; int size; int start; int end; line = row (cursor); size = mIndex.size (); if (line < size) { start = mIndex.elementAt (line); line++; if (line <= size) end = mIndex.elementAt (line); else end = mSource.offset (); } else // current line { start = mIndex.elementAt (line - 1); end = mSource.offset (); } return (getText (start, end)); } /** * Get the text line the position of the cursor lies on. * @param position The position to calculate for. * @return The contents of the URL or file corresponding to the line number * containg the cursor position. */ public String getLine (int position) { return (getLine (new Cursor (this, position))); } /** * Display some of this page as a string. * @return The last few characters the source read in. */ public String toString () { StringBuffer buffer; int start; String ret; if (mSource.offset () > 0) { buffer = new StringBuffer (43); start = mSource.offset () - 40; if (0 > start) start = 0; else buffer.append ("..."); getText (buffer, start, mSource.offset ()); ret = buffer.toString (); } else ret = super.toString (); return (ret); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -