/** * Returns a name corresponding to the name segment, or throw an exception otherwise. * However, accepts null segments. * * TODO determine whether to keep this method */ @SuppressWarnings("unused") @Nullable static QName toNameNullSafe(@Nullable Object segment) { return segment != null ? toName(segment) : null; }
/** * Returns the value of the first segment if it is a name segment; otherwise null. */ @NotNull default ItemName firstToName() { return ItemPath.toName(first()); }
@Override public ItemName lastName() { for (int i = segments.size() - 1; i >= 0; i--) { Object segment = segments.get(i); if (ItemPath.isName(segment)) { return ItemPath.toName(segment); } } return null; }
/** * Returns true if the path contains the specified name (requires exact match). */ default boolean containsNameExactly(QName name) { return getSegments().stream().anyMatch(component -> isName(component) && name.equals(toName(component))); }
/** * If the path consists of a single name segment (not variable nor special symbol), returns the corresponding value. * Otherwise throws an exception. */ @NotNull default ItemName asSingleNameOrFail() { if (isSingleName()) { return ItemPath.toName(first()); } else { throw new IllegalArgumentException("Expected a single-name path, bug got "+this); } }
ItemName getItemName() { if (itemPath.size() != 1) { throw new IllegalStateException("Expected single-item path, found '" + itemPath + "' instead."); } return ItemPath.toName(itemPath.first()); }
/** * Returns the value of the first name segment or null if there's no name segment. * NOTE: The difference between firstToName and firstName is that the former always looks * at the first segment and tries to interpret it as a name. The latter, however, tries to * find the first segment of Name type. */ @Nullable default ItemName firstName() { int i = firstNameIndex(); return i >= 0 ? toName(getSegment(i)) : null; }
/** * If the path consists of a single name segment (not variable nor special symbol), returns the corresponding value. * Otherwise returns null. */ default QName asSingleName() { return isSingleName() ? ItemPath.toName(first()) : null; }
@Override public <ID extends ItemDefinition> ID findItemDefinition(@NotNull ItemPath path, @NotNull Class<ID> clazz) { if (path.size() == 1 && path.startsWithName()) { return findLocalItemDefinition(ItemPath.toName(path.first()), clazz, false); } throw new UnsupportedOperationException("TODO implement if needed"); }
@NotNull public ItemName getElementName() { if (definition != null) { return definition.getName(); // this is more precise, as the name in path can be unqualified } if (fullPath.isEmpty()) { throw new IllegalStateException("Empty full path in filter "+this); } Object last = fullPath.last(); if (ItemPath.isName(last)) { return ItemPath.toName(last); } else { throw new IllegalStateException("Got "+last+" as a last path segment in value filter "+this); } }
private static boolean isMetadata(ItemPathType itemPath) { boolean retMeta = false; for (Object ips : itemPath.getItemPath().getSegments()) { if (ItemPath.isName(ips) && ObjectType.F_METADATA.getLocalPart().equals(ItemPath.toName(ips).getLocalPart())) { return true; } } return retMeta; }
private String formatPath(ItemDelta itemDelta) { if (itemDelta.getDefinition() != null && itemDelta.getDefinition().getDisplayName() != null) { return itemDelta.getDefinition().getDisplayName(); } StringBuilder sb = new StringBuilder(); for (Object segment : itemDelta.getPath().getSegments()) { if (ItemPath.isName(segment)) { if (sb.length() > 0) { sb.append("/"); } sb.append(ItemPath.toName(segment).getLocalPart()); } } return sb.toString(); }
public static void assertPathEqualsExceptForPrefixes(String message, ItemPath expected, ItemPath actual) { assertEquals(message + ": wrong path size", expected.size(), actual.size()); for (int i = 0; i < expected.size(); i++) { Object expectedSegment = expected.getSegment(i); Object actualSegment = actual.getSegment(i); if (ItemPath.isName(expectedSegment)) { QName qnameExpected = ItemPath.toName(expectedSegment); QName qnameActual = ItemPath.toName(actualSegment); assertEquals(message + ": wrong NS in path segment #" + (i+1), qnameExpected.getNamespaceURI(), qnameActual.getNamespaceURI()); assertEquals(message + ": wrong local part in path segment #" + (i+1), qnameExpected.getLocalPart(), qnameActual.getLocalPart()); } else { if (!ItemPath.segmentsEquivalent(expectedSegment, actualSegment)) { System.out.println("hi"); } assertTrue(message + ": wrong path segment #" + (i+1) + ": exp=" + expectedSegment + ", act=" + actualSegment, ItemPath.segmentsEquivalent(expectedSegment, actualSegment)); } } }
@Override public Object find(ItemPath path) { if (path == null || path.isEmpty()) { return this; } Object first = path.first(); if (!ItemPath.isName(first)) { throw new IllegalArgumentException("Attempt to lookup item using a non-name path "+path+" in "+this); } ItemName subName = ItemPath.toName(first); ItemPath rest = path.rest(); Item<?,?> subItem = findItem(subName); if (subItem == null) { return null; } return subItem.find(rest); }
@Override public <IV extends PrismValue,ID extends ItemDefinition> PartiallyResolvedItem<IV,ID> findPartial(ItemPath path) { if (path == null || path.isEmpty()) { // Incomplete path return null; } Object first = path.first(); if (!ItemPath.isName(first)) { throw new IllegalArgumentException("Attempt to lookup item using a non-name path "+path+" in "+this); } ItemName subName = ItemPath.toName(first); ItemPath rest = path.rest(); Item<?,?> subItem = findItem(subName); if (subItem == null) { return null; } return subItem.findPartial(rest); }
private boolean containsItem(PrismContainerValue prismContainerValue, ItemPath itemPath) { ItemName first = ItemPath.toName(itemPath.first()); Item item = prismContainerValue.findItem(first); if (item == null) { return false; } ItemPath pathTail = stripFirstIds(itemPath); if (item instanceof PrismContainer) { return containsItem((PrismContainer) item, pathTail); } else if (item instanceof PrismReference) { return pathTail.isEmpty(); // we do not want to look inside references } else if (item instanceof PrismProperty) { return pathTail.isEmpty(); // ...neither inside atomic values } else { return false; // should not occur anyway } }
private void assertNormalizedPath(UniformItemPath normalized, Object... expected) { assertEquals("wrong path length",normalized.size(), expected.length); for(int i=0; i<normalized.size(); i+=2) { ItemPathSegment nameSegment = normalized.getSegment(i); assert ItemPath.isName(nameSegment) : "Expected name segment but it was "+nameSegment.getClass(); QName name = ItemPath.toName(nameSegment); assert name != null : "name is null"; assert name.getNamespaceURI().equals(NS) : "wrong namespace: "+name.getNamespaceURI(); assert name.getLocalPart().equals(expected[i]) : "wrong local name, expected "+expected[i]+", was "+name.getLocalPart(); if (i + 1 < expected.length) { Object idSegment = normalized.getSegment(i+1); assert ItemPath.isId(idSegment) : "Expected is segment but it was "+idSegment.getClass(); Long id = ItemPath.toId(idSegment); assertId(id, (Long)expected[i+1]); } } }
static boolean segmentsEquivalent(Object o1, Object o2) { if (ItemPath.isName(o1)) { return ItemPath.isName(o2) && QNameUtil.match(ItemPath.toName(o1), ItemPath.toName(o2)); } else if (ItemPath.isVariable(o1)) { return ItemPath.isVariable(o2) && QNameUtil.match(ItemPath.toVariableName(o1), ItemPath.toVariableName(o2)); } else if (ItemPath.isSpecial(o1)) { return ItemPath.isSpecial(o2) && QNameUtil.match(ItemPathSegmentUtil.getSpecialSymbol(o1), ItemPathSegmentUtil.getSpecialSymbol(o2)); } else if (ItemPath.isId(o1)) { return ItemPath.isId(o2) && Objects.equals(ItemPath.toId(o1), ItemPath.toId(o2)); } else { return false; } }
private boolean isDynamic(ItemPath path) { for (Object segment : path.getSegments()) { if (ItemPath.isName(segment)) { QName name = ItemPath.toName(segment); if (QNameUtil.match(name, ShadowType.F_ATTRIBUTES) || QNameUtil.match(name, ObjectType.F_EXTENSION)) { return true; } } } return false; }
protected ObjectDelta<ShadowType> createModifyAccountShadowReplaceDelta(String accountOid, PrismObject<ResourceType> resource, ItemPath itemPath, Object... newRealValue) throws SchemaException, ObjectNotFoundException, CommunicationException, ConfigurationException { if (itemPath.startsWithName(ShadowType.F_ATTRIBUTES)) { PropertyDelta<?> attributeDelta = createAttributeReplaceDelta(resource, ItemPath.toName(itemPath.last()), newRealValue); ObjectDelta<ShadowType> accountDelta = prismContext.deltaFactory().object() .createModifyDelta(accountOid, attributeDelta, ShadowType.class); return accountDelta; } else { ObjectDelta<ShadowType> accountDelta = prismContext.deltaFactory().object().createModificationReplaceProperty( ShadowType.class, accountOid, itemPath, newRealValue); return accountDelta; } }