📄 minimalhtmlwriter.java
字号:
* * @exception IOException on any I/O error */ protected void writeLeaf(Element elem) throws IOException { indent(); if (elem.getName() == StyleConstants.IconElementName) { writeImage(elem); } else if (elem.getName() == StyleConstants.ComponentElementName) { writeComponent(elem); } } /** * Responsible for handling Icon Elements; * deliberately unimplemented. How to implement this method is * an issue of policy. For example, if you're generating * an <img> tag, how should you * represent the src attribute (the location of the image)? * In certain cases it could be a URL, in others it could * be read from a stream. * * @param elem element of type StyleConstants.IconElementName */ protected void writeImage(Element elem) throws IOException { } /** * Responsible for handling Component Elements; * deliberately unimplemented. * How this method is implemented is a matter of policy. */ protected void writeComponent(Element elem) throws IOException { } /** * Returns true if the element is a text element. * */ protected boolean isText(Element elem) { return (elem.getName() == AbstractDocument.ContentElementName); } /** * Writes out the attribute set * in an HTML-compliant manner. * * @exception IOException on any I/O error * @exception BadLocationException if pos represents an invalid * location within the document. */ protected void writeContent(Element elem, boolean needsIndenting) throws IOException, BadLocationException { AttributeSet attr = elem.getAttributes(); writeNonHTMLAttributes(attr); if (needsIndenting) { indent(); } writeHTMLTags(attr); text(elem); } /** * Generates * bold <b>, italic <i>, and <u> tags for the * text based on its attribute settings. * * @exception IOException on any I/O error */ protected void writeHTMLTags(AttributeSet attr) throws IOException { int oldMask = fontMask; setFontMask(attr); int endMask = 0; int startMask = 0; if ((oldMask & BOLD) != 0) { if ((fontMask & BOLD) == 0) { endMask |= BOLD; } } else if ((fontMask & BOLD) != 0) { startMask |= BOLD; } if ((oldMask & ITALIC) != 0) { if ((fontMask & ITALIC) == 0) { endMask |= ITALIC; } } else if ((fontMask & ITALIC) != 0) { startMask |= ITALIC; } if ((oldMask & UNDERLINE) != 0) { if ((fontMask & UNDERLINE) == 0) { endMask |= UNDERLINE; } } else if ((fontMask & UNDERLINE) != 0) { startMask |= UNDERLINE; } writeEndMask(endMask); writeStartMask(startMask); } /** * Tweaks the appropriate bits of fontMask * to reflect whether the text is to be displayed in * bold, italic, and/or with an underline. * */ private void setFontMask(AttributeSet attr) { if (StyleConstants.isBold(attr)) { fontMask |= BOLD; } if (StyleConstants.isItalic(attr)) { fontMask |= ITALIC; } if (StyleConstants.isUnderline(attr)) { fontMask |= UNDERLINE; } } /** * Writes out start tags <u>, <i>, and <b> based on * the mask settings. * * @exception IOException on any I/O error */ private void writeStartMask(int mask) throws IOException { if (mask != 0) { if ((mask & UNDERLINE) != 0) { write("<u>"); } if ((mask & ITALIC) != 0) { write("<i>"); } if ((mask & BOLD) != 0) { write("<b>"); } } } /** * Writes out end tags for <u>, <i>, and <b> based on * the mask settings. * * @exception IOException on any I/O error */ private void writeEndMask(int mask) throws IOException { if (mask != 0) { if ((mask & BOLD) != 0) { write("</b>"); } if ((mask & ITALIC) != 0) { write("</i>"); } if ((mask & UNDERLINE) != 0) { write("</u>"); } } } /** * Writes out the remaining * character-level attributes (attributes other than bold, * italic, and underline) in an HTML-compliant way. Given that * attributes such as font family and font size have no direct * mapping to HTML tags, a <span> tag is generated and its * style attribute is set to contain the list of remaining * attributes just like inline styles. * * @exception IOException on any I/O error */ protected void writeNonHTMLAttributes(AttributeSet attr) throws IOException { String style = ""; String separator = "; "; if (inFontTag() && fontAttributes.isEqual(attr)) { return; } boolean first = true; Color color = (Color)attr.getAttribute(StyleConstants.Foreground); if (color != null) { style += "color: " + css.styleConstantsValueToCSSValue ((StyleConstants)StyleConstants.Foreground, color); first = false; } Integer size = (Integer)attr.getAttribute(StyleConstants.FontSize); if (size != null) { if (!first) { style += separator; } style += "font-size: " + size.intValue() + "pt"; first = false; } String family = (String)attr.getAttribute(StyleConstants.FontFamily); if (family != null) { if (!first) { style += separator; } style += "font-family: " + family; first = false; } if (style.length() > 0) { if (fontMask != 0) { writeEndMask(fontMask); fontMask = 0; } startSpanTag(style); fontAttributes = attr; } else if (fontAttributes != null) { writeEndMask(fontMask); fontMask = 0; endSpanTag(); } } /** * Returns true if we are currently in a <font> tag. */ protected boolean inFontTag() { return (fontAttributes != null); } /** * This is no longer used, instead <span> will be written out. * <p> * Writes out an end tag for the <font> tag. * * @exception IOException on any I/O error */ protected void endFontTag() throws IOException { write(NEWLINE); writeEndTag("</font>"); fontAttributes = null; } /** * This is no longer used, instead <span> will be written out. * <p> * Writes out a start tag for the <font> tag. * Because font tags cannot be nested, * this method closes out * any enclosing font tag before writing out a * new start tag. * * @exception IOException on any I/O error */ protected void startFontTag(String style) throws IOException { boolean callIndent = false; if (inFontTag()) { endFontTag(); callIndent = true; } writeStartTag("<font style=\"" + style + "\">"); if (callIndent) { indent(); } } /** * Writes out a start tag for the <font> tag. * Because font tags cannot be nested, * this method closes out * any enclosing font tag before writing out a * new start tag. * * @exception IOException on any I/O error */ private void startSpanTag(String style) throws IOException { boolean callIndent = false; if (inFontTag()) { endSpanTag(); callIndent = true; } writeStartTag("<span style=\"" + style + "\">"); if (callIndent) { indent(); } } /** * Writes out an end tag for the <span> tag. * * @exception IOException on any I/O error */ private void endSpanTag() throws IOException { write(NEWLINE); writeEndTag("</span>"); fontAttributes = null; } /** * Adds the style named <code>style</code> to the style mapping. This * returns the name that should be used when outputting. CSS does not * allow the full Unicode set to be used as a style name. */ private String addStyleName(String style) { if (styleNameMapping == null) { return style; } StringBuffer sb = null; for (int counter = style.length() - 1; counter >= 0; counter--) { if (!isValidCharacter(style.charAt(counter))) { if (sb == null) { sb = new StringBuffer(style); } sb.setCharAt(counter, 'a'); } } String mappedName = (sb != null) ? sb.toString() : style; while (styleNameMapping.get(mappedName) != null) { mappedName = mappedName + 'x'; } styleNameMapping.put(style, mappedName); return mappedName; } /** * Returns the mapped style name corresponding to <code>style</code>. */ private String mapStyleName(String style) { if (styleNameMapping == null) { return style; } String retValue = (String)styleNameMapping.get(style); return (retValue == null) ? style : retValue; } private boolean isValidCharacter(char character) { return ((character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z')); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -