View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.doxia.module.markdown;
20  
21  import java.io.IOException;
22  import java.io.Writer;
23  
24  import org.apache.commons.lang3.StringUtils;
25  
26  /**
27   * Decorates an existing writer to additionally temporarily buffer the last two lines written.
28   * Useful to collapse subsequent new lines or blank lines by evaluating {@link #isWriterAfterBlankLine()} and {@link #isWriterAfterBlankLine()}.
29   * The buffering does not affect or defer delegation to the underlying writer, though.
30   */
31  public class LastTwoLinesBufferingWriter extends Writer {
32  
33      private final Writer out;
34      private String previousLine;
35      private StringBuilder currentLine;
36      private final String lineSeparator;
37  
38      public LastTwoLinesBufferingWriter(Writer out) {
39          // don't use System.lineSeparator, as overwritten in AbstractModuleTest
40          this(out, System.getProperty("line.separator"));
41      }
42  
43      LastTwoLinesBufferingWriter(Writer out, String lineSeparator) {
44          super();
45          this.out = out;
46          this.previousLine = "";
47          this.currentLine = new StringBuilder();
48          this.lineSeparator = lineSeparator;
49      }
50  
51      public boolean isWriterAtStartOfNewLine() {
52          return currentLine.length() == 0;
53      }
54  
55      public boolean isWriterAfterBlankLine() {
56          return StringUtils.isAllBlank(currentLine) && StringUtils.isAllBlank(previousLine);
57      }
58  
59      @Override
60      public void write(char[] cbuf, int off, int len) throws IOException {
61          int offsetWrittenInLineBuffer = off;
62          int index = 0;
63          while (index < len) {
64              // potentially a line break...
65              if (cbuf[off + index] == '\r' || cbuf[off + index] == '\n') {
66                  int lenToWrite = index + 1 - (offsetWrittenInLineBuffer - off);
67                  flushLine(cbuf, offsetWrittenInLineBuffer, lenToWrite);
68                  offsetWrittenInLineBuffer += lenToWrite;
69              }
70              index++;
71          }
72          flushLine(cbuf, offsetWrittenInLineBuffer, index - (offsetWrittenInLineBuffer - off));
73          out.write(cbuf, off, len);
74      }
75  
76      private void flushLine(char[] cbuf, int off, int len) {
77          this.currentLine.append(cbuf, off, len);
78          // really a line break?
79          if (currentLine.toString().endsWith(lineSeparator)) {
80              previousLine = currentLine.toString();
81              currentLine.setLength(0);
82          }
83      }
84  
85      @Override
86      public void flush() throws IOException {
87          out.flush();
88      }
89  
90      @Override
91      public void close() throws IOException {
92          out.close();
93      }
94  }