📄 pprint.java
字号:
{
addC('&', linelen++);
if (this.configuration.NumEntities)
{
addC('#', linelen++);
addC('1', linelen++);
addC('6', linelen++);
addC('0', linelen++);
}
else
{
addC('n', linelen++);
addC('b', linelen++);
addC('s', linelen++);
addC('p', linelen++);
}
addC(';', linelen++);
}
else
addC(c, linelen++);
return;
}
}
/* otherwise ISO 2022 characters are passed raw */
if (this.configuration.CharEncoding == Configuration.ISO2022 ||
this.configuration.CharEncoding == Configuration.RAW)
{
addC(c, linelen++);
return;
}
/* if preformatted text, map to space */
if (c == 160 && ((mode & PREFORMATTED) != 0))
{
addC(' ', linelen++);
return;
}
/*
Filters from Word and PowerPoint often use smart
quotes resulting in character codes between 128
and 159. Unfortunately, the corresponding HTML 4.0
entities for these are not widely supported. The
following converts dashes and quotation marks to
the nearest ASCII equivalent. My thanks to
Andrzej Novosiolov for his help with this code.
*/
if (this.configuration.MakeClean)
{
if (c >= 0x2013 && c <= 0x201E)
{
switch (c) {
case 0x2013:
case 0x2014:
c = '-';
break;
case 0x2018:
case 0x2019:
case 0x201A:
c = '\'';
break;
case 0x201C:
case 0x201D:
case 0x201E:
c = '"';
break;
}
}
}
/* don't map latin-1 chars to entities */
if (this.configuration.CharEncoding == Configuration.LATIN1)
{
if (c > 255) /* multi byte chars */
{
if (!this.configuration.NumEntities)
{
entity = EntityTable.getDefaultEntityTable().entityName((short)c);
if (entity != null)
entity = "&" + entity + ";";
else
entity = "&#" + c + ";";
}
else
entity = "&#" + c + ";";
for (int i = 0; i < entity.length(); i++)
addC((int)entity.charAt(i), linelen++);
return;
}
if (c > 126 && c < 160)
{
entity = "&#" + c + ";";
for (int i = 0; i < entity.length(); i++)
addC((int)entity.charAt(i), linelen++);
return;
}
addC(c, linelen++);
return;
}
/* don't map utf8 chars to entities */
if (this.configuration.CharEncoding == Configuration.UTF8)
{
addC(c, linelen++);
return;
}
/* use numeric entities only for XML */
if (this.configuration.XmlTags)
{
/* if ASCII use numeric entities for chars > 127 */
if (c > 127 && this.configuration.CharEncoding == Configuration.ASCII)
{
entity = "&#" + c + ";";
for (int i = 0; i < entity.length(); i++)
addC((int)entity.charAt(i), linelen++);
return;
}
/* otherwise output char raw */
addC(c, linelen++);
return;
}
/* default treatment for ASCII */
if (c > 126 || (c < ' ' && c != '\t'))
{
if (!this.configuration.NumEntities)
{
entity = EntityTable.getDefaultEntityTable().entityName((short)c);
if (entity != null)
entity = "&" + entity + ";";
else
entity = "&#" + c + ";";
}
else
entity = "&#" + c + ";";
for (int i = 0; i < entity.length(); i++)
addC((int)entity.charAt(i), linelen++);
return;
}
addC(c, linelen++);
}
/*
The line buffer is uint not char so we can
hold Unicode values unencoded. The translation
to UTF-8 is deferred to the outc routine called
to flush the line buffer.
*/
private void printText(Out fout, short mode, int indent,
byte[] textarray, int start, int end)
{
int i, c;
MutableInteger ci = new MutableInteger();
for (i = start; i < end; ++i)
{
if (indent + linelen >= this.configuration.wraplen)
wrapLine(fout, indent);
c = ((int)textarray[i]) & 0xFF; // Convert to unsigned.
/* look for UTF-8 multibyte character */
if (c > 0x7F)
{
i += getUTF8(textarray, i, ci);
c = ci.value;
}
if (c == '\n')
{
flushLine(fout, indent);
continue;
}
printChar(c, mode);
}
}
private void printString(Out fout, int indent, String str)
{
for (int i = 0; i < str.length(); i++ )
addC((int)str.charAt(i), linelen++);
}
private void printAttrValue(Out fout, int indent, String value, int delim, boolean wrappable)
{
int c;
MutableInteger ci = new MutableInteger();
boolean wasinstring = false;
byte[] valueChars = null;
int i;
short mode = (wrappable ? (short)(NORMAL | ATTRIBVALUE) :
(short)(PREFORMATTED | ATTRIBVALUE));
if (value != null)
{
valueChars = Lexer.getBytes(value);
}
/* look for ASP, Tango or PHP instructions for computed attribute value */
if (valueChars != null && valueChars.length >= 5 && valueChars[0] == '<')
{
if (valueChars[1] == '%' || valueChars[1] == '@'||
(new String(valueChars, 0, 5)).equals("<?php"))
mode |= CDATA;
}
if (delim == 0)
delim = '"';
addC('=', linelen++);
/* don't wrap after "=" for xml documents */
if (!this.configuration.XmlOut) {
if (indent + linelen < this.configuration.wraplen)
wraphere = linelen;
if (indent + linelen >= this.configuration.wraplen)
wrapLine(fout, indent);
if (indent + linelen < this.configuration.wraplen)
wraphere = linelen;
else
condFlushLine(fout, indent);
}
addC(delim, linelen++);
if (value != null)
{
InString = false;
i = 0;
while (i < valueChars.length)
{
c = ((int)valueChars[i]) & 0xFF; // Convert to unsigned.
if (wrappable && c == ' ' && indent + linelen < this.configuration.wraplen)
{
wraphere = linelen;
wasinstring = InString;
}
if (wrappable && wraphere > 0 && indent + linelen >= this.configuration.wraplen)
wrapAttrVal(fout, indent, wasinstring);
if (c == delim)
{
String entity;
entity = (c == '"' ? """ : "'");
for (int j = 0; j < entity.length(); j++ )
addC(entity.charAt(j), linelen++);
++i;
continue;
}
else if (c == '"')
{
if (this.configuration.QuoteMarks)
{
addC('&', linelen++);
addC('q', linelen++);
addC('u', linelen++);
addC('o', linelen++);
addC('t', linelen++);
addC(';', linelen++);
}
else
addC('"', linelen++);
if (delim == '\'')
InString = !InString;
++i;
continue;
}
else if (c == '\'')
{
if (this.configuration.QuoteMarks)
{
addC('&', linelen++);
addC('#', linelen++);
addC('3', linelen++);
addC('9', linelen++);
addC(';', linelen++);
}
else
addC('\'', linelen++);
if (delim == '"')
InString = !InString;
++i;
continue;
}
/* look for UTF-8 multibyte character */
if (c > 0x7F)
{
i += getUTF8(valueChars, i, ci);
c = ci.value;
}
++i;
if (c == '\n')
{
flushLine(fout, indent);
continue;
}
printChar(c, mode);
}
}
InString = false;
addC(delim, linelen++);
}
private void printAttribute(Out fout, int indent, Node node, AttVal attr)
{
String name;
boolean wrappable = false;
if (this.configuration.IndentAttributes)
{
flushLine(fout, indent);
indent += this.configuration.spaces;
}
name = attr.attribute;
if (indent + linelen >= this.configuration.wraplen)
wrapLine(fout, indent);
if (!this.configuration.XmlTags && !this.configuration.XmlOut && attr.dict != null)
{
if (AttributeTable.getDefaultAttributeTable().isScript(name))
wrappable = this.configuration.WrapScriptlets;
else if (!attr.dict.nowrap && this.configuration.WrapAttVals)
wrappable = true;
}
if (indent + linelen < this.configuration.wraplen)
{
wraphere = linelen;
addC(' ', linelen++);
}
else
{
condFlushLine(fout, indent);
addC(' ', linelen++);
}
for (int i = 0; i < name.length(); i++ )
addC((int)Lexer.foldCase(name.charAt(i),
this.configuration.UpperCaseAttrs,
this.configuration.XmlTags),
linelen++);
if (indent + linelen >= this.configuration.wraplen)
wrapLine(fout, indent);
if (attr.value == null)
{
if (this.configuration.XmlTags || this.configuration.XmlOut)
printAttrValue(fout, indent, attr.attribute, attr.delim, true);
else if (!attr.isBoolAttribute() && !Node.isNewNode(node))
printAttrValue(fout, indent, "", attr.delim, true);
else if (indent + linelen < this.configuration.wraplen)
wraphere = linelen;
}
else
printAttrValue(fout, indent, attr.value, attr.delim, wrappable);
}
private void printAttrs(Out fout, int indent,
Node node, AttVal attr)
{
if (attr != null)
{
if (attr.next != null)
printAttrs(fout, indent, node, attr.next);
if (attr.attribute != null)
printAttribute(fout, indent, node, attr);
else if (attr.asp != null)
{
addC(' ', linelen++);
printAsp(fout, indent, attr.asp);
}
else if (attr.php != null)
{
addC(' ', linelen++);
printPhp(fout, indent, attr.php);
}
}
/* add xml:space attribute to pre and other elements */
if (configuration.XmlOut &&
configuration.XmlSpace &&
ParserImpl.XMLPreserveWhiteSpace(node) &&
node.getAttrByName("xml:space") == null)
printString(fout, indent, " xml:space=\"preserve\"");
}
/*
Line can be wrapped immediately after inline start tag provided
if follows a text node ending in a space, or it parent is an
inline element that that rule applies to. This behaviour was
reverse engineered from Netscape 3.0
*/
private static boolean afterSpace(Node node)
{
Node prev;
int c;
if (node == null || node.tag == null || !((node.tag.model & Dict.CM_INLINE) != 0))
return true;
prev = node.prev;
if (prev != null)
{
if (prev.type == Node.TextNode && prev.end > prev.start)
{
c = ((int)prev.textarray[prev.end - 1]) & 0xFF; // Convert to unsigned.
if (c == 160 || c == ' ' || c == '\n')
return true;
}
return false;
}
return afterSpace(node.parent);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -