use of org.w3c.dom.ls.LSParserFilter in project cuba by cuba-platform.
the class XMLConverter method parseCommitRequest.
@Override
public CommitRequest parseCommitRequest(String content) {
try {
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS lsImpl = (DOMImplementationLS) registry.getDOMImplementation("LS");
LSParser requestConfigParser = lsImpl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
// Set options on the parser
DOMConfiguration config = requestConfigParser.getDomConfig();
config.setParameter("validate", Boolean.TRUE);
config.setParameter("element-content-whitespace", Boolean.FALSE);
config.setParameter("comments", Boolean.FALSE);
requestConfigParser.setFilter(new LSParserFilter() {
@Override
public short startElement(Element elementArg) {
return LSParserFilter.FILTER_ACCEPT;
}
@Override
public short acceptNode(Node nodeArg) {
return StringUtils.isBlank(nodeArg.getTextContent()) ? LSParserFilter.FILTER_REJECT : LSParserFilter.FILTER_ACCEPT;
}
@Override
public int getWhatToShow() {
return NodeFilter.SHOW_TEXT;
}
});
LSInput lsInput = lsImpl.createLSInput();
lsInput.setStringData(content);
Document commitRequestDoc = requestConfigParser.parse(lsInput);
Node rootNode = commitRequestDoc.getFirstChild();
if (!"CommitRequest".equals(rootNode.getNodeName()))
throw new IllegalArgumentException("Not a CommitRequest xml passed: " + rootNode.getNodeName());
CommitRequest result = new CommitRequest();
NodeList children = rootNode.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
String childNodeName = child.getNodeName();
if ("commitInstances".equals(childNodeName)) {
NodeList entitiesNodeList = child.getChildNodes();
Set<String> commitIds = new HashSet<>(entitiesNodeList.getLength());
for (int j = 0; j < entitiesNodeList.getLength(); j++) {
Node idNode = entitiesNodeList.item(j).getAttributes().getNamedItem("id");
if (idNode == null)
continue;
String id = idNode.getTextContent();
if (id.startsWith("NEW-"))
id = id.substring(id.indexOf('-') + 1);
commitIds.add(id);
}
result.setCommitIds(commitIds);
result.setCommitInstances(parseNodeList(result, entitiesNodeList));
} else if ("removeInstances".equals(childNodeName)) {
NodeList entitiesNodeList = child.getChildNodes();
List removeInstances = parseNodeList(result, entitiesNodeList);
result.setRemoveInstances(removeInstances);
} else if ("softDeletion".equals(childNodeName)) {
result.setSoftDeletion(Boolean.parseBoolean(child.getTextContent()));
}
}
return result;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Aggregations