Search in sources :

Example 1 with JsonReference

use of com.reprezen.swagedit.core.json.references.JsonReference in project KaiZen-OpenAPI-Editor by RepreZen.

the class JsonReferenceHyperlinkDetector method doDetect.

@Override
protected IHyperlink[] doDetect(JsonDocument doc, ITextViewer viewer, HyperlinkInfo info, JsonPointer pointer) {
    URI baseURI = getBaseURI();
    AbstractNode node = doc.getModel().find(pointer);
    JsonReference reference = getFactory().createSimpleReference(getBaseURI(), node);
    if (reference == null) {
        reference = getFactory().create(node);
    }
    if (reference.isInvalid() || reference.isMissing(doc, getBaseURI())) {
        return null;
    }
    if (reference.isLocal()) {
        IRegion target = doc.getRegion(reference.getPointer());
        if (target == null) {
            return null;
        }
        return new IHyperlink[] { new SwaggerHyperlink(reference.getPointer().toString(), viewer, info.region, target) };
    } else {
        URI resolved;
        try {
            resolved = baseURI.resolve(reference.getUri());
        } catch (IllegalArgumentException e) {
            // the given string violates RFC 2396
            return null;
        }
        IFile file = DocumentUtils.getWorkspaceFile(resolved);
        if (file != null && file.exists()) {
            return new IHyperlink[] { createFileHyperlink(info.region, info.text, file, reference.getPointer()) };
        }
    }
    return null;
}
Also used : JsonReference(com.reprezen.swagedit.core.json.references.JsonReference) IFile(org.eclipse.core.resources.IFile) AbstractNode(com.reprezen.swagedit.core.model.AbstractNode) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) URI(java.net.URI) IRegion(org.eclipse.jface.text.IRegion)

Example 2 with JsonReference

use of com.reprezen.swagedit.core.json.references.JsonReference in project KaiZen-OpenAPI-Editor by RepreZen.

the class Mocks method mockHyperlinkDetector.

public static JsonReferenceHyperlinkDetector mockHyperlinkDetector(final URI uri, final JsonNode document) {
    final JsonDocumentManager manager = mock(JsonDocumentManager.class);
    final IFile file = mock(IFile.class);
    when(file.exists()).thenReturn(true);
    when(manager.getDocument(Mockito.any(URI.class))).thenReturn(document);
    when(manager.getFile(Mockito.any(URI.class))).thenReturn(file);
    return new SwaggerReferenceHyperlinkDetector() {

        // allow running tests as non plugin tests
        protected URI getBaseURI() {
            return uri;
        }

        protected JsonReferenceFactory getFactory() {
            return new JsonReferenceFactory() {

                @Override
                public JsonReference createSimpleReference(URI baseURI, AbstractNode valueNode) {
                    JsonReference ref = super.createSimpleReference(baseURI, valueNode);
                    if (ref != null) {
                        ref.setDocumentManager(manager);
                    }
                    return ref;
                }

                @Override
                public JsonReference create(AbstractNode node) {
                    JsonReference ref = super.create(node);
                    ref.setDocumentManager(manager);
                    return ref;
                }

                @Override
                public JsonReference create(JsonNode node) {
                    JsonReference ref = super.create(node);
                    ref.setDocumentManager(manager);
                    return ref;
                }

                ;
            };
        }
    };
}
Also used : JsonReference(com.reprezen.swagedit.core.json.references.JsonReference) IFile(org.eclipse.core.resources.IFile) SwaggerReferenceHyperlinkDetector(com.reprezen.swagedit.editor.hyperlinks.SwaggerReferenceHyperlinkDetector) AbstractNode(com.reprezen.swagedit.core.model.AbstractNode) JsonReferenceFactory(com.reprezen.swagedit.core.json.references.JsonReferenceFactory) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonDocumentManager(com.reprezen.swagedit.core.json.references.JsonDocumentManager) URI(java.net.URI)

Example 3 with JsonReference

use of com.reprezen.swagedit.core.json.references.JsonReference in project KaiZen-OpenAPI-Editor by RepreZen.

the class JsonReferenceFactoryTest method test4.

@Test
public void test4() throws URISyntaxException {
    JsonReferenceFactory factory = new JsonReferenceFactory();
    JsonReference result = factory.doCreate("file://path/to/file/doc.yaml#/definitions/Foo", null);
    assertEquals(URI.create("file://path/to/file/doc.yaml#/definitions/Foo"), result.getUri());
    assertFalse(result.isLocal());
    assertTrue(result.isAbsolute());
    assertEquals(JsonPointer.compile("/definitions/Foo"), result.getPointer());
}
Also used : JsonReference(com.reprezen.swagedit.core.json.references.JsonReference) JsonReferenceFactory(com.reprezen.swagedit.core.json.references.JsonReferenceFactory) Test(org.junit.Test)

Example 4 with JsonReference

use of com.reprezen.swagedit.core.json.references.JsonReference in project KaiZen-OpenAPI-Editor by RepreZen.

the class JsonReferenceFactoryTest method test3.

@Test
public void test3() throws URISyntaxException {
    JsonReferenceFactory factory = new JsonReferenceFactory();
    JsonReference result = factory.doCreate("../doc.yaml#/definitions/Foo", null);
    assertEquals(new URI(null, "../doc.yaml", "/definitions/Foo"), result.getUri());
    assertFalse(result.isLocal());
    assertFalse(result.isAbsolute());
    assertEquals(JsonPointer.compile("/definitions/Foo"), result.getPointer());
}
Also used : JsonReference(com.reprezen.swagedit.core.json.references.JsonReference) JsonReferenceFactory(com.reprezen.swagedit.core.json.references.JsonReferenceFactory) URI(java.net.URI) Test(org.junit.Test)

Example 5 with JsonReference

use of com.reprezen.swagedit.core.json.references.JsonReference in project KaiZen-OpenAPI-Editor by RepreZen.

the class JsonReferenceFactoryTest method test5.

@Test
public void test5() throws URISyntaxException {
    JsonReferenceFactory factory = new JsonReferenceFactory();
    JsonReference result = factory.doCreate("file://path/to/file/doc.yaml#", null);
    assertEquals(URI.create("file://path/to/file/doc.yaml#"), result.getUri());
    assertFalse(result.isLocal());
    assertTrue(result.isAbsolute());
    assertEquals(JsonPointer.compile(""), result.getPointer());
}
Also used : JsonReference(com.reprezen.swagedit.core.json.references.JsonReference) JsonReferenceFactory(com.reprezen.swagedit.core.json.references.JsonReferenceFactory) Test(org.junit.Test)

Aggregations

JsonReference (com.reprezen.swagedit.core.json.references.JsonReference)9 JsonReferenceFactory (com.reprezen.swagedit.core.json.references.JsonReferenceFactory)8 URI (java.net.URI)6 Test (org.junit.Test)6 AbstractNode (com.reprezen.swagedit.core.model.AbstractNode)3 IFile (org.eclipse.core.resources.IFile)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 JsonDocumentManager (com.reprezen.swagedit.core.json.references.JsonDocumentManager)2 SwaggerReferenceHyperlinkDetector (com.reprezen.swagedit.editor.hyperlinks.SwaggerReferenceHyperlinkDetector)1 IRegion (org.eclipse.jface.text.IRegion)1 IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)1