xmlprinter.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,723 行 · 第 1/3 页
JAVA
1,723 行
if (! _isHtml || code < 0) { _indent -= 2; } if (_lastTextChar <= WHITESPACE) { if (_lastTextChar != NEWLINE) println(); for (int i = 0; i < _indent; i++) print(' '); } _lastTextChar = NULL_SPACE; } /** * Handle the pretty printing after the closing of a tag. */ private void closePretty(String qName) { int code = _isHtml ? _prettyMap.get(qName.toLowerCase()) : -1; if (code == PRE) { _preCount--; _lastTextChar = NULL_SPACE; return; } if (_preCount > 0) return; if (! _isHtml || code < 0) { _indent -= 2; } if (code != NO_PRETTY) _lastTextChar = NULL_SPACE; else _lastTextChar = 'a'; } /** * Prints a processing instruction * * @param name the name of the processing instruction * @param data the processing instruction data */ public void processingInstruction(String name, String data) throws IOException { if (_isText) return; if (_currentElement != null) completeOpenTag(); if (_isTop && ! _isHtml && ! _isAutomaticMethod) { printHeader(null); _isTop = false; } print("<?"); print(name); if (data != null && data.length() > 0) { print(" "); print(data); } if (isHtml()) print(">"); else print("?>"); _lastTextChar = NULL_SPACE; } /** * Prints a comment * * @param data the comment data */ public void comment(String data) throws IOException { if (_isText) return; int textChar = _lastTextChar; if (_currentElement != null) completeOpenTag(); if (_isPretty && _preCount <= 0 && (textChar == OMITTED_NEWLINE || textChar == NULL_SPACE)) { println(); for (int i = 0; i < _indent; i++) print(' '); } print("<!--"); print(data); print("-->"); _lastTextChar = NULL_SPACE; } /** * Returns true if the text is currently being escaped */ public boolean getEscapeText() { return _escapeText; } /** * Sets true if the text should be escaped, else it will be printed * verbatim. */ public void setEscapeText(boolean isEscaped) { _escapeText = isEscaped; } /** * Prints text. If the text is escaped, codes like < will be printed as * &lt;. */ public void text(String text) throws IOException { int length = text.length(); for (int offset = 0; offset < length; offset += _cbuf.length) { int sublen = length - offset; if (sublen > _cbuf.length) sublen = _cbuf.length; text.getChars(offset, offset + sublen, _cbuf, 0); text(_cbuf, 0, sublen); } } /** * Prints text. If the text is escaped, codes like < will be printed as * &lt;. */ public void text(char []buffer, int offset, int length) throws IOException { if (length == 0) return; int prevTextChar = _lastTextChar; if ((_isPretty && _preCount <= 0 || _isTop) && ! _isText && trimPrettyWhitespace(buffer, offset, length)) { if (prevTextChar <= WHITESPACE) return; if (_lastTextChar == OMITTED_SPACE) _lastTextChar = SPACE; if (_lastTextChar == OMITTED_NEWLINE) _lastTextChar = NEWLINE; } int nextTextChar = _lastTextChar; if (_currentElement != null) { completeOpenTag(); if (_isPretty && _preCount <= 0 && prevTextChar <= OMITTED) println(); } _lastTextChar = nextTextChar; if (! _isTop) { } else if (! _isJsp) { _isTop = false; } else if (_isAutomaticMethod) { } else if (! _isHtml) { printHeader(null); _isTop = false; } if (_isHtml && ! _hasMetaContentType && ! _inHead) { int textChar = _lastTextChar; if (_isPretty && _preCount <= 0 && prevTextChar <= OMITTED) println(); // printHeadContentType(); _lastTextChar = textChar; prevTextChar = 'a'; } if (! _isPretty || _preCount > 0) { } else if (prevTextChar == OMITTED_NEWLINE) { if (buffer[offset] != '\n') println(); } else if (prevTextChar == OMITTED_SPACE) { char ch = buffer[offset]; if (ch != ' ' && ch != '\n') print(' '); } if (_lineMap == null) { } else if (_locator != null) { _lineMap.add(_locator.getFilename(), _locator.getLineNumber(), _line); } else if (_srcFilename != null) _lineMap.add(_srcFilename, _srcLine, _line); if (! _escapeText || _inVerbatim || _entities == null) print(buffer, offset, length); else _entities.printText(this, buffer, offset, length, false); } /** * If the text is completely whitespace, skip it. */ boolean trimPrettyWhitespace(char []buffer, int offset, int length) { char textChar = 'a'; int i = length - 1; for (; i >= 0; i--) { char ch = buffer[offset + i]; if (ch == '\r' || ch == '\n') { if (textChar != NEWLINE) textChar = OMITTED_NEWLINE; } else if (ch == ' ' || ch == '\t') { if (textChar == 'a' || textChar == NULL_SPACE) textChar = OMITTED_SPACE; } else if (textChar == OMITTED_NEWLINE) { textChar = NEWLINE; break; } else if (textChar == OMITTED_SPACE) { textChar = SPACE; break; } else break; } _lastTextChar = textChar; return (i < 0 && textChar <= WHITESPACE); } public void cdata(String text) throws IOException { if (text.length() == 0) return; _isTop = false; if (_currentElement != null) completeOpenTag(); if (_lineMap != null && _srcFilename != null) _lineMap.add(_srcFilename, _srcLine, _line); print("<![CDATA["); print(text); print("]]>"); _lastTextChar = NEWLINE; } private void completeOpenTag() throws IOException { boolean isHead = (_isHtml && ! _hasMetaContentType && _currentElement.equalsIgnoreCase("head")); finishAttributes(); print(">"); if (isHead) printHeadContentType(); } public void cdata(char []buffer, int offset, int length) throws IOException { cdata(new String(buffer, offset, length)); } private void printHeadContentType() throws IOException { printMetaContentType(); } private void printMetaContentType() throws IOException { if (! _includeContentType) return; _hasMetaContentType = true; if (_lastTextChar != NEWLINE) println(); if (_encoding == null || _encoding.equals("US-ASCII")) _encoding = "ISO-8859-1"; String mimeType = _mimeType; if (mimeType == null) mimeType = "text/html"; println(" <meta http-equiv=\"Content-Type\" content=\"" + mimeType + "; charset=" + _encoding + "\">"); _lastTextChar = NEWLINE; } void printDecl(String text) throws IOException { for (int i = 0; i < text.length(); i++) { char ch = text.charAt(i); switch (ch) { case '&': if (i + 1 < text.length() && text.charAt(i + 1) == '#') print("&"); else print(ch); break; case '"': print("""); break; case '\'': print("'"); break; case '\n': print("\n"); break; default: print(ch); } } } /** * Prints a newline to the output stream. */ void println() throws IOException { print('\n'); } void println(String text) throws IOException { print(text); println(); } /** * Prints a char buffer. */ void print(char []buf) throws IOException { print(buf, 0, buf.length); } /** * Prints a char buffer. */ void print(char []buf, int off, int len) throws IOException { for (int i = 0; i < len; i++) print(buf[off + i]); } /** * Prints a chunk of text. */ void print(String text) throws IOException { int len = text.length(); for (int i = 0; i < len; i++) { char ch = text.charAt(i); print(ch); } } /** * Prints a character. */ void print(char ch) throws IOException { if (_capacity <= _length) { _os.print(_buffer, 0, _length); _length = 0; } _buffer[_length++] = ch; if (ch == '\n') _line++; } /** * Prints an integer to the output stream. */ void print(int i) throws IOException { if (i < 0) { } else if (i < 10) { print((char) ('0' + i)); return; } else if (i < 100) { print((char) ('0' + i / 10)); print((char) ('0' + i % 10)); return; } if (_length >= 0) { _os.print(_buffer, 0, _length); _length = 0; } _os.print(i); } private void flush() throws IOException { if (_length >= 0) { _os.print(_buffer, 0, _length); _length = 0; } if (_isEnclosedStream) _os.flush(); } private void close() throws IOException { flush(); if (_isEnclosedStream) _os.close(); } static void add(IntMap map, String name, int value) { map.put(name, value); map.put(name.toUpperCase(), value); } static void add(HashMap<String,String> map, String name) { map.put(name, name); map.put(name.toUpperCase(), name); } static { _empties = new IntMap(); add(_empties, "basefont", ALWAYS_EMPTY); add(_empties, "br", ALWAYS_EMPTY); add(_empties, "area", ALWAYS_EMPTY); add(_empties, "link", ALWAYS_EMPTY); add(_empties, "img", ALWAYS_EMPTY); add(_empties, "param", ALWAYS_EMPTY); add(_empties, "hr", ALWAYS_EMPTY); add(_empties, "input", ALWAYS_EMPTY); add(_empties, "col", ALWAYS_EMPTY); add(_empties, "frame", ALWAYS_EMPTY); add(_empties, "isindex", ALWAYS_EMPTY); add(_empties, "base", ALWAYS_EMPTY); add(_empties, "meta", ALWAYS_EMPTY); add(_empties, "p", ALWAYS_EMPTY); add(_empties, "li", ALWAYS_EMPTY); add(_empties, "option", EMPTY_IF_EMPTY); _booleanAttrs = new HashMap<String,String>(); // input add(_booleanAttrs, "checked"); // dir, menu, dl, ol, ul add(_booleanAttrs, "compact"); // object add(_booleanAttrs, "declare"); // script add(_booleanAttrs, "defer"); // button, input, optgroup, option, select, textarea add(_booleanAttrs, "disabled"); // img add(_booleanAttrs, "ismap"); // select add(_booleanAttrs, "multiple"); // area add(_booleanAttrs, "nohref"); // frame add(_booleanAttrs, "noresize"); // hr add(_booleanAttrs, "noshade"); // td, th add(_booleanAttrs, "nowrap"); // textarea, input add(_booleanAttrs, "readonly"); // option add(_booleanAttrs, "selected"); _prettyMap = new IntMap(); // next two break browsers add(_prettyMap, "img", NO_PRETTY); add(_prettyMap, "a", NO_PRETTY); add(_prettyMap, "embed", NO_PRETTY); add(_prettyMap, "th", NO_PRETTY); add(_prettyMap, "td", NO_PRETTY); // inline tags look better without the indent add(_prettyMap, "tt", INLINE); add(_prettyMap, "i", INLINE); add(_prettyMap, "b", INLINE); add(_prettyMap, "big", INLINE); add(_prettyMap, "em", INLINE); add(_prettyMap, "string", INLINE); add(_prettyMap, "dfn", INLINE); add(_prettyMap, "code", INLINE); add(_prettyMap, "samp", INLINE); add(_prettyMap, "kbd", INLINE); add(_prettyMap, "var", INLINE); add(_prettyMap, "cite", INLINE); add(_prettyMap, "abbr", INLINE); add(_prettyMap, "acronym", INLINE); add(_prettyMap, "object", INLINE); add(_prettyMap, "q", INLINE); add(_prettyMap, "sub", INLINE); add(_prettyMap, "sup", INLINE); add(_prettyMap, "font", INLINE); add(_prettyMap, "small", INLINE); add(_prettyMap, "span", INLINE); add(_prettyMap, "bdo", INLINE); add(_prettyMap, "jsp:expression", INLINE); add(_prettyMap, "textarea", PRE); add(_prettyMap, "pre", PRE); add(_prettyMap, "html", NO_INDENT); add(_prettyMap, "body", NO_INDENT); add(_prettyMap, "ul", NO_INDENT); add(_prettyMap, "table", NO_INDENT); add(_prettyMap, "frameset", NO_INDENT); _verbatimTags = new HashMap<String,String>(); add(_verbatimTags, "script"); add(_verbatimTags, "style"); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?