private static XPathNodeTest.PrincipalType addContextNames(XPathNodeTest element, ContextProvider contextProvider, PrefixedName prefixedName, Set<? super LookupElement> list) {
    final NamespaceContext namespaceContext = contextProvider.getNamespaceContext();
    final XmlElement context = contextProvider.getContextElement();

    final XPathNodeTest.PrincipalType principalType = element.getPrincipalType();
    if (principalType == XPathNodeTest.PrincipalType.ELEMENT) {
      final Set<QName> elementNames = contextProvider.getElements(false);
      if (elementNames != null) {
        for (QName pair : elementNames) {
          if ("*".equals(pair.getLocalPart())) continue;

          if (namespaceMatches(prefixedName, pair.getNamespaceURI(), namespaceContext, context, true)) {
            if (prefixedName.getPrefix() == null && namespaceContext != null) {
              final String p = namespaceContext.getPrefixForURI(pair.getNamespaceURI(), context);
              list.add(new NodeLookup(makePrefix(p) + pair.getLocalPart(), XPathNodeTest.PrincipalType.ELEMENT));
            } else {
              list.add(new NodeLookup(pair.getLocalPart(), XPathNodeTest.PrincipalType.ELEMENT));
            }
          }
        }
      }
    } else if (principalType == XPathNodeTest.PrincipalType.ATTRIBUTE) {
      final Set<QName> attributeNames = contextProvider.getAttributes(false);
      if (attributeNames != null) {
        for (QName pair : attributeNames) {
          if ("*".equals(pair.getLocalPart())) continue;

          if (namespaceMatches(prefixedName, pair.getNamespaceURI(), namespaceContext, context, false)) {
            if (prefixedName.getPrefix() == null && namespaceContext != null) {
              final String p = namespaceContext.getPrefixForURI(pair.getNamespaceURI(), context);
              list.add(new NodeLookup(makePrefix(p) + pair.getLocalPart(), XPathNodeTest.PrincipalType.ATTRIBUTE));
            } else {
              list.add(new NodeLookup(pair.getLocalPart(), XPathNodeTest.PrincipalType.ATTRIBUTE));
            }
          }
        }
      }
    }
    return principalType;
  }
private static XPathNodeTest.PrincipalType addContextNames(XPathNodeTest element, ContextProvider contextProvider, PrefixedName prefixedName, Set<? super LookupElement> list)