Skip to content

Instantly share code, notes, and snippets.

@arafalov
Created August 7, 2016 14:21
Show Gist options
  • Save arafalov/85ec492ecc1a08139d7b188af94ddb66 to your computer and use it in GitHub Desktop.
Save arafalov/85ec492ecc1a08139d7b188af94ddb66 to your computer and use it in GitHub Desktop.
package com.solrstart;
import org.attoparser.IMarkupHandler;
import org.attoparser.IMarkupParser;
import org.attoparser.MarkupParser;
import org.attoparser.ParseException;
import org.attoparser.config.ParseConfiguration;
import org.attoparser.discard.DiscardMarkupHandler;
import org.attoparser.select.NodeSelectorMarkupHandler;
import org.attoparser.simple.AbstractSimpleMarkupHandler;
import org.attoparser.simple.SimplifierMarkupHandler;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Reader;
import java.util.Map;
public class AttoparserTest {
private static class ElementHandler extends AbstractSimpleMarkupHandler {
@Override
public void handleOpenElement(String elementName, Map<String, String> attributes, int line, int col) throws ParseException {
super.handleOpenElement(elementName, attributes, line, col);
handleElement(elementName, attributes, line);
}
@Override
public void handleStandaloneElement(String elementName, Map<String, String> attributes, boolean minimized, int line, int col) throws ParseException {
super.handleStandaloneElement(elementName, attributes, minimized, line, col);
handleElement(elementName, attributes, line);
}
private void handleElement(String elementName, Map<String, String> attributes, int line) {
System.out.printf("Element: %s at line %d\n", elementName, line);
if (attributes != null) {
for (String attribName : attributes.keySet()) {
System.out.printf(" %s=%s\n", attribName, attributes.get(attribName));
}
}
System.out.println();
}
}
public static void main(String[] args) throws FileNotFoundException, ParseException {
// Open file (managed-schema)
// File all elements of type <filter>
// Enumerate all attributes it has
Reader reader = new BufferedReader(new FileReader(args[0]));
final IMarkupHandler simplifyingHandler = new SimplifierMarkupHandler(new ElementHandler());
final IMarkupHandler nodeSelectingHandler = new NodeSelectorMarkupHandler(simplifyingHandler, new DiscardMarkupHandler(), "filter");
final IMarkupParser parser = new MarkupParser(ParseConfiguration.xmlConfiguration());
parser.parse(reader, nodeSelectingHandler);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment