Search in sources :

Example 1 with Literal

use of org.exquery.xquery.Literal in project exist by eXist-db.

the class RegistryFunctionsTest method outputMethod.

@Test
public void outputMethod() throws SerializationAnnotationException, TransformerException, IOException, SAXException {
    // test setup
    final String methodStr = "html5";
    final MethodAnnotation method = new MethodAnnotation();
    method.setName(SerializationAnnotationName.method.getQName());
    method.setLiterals(new Literal[] { new Literal() {

        @Override
        public Type getType() {
            return Type.STRING;
        }

        @Override
        public String getValue() {
            return methodStr;
        }
    } });
    method.initialise();
    // execute serialize method
    final MemTreeBuilder builder = new MemTreeBuilder();
    builder.startDocument();
    RegistryFunctions.serializeSerializationAnnotation(builder, method);
    builder.endDocument();
    // assert result
    final String xmlResult = documentToString(builder.getDocument());
    final Source srcExpected = Input.fromString("<method xmlns=\"http://www.w3.org/2010/xslt-xquery-serialization\">" + methodStr + "</method>").build();
    final Source srcActual = Input.fromString(xmlResult).build();
    final Diff diff = DiffBuilder.compare(srcExpected).withTest(srcActual).checkForIdentical().build();
    assertFalse(diff.toString(), diff.hasDifferences());
}
Also used : Type(org.exquery.xquery.Type) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) Diff(org.xmlunit.diff.Diff) Literal(org.exquery.xquery.Literal) MethodAnnotation(org.exquery.serialization.annotation.MethodAnnotation) Override(java.lang.Override) DOMSource(javax.xml.transform.dom.DOMSource) Test(org.junit.Test)

Example 2 with Literal

use of org.exquery.xquery.Literal in project exist by eXist-db.

the class RegistryFunctionsTest method outputMediaType.

@Test
public void outputMediaType() throws URISyntaxException, TransformerException, IOException, SAXException, SerializationAnnotationException {
    // test setup
    final String internetMediaType = "application/octet-stream";
    final MediaTypeAnnotation mediaType = new MediaTypeAnnotation();
    mediaType.setName(SerializationAnnotationName.mediatype.getQName());
    mediaType.setLiterals(new Literal[] { new Literal() {

        @Override
        public Type getType() {
            return Type.STRING;
        }

        @Override
        public String getValue() {
            return internetMediaType;
        }
    } });
    mediaType.initialise();
    // execute serialize method
    final MemTreeBuilder builder = new MemTreeBuilder();
    builder.startDocument();
    RegistryFunctions.serializeSerializationAnnotation(builder, mediaType);
    builder.endDocument();
    // assert result
    final String xmlResult = documentToString(builder.getDocument());
    final Source srcExpected = Input.fromString("<media-type xmlns=\"http://www.w3.org/2010/xslt-xquery-serialization\">" + internetMediaType + "</media-type>").build();
    final Source srcActual = Input.fromString(xmlResult).build();
    final Diff diff = DiffBuilder.compare(srcExpected).withTest(srcActual).checkForIdentical().build();
    assertFalse(diff.toString(), diff.hasDifferences());
}
Also used : Type(org.exquery.xquery.Type) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) MediaTypeAnnotation(org.exquery.serialization.annotation.MediaTypeAnnotation) Diff(org.xmlunit.diff.Diff) Literal(org.exquery.xquery.Literal) Override(java.lang.Override) DOMSource(javax.xml.transform.dom.DOMSource) Test(org.junit.Test)

Example 3 with Literal

use of org.exquery.xquery.Literal in project exist by eXist-db.

the class RegistryFunctions method serializeAnnotations.

