Skip to content

Instantly share code, notes, and snippets.

@oschrenk
Created January 13, 2012 15:35
Show Gist options
  • Save oschrenk/1607001 to your computer and use it in GitHub Desktop.
Save oschrenk/1607001 to your computer and use it in GitHub Desktop.
FlatTree - Example for parsing a flat file with hierarchical structure
public abstract class AbstractFlatTreeParser<T> implements Parser<T> {
/**
* Gets the tree reader.
*
* @param root
* the root
* @param path
* the path
* @return the tree reader
* @throws FileNotFoundException
* the file not found exception
*/
protected TreeReader getTreeReader(final Node root, final File path)
throws FileNotFoundException {
return new TreeReader(root, new FlatReader(getReader(path)));
}
/**
* Gets the reader.
*
* @param path
* the path
* @return the reader
* @throws FileNotFoundException
* the file not found exception
*/
protected Reader getReader(final File path) throws FileNotFoundException {
return Files.newReader(path, Charsets.UTF_8);
}
LFlatTree;http://flattree.sourceforge.net;
Fdelimited columns;supports escaping,quoting,multiple lines
Ffixed columns
Fmixed columns
Freading;hierarchical API
Fwriting;hierarchical API
FSAX adapter
FStax adapter
FXStream adapter
LX-Files
Fparses anything;supports CVS,FLR,INI,HTML,XML,XSL,XLS,DOC,...
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import flattree.TreeReader;
import flattree.tree.ConstantLeaf;
import flattree.tree.DelimitedLeaf;
import flattree.tree.DelimitedNode;
import flattree.tree.Node;
import flattree.tree.SyntheticNode;
public class HierarchicalFlatTreeParser extends
AbstractFlatTreeParser<List<String>> {
/** The Constant ROOT. */
private static final Node ROOT = new SyntheticNode("Root")
.add(new DelimitedNode("Library")
.add(new ConstantLeaf("L"))
.add(new DelimitedLeaf("Name", ';'))
.add(new DelimitedLeaf("URL", ';'))
.add(new DelimitedNode("Feature")
.add(new ConstantLeaf("F"))
.add(new DelimitedLeaf("Name", ';'))
.add(new DelimitedLeaf("Details", ';'))));
@SuppressWarnings("unused")
@Override
public List<String> parse(final File path) throws IOException {
final TreeReader reader = getTreeReader(ROOT, path);
final List<String> libraryNames = new LinkedList<String>();
// read synthetic root
reader.read();
while (reader.read() == TreeReader.START) {
if (reader.getName().equals("Library")) {
final String libraryName = reader.getValues().get("Name");
final String libraryUrl = reader.getValues().get("URL");
// System.out.println("LIB: " + libraryName);
libraryNames.add(libraryName);
while (reader.read() == TreeReader.START) {
if ("Feature".equals(reader.getName())) {
final String featureName = reader.getValues().get(
"Name");
final String featureDetails = reader.getValues().get(
"Details");
// System.out.println(featureName);
}
skip(reader);
}
} else {
skip(reader);
}
}
return libraryNames;
}
private void skip(final TreeReader reader) {
int started = 1;
while (true) {
if (reader.read() == TreeReader.START) {
started++;
} else {
started--;
}
if (started == 0) {
break;
}
}
}
}
public class HierarchicalFlatTreeParserTest {
/** The Constant PATH. */
private static final String PATH = "/hierarchical.txt";
/** The path. */
private File path;
@Before
public void setup() {
path = new File(this.getClass().getResource(PATH).getFile());
}
@Test
public void testHierarchical() throws IOException {
final Parser<List<String>> parser = new HierarchicalFlatTreeParser();
final List<String> parse = parser.parse(path);
assertNotNull(parse);
assertFalse(parse.isEmpty());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment