int startOffset = 0;
int endOffset = 0;
int crtOffset = 0;
String chunk = null;
JRPrintHyperlink hyperlink = null;
Element element = null;
Element parent = null;
boolean bodyOccurred = false;
int[] orderedListIndex = new int[elements.size()];
String whitespace = " ";
String[] whitespaces = new String[elements.size()];
for(int i = 0; i < elements.size(); i++)
{
whitespaces[i] = "";
}
JRStyledText styledText = new JRStyledText();
for(int i = 0; i < elements.size(); i++)
{
if (bodyOccurred && chunk != null)
{
styledText.append(chunk);
Map styleAttributes = getAttributes(element.getAttributes());
if (hyperlink != null)
{
styleAttributes.put(JRTextAttribute.HYPERLINK, hyperlink);
hyperlink = null;
}
if (!styleAttributes.isEmpty())
{
styledText.addRun(new JRStyledText.Run(styleAttributes,
startOffset + crtOffset, endOffset + crtOffset));
}
}
chunk = null;
element = (Element)elements.get(i);
parent = element.getParentElement();
startOffset = element.getStartOffset();
endOffset = element.getEndOffset();
AttributeSet attrs = element.getAttributes();
Object elementName = attrs.getAttribute(AbstractDocument.ElementNameAttribute);
Object object = (elementName != null) ? null : attrs.getAttribute(StyleConstants.NameAttribute);
if (object instanceof HTML.Tag)
{
HTML.Tag htmlTag = (HTML.Tag) object;
if(htmlTag == Tag.BODY)
{
bodyOccurred = true;
crtOffset = - startOffset;
}
else if(htmlTag == Tag.BR)
{
chunk = "\n";
}
else if(htmlTag == Tag.OL)
{
orderedListIndex[i] = 0;
String parentName = parent.getName().toLowerCase();
whitespaces[i] = whitespaces[elements.indexOf(parent)] + whitespace;
if(parentName.equals("li"))
{
chunk = "";
}
else
{
chunk = "\n";
++crtOffset;
}
}
else if(htmlTag == Tag.UL)
{
whitespaces[i] = whitespaces[elements.indexOf(parent)] + whitespace;
String parentName = parent.getName().toLowerCase();
if(parentName.equals("li"))
{
chunk = "";
}
else
{
chunk = "\n";
++crtOffset;
}
}
else if(htmlTag == Tag.LI)
{
whitespaces[i] = whitespaces[elements.indexOf(parent)];
if(element.getElement(0) != null &&
(element.getElement(0).getName().toLowerCase().equals("ol") || element.getElement(0).getName().toLowerCase().equals("ul"))
)
{
chunk = "";
}
else if(parent.getName().equals("ol"))
{
int index = elements.indexOf(parent);
chunk = whitespaces[index] + String.valueOf(++orderedListIndex[index]) + ". ";
}
else
{
chunk = whitespaces[elements.indexOf(parent)] + "\u2022 ";
}
crtOffset += chunk.length();
}
else if (element instanceof LeafElement)
{
if (element instanceof RunElement)
{
RunElement runElement = (RunElement)element;
AttributeSet attrSet = (AttributeSet)runElement.getAttribute(Tag.A);
if (attrSet != null)
{
hyperlink = new JRBasePrintHyperlink();
hyperlink.setHyperlinkType(HyperlinkTypeEnum.REFERENCE);
hyperlink.setHyperlinkReference((String)attrSet.getAttribute(HTML.Attribute.HREF));
hyperlink.setLinkTarget((String)attrSet.getAttribute(HTML.Attribute.TARGET));
}
}
try
{
chunk = document.getText(startOffset, endOffset - startOffset);