Skip to content

Instantly share code, notes, and snippets.

@pcdavid
Last active June 17, 2023 15:07
Show Gist options
  • Save pcdavid/0897ad9d288917c59beed42e61b28631 to your computer and use it in GitHub Desktop.
Save pcdavid/0897ad9d288917c59beed42e61b28631 to your computer and use it in GitHub Desktop.
Extract all AQL expressions from a Sirius Desktop VSM
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.function.Predicate;
public class ExtractVSMExpressions {
private static class AttributesCollector extends DefaultHandler {
private final Set<String> expressions;
private final Predicate<String> attributePredicate;
public AttributesCollector(Predicate<String> attributePredicate, Set<String> expressions) {
this.attributePredicate = Objects.requireNonNull(attributePredicate);
this.expressions = Objects.requireNonNull(expressions);
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
for (int i = 0; i < attributes.getLength(); i++) {
String value = attributes.getValue(i);
if (this.attributePredicate.test(value)) {
expressions.add(value);
}
}
}
}
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
Set<String> expressions = new HashSet<>();
Predicate<String> isAQLExpression = (value) -> value.startsWith("aql:");
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
saxParser.parse(Path.of(args[0]).toFile(), new AttributesCollector(isAQLExpression, expressions));
expressions.stream().sorted().forEach(System.out::println);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment