org.eclipse.mylyn.wikitext.core_1.6.1.v20120425-0100

dyEndRegion = region;
  }
  
  public XslfoDocumentBuilder.Region getBodyEndRegion()
  {
    return bodyEndRegion;
  }
}

/* Location:
 * Qualified Name:     org.eclipse.mylyn.wikitext.core.parser.builder.XslfoDocumentBuilder.Configuration
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.mylyn.wikitext.core.parser.builder;

import org.eclipse.mylyn.wikitext.core.parser.DocumentBuilder.BlockType;

class DocBookDocumentBuilder$BlockDescription
{
  DocumentBuilder.BlockType type;
  int size;
  int entrySize;
  final String[] nestedElementNames;
  final boolean closeElementsOnBlockStart;
  
  public DocBookDocumentBuilder$BlockDescription(DocumentBuilder.BlockType type, int size, String[] nestedElementNames, boolean closeElementsOnBlockStart)
  {
    this.size = size;
    entrySize = (nestedElementNames == null ? 0 : nestedElementNames.length);
    this.type = type;
    this.nestedElementNames = nestedElementNames;
    this.closeElementsOnBlockStart = closeElementsOnBlockStart;
  }
}

/* Location:
 * Qualified Name:     org.eclipse.mylyn.wikitext.core.parser.builder.DocBookDocumentBuilder.BlockDescription
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.mylyn.wikitext.core.parser.builder;

import java.io.Writer;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Stack;
import java.util.TreeMap;
import java.util.logging.Logger;
import org.eclipse.mylyn.internal.wikitext.core.util.css.CssParser;
import org.eclipse.mylyn.internal.wikitext.core.util.css.CssRule;
import org.eclipse.mylyn.wikitext.core.parser.Attributes;
import org.eclipse.mylyn.wikitext.core.parser.DocumentBuilder.BlockType;
import org.eclipse.mylyn.wikitext.core.parser.DocumentBuilder.SpanType;
import org.eclipse.mylyn.wikitext.core.parser.ImageAttributes;
import org.eclipse.mylyn.wikitext.core.parser.ListAttributes;
import org.eclipse.mylyn.wikitext.core.parser.TableAttributes;
import org.eclipse.mylyn.wikitext.core.parser.outline.OutlineItem;
import org.eclipse.mylyn.wikitext.core.util.XmlStreamWriter;

public class XslfoDocumentBuilder
  extends AbstractXmlDocumentBuilder
{
  private static final String CSS_RULE_BACKGROUND_COLOR = "background-color";
  private static final String CSS_RULE_COLOR = "color";
  private static final String CSS_RULE_VERTICAL_ALIGN = "vertical-align";
  private static final String CSS_RULE_TEXT_DECORATION = "text-decoration";
  private static final String CSS_RULE_FONT_FAMILY = "font-family";
  private static final String CSS_RULE_FONT_SIZE = "font-size";
  private static final String CSS_RULE_FONT_WEIGHT = "font-weight";
  private static final String CSS_RULE_FONT_STYLE = "font-style";
  private static final char[] BULLET_CHARS = { '�' };
  private static Map<DocumentBuilder.BlockType, String> blockTypeToCssStyles = new HashMap();
  private static Map<DocumentBuilder.SpanType, String> spanTypeToCssStyles;
  
  static
  {
    blockTypeToCssStyles.put(DocumentBuilder.BlockType.CODE, "font-family: monospace;");
    blockTypeToCssStyles.put(DocumentBuilder.BlockType.PREFORMATTED, "font-family: monospace;");
    blockTypeToCssStyles.put(DocumentBuilder.BlockType.TABLE_CELL_HEADER, "font-weight: bold;");
    
    spanTypeToCssStyles = new HashMap();
    
    spanTypeToCssStyles.put(DocumentBuilder.SpanType.STRONG, "font-weight: bold;");
    spanTypeToCssStyles.put(DocumentBuilder.SpanType.BOLD, "font-weight: bold;");
    spanTypeToCssStyles.put(DocumentBuilder.SpanType.MONOSPACE, "font-family: monospace;");
    spanTypeToCssStyles.put(DocumentBuilder.SpanType.CODE, "font-family: monospace;");
    spanTypeToCssStyles.put(DocumentBuilder.SpanType.CITATION, "font-style: italic;");
    spanTypeToCssStyles.put(DocumentBuilder.SpanType.EMPHASIS, "font-style: italic;");
    spanTypeToCssStyles.put(DocumentBuilder.SpanType.ITALIC, "font-style: italic;");
    spanTypeToCssStyles.put(DocumentBuilder.SpanType.DELETED, "text-decoration: line-through;");
    spanTypeToCssStyles.put(DocumentBuilder.SpanType.INSERTED, "text-decoration: underline;");
    spanTypeToCssStyles.put(DocumentBuilder.SpanType.UNDERLINED, "text-decoration: underline;");
    spanTypeToCssStyles.put(DocumentBuilder.SpanType.SUBSCRIPT, "vertical-align: sub;");
    spanTypeToCssStyles.put(DocumentBuilder.SpanType.SUPERSCRIPT, "vertical-align: super;");
  }
  
  private final String foNamespaceUri = "http://www.w3.org/1999/XSL/Format";
  private boolean pageOpen = false;
  private int h1Count = 0;
  private final Stack<ElementInfo> elementInfos = new Stack();
  private Configuration configuration = new Configuration();
  private OutlineItem outline;
  
  public XslfoDocumentBuilder(Writer out)
  {
    super(out);
  }
  
  public XslfoDocumentBuilder(XmlStreamWriter writer)
  {
    super(writer);
  }
  
  public void acronym(String text, String definition)
  {
    characters(text);
  }
  
  public OutlineItem getOutline()
  {
    return outline;
  }
  
  public void setOutline(OutlineItem outline)
  {
    this.outline = outline;
  }
  
  private static class ElementInfo
  {
    int size = 1;
  }
  
  private static class SpanInfo
    extends XslfoDocumentBuilder.ElementInfo
  {
    final DocumentBuilder.SpanType type;
    
    public SpanInfo(DocumentBuilder.SpanType type)
    {
      super();
      this.type = type;
    }
  }
  
  private static class BlockInfo
    extends XslfoDocumentBuilder.ElementInfo
  {
    final DocumentBuilder.BlockType type;
    int listItemCount;
    BlockInfo previousChild;
    
    public BlockInfo(DocumentBuilder.BlockType type)
    {
      super();
      this.type = type;
    }
  }
  
  public void beginBlock(DocumentBuilder.BlockType type, Attributes attributes)
  {
    BlockInfo thisInfo = new BlockInfo(type);
    BlockInfo parentBlock = findCurrentBlock();
    
    String cssStyles = (String)blockTypeToCssStyles.get(type);
    Map<String, String> attrs = cssStyles == null ? null : attributesFromCssStyles(cssStyles);
    if (attributes.getCssStyle() != null)
    {
      Map<String, String> otherAttrs = attributesFromCssStyles(attributes.getCssStyle());
      if (attrs == null) {
        attrs = otherAttrs;
      } else if (!otherAttrs.isEmpty()) {
        attrs.putAll(otherAttrs);
      }
    }
    BlockInfo listInfo;
    switch (type)
    {
    case PANEL: 
    case PARAGRAPH: 
    case TABLE_ROW: 
      writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "list-block");
      writer.writeAttribute("provisional-label-separation", "0.2em");
      writer.writeAttribute("provisional-distance-between-starts", "1.2em");
      if (findBlockInfo(DocumentBuilder.BlockType.LIST_ITEM) == null) {
        addSpaceBefore();
      }
      break;
    case WARNING: 
      if ((parentBlock == null) || (type != DocumentBuilder.BlockType.DEFINITION_LIST)) {
        throw new IllegalStateException();
      }
      boolean firstItem = false;
      if ((previousChild != null) && (previousChild.type == DocumentBuilder.BlockType.DEFINITION_TERM))
      {
        firstItem = true;
        writer.writeEndElement();
        size -= 1;
        
        writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "list-item-body");
        size += 1;
        writer.writeAttribute("start-indent", "body-start()");
        writer.writeEmptyElement("http://www.w3.org/1999/XSL/Format", "block");
      }
      writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "block");
      if ((attrs == null) || (!attrs.containsKey("font-size"))) {
        configureFontSize(0);
      }
      writer.writeAttribute("space-before", firstItem ? "1.2em" : "0.2em");
      break;
    case TIP: 
      if ((parentBlock == null) || (type != DocumentBuilder.BlockType.DEFINITION_LIST)) {
        throw new IllegalStateException();
      }
      if ((previousChild != null) && (previousChild.type == DocumentBuilder.BlockType.DEFINITION_ITEM))
      {
        writer.writeEndElement();
        size -= 1;
        writer.writeEndElement();
        size -= 1;
      }
      if ((previousChild == null) || (previousChild.type != DocumentBuilder.BlockType.DEFINITION_TERM))
      {
        writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "list-item");
        writer.writeAttribute("space-before", "0.2em");
        size += 1;
        writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "list-item-label");
        writer.writeAttribute("end-indent", "label-end()");
        size += 1;
      }
      writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "block");
      if ((attrs == null) || (!attrs.containsKey("font-size"))) {
        configureFontSize(0);
      }
      if ((attrs == null) || (!attrs.containsKey("font-weight"))) {
        writer.writeAttribute("font-weight", "bold");
      }
      break;
    case PREFORMATTED: 
      listInfo = getListBlockInfo();
      listItemCount += 1;
      writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "list-item");
      
      writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "list-item-label");
      writer.writeAttribute("end-indent", "label-end()");
      writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "block");
      configureFontSize(0);
      if (type == DocumentBuilder.BlockType.NUMERIC_LIST)
      {
        if ((attributes instanceof ListAttributes))
        {
          ListAttributes listAttributes = (ListAttributes)attributes;
          if (listAttributes.getStart() != null) {
            try
            {
              listItemCount = (Integer.parseInt(listAttributes.getStart(), 10) - 1);
            }
            catch (NumberFormatException localNumberFormatException) {}
          }
        }
        writer.writeCharacters(String.format("%s.", new Object[] { Integer.valueOf(listItemCount) }));
      }
      else
      {
        writer.writeCharacters(BULLET_CHARS, 0, BULLET_CHARS.length);
      }
      writer.writeEndElement();
      writer.writeEndElement();
      
      writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "list-item-body");
      size += 1;
      writer.writeAttribute("start-indent", "body-start()");
      writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "block");
      configureFontSize(0);
      size += 1;
      
      break;
    case BULLETED_LIST: 
    case INFORMATION: 
      writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "block");
      if ((attrs == null) || (!attrs.containsKey("font-size"))) {
        configureFontSize(0);
      }
      addSpaceBefore();
      break;
    case NOTE: 
    case NUMERIC_LIST: 
      writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "block");
      writer.writeAttribute("hyphenate", "false");
      
      writer.writeAttribute("white-space-collapse", "false");
      writer.writeAttribute("white-space-treatment", "preserve");
      writer.writeAttribute("linefeed-treatment", "preserve");
      writer.writeAttribute("text-align", "start");
      if ((attrs == null) || (!attrs.containsKey("font-size"))) {
        configureFontSize(0);
      }
      addSpaceBefore();
      break;
    case LIST_ITEM: 
      writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "block");
      if ((attrs == null) || (!attrs.containsKey("font-size"))) {
        configureFontSize(0);
      }
      indentLeftAndRight(attrs, "2em");
      addSpaceBefore();
      break;
    case FOOTNOTE: 
      writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "block");
      if ((attrs == null) || (!attrs.containsKey("font-size"))) {
        configureFontSize(0);
      }
      break;
    case CODE: 
    case DEFINITION_ITEM: 
    case DEFINITION_LIST: 
    case DEFINITION_TERM: 
    case DIV: 
      writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "block");
      if ((attrs == null) || (!attrs.containsKey("font-size"))) {
        configureFontSize(0);
      }
      indentLeftAndRight(attrs, "2em");
      addSpaceBefore();
      if ((attributes.getTitle() != null) || (configuration.panelText))
      {
        writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "block");
        if (configuration.panelText)
        {
          String text = null;
          switch (type)
          {
          case DEFINITION_TERM: 
            text = Messages.getString("XslfoDocumentBuilder.Note");
            break;
          case CODE: 
            text = Messages.getString("XslfoDocumentBuilder.Tip");
            break;
          case DEFINITION_ITEM: 
            text = Messages.getString("XslfoDocumentBuilder.Warning");
          }
          if (text != null)
          {
            writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "inline");
            writer.writeAttribute("font-style", "italic");
            characters(text);
            writer.writeEndElement();
          }
        }
        if (attributes.getTitle() != null)
        {
          writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "inline");
          writer.writeAttribute("font-weight", "bold");
          characters(attributes.getTitle());
          writer.writeEndElement();
        }
        writer.writeEndElement();
      }
      break;
    case QUOTE: 
      writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "table");
      applyTableAttributes(attributes);
      writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "table-body");
      size += 1;
      break;
    case TABLE_CELL_HEADER: 
    case TABLE_CELL_NORMAL: 
      writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "table-cell");
      writer.writeAttribute("padding-left", "2pt");
      writer.writeAttribute("padding-right", "2pt");
      writer.writeAttribute("padding-top", "2pt");
      writer.writeAttribute("padding-bottom", "2pt");
      writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "block");
      if ((attrs == null) || (!attrs.containsKey("font-size"))) {
        configureFontSize(0);
      }
      size += 1;
      break;
    case TABLE: 
      writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "table-row");
      break;
    default: 
      throw new IllegalStateException(type.name());
    }
    if (attrs != null) {
      for (Map.Entry<String, String> ent : new TreeMap(attrs).entrySet()) {
        writer.writeAttribute((String)ent.getKey(), (String)ent.getValue());
      }
    }
    if (parentBlock != null) {
      previousChild = thisInfo;
    }
    elementInfos.push(thisInfo);
  }
  
  private void indentLeftAndRight(Map<String, String> attrs, String indentSize)
  {
    if ((attrs == null) || (!attrs.containsKey("margin-left"))) {
      writer.writeAttribute("margin-left", indentSize);
    }
    if ((attrs == null) || (!attrs.containsKey("margin-right"))) {
      writer.writeAttribute("margin-right", indentSize);
    }
  }
  
  private void configureFontSize(int level)
  {
    writer.writeAttribute("font-size", String.format("%spt", new Object[] { Float.valueOf(configuration.fontSizes[level]) }));
  }
  
  private void addSpaceBefore()
  {
    writer.writeAttribute("space-before.optimum", "1em");
    writer.writeAttribute("space-before.minimum", "0.8em");
    writer.writeAttribute("space-before.maximum", "1.2em");
  }
  
  public void endBlock()
  {
    ElementInfo elementInfo = (ElementInfo)elementInfos.pop();
    if (!(elementInfo instanceof BlockInfo)) {
      throw new IllegalStateException();
    }
    close(elementInfo);
  }
  
  private void close(ElementInfo elementInfo)
  {
    while (size > 0)
    {
      size -= 1;
      writer.writeEndElement();
    }
  }
  
  private void applyTableAttributes(Attributes attributes)
  {
    boolean haveWidth = false;
    if ((attributes instanceof TableAttributes))
    {
      TableAttributes tableAttributes = (TableAttributes)attributes;
      if (tableAttributes.getBgcolor() != null) {
        writer.writeAttribute("background-color", tableAttributes.getBgcolor());
      }
      if (tableAttributes.getWidth() != null)
      {
        writer.writeAttribute("width", tableAttributes.getWidth());
        haveWidth = true;
      }
    }
    if (!haveWidth) {
      writer.writeAttribute("width", "auto");
    }
    writer.writeAttribute("border-collapse", "collapse");
  }
  
  private BlockInfo getListBlockInfo()
  {
    for (int x = elementInfos.size() - 1; x >= 0; x--)
    {
      ElementInfo elementInfo = (ElementInfo)elementInfos.get(x);
      if ((elementInfo instanceof BlockInfo))
      {
        BlockInfo info = (BlockInfo)elementInfo;
        if ((type == DocumentBuilder.BlockType.BULLETED_LIST) || (type == DocumentBuilder.BlockType.NUMERIC_LIST) || 
          (type == DocumentBuilder.BlockType.DEFINITION_LIST)) {
          return info;
        }
      }
    }
    return null;
  }
  
  private BlockInfo findBlockInfo(DocumentBuilder.BlockType type)
  {
    for (int x = elementInfos.size() - 1; x >= 0; x--)
    {
      ElementInfo elementInfo = (ElementInfo)elementInfos.get(x);
      if ((elementInfo instanceof BlockInfo))
      {
        BlockInfo info = (BlockInfo)elementInfo;
        if (type == type) {
          return info;
        }
      }
    }
    return null;
  }
  
  private BlockInfo findCurrentBlock()
  {
    for (int x = elementInfos.size() - 1; x >= 0; x--)
    {
      ElementInfo elementInfo = (ElementInfo)elementInfos.get(x);
      if ((elementInfo instanceof BlockInfo)) {
        return (BlockInfo)elementInfo;
      }
    }
    return null;
  }
  
  private void writeMargins(Margins margins, XmlStreamWriter writer)
  {
    if (margins == null) {
      return;
    }
    writer.writeAttribute("margin-top", String.format("%scm", new Object[] { Float.valueOf(marginTop) }));
    writer.writeAttribute("margin-bottom", String.format("%scm", new Object[] { Float.valueOf(marginBottom) }));
    writer.writeAttribute("margin-left", String.format("%scm", new Object[] { Float.valueOf(marginLeft) }));
    writer.writeAttribute("margin-right", String.format("%scm", new Object[] { Float.valueOf(marginRight) }));
  }
  
  private void writeRegion(Region region, XmlStreamWriter writer)
  {
    if (region == null) {
      return;
    }
    writer.writeEmptyElement("http://www.w3.org/1999/XSL/Format", "region-" + location);
    writer.writeAttribute("extent", String.format("%scm", new Object[] { Float.valueOf(configuration.pageHeight) }));
    writer.writeAttribute("precedence", Boolean.toString(precedence));
    if (name != null) {
      writer.writeAttribute("region-name", name);
    }
  }
  
  public void beginDocument()
  {
    writer.setDefaultNamespace("http://www.w3.org/1999/XSL/Format");
    
    writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "root");
    writer.writeNamespace("", "http://www.w3.org/1999/XSL/Format");
    
    writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "layout-master-set");
    writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "simple-page-master");
    writer.writeAttribute("master-name", "page-layout");
    writer.writeAttribute("page-height", String.format("%scm", new Object[] { Float.valueOf(configuration.pageHeight) }));
    writer.writeAttribute("page-width", String.format("%scm", new Object[] { Float.valueOf(configuration.pageWidth) }));
    if (configuration.getPageMargins() == null) {
      writer.writeAttribute("margin", String.format("%scm", new Object[] { Float.valueOf(configuration.pageMargin) }));
    } else {
      writeMargins(configuration.getPageMargins(), writer);
    }
    writer.writeEmptyElement("http://www.w3.org/1999/XSL/Format", "region-body");
    if (configuration.getBodyMargins() == null)
    {
      if (hasPageFooter()) {
        writer.writeAttribute("margin-bottom", "3cm");
      }
    }
    else {
      writeMargins(configuration.getBodyMargins(), writer);
    }
    if (configuration.bodyAfterRegion == null)
    {
      if (hasPageFooter()) {
        writeRegion(new Region("after", "footer", 2.0F, false), writer);
      }
    }
    else {
      writeRegion(configuration.getBodyAfterRegion(), writer);
    }
    writeRegion(configuration.getBodyBeforeRegion(), writer);
    writeRegion(configuration.getBodyEndRegion(), writer);
    writeRegion(configuration.getBodyStartRegion(), writer);
    
    writer.writeEndElement();
    writer.writeEndElement();
    if ((outline != null) && (!outline.getChildren().isEmpty()))
    {
      writer.writeStartElement("bookmark-tree");
      emitToc(writer, outline.getChildren());
      writer.writeEndElement();
    }
    if (configuration.getTitle() != null) {
      emitTitlePage();
    }
    openPage();
    openFlow(false);
  }
  
  private boolean hasPageFooter()
  {
    return (configuration.copyright != null) || (configuration.pageNumbering);
  }
  
  private void emitTitlePage()
  {
    openPage();
    openFlow(true);
    writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "block");
    if (configuration.title != null)
    {
      writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "block");
      writer.writeAttribute("font-weight", "bold");
      writer.writeAttribute("font-size", "25pt");
      writer.writeAttribute("text-align", "center");
      writer.writeAttribute("space-before", "19pt");
      writer.writeCharacters(configuration.title);
      writer.writeEndElement();
    }
    if (configuration.subTitle != null)
    {
      writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "block");
      writer.writeAttribute("font-weight", "bold");
      writer.writeAttribute("font-size", "18pt");
      writer.writeAttribute("text-align", "center");
      writer.writeAttribute("space-before", "15pt");
      writer.writeCharacters(configuration.subTitle);
      writer.writeEndElement();
    }
    if (configuration.version != null)
    {
      writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "block");
      writer.writeAttribute("font-weight", "bold");
      writer.writeAttribute("font-size", "14pt");
      writer.writeAttribute("text-align", "center");
      writer.writeAttribute("space-before", "13pt");
      writer.writeCharacters(configuration.version);
      writer.writeEndElement();
    }
    if (configuration.date != null)
    {
      writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "block");
      writer.writeAttribute("font-weight", "bold");
      writer.writeAttribute("font-size", "14pt");
      writer.writeAttribute("text-align", "center");
      writer.writeAttribute("space-before", "13pt");
      writer.writeCharacters(configuration.date);
      writer.writeEndElement();
    }
    writer.writeEmptyElement("http://www.w3.org/1999/XSL/Format", "block");
    writer.writeAttribute("break-after", "page");
    writer.writeEndElement();
    
    closeFlow();
    closePage();
  }
  
  private void openFlow(boolean titlePage)
  {
    if (hasPageFooter())
    {
      boolean hasCopyrightText = (configuration.copyright != null) && 
        (configuration.copyright.trim().length() > 0);
      boolean hasPageNumber = (configuration.pageNumbering) && (!titlePage);
      if ((hasCopyrightText) || (hasPageNumber))
      {
        writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "static-content");
        writer.writeAttribute("flow-name", "footer");
        if (hasCopyrightText)
        {
          writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "block");
          configureFontSize(0);
          writer.writeAttribute("text-align", "center");
          
          writer.writeCharacters(configuration.copyright);
          
          writer.writeEndElement();
        }
        if (hasPageNumber)
        {
          writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "block");
          configureFontSize(0);
          writer.writeAttribute("text-align", "outside");
          
          writer.writeEmptyElement("http://www.w3.org/1999/XSL/Format", "page-number");
          
          writer.writeEndElement();
        }
        writer.writeEndElement();
      }
    }
    writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "flow");
    writer.writeAttribute("flow-name", "xsl-region-body");
  }
  
  private void closeFlow()
  {
    writer.writeEndElement();
  }
  
  private void openPage()
  {
    pageOpen = true;
    writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "page-sequence");
    writer.writeAttribute("master-reference", "page-layout");
  }
  
  private void closePage()
  {
    writer.writeEndElement();
    pageOpen = false;
  }
  
  private void emitToc(XmlStreamWriter writer, List<OutlineItem> children)
  {
    for (OutlineItem item : children)
    {
      writer.writeStartElement("bookmark");
      writer.writeAttribute("internal-destination", item.getId());
      
      writer.writeStartElement("bookmark-title");
      writer.writeCharacters(item.getLabel());
      writer.writeEndElement();
      if (!item.getChildren().isEmpty()) {
        emitToc(writer, item.getChildren());
      }
      writer.writeEndElement();
    }
  }
  
  public void endDocument()
  {
    if (pageOpen)
    {
      closeFlow();
      closePage();
    }
    writer.writeEndElement();
    writer.close();
  }
  
  public void beginHeading(int level, Attributes attributes)
  {
    if ((level == 1) && (++h1Count > 1) && (configuration.pageBreakOnHeading1))
    {
      if (pageOpen)
      {
        closeFlow();
        closePage();
      }
      openPage();
      openFlow(false);
    }
    writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "block");
    writer.writeAttribute("keep-with-next.within-column", "always");
    writer.writeAttribute("font-weight", "bold");
    configureFontSize(level);
    writer.writeAttribute("space-before.optimum", "10pt");
    writer.writeAttribute("space-before.minimum", "10pt * 0.8");
    writer.writeAttribute("space-before.maximum", "10pt * 1.2");
    if (attributes.getId() != null) {
      writer.writeAttribute("id", attributes.getId());
    }
  }
  
  public void endHeading()
  {
    writer.writeEndElement();
  }
  
  private Map<String, String> attributesFromCssStyles(String styles)
  {
    if (styles == null) {
      return Collections.emptyMap();
    }
    List<CssRule> rules = new CssParser().parseBlockContent(styles);
    if (rules.isEmpty()) {
      return Collections.emptyMap();
    }
    Map<String, String> mapping = new HashMap();
    for (CssRule rule : rules) {
      if ("vertical-align".equals(name))
      {
        if ("super".equals(value))
        {
          mapping.put("font-size", "75%");
          mapping.put("baseline-shift", "super");
        }
        else if ("sub".equals(value))
        {
          mapping.put("font-size", "75%");
          mapping.put("baseline-shift", "sub");
        }
      }
      else if (("text-decoration".equals(name)) || 
        ("font-family".equals(name)) || 
        ("font-size".equals(name)) || 
        ("font-weight".equals(name)) || 
        ("font-style".equals(name)) || 
        ("background-color".equals(name)) || 
        ("color".equals(name))) {
        mapping.put(name, value);
      }
    }
    return mapping;
  }
  
  public void beginSpan(DocumentBuilder.SpanType type, Attributes attributes)
  {
    SpanInfo info = new SpanInfo(type);
    
    writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "inline");
    
    String cssStyles = (String)spanTypeToCssStyles.get(type);
    Map<String, String> attrs = cssStyles == null ? null : attributesFromCssStyles(cssStyles);
    if (attributes.getCssStyle() != null)
    {
      Map<String, String> otherAttrs = attributesFromCssStyles(attributes.getCssStyle());
      if (attrs == null) {
        attrs = otherAttrs;
      } else if (!otherAttrs.isEmpty()) {
        attrs.putAll(otherAttrs);
      }
    }
    if (attrs != null) {
      for (Map.Entry<String, String> ent : new TreeMap(attrs).entrySet()) {
        writer.writeAttribute((String)ent.getKey(), (String)ent.getValue());
      }
    }
    elementInfos.push(info);
  }
  
  public void endSpan()
  {
    ElementInfo elementInfo = (ElementInfo)elementInfos.pop();
    if (!(elementInfo instanceof SpanInfo)) {
      throw new IllegalStateException();
    }
    close(elementInfo);
  }
  
  public void characters(String text)
  {
    writer.writeCharacters(text);
  }
  
  public void charactersUnescaped(String literal)
  {
    Logger.getLogger(XslfoDocumentBuilder.class.getName()).warning("escaping XML literal");
    writer.writeCharacters(literal);
  }
  
  public void entityReference(String entity)
  {
    writer.writeEntityRef(entity);
  }
  
  public void image(Attributes attributes, String url)
  {
    writer.writeEmptyElement("http://www.w3.org/1999/XSL/Format", "external-graphic");
    writer.writeAttribute("src", String.format("url(%s)", new Object[] { makeUrlAbsolute(url) }));
    applyImageAttributes(attributes);
  }
  
  private void applyImageAttributes(Attributes attributes)
  {
    boolean sizeSpecified = false;
    boolean scaleToFit = true;
    if ((attributes instanceof ImageAttributes))
    {
      ImageAttributes imageAttributes = (ImageAttributes)attributes;
      if (imageAttributes.getWidth() > 0)
      {
        sizeSpecified = true;
        emitImageSize("width", imageAttributes.getWidth(), imageAttributes.isWidthPercentage());
      }
      if (imageAttributes.getHeight() > 0)
      {
        sizeSpecified = true;
        emitImageSize("height", imageAttributes.getHeight(), imageAttributes.isHeightPercentage());
      }
    }
    if (!sizeSpecified)
    {
      writer.writeAttribute("width", "100%");
      writer.writeAttribute("content-height", "100%");
    }
    if (scaleToFit)
    {
      writer.writeAttribute("content-width", "scale-to-fit");
      writer.writeAttribute("scaling", "uniform");
    }
  }
  
  private void emitImageSize(String attributeName, int units, boolean isPercentage)
  {
    writer.writeAttribute(attributeName, String.format("%s%s", new Object[] { Integer.valueOf(units), isPercentage ? "%" : "px" }));
  }
  
  public void imageLink(Attributes linkAttributes, Attributes imageAttributes, String href, String imageUrl)
  {
    writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "basic-link");
    String destinationUrl = makeUrlAbsolute(href);
    if (destinationUrl.startsWith("#")) {
      writer.writeAttribute("internal-destination", destinationUrl.substring(1));
    } else {
      writer.writeAttribute("external-destination", String.format("url(%s)", new Object[] { destinationUrl }));
    }
    image(imageAttributes, imageUrl);
    writer.writeEndElement();
  }
  
  public void lineBreak()
  {
    writer.writeEmptyElement("http://www.w3.org/1999/XSL/Format", "block");
  }
  
  public void link(Attributes attributes, String hrefOrHashName, String text)
  {
    writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "basic-link");
    String destinationUrl = makeUrlAbsolute(hrefOrHashName);
    boolean internal = destinationUrl.startsWith("#");
    if (configuration.underlineLinks) {
      writer.writeAttribute("text-decoration", "underline");
    }
    if (internal) {
      writer.writeAttribute("internal-destination", destinationUrl.substring(1));
    } else {
      writer.writeAttribute("external-destination", String.format("url(%s)", new Object[] { destinationUrl }));
    }
    characters(text);
    writer.writeEndElement();
    if ((configuration.showExternalLinks) && (!internal))
    {
      characters(Messages.getString("XslfoDocumentBuilder.beforeLink"));
      writer.writeStartElement("http://www.w3.org/1999/XSL/Format", "basic-link");
      writer.writeAttribute("external-destination", String.format("url(%s)", new Object[] { destinationUrl }));
      characters(destinationUrl);
      characters(Messages.getString("XslfoDocumentBuilder.afterLink"));
      writer.writeEndElement();
    }
  }
  
  public Configuration getConfiguration()
  {
    return configuration;
  }
  
  public void setConfiguration(Configuration configuration)
  {
    if (configuration == null) {
      throw new IllegalArgumentException();
    }
    this.configuration = configuration;
  }
  
  public static class Region
    implements Cloneable
  {
    private float extent = 3.0F;
    private boolean precedence = false;
    private String location = null;
    private String name = null;
    
    public Region() {}
    
    public Region(String name, float extent, boolean precedence)
    {
      this.name = name;
      this.extent = extent;
      this.precedence = precedence;
    }
    
    public Region(String location, String name, float extent, boolean precedence)
    {
      this.location = location;
      this.extent = extent;
      this.precedence = precedence;
      this.name = name;
    }
    
    public void setExtent(float extent)
    {
      this.extent = extent;
    }
    
    public float getExtent()
    {
      return extent;
    }
    
    public void setPrecedence(boolean precedence)
    {
      this.precedence = precedence;
    }
    
    public boolean isPrecedence()
    {
      return precedence;
    }
    
    public void setName(String name)
    {
      this.name = name;
    }
    
    public String getName()
    {
      return name;
    }
  }
  
  public static class Margins
    implements Cloneable
  {
    private float marginTop = 1.5F;
    private float marginBottom = 1.5F;
    private float marginLeft = 1.5F;
    private float marginRight = 1.5F;
    
    public Margins() {}
    
    public Margins(float top, float bottom, float left, float right)
    {
      marginTop = top;
      marginBottom = bottom;
      marginLeft = left;
      marginRight = right;
    }
    
    public void setMarginTop(float marginTop)
    {
      this.marginTop = marginTop;
    }
    
    public float getMarginTop()
    {
      return marginTop;
    }
    
    public void setMarginBottom(float marginBottom)
    {
      this.marginBottom = marginBottom;
    }
    
    public float getMarginBottom()
    {
      return marginBottom;
    }
    
    public void setMarginLeft(float marginLeft)
    {
      this.marginLeft = marginLeft;
    }
    
    public float getMarginLeft()
    {
      return marginLeft;
    }
    
    public void setMarginRight(float marginRight)
    {
      this.marginRight = marginRight;
    }
    
    public float getMarginRight()
    {
      return marginRight;
    }
  }
  
  public static class Configuration
    implements Cloneable
  {
    private float[] fontSizes = { 12.0F, 18.0F, 15.0F, 13.2F, 12.0F, 10.4F, 8.0F };
    private final float[] fontSizeMultipliers = { 1.0F, 1.5F, 1.25F, 1.1F, 1.0F, 0.83F, 0.67F };
    private boolean pageBreakOnHeading1 = true;
    private boolean showExternalLinks = true;
    private boolean underlineLinks = false;
    private boolean panelText = true;
    private String title;
    private String subTitle;
    private String version;
    private String date;
    private String author;
    private String copyright;
    private boolean pageNumbering = true;
    private float pageMargin = 1.5F;
    private float pageHeight = 29.7F;
    private float pageWidth = 21.0F;
    private XslfoDocumentBuilder.Margins pageMargins = null;
    private XslfoDocumentBuilder.Margins bodyMargins = null;
    private XslfoDocumentBuilder.Region bodyBeforeRegion = null;
    private XslfoDocumentBuilder.Region bodyAfterRegion = null;
    private XslfoDocumentBuilder.Region bodyStartRegion = null;
    private XslfoDocumentBuilder.Region bodyEndRegion = null;
    private float referenceOrientation = 90.0F;
    
    public Configuration()
    {
      setFontSize(10.0F);
    }
    
    public Configuration clone()
    {
      try
      {
        return (Configuration)super.clone();
      }
      catch (CloneNotSupportedException e)
      {
        throw new IllegalStateException(e);
      }
    }
    
    public void setFontSize(float fontSize)
    {
      fontSizes = new float[fontSizeMultipliers.length];
      for (int x = 0; x < fontSizeMultipliers.length; x++) {
        fontSizes[x] = (fontSizeMultipliers[x] * fontSize);
      }
    }
    
    public float getFontSize()
    {
      return fontSizes[0];
    }
    
    public void setFontSizeMultipliers(float[] fontSizeMultipliers)
    {
      if (fontSizeMultipliers.length != 7) {
        throw new IllegalArgumentException();
      }
      for (int x = 0; x < fontSizeMultipliers.length; x++) {
        if (fontSizeMultipliers[x] < 0.2D) {
          throw new IllegalArgumentException();
        }
      }
      System.arraycopy(fontSizeMultipliers, 0, this.fontSizeMultipliers, 0, 7);
    }
    
    public float[] getFontSizeMultipliers()
    {
      float[] values = new float[7];
      System.arraycopy(fontSizeMultipliers, 0, values, 0, 7);
      return values;
    }
    
    public boolean isShowExternalLinks()
    {
      return showExternalLinks;
    }
    
    public void setShowExternalLinks(boolean showExternalLinks)
    {
      this.showExternalLinks = showExternalLinks;
    }
    
    public boolean isUnderlineLinks()
    {
      return underlineLinks;
    }
    
    public void setUnderlineLinks(boolean underlineLinks)
    {
      this.underlineLinks = underlineLinks;
    }
    
    public boolean isPageBreakOnHeading1()
    {
      return pageBreakOnHeading1;
    }
    
    public void setPageBreakOnHeading1(boolean pageBreakOnHeading1)
    {
      this.pageBreakOnHeading1 = pageBreakOnHeading1;
    }
    
    public String getTitle()
    {
      return title;
    }
    
    public void setTitle(String title)
    {
      this.title = title;
    }
    
    public String getSubTitle()
    {
      return subTitle;
    }
    
    public void setSubTitle(String subTitle)
    {
      this.subTitle = subTitle;
    }
    
    public boolean isPanelText()
    {
      return panelText;
    }
    
    public void setPanelText(boolean panelText)
    {
      this.panelText = panelText;
    }
    
    public String getVersion()
    {
      return version;
    }
    
    public void setVersion(String version)
    {
      this.version = version;
    }
    
    public String getDate()
    {
      return date;
    }
    
    public void setDate(String date)
    {
      this.date = date;
    }
    
    public String getAuthor()
    {
      return author;
    }
    
    public void setAuthor(String author)
    {
      this.author = author;
    }
    
    public String getCopyright()
    {
      return copyright;
    }
    
    public void setCopyright(String copyright)
    {
      this.copyright = copyright;
    }
    
    public boolean isPageNumbering()
    {
      return pageNumbering;
    }
    
    public void setPageNumbering(boolean pageNumbering)
    {
      this.pageNumbering = pageNumbering;
    }
    
    public float getPageMargin()
    {
      return pageMargin;
    }
    
    public void setPageMargin(float pageMargin)
    {
      this.pageMargin = pageMargin;
    }
    
    public float getPageHeight()
    {
      return pageHeight;
    }
    
    public void setPageHeight(float pageHeight)
    {
      this.pageHeight = pageHeight;
    }
    
    public float getPageWidth()
    {
      return pageWidth;
    }
    
    public void setPageWidth(float pageWidth)
    {
      this.pageWidth = pageWidth;
    }
    
    public void setReferenceOrientation(float referenceOrientation)
    {
      this.referenceOrientation = referenceOrientation;
    }
    
    public float getReferenceOrientation()
    {
      return referenceOrientation;
    }
    
    public XslfoDocumentBuilder.Margins getPageMargins()
    {
      return pageMargins;
    }
    
    public XslfoDocumentBuilder.Margins getBodyMargins()
    {
      return bodyMargins;
    }
    
    public void setPageMargins(XslfoDocumentBuilder.Margins pageMargins)
    {
      this.pageMargins = pageMargins;
    }
    
    public void setBodyMargins(XslfoDocumentBuilder.Mar
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

Further reading...

For more information on Java 1.5 Tiger, you may find Java 1.5 Tiger, A developer's Notebook by D. Flanagan and B. McLaughlin from O'Reilly of interest.

New!JAR listings


Copyright 2006-2019. Infinite Loop Ltd