/** Initializes the output. Sets the expected encoding and content type. */ public void initResponse() { // set content type and encoding final SerializerOptions opts = sopts(); final String enc = opts.get(SerializerOptions.ENCODING); res.setCharacterEncoding(enc); final String ct = mediaType(opts); res.setContentType(new TokenBuilder(ct).add(CHARSET).add(enc).toString()); }
/** * Runs a query and checks the serialization parameters. * * @throws IOException I/O exception */ @Test public void queryOptions() throws IOException { final Query query = session.query("declare option output:encoding 'US-ASCII';()"); query.execute(); final SerializerOptions sp = new SerializerOptions(); sp.parse(query.options()); assertEquals("US-ASCII", sp.get(SerializerOptions.ENCODING)); query.close(); }
/** * Returns the media type defined in the specified serialization parameters. * * @param sopts serialization parameters * @return media type */ public static String mediaType(final SerializerOptions sopts) { // set content type final String type = sopts.get(SerializerOptions.MEDIA_TYPE); if (!type.isEmpty()) return type; // determine content type dependent on output method final SerialMethod sm = sopts.get(SerializerOptions.METHOD); if (sm == SerialMethod.RAW) return APP_OCTET; if (sm == SerialMethod.XML) return APP_XML; if (sm == SerialMethod.XHTML || sm == SerialMethod.HTML) return TEXT_HTML; if (sm == SerialMethod.JSON) { final JsonSerialOptions jprop = sopts.get(SerializerOptions.JSON); return jprop.get(JsonOptions.FORMAT) == JsonFormat.JSONML ? APP_JSONML : APP_JSON; } return TEXT_PLAIN; }
/** Tests the namespace stripping option (Option {@link MainOptions#STRIPNS}). */ @Test public void parse() { set(MainOptions.STRIPNS, true); set(MainOptions.SERIALIZER, SerializerOptions.get(false)); final String doc = "<e xmlns='A'><b:f xmlns:b='B'/></e>"; for (final boolean b : new boolean[] {false, true}) { set(MainOptions.INTPARSE, b); execute(new CreateDB(NAME, doc)); assertEquals("<e><f/></e>", query(".")); assertEquals("<f/>", query("e/f")); } }
/** Tests the xml:space attribute. */ @Test public void xmlSpace() { set(MainOptions.SERIALIZER, SerializerOptions.get(false)); final String in = "<x><a xml:space='default'> </a><a> </a>" + "<a xml:space='preserve'> </a></x>"; final String out = "<x><a xml:space=\"default\"/><a/>" + "<a xml:space=\"preserve\"> </a></x>"; for (final boolean b : new boolean[] {true, false}) { set(MainOptions.INTPARSE, b); execute(new CreateDB(NAME, in)); assertEquals("Internal parser: " + b, out, query(".")); } }