com.intellij.json.psi.JsonValue Java Examples

The following examples show how to use com.intellij.json.psi.JsonValue. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JsonSchemaInspection.java    From intellij-swagger with MIT License 5 votes vote down vote up
private PsiElementVisitor createVisitor(
    final String schemaFileName,
    final ProblemsHolder holder,
    final LocalInspectionToolSession session,
    final PsiFile file) {

  JsonValue root =
      file instanceof JsonFile
          ? ObjectUtils.tryCast(file.getFirstChild(), JsonValue.class)
          : null;
  if (root == null) return PsiElementVisitor.EMPTY_VISITOR;

  JsonSchemaService service = JsonSchemaService.Impl.get(file.getProject());

  final URL url = ResourceUtil.getResource(getClass(), "schemas", schemaFileName);
  final VirtualFile virtualFile = VfsUtil.findFileByURL(url);

  final JsonSchemaObject schema = service.getSchemaObjectForSchemaFile(virtualFile);

  return new JsonElementVisitor() {
    @Override
    public void visitElement(PsiElement element) {
      if (element == root) {
        final JsonLikePsiWalker walker = JsonLikePsiWalker.getWalker(element, schema);
        if (walker == null) return;

        JsonComplianceCheckerOptions options = new JsonComplianceCheckerOptions(false);

        new JsonSchemaComplianceChecker(schema, holder, walker, session, options)
            .annotate(element);
      }
    }
  };
}
 
Example #2
Source File: JsonReferenceInspection.java    From intellij-swagger with MIT License 5 votes vote down vote up
@NotNull
@Override
public PsiElementVisitor buildVisitor(
    @NotNull final ProblemsHolder holder,
    final boolean isOnTheFly,
    @NotNull final LocalInspectionToolSession session) {
  final PsiFile file = holder.getFile();
  final VirtualFile virtualFile = file.getVirtualFile();
  final Project project = holder.getProject();

  boolean checkRefs =
      indexFacade.isMainSpecFile(virtualFile, project)
          || indexFacade.isPartialSpecFile(virtualFile, project);

  return new JsonElementVisitor() {
    @Override
    public void visitProperty(@NotNull JsonProperty o) {
      if (!checkRefs) {
        return;
      }
      if ("$ref".equals(o.getName())) {
        JsonValue value = o.getValue();

        if (!(value instanceof JsonStringLiteral)) {
          return;
        }

        final String unquotedValue = StringUtil.unquoteString(value.getText());

        if (!unquotedValue.startsWith("http")) {
          doCheck(holder, value, new CreateJsonReferenceIntentionAction(unquotedValue));
        }
      }
      super.visitProperty(o);
    }
  };
}
 
Example #3
Source File: GraphQLIntrospectEndpointUrlLineMarkerProvider.java    From js-graphql-intellij-plugin with MIT License 5 votes vote down vote up
private GraphQLConfigVariableAwareEndpoint getEndpoint(String url, JsonProperty urlJsonProperty) {
    try {

        // if the endpoint is just the url string, headers are not supported
        final JsonProperty parent = PsiTreeUtil.getParentOfType(urlJsonProperty, JsonProperty.class);
        final boolean supportsHeaders = parent != null && !"endpoints".equals(parent.getName());

        final String name = supportsHeaders ? parent.getName() : url;

        final GraphQLConfigEndpoint endpointConfig = new GraphQLConfigEndpoint(null, name, url);

        if (supportsHeaders) {
            final Stream<JsonProperty> jsonPropertyStream = PsiTreeUtil.getChildrenOfTypeAsList(urlJsonProperty.getParent(), JsonProperty.class).stream();
            final Optional<JsonProperty> headers = jsonPropertyStream.filter(p -> "headers".equals(p.getName())).findFirst();
            headers.ifPresent(headersProp -> {
                final JsonValue jsonValue = headersProp.getValue();
                if (jsonValue != null) {
                    endpointConfig.headers = new Gson().<Map<String, Object>>fromJson(jsonValue.getText(), Map.class);
                }
            });
        }


        return new GraphQLConfigVariableAwareEndpoint(endpointConfig, urlJsonProperty.getProject());

    } catch (Exception e) {
        Notifications.Bus.notify(new Notification("GraphQL", "GraphQL Configuration Error", e.getMessage(), NotificationType.ERROR), urlJsonProperty.getProject());
    }
    return null;
}