Methods Summary |
---|
private java.lang.String | addStyleName(java.lang.String style)Adds the style named style 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.
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;
|
protected void | endFontTag()This is no longer used, instead <span> will be written out.
Writes out an end tag for the <font> tag.
write(NEWLINE);
writeEndTag("</font>");
fontAttributes = null;
|
private void | endSpanTag()Writes out an end tag for the <span> tag.
write(NEWLINE);
writeEndTag("</span>");
fontAttributes = null;
|
protected boolean | inFontTag()Returns true if we are currently in a <font> tag.
return (fontAttributes != null);
|
protected boolean | isText(javax.swing.text.Element elem)Returns true if the element is a text element.
return (elem.getName() == AbstractDocument.ContentElementName);
|
private boolean | isValidCharacter(char character)
return ((character >= 'a" && character <= 'z") ||
(character >= 'A" && character <= 'Z"));
|
private java.lang.String | mapStyleName(java.lang.String style)Returns the mapped style name corresponding to style .
if (styleNameMapping == null) {
return style;
}
String retValue = (String)styleNameMapping.get(style);
return (retValue == null) ? style : retValue;
|
private void | setFontMask(javax.swing.text.AttributeSet attr)Tweaks the appropriate bits of fontMask
to reflect whether the text is to be displayed in
bold, italic, and/or with an underline.
if (StyleConstants.isBold(attr)) {
fontMask |= BOLD;
}
if (StyleConstants.isItalic(attr)) {
fontMask |= ITALIC;
}
if (StyleConstants.isUnderline(attr)) {
fontMask |= UNDERLINE;
}
|
protected void | startFontTag(java.lang.String style)This is no longer used, instead <span> will be written out.
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.
boolean callIndent = false;
if (inFontTag()) {
endFontTag();
callIndent = true;
}
writeStartTag("<font style=\"" + style + "\">");
if (callIndent) {
indent();
}
|
private void | startSpanTag(java.lang.String style)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.
boolean callIndent = false;
if (inFontTag()) {
endSpanTag();
callIndent = true;
}
writeStartTag("<span style=\"" + style + "\">");
if (callIndent) {
indent();
}
|
protected void | text(javax.swing.text.Element elem)Writes out text.
String contentStr = getText(elem);
if ((contentStr.length() > 0) &&
(contentStr.charAt(contentStr.length()-1) == NEWLINE)) {
contentStr = contentStr.substring(0, contentStr.length()-1);
}
if (contentStr.length() > 0) {
write(contentStr);
}
|
public void | write()Generates HTML output
from a StyledDocument.
styleNameMapping = new Hashtable();
writeStartTag("<html>");
writeHeader();
writeBody();
writeEndTag("</html>");
|
protected void | writeAttributes(javax.swing.text.AttributeSet attr)Writes out all the attributes for the
following types:
StyleConstants.ParagraphConstants,
StyleConstants.CharacterConstants,
StyleConstants.FontConstants,
StyleConstants.ColorConstants.
The attribute name and value are separated by a colon.
Each pair is separated by a semicolon.
Enumeration attributeNames = attr.getAttributeNames();
while (attributeNames.hasMoreElements()) {
Object name = attributeNames.nextElement();
if ((name instanceof StyleConstants.ParagraphConstants) ||
(name instanceof StyleConstants.CharacterConstants) ||
(name instanceof StyleConstants.FontConstants) ||
(name instanceof StyleConstants.ColorConstants)) {
indent();
write(name.toString());
write(':");
write(css.styleConstantsValueToCSSValue
((StyleConstants)name, attr.getAttribute(name)).
toString());
write(';");
write(NEWLINE);
}
}
|
protected void | writeBody()Iterates over the elements in the document
and processes elements based on whether they are
branch elements or leaf elements. This method specially handles
leaf elements that are text.
ElementIterator it = getElementIterator();
/*
This will be a section element for a styled document.
We represent this element in HTML as the body tags.
Therefore we ignore it.
*/
it.current();
Element next = null;
writeStartTag("<body>");
boolean inContent = false;
while((next = it.next()) != null) {
if (!inRange(next)) {
continue;
}
if (next instanceof AbstractDocument.BranchElement) {
if (inContent) {
writeEndParagraph();
inContent = false;
fontMask = 0;
}
writeStartParagraph(next);
} else if (isText(next)) {
writeContent(next, !inContent);
inContent = true;
} else {
writeLeaf(next);
inContent = true;
}
}
if (inContent) {
writeEndParagraph();
}
writeEndTag("</body>");
|
protected void | writeComponent(javax.swing.text.Element elem)Responsible for handling Component Elements;
deliberately unimplemented.
How this method is implemented is a matter of policy.
|
protected void | writeContent(javax.swing.text.Element elem, boolean needsIndenting)Writes out the attribute set
in an HTML-compliant manner.
AttributeSet attr = elem.getAttributes();
writeNonHTMLAttributes(attr);
if (needsIndenting) {
indent();
}
writeHTMLTags(attr);
text(elem);
|
private void | writeEndMask(int mask)Writes out end tags for <u>, <i>, and <b> based on
the mask settings.
if (mask != 0) {
if ((mask & BOLD) != 0) {
write("</b>");
}
if ((mask & ITALIC) != 0) {
write("</i>");
}
if ((mask & UNDERLINE) != 0) {
write("</u>");
}
}
|
protected void | writeEndParagraph()Emits an end tag for a <p>
tag. Before writing out the tag, this method ensures
that all other tags that have been opened are
appropriately closed off.
writeEndMask(fontMask);
if (inFontTag()) {
endSpanTag();
} else {
write(NEWLINE);
}
writeEndTag("</p>");
|
protected void | writeEndTag(java.lang.String endTag)Writes out an end tag appropriately
indented. Also decrements the indent level.
decrIndent();
indent();
write(endTag);
write(NEWLINE);
|
protected void | writeHTMLTags(javax.swing.text.AttributeSet attr)Generates
bold <b>, italic <i>, and <u> tags for the
text based on its attribute settings.
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);
|
protected void | writeHeader()Writes out the <head> and <style>
tags, and then invokes writeStyles() to write
out all the named styles as the content of the
<style> tag. The content is surrounded by
valid HTML comment markers to ensure that the
document is viewable in applications/browsers
that do not support the tag.
writeStartTag("<head>");
writeStartTag("<style>");
writeStartTag("<!--");
writeStyles();
writeEndTag("-->");
writeEndTag("</style>");
writeEndTag("</head>");
|
protected void | writeImage(javax.swing.text.Element 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.
|
protected void | writeLeaf(javax.swing.text.Element elem)Responsible for writing out other non-text leaf
elements.
indent();
if (elem.getName() == StyleConstants.IconElementName) {
writeImage(elem);
} else if (elem.getName() == StyleConstants.ComponentElementName) {
writeComponent(elem);
}
|
protected void | writeNonHTMLAttributes(javax.swing.text.AttributeSet attr)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.
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();
}
|
private void | writeStartMask(int mask)Writes out start tags <u>, <i>, and <b> based on
the mask settings.
if (mask != 0) {
if ((mask & UNDERLINE) != 0) {
write("<u>");
}
if ((mask & ITALIC) != 0) {
write("<i>");
}
if ((mask & BOLD) != 0) {
write("<b>");
}
}
|
protected void | writeStartParagraph(javax.swing.text.Element elem)Emits the start tag for a paragraph. If
the paragraph has a named style associated with it,
then this method also generates a class attribute for the
<p> tag and sets its value to be the name of the
style.
AttributeSet attr = elem.getAttributes();
Object resolveAttr = attr.getAttribute(StyleConstants.ResolveAttribute);
if (resolveAttr instanceof StyleContext.NamedStyle) {
writeStartTag("<p class=" + mapStyleName(((StyleContext.NamedStyle)resolveAttr).getName()) + ">");
} else {
writeStartTag("<p>");
}
|
protected void | writeStartTag(java.lang.String tag)Writes out a start tag appropriately
indented. Also increments the indent level.
indent();
write(tag);
write(NEWLINE);
incrIndent();
|
protected void | writeStyles()Writes out all the named styles as the
content of the <style> tag.
/*
* Access to DefaultStyledDocument done to workaround
* a missing API in styled document to access the
* stylenames.
*/
DefaultStyledDocument styledDoc = ((DefaultStyledDocument)getDocument());
Enumeration styleNames = styledDoc.getStyleNames();
while (styleNames.hasMoreElements()) {
Style s = styledDoc.getStyle((String)styleNames.nextElement());
/** PENDING: Once the name attribute is removed
from the list we check check for 0. **/
if (s.getAttributeCount() == 1 &&
s.isDefined(StyleConstants.NameAttribute)) {
continue;
}
indent();
write("p." + addStyleName(s.getName()));
write(" {\n");
incrIndent();
writeAttributes(s);
decrIndent();
indent();
write("}\n");
}
|