if (textElement.isLeaf())
{
final AttributeSet attributes = textElement.getAttributes();
if (HTML.Tag.IMG.equals(attributes.getAttribute(StyleConstants.NameAttribute)))
{
final Element result = new Element();
result.setName(textElement.getName());
result.setElementType(ContentType.INSTANCE);
final String src = (String) attributes.getAttribute(HTML.Attribute.SRC);
final String alt = (String) attributes.getAttribute(HTML.Attribute.TITLE);
result.setAttribute(AttributeNames.Core.NAMESPACE, AttributeNames.Core.VALUE, convertURL(src));
result.setAttribute(AttributeNames.Html.NAMESPACE, AttributeNames.Html.TITLE, alt);
result.setAttribute(AttributeNames.Html.NAMESPACE, AttributeNames.Swing.TOOLTIP, alt);
if (attributes.isDefined(HTML.Attribute.WIDTH) &&
attributes.isDefined(HTML.Attribute.HEIGHT))
{
result.getStyle().setStyleProperty(ElementStyleKeys.SCALE, Boolean.TRUE);
result.getStyle().setStyleProperty(ElementStyleKeys.KEEP_ASPECT_RATIO, Boolean.FALSE);
result.getStyle().setStyleProperty(ElementStyleKeys.MIN_WIDTH,
parseLength(String.valueOf(attributes.getAttribute(HTML.Attribute.WIDTH))));
result.getStyle().setStyleProperty(ElementStyleKeys.MIN_HEIGHT,
parseLength(String.valueOf(attributes.getAttribute(HTML.Attribute.HEIGHT))));
}
else if (attributes.isDefined(HTML.Attribute.WIDTH))
{
result.getStyle().setStyleProperty(ElementStyleKeys.SCALE, Boolean.TRUE);
result.getStyle().setStyleProperty(ElementStyleKeys.KEEP_ASPECT_RATIO, Boolean.TRUE);
result.getStyle().setStyleProperty(ElementStyleKeys.MIN_WIDTH,
parseLength(String.valueOf(attributes.getAttribute(HTML.Attribute.WIDTH))));
result.getStyle().setStyleProperty(ElementStyleKeys.DYNAMIC_HEIGHT, Boolean.TRUE);
}
else if (attributes.isDefined(HTML.Attribute.HEIGHT))
{
result.getStyle().setStyleProperty(ElementStyleKeys.SCALE, Boolean.TRUE);
result.getStyle().setStyleProperty(ElementStyleKeys.KEEP_ASPECT_RATIO, Boolean.TRUE);
result.getStyle().setStyleProperty(ElementStyleKeys.MIN_HEIGHT,
parseLength(String.valueOf(attributes.getAttribute(HTML.Attribute.HEIGHT))));
result.getStyle().setStyleProperty(ElementStyleKeys.DYNAMIC_HEIGHT, Boolean.TRUE);
}
else
{
result.getStyle().setStyleProperty(ElementStyleKeys.SCALE, Boolean.FALSE);
result.getStyle().setStyleProperty(ElementStyleKeys.KEEP_ASPECT_RATIO, Boolean.TRUE);
result.getStyle().setStyleProperty(ElementStyleKeys.DYNAMIC_HEIGHT, Boolean.TRUE);
}
configureStyle(textElement, result);
return result;
}
final javax.swing.text.Element parent = textElement.getParentElement();
final int endOffset = textElement.getEndOffset();
final int startOffset = textElement.getStartOffset();
final String text = textElement.getDocument().getText(startOffset, endOffset - startOffset);
if (parent != null)
{
final HTML.Tag tag = findTag(parent.getAttributes());
if ("\n".equals(text))
{
if (BLOCK_ELEMENTS.contains(tag) ||
"paragraph".equals(textElement.getName()) ||
"section".equals(textElement.getName()))
{
if (parent.getElementCount() > 0 && parent.getElement(parent.getElementCount() - 1) == textElement)
{
// Skipping an artificial \n at the end of paragraph element. This is generated by the swing
// parser and really messes things up here.
return null;
}
}
}
}
final Element result = new Element();
result.setName(textElement.getName());
result.setElementType(LabelType.INSTANCE);
result.setAttribute(AttributeNames.Core.NAMESPACE, AttributeNames.Core.VALUE, text);
configureStyle(textElement, result);
if (HTML.Tag.BR.equals(textElement.getAttributes().getAttribute(StyleConstants.NameAttribute)))
{
result.setAttribute(AttributeNames.Core.NAMESPACE, AttributeNames.Core.VALUE, "\n");
result.getStyle().setStyleProperty(TextStyleKeys.TRIM_TEXT_CONTENT, Boolean.FALSE);
result.getStyle().setStyleProperty(TextStyleKeys.WHITE_SPACE_COLLAPSE, WhitespaceCollapse.PRESERVE);
}
return result;
}
// we need to intercept for <UL> and <OL> here
final Band band = new Band();
configureStyle(textElement, band);
configureBand(textElement, band);
final boolean bandIsInline = isInlineElement(band);
final int size = textElement.getElementCount();
Band inlineContainer = null;
for (int i = 0; i < size; i++)
{
final Element element = process(textElement.getElement(i));
if (element == null)
{
continue;
}
if (isInlineElement(element) == bandIsInline)
{
band.addElement(element);
continue;
}
if (band.getElementCount() == 0)
{
inlineContainer = new Band();
inlineContainer.getStyle().setStyleProperty(BandStyleKeys.LAYOUT, "inline");
inlineContainer.addElement(element);
band.addElement(inlineContainer);
continue;
}
final Element maybeInlineContainer = (Element) band.getElement(band.getElementCount() - 1);
if (maybeInlineContainer == inlineContainer)
{
// InlineContainer cannot be null at this point, as band.getElement never returns null.
//noinspection ConstantConditions
inlineContainer.addElement(element);