jfxtras.icalendarfx.content.UnfoldingStringIterator Maven / Gradle / Ivy
package jfxtras.icalendarfx.content;
import java.util.Iterator;
/**
* A simple {@code Iterator} decorator that unfolds line according to the
* RFC 5545 iCalendar standard. Lines are unfolded automatically by calling the next() method.
* Non-folded lines are preserved by using a push-back mechanism.
*
* @author David Bal
*
*/
public class UnfoldingStringIterator implements Iterator
{
/** The iterator being decorated. */
private final Iterator iterator;
private String lastLine;
/**
* Constructor.
*
* @param iterator the iterator to decorate as unfolding
*/
public UnfoldingStringIterator(final Iterator iterator)
{
super();
this.iterator = iterator;
}
@Override
public boolean hasNext()
{
return (lastLine != null) ? true : iterator.hasNext();
}
@Override
public String next()
{
StringBuilder builder = new StringBuilder(200);
if (lastLine != null)
{
builder.append(lastLine);
lastLine = null;
} else
{
builder.append(iterator.next());
}
boolean isContinuationLine = false;
String currentLine = (iterator.hasNext()) ? iterator.next() : null;
while (currentLine != null)
{
if (! currentLine.isEmpty())
{
isContinuationLine = (currentLine.charAt(0) == ' ') || (currentLine.charAt(0) == '\t');
if (isContinuationLine)
{
builder.append(currentLine.substring(1, currentLine.length()));
} else
{
break;
}
}
currentLine = iterator.next();
}
pushBackLine(currentLine);
return builder.toString();
}
private void pushBackLine(String s)
{
lastLine = s;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy