use of org.gephi.utils.Serialization in project gephi by gephi.
the class FilterModelPersistenceProvider method readXML.
public void readXML(XMLStreamReader reader, FilterModelImpl model) throws XMLStreamException {
Serialization serialization = new Serialization(model.getGraphModel());
Map<Integer, Query> idMap = new HashMap<>();
boolean end = false;
while (reader.hasNext() && !end) {
Integer eventType = reader.next();
if (eventType.equals(XMLEvent.START_ELEMENT)) {
String name = reader.getLocalName();
if ("autorefresh".equalsIgnoreCase(name)) {
String val = reader.getAttributeValue(null, "value");
model.setAutoRefresh(Boolean.parseBoolean(val));
} else if ("query".equalsIgnoreCase(name)) {
String id = reader.getAttributeValue(null, "id");
String parent = reader.getAttributeValue(null, "parent");
Query query = readQuery(reader, model, serialization);
if (query != null) {
idMap.put(Integer.parseInt(id), query);
if (parent != null) {
int parentId = Integer.parseInt(parent);
Query parentQuery = idMap.get(parentId);
// For example a partition filter, which depends on partitions, and partitions are not serialized
if (parentQuery != null) {
model.setSubQuery(parentQuery, query);
}
} else {
// Top query
model.addFirst(query);
}
}
} else if ("savedquery".equalsIgnoreCase(name)) {
String id = reader.getAttributeValue(null, "id");
String parent = reader.getAttributeValue(null, "parent");
Query query = readQuery(reader, model, serialization);
if (query != null) {
idMap.put(Integer.parseInt(id), query);
if (parent != null) {
int parentId = Integer.parseInt(parent);
Query parentQuery = idMap.get(parentId);
if (parentQuery != null) {
AbstractQueryImpl impl = (AbstractQueryImpl) parentQuery;
impl.addSubQuery(query);
}
} else {
model.getLibrary().saveQuery(query);
}
}
}
} else if (eventType.equals(XMLStreamReader.END_ELEMENT)) {
if ("filtermodel".equalsIgnoreCase(reader.getLocalName())) {
end = true;
}
}
}
// Init filters
Graph graph;
graph = model.getGraphModel().getGraph();
for (Query rootQuery : model.getQueries()) {
for (Query q : rootQuery.getDescendantsAndSelf()) {
Filter filter = q.getFilter();
if (filter instanceof NodeFilter || filter instanceof EdgeFilter || filter instanceof ElementFilter) {
FilterProcessor filterProcessor = new FilterProcessor();
filterProcessor.init(filter, graph);
}
}
}
}
use of org.gephi.utils.Serialization in project gephi by gephi.
the class FilterModelPersistenceProvider method writeQuery.
private void writeQuery(String code, XMLStreamWriter writer, FilterModelImpl model, Query query, int parentId) throws XMLStreamException {
Serialization serialization = new Serialization(model.getGraphModel());
writer.writeStartElement(code);
int id = queryId++;
writer.writeAttribute("id", String.valueOf(id));
if (parentId != -1) {
writer.writeAttribute("parent", String.valueOf(parentId));
}
Filter filter = query.getFilter();
FilterBuilder builder = model.getLibrary().getBuilder(filter);
writer.writeAttribute("builder", builder.getClass().getName());
writer.writeAttribute("filter", filter.getClass().getName());
if (query.getName() != null) {
writer.writeAttribute("name", query.getName());
}
// Params
for (int i = 0; i < query.getPropertiesCount(); i++) {
FilterProperty prop = query.getFilter().getProperties()[i];
writeParameter(writer, i, prop, serialization);
}
writer.writeEndElement();
for (Query child : query.getChildren()) {
writeQuery(code, writer, model, child, id);
}
}
Aggregations