use of com.nedap.archie.query.APathQuery in project archetype-languageserver by nedap.
the class PartialAOMPathQuery method findLSPPartial.
// TODO: check if/how this is different from AOMPathQuery.findPartial, and potentially remove
public PartialMatch findLSPPartial(CComplexObject root) {
List<ArchetypeModelObject> result = new ArrayList<>();
boolean findThroughDifferentialPaths = true, matchSpecializedNodes = true;
result.add(root);
List<PathSegment> pathSegments = getPathSegments();
for (int i = 0; i < pathSegments.size(); i++) {
List<ArchetypeModelObject> previousResult = result;
PathSegment segment = pathSegments.get(i);
CAttribute differentialAttribute = null;
if (findThroughDifferentialPaths) {
differentialAttribute = findMatchingDifferentialPath(pathSegments.subList(i, pathSegments.size()), result);
}
if (differentialAttribute != null) {
// skip a few pathsegments for this differential path match
i = i + new APathQuery(differentialAttribute.getDifferentialPath()).getPathSegments().size() - 1;
PathSegment lastPathSegment = pathSegments.get(i);
ArchetypeModelObject oneMatchingObject = findOneMatchingObject(differentialAttribute, lastPathSegment, matchSpecializedNodes);
if (oneMatchingObject != null) {
result = Lists.newArrayList(oneMatchingObject);
} else {
result = findOneSegment(segment, result, matchSpecializedNodes);
}
} else {
result = findOneSegment(segment, result, matchSpecializedNodes);
}
List<ArchetypeModelObject> returnValue = previousResult.stream().filter((object) -> object != null).collect(Collectors.toList());
if (result.isEmpty()) {
// we have to return our partial match here
return new PartialMatch(pathSegments, pathSegments.subList(i, pathSegments.size()), returnValue);
}
}
List<ArchetypeModelObject> returnValue = result.stream().filter((object) -> object != null).collect(Collectors.toList());
return new PartialMatch(pathSegments, new ArrayList<>(), returnValue);
}
use of com.nedap.archie.query.APathQuery in project archetype-languageserver by nedap.
the class DocumentInformation method lookupCObjectOrAttribute.
/**
* Archetype path lookup. Definition only for now. Sorry for the ugly code, but this works rather well :)
* @param path
* @return
*/
public DocumentSymbol lookupCObjectOrAttribute(String path, boolean returnResultSoFar) {
APathQuery query = new APathQuery(path);
List<PathSegment> pathSegments = query.getPathSegments();
List<DocumentSymbol> documentSymbols = DocumentSymbolUtils.getDocumentSymbols(symbols);
if (documentSymbols.isEmpty()) {
return null;
}
DocumentSymbol definitionSections = DocumentSymbolUtils.getDocumentSymbolOrThrow(documentSymbols.get(0).getChildren(), DEFINITION_SECTION_NAME);
DocumentSymbol rootNode = definitionSections.getChildren().get(0);
DocumentSymbol currentSymbol = rootNode;
for (int i = 0; i < pathSegments.size(); i++) {
PathSegment segment = pathSegments.get(i);
// search attribute
if (currentSymbol.getChildren() == null || currentSymbol.getChildren().isEmpty()) {
return currentSymbol;
}
{
DocumentSymbol foundAttribute = findAttribute(currentSymbol, segment, pathSegments, i);
if (foundAttribute == null) {
return returnResultSoFar ? currentSymbol : null;
} else if (foundAttribute.getName().startsWith("/")) {
// differential path. Skip a couple of path segments
int numberOfSegments = new APathQuery(foundAttribute.getName()).getPathSegments().size();
i = i + numberOfSegments - 1;
segment = pathSegments.get(i);
}
currentSymbol = foundAttribute;
}
if (currentSymbol.getChildren() == null || currentSymbol.getChildren().isEmpty()) {
return returnCurrentSymbolIfPossible(returnResultSoFar, pathSegments, currentSymbol, i);
}
if (segment.getIndex() != null) {
if (segment.getIndex() > currentSymbol.getChildren().size()) {
return returnResultSoFar ? currentSymbol : null;
}
currentSymbol = currentSymbol.getChildren().get(segment.getIndex() - 1);
} else if (segment.hasIdCode() || segment.hasArchetypeRef()) {
DocumentSymbol foundCObject = findCObject(currentSymbol, segment);
if (foundCObject == null) {
return returnCurrentSymbolIfPossible(returnResultSoFar, pathSegments, currentSymbol, i);
}
currentSymbol = foundCObject;
} else {
if (i == pathSegments.size() - 1) {
// this just points at an attribute, that's ok.
return returnCurrentSymbolIfPossible(returnResultSoFar, pathSegments, currentSymbol, i);
} else // let's hope it's size 1
if (currentSymbol.getChildren() == null || currentSymbol.getChildren().isEmpty()) {
return returnCurrentSymbolIfPossible(returnResultSoFar, pathSegments, currentSymbol, i);
} else if (currentSymbol.getChildren().size() == 1) {
currentSymbol = currentSymbol.getChildren().get(0);
} else {
throw new RuntimeException("cannot find path, it has too many possible values without an id at segment " + segment);
}
}
}
return currentSymbol;
}
Aggregations