Skip to content

Commit

Permalink
Rely on system property again for line separator
Browse files Browse the repository at this point in the history
Define enum for entry types
  • Loading branch information
kwin committed Dec 19, 2023
1 parent b23a1f3 commit ca72f70
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@
package org.apache.maven.doxia.index;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
* Representing the section tree within a document with the most important metadata per section.
* Representing the index tree within a document with the most important metadata per entry.
* Currently this only contains entries for sections, but in the future may be extended, therefore it
* is recommended to use {@link #getType()} to filter out irrelevant entries.
*
* @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
*/
Expand Down Expand Up @@ -53,10 +56,41 @@ public class IndexEntry {
*/
private List<IndexEntry> childEntries = new ArrayList<>();

public enum Type {
/**
* Used for unknown types but also for the root entry
*/
UNKNOWN,
SECTION_1,
SECTION_2,
SECTION_3,
SECTION_4,
SECTION_5,
SECTION_6,
DEFINED_TERM,
FIGURE,
TABLE;

static Type fromSectionLevel(int level) {
if (level < SECTION_1.ordinal() || level > SECTION_6.ordinal()) {
throw new IllegalArgumentException("Level must be between " + SECTION_1.ordinal() + " and "
+ SECTION_6.ordinal() + " but is " + level);
}
return Arrays.stream(Type.values())
.filter(t -> level == t.ordinal())
.findAny()
.orElseThrow(() -> new IllegalStateException("Could not find enum with ordinal " + level));
}
};
/**
* The type of the entry, one of the types defined by {@link IndexingSink}
*/
private final int type;
private final Type type;

/**
* System-dependent EOL.
*/
private static final String EOL = System.getProperty("line.separator");

/**
* Constructor for root entry.
Expand All @@ -74,16 +108,17 @@ public IndexEntry(String newId) {
* @param newId The id. May be null.
*/
public IndexEntry(IndexEntry newParent, String newId) {
this(newParent, newId, 0);
this(newParent, newId, Type.UNKNOWN);
}

/**
* Constructor.
*
* @param newParent The parent. May be null.
* @param newId The id. May be null.
* @param
*/
public IndexEntry(IndexEntry newParent, String newId, int type) {
public IndexEntry(IndexEntry newParent, String newId, Type type) {
this.parent = newParent;
this.id = newId;

Expand Down Expand Up @@ -126,7 +161,7 @@ protected void setId(String id) {
* @return the type of this entry
* @since 2.0.0
*/
public int getType() {
public Type getType() {
return type;
}

Expand Down Expand Up @@ -310,7 +345,7 @@ public String toString(int depth) {
message.append(", title: ").append(title);
}

message.append(System.lineSeparator());
message.append(EOL);

StringBuilder indent = new StringBuilder();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Stack;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.maven.doxia.index.IndexEntry.Type;
import org.apache.maven.doxia.parser.BufferingSinkProxyFactory;
import org.apache.maven.doxia.parser.BufferingSinkProxyFactory.BufferingSink;
import org.apache.maven.doxia.parser.SinkWrapper;
Expand All @@ -39,38 +40,11 @@
* @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
*/
public class IndexingSink extends SinkWrapper {
/** Section 1. */
private static final int TYPE_SECTION_1 = 1;

/** Section 2. */
private static final int TYPE_SECTION_2 = 2;

/** Section 3. */
private static final int TYPE_SECTION_3 = 3;

/** Section 4. */
private static final int TYPE_SECTION_4 = 4;

/** Section 5. */
private static final int TYPE_SECTION_5 = 5;

/** Defined term. */
private static final int TYPE_DEFINED_TERM = 6;

/** Figure. */
private static final int TYPE_FIGURE = 7;

/** Table. */
private static final int TYPE_TABLE = 8;

/** Title. */
private static final int TITLE = 9;

/** The current type. */
private int type;

/** The current title. */
private String title;
/** The current type.
* TODO: remove redundant type which is equivalent to type of
* */
private Type type;

/** The stack. */
private final Stack<IndexEntry> stack;
Expand All @@ -84,6 +58,7 @@ public class IndexingSink extends SinkWrapper {
private final IndexEntry rootEntry;

private boolean isComplete;
private boolean isTitle;
/**
* @deprecated legacy constructor, use {@link #IndexingSink(Sink)} with {@link SinkAdapter} as argument and call {@link #getRootEntry()} to retrieve the index tree afterwards.
*/
Expand All @@ -106,8 +81,7 @@ private IndexingSink(IndexEntry rootEntry, Sink delegate) {
stack.push(rootEntry);
usedIds = new HashMap<>();
usedIds.put(rootEntry.getId(), new AtomicInteger());
this.type = 0;
this.title = null;
this.type = Type.UNKNOWN;
}

/**
Expand All @@ -119,17 +93,18 @@ private IndexingSink(IndexEntry rootEntry, Sink delegate) {
public IndexEntry getRootEntry() {
if (!isComplete) {
throw new IllegalStateException(
"This sink has not been closed yet, i.e. the index tree is not complete yet");
"The sink has not been closed yet, i.e. the index tree is not complete yet");
}
return rootEntry;
}
/**
* <p>Getter for the field <code>title</code>.</p>
* Shortcut for {@link #getRootEntry()} followed by {@link IndexEntry#getTitle()}.
*
* @return the title
*/
public String getTitle() {
return title;
return rootEntry.getTitle();
}

// ----------------------------------------------------------------------
Expand All @@ -138,20 +113,21 @@ public String getTitle() {

@Override
public void title(SinkEventAttributes attributes) {
this.type = TITLE;
isTitle = true;
super.title(attributes);
}

@Override
public void title_() {
this.type = 0;
isTitle = false;
super.title_();
}

@Override
public void section(int level, SinkEventAttributes attributes) {
super.section(level, attributes);
pushNewEntry(level);
this.type = IndexEntry.Type.fromSectionLevel(level);
pushNewEntry(type);
}

@Override
Expand All @@ -160,44 +136,24 @@ public void section_(int level) {
super.section_(level);
}

@Override
public void sectionTitle(int level, SinkEventAttributes attributes) {
this.type = level;
super.sectionTitle(level, attributes);
}

@Override
public void sectionTitle_(int level) {
indexEntryComplete();
super.sectionTitle_(level);
}

// public void definedTerm()
// {
// type = TYPE_DEFINED_TERM;
// }
//
// public void figureCaption()
// {
// type = TYPE_FIGURE;
// }
//
// public void tableCaption()
// {
// type = TYPE_TABLE;
// }

@Override
public void text(String text, SinkEventAttributes attributes) {
if (isTitle) {
rootEntry.setTitle(text);
return;
}
switch (this.type) {
case TITLE:
this.title = text;
break;
case TYPE_SECTION_1:
case TYPE_SECTION_2:
case TYPE_SECTION_3:
case TYPE_SECTION_4:
case TYPE_SECTION_5:
case SECTION_1:
case SECTION_2:
case SECTION_3:
case SECTION_4:
case SECTION_5:
// -----------------------------------------------------------------------
// Sanitize the id. The most important step is to remove any blanks
// -----------------------------------------------------------------------
Expand All @@ -211,10 +167,7 @@ public void text(String text, SinkEventAttributes attributes) {

setEntryId(entry, title);
break;
// Dunno how to handle these yet
case TYPE_DEFINED_TERM:
case TYPE_FIGURE:
case TYPE_TABLE:
// Dunno how to handle others yet
default:
break;
}
Expand All @@ -229,12 +182,11 @@ public void anchor(String name, SinkEventAttributes attributes) {

private boolean parseAnchor(String name) {
switch (type) {
case TITLE:
case TYPE_SECTION_1:
case TYPE_SECTION_2:
case TYPE_SECTION_3:
case TYPE_SECTION_4:
case TYPE_SECTION_5:
case SECTION_1:
case SECTION_2:
case SECTION_3:
case SECTION_4:
case SECTION_5:
IndexEntry entry = stack.lastElement();
entry.setAnchor(true);
setEntryId(entry, name);
Expand Down Expand Up @@ -270,7 +222,7 @@ String getUniqueId(String id) {
}

void indexEntryComplete() {
this.type = 0;
this.type = Type.UNKNOWN;
// remove buffering sink from pipeline
BufferingSink bufferingSink = BufferingSinkProxyFactory.castAsBufferingSink(getWrappedSink());
setWrappedSink(bufferingSink.getBufferedSink());
Expand All @@ -291,12 +243,10 @@ protected void onIndexEntry(IndexEntry entry) {}
/**
* Creates and pushes a new IndexEntry onto the top of this stack.
*/
private void pushNewEntry(int level) {

IndexEntry entry = new IndexEntry(peek(), "");
private void pushNewEntry(Type type) {

IndexEntry entry = new IndexEntry(peek(), "", type);
entry.setTitle("");

stack.push(entry);
// now buffer everything till the next index metadata is complete
setWrappedSink(new BufferingSinkProxyFactory().createWrapper(getWrappedSink()));
Expand Down

0 comments on commit ca72f70

Please sign in to comment.