private static void serializeAnnotations(final MemTreeBuilder builder, final ResourceFunction resourceFn) {
    final List<Annotation> annotations = new ArrayList<>();
    annotations.addAll(resourceFn.getHttpMethodAnnotations());
    annotations.addAll(resourceFn.getConsumesAnnotations());
    annotations.addAll(resourceFn.getProducesAnnotations());
    for (final Annotation annotation : annotations) {
        builder.startElement(QName.fromJavaQName(annotation.getName()), null);
        final Literal[] literals = annotation.getLiterals();
        if (literals != null) {
            for (final Literal literal : literals) {
                if (annotation instanceof ConsumesAnnotation || annotation instanceof ProducesAnnotation) {
                    builder.startElement(INTERNET_MEDIA_TYPE, null);
                    builder.characters(literal.getValue());
                    builder.endElement();
                }
            }
        }
        builder.endElement();
    }
    // path annotation
    if (resourceFn.getPathAnnotation() != null) {
        final PathAnnotation pathAnnotation = resourceFn.getPathAnnotation();
        final AttributesImpl attrs = new AttributesImpl();
        attrs.addAttribute(null, SPECIFICITY_METRIC, "", "string", Long.toString(pathAnnotation.getPathSpecificityMetric()));
        builder.startElement(QName.fromJavaQName(pathAnnotation.getName()), attrs);
        final String[] segments = pathAnnotation.getLiterals()[0].getValue().split("/");
        for (final String segment : segments) {
            if (!segment.isEmpty()) {
                builder.startElement(SEGMENT, null);
                builder.characters(segment);
                builder.endElement();
            }
        }
        builder.endElement();
    }
    // parameter annotations
    for (final ParameterAnnotation parameterAnnotation : resourceFn.getParameterAnnotations()) {
        final Literal[] literals = parameterAnnotation.getLiterals();
        final AttributesImpl attrs = new AttributesImpl();
        attrs.addAttribute(null, NAME, "", "string", literals[0].getValue());
        attrs.addAttribute(null, ARGUMENT, "", "string", literals[1].getValue());
        if (literals.length == 3) {
            attrs.addAttribute(null, DEFAULT_VALUE, "", "string", literals[2].getValue());
        }
        builder.startElement(QName.fromJavaQName(parameterAnnotation.getName()), attrs);
        builder.endElement();
    }
    // serialization annotations
    for (final SerializationAnnotation serializationAnnotation : resourceFn.getSerializationAnnotations()) {
        serializeSerializationAnnotation(builder, serializationAnnotation);
    }
}
Also used : ArrayList(java.util.ArrayList) SerializationAnnotation(org.exquery.serialization.annotation.SerializationAnnotation) Annotation(org.exquery.xquery3.Annotation) AbstractYesNoSerializationAnnotation(org.exquery.serialization.annotation.AbstractYesNoSerializationAnnotation) MethodAnnotation(org.exquery.serialization.annotation.MethodAnnotation) SerializationAnnotation(org.exquery.serialization.annotation.SerializationAnnotation) AbstractYesNoSerializationAnnotation(org.exquery.serialization.annotation.AbstractYesNoSerializationAnnotation) AttributesImpl(org.xml.sax.helpers.AttributesImpl) Literal(org.exquery.xquery.Literal)

Aggregations

Literal (org.exquery.xquery.Literal)3 Override (java.lang.Override)2 DOMSource (javax.xml.transform.dom.DOMSource)2 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)2 MethodAnnotation (org.exquery.serialization.annotation.MethodAnnotation)2 Type (org.exquery.xquery.Type)2 Test (org.junit.Test)2 Diff (org.xmlunit.diff.Diff)2 ArrayList (java.util.ArrayList)1 AbstractYesNoSerializationAnnotation (org.exquery.serialization.annotation.AbstractYesNoSerializationAnnotation)1 MediaTypeAnnotation (org.exquery.serialization.annotation.MediaTypeAnnotation)1 SerializationAnnotation (org.exquery.serialization.annotation.SerializationAnnotation)1 Annotation (org.exquery.xquery3.Annotation)1 AttributesImpl (org.xml.sax.helpers.AttributesImpl)1