Module java.desktop
Package javax.swing.text

Class AbstractWriter

java.lang.Object
javax.swing.text.AbstractWriter
直接已知的子类:
HTMLWriter, MinimalHTMLWriter

public abstract class AbstractWriter extends Object
AbstractWriter是一个实际执行写出元素树及其属性工作的抽象类。就每行写出的内容而言,写出器默认为100。但是这个值可以由子类设置。
  • Field Details

  • Constructor Details

    • AbstractWriter

      protected AbstractWriter(Writer w, Document doc)
      创建一个新的AbstractWriter。使用文档的默认根元素初始化ElementIterator。
      参数:
      w - 一个Writer。
      doc - 一个Document
    • AbstractWriter

      protected AbstractWriter(Writer w, Document doc, int pos, int len)
      创建一个新的AbstractWriter。使用传入的元素初始化ElementIterator。
      参数:
      w - 一个Writer
      doc - 一个Element
      pos - 要获取内容的文档位置。
      len - 要写出的数量。
    • AbstractWriter

      protected AbstractWriter(Writer w, Element root)
      创建一个新的AbstractWriter。使用传入的元素初始化ElementIterator。
      参数:
      w - 一个Writer
      root - 一个Element
    • AbstractWriter

      protected AbstractWriter(Writer w, Element root, int pos, int len)
      创建一个新的AbstractWriter。使用传入的元素初始化ElementIterator。
      参数:
      w - 一个Writer
      root - 一个Element
      pos - 要获取内容的文档位置。
      len - 要写出的数量。
  • Method Details

    • getStartOffset

      public int getStartOffset()
      返回要输出的第一个偏移量。
      返回:
      要输出的第一个偏移量
      自从:
      1.3
    • getEndOffset

      public int getEndOffset()
      返回要输出的最后偏移量。
      返回:
      要输出的最后偏移量
      自从:
      1.3
    • getElementIterator

      protected ElementIterator getElementIterator()
      获取ElementIterator。
      返回:
      ElementIterator。
    • getWriter

      protected Writer getWriter()
      返回用于输出内容的Writer。
      返回:
      用于输出内容的Writer
      自从:
      1.3
    • getDocument

      protected Document getDocument()
      获取文档。
      返回:
      Document。
    • inRange

      protected boolean inRange(Element next)
      此方法确定当前元素是否在指定范围内。当未指定范围时,范围被初始化为整个文档。如果指定的范围与元素的范围相交,则返回true。
      参数:
      next - 一个Element。
      返回:
      指示元素是否在范围内的布尔值。
    • write

      protected abstract void write() throws IOException, BadLocationException
      这个抽象方法需要子类实现。其责任是遍历元素并使用write()方法以期望的格式生成输出。
      抛出:
      IOException - 如果发生I/O问题
      BadLocationException - 对于文档中的无效位置
    • getText

      protected String getText(Element elem) throws BadLocationException
      返回与元素关联的文本。这里的假设是元素是一个叶子元素。遇到时会抛出BadLocationException。
      参数:
      elem - 一个Element
      返回:
      文本作为String
      抛出:
      BadLocationException - 如果pos表示文档中的无效位置
    • text

      protected void text(Element elem) throws BadLocationException, IOException
      写出文本。如果在构造函数调用时指定了范围,则只会写出适当范围的文本。
      参数:
      elem - 一个Element。
      抛出:
      IOException - 任何I/O错误
      BadLocationException - 如果pos表示文档中的无效位置。
    • setLineLength

      protected void setLineLength(int l)
      允许子类设置他们希望每行写出的字符数。默认为100。
      参数:
      l - 最大行长度。
    • getLineLength

      protected int getLineLength()
      返回最大行长度。
      返回:
      最大行长度
      自从:
      1.3
    • setCurrentLineLength

      protected void setCurrentLineLength(int length)
      设置当前行长度。
      参数:
      length - 新的行长度
      自从:
      1.3
    • getCurrentLineLength

      protected int getCurrentLineLength()
      返回当前行长度。
      返回:
      当前行长度
      自从:
      1.3
    • isLineEmpty

      protected boolean isLineEmpty()
      如果当前行应被视为空行,则返回true。当getCurrentLineLength == 0 || 在空行上调用indent时为true。
      返回:
      如果当前行应被视为空行,则为true
      自从:
      1.3
    • setCanWrapLines

      protected void setCanWrapLines(boolean newValue)
      设置是否可以换行。在写出行时可以切换此选项。例如,输出HTML时,当输出引用字符串时可以将其设置为false。
      参数:
      newValue - 换行的新值
      自从:
      1.3
    • getCanWrapLines

      protected boolean getCanWrapLines()
      返回是否可以换行。如果为false,则不会输出任何行分隔符。
      返回:
      是否可以换行
      自从:
      1.3
    • setIndentSpace

      protected void setIndentSpace(int space)
      允许子类指定缩进映射到多少个空格。进行缩进时,缩进级别将乘以此映射。默认为2。
      参数:
      space - 表示缩进映射的整数。
    • getIndentSpace

      protected int getIndentSpace()
      返回要缩进的空格数。
      返回:
      要缩进的空格数
      自从:
      1.3
    • setLineSeparator

      public void setLineSeparator(String value)
      设置用于表示换行的字符串。这在构造函数中从Document或System属性line.separator初始化。
      参数:
      value - 新的换行符
      自从:
      1.3
    • getLineSeparator

      public String getLineSeparator()
      返回用于表示换行的字符串。
      返回:
      用于表示换行的字符串
      自从:
      1.3
    • incrIndent

      protected void incrIndent()
      增加缩进级别。如果缩进会导致getIndentSpace() * getIndentLevel() 大于getLineLength(),则不会进行缩进。
    • decrIndent

      protected void decrIndent()
      减少缩进级别。
    • getIndentLevel

      protected int getIndentLevel()
      返回当前缩进级别。即,incrIndent被调用的次数减去decrIndent被调用的次数。
      返回:
      当前缩进级别
      自从:
      1.3
    • indent

      protected void indent() throws IOException
      进行缩进。写出的空格数为缩进级别乘以空格映射。如果当前行为空,则不会使当前行被视为空行。
      抛出:
      IOException - 任何I/O错误
    • write

      protected void write(char ch) throws IOException
      写出一个字符。这是通过调用接受char[]的write方法来实现的。
      参数:
      ch - 一个字符。
      抛出:
      IOException - 任何I/O错误
    • write

      protected void write(String content) throws IOException
      写出一个字符串。这是通过调用接受char[]的write方法来实现的。
      参数:
      content - 一个字符串。
      抛出:
      IOException - 任何I/O错误
    • writeLineSeparator

      protected void writeLineSeparator() throws IOException
      写入行分隔符。这会直接调用output,同时将lineLength设置为0。
      抛出:
      IOException - 任何I/O错误
      自版本:
      1.3
    • write

      protected void write(char[] chars, int startIndex, int length) throws IOException
      所有写入方法都调用这个方法。如果getCanWrapLines()返回false,这将调用output来处理不包含换行符的每个chars序列,然后调用writeLineSeparator。另一方面,如果getCanWrapLines()返回true,这将根据需要拆分字符串,以便尊重getLineLength。唯一的例外是,如果当前字符串不包含空格,并且不适合在这种情况下,行长度将超过getLineLength
      参数:
      chars - 要输出的字符
      startIndex - 起始索引
      length - 输出长度
      抛出:
      IOException - 任何I/O错误
      自版本:
      1.3
    • writeAttributes

      protected void writeAttributes(AttributeSet attr) throws IOException
      将属性集写出为" <name>=<value>"对。遇到时会抛出IOException。
      参数:
      attr - 一个属性集。
      抛出:
      IOException - 任何I/O错误
    • output

      protected void output(char[] content, int start, int length) throws IOException
      写出内容的最后一步。所有写入方法最终都会到达这个方法,该方法会在Writer上调用write

      此方法还根据length更新行长度。如果调用此方法输出换行符,则当前行长度将需要重置,因为它将不再有效。这由调用者负责。使用writeLineSeparator来写入换行符,这将正确更新当前行长度。

      参数:
      content - 要输出的字符
      start - 起始索引
      length - 输出长度
      抛出:
      IOException - 任何I/O错误
      自版本:
      1.3