示例#1
0
  /**
   * Constructor.
   *
   * @param os output stream
   * @param sopts serializer options
   * @throws QueryIOException query I/O exception
   */
  protected OutputSerializer(final OutputStream os, final SerializerOptions sopts)
      throws QueryIOException {

    this.sopts = sopts;
    indent = sopts.yes(INDENT);

    // project-specific options
    indents = sopts.get(INDENTS);
    tab = sopts.yes(TABULATOR) ? '\t' : ' ';

    encoding = Strings.normEncoding(sopts.get(ENCODING), true);
    PrintOutput po;
    if (encoding == Strings.UTF8) {
      po = PrintOutput.get(os);
    } else {
      try {
        po = new EncoderOutput(os, Charset.forName(encoding));
      } catch (final Exception ex) {
        throw SERENCODING_X.getIO(encoding);
      }
    }
    final int limit = sopts.get(LIMIT);
    if (limit != -1) po.setLimit(limit);

    final byte[] nl = token(sopts.get(NEWLINE).newline());
    if (nl.length != 1 || nl[0] != '\n') po = new NewlineOutput(po, nl);
    out = po;
  }
示例#2
0
  /**
   * 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;
  }
示例#3
0
 /** 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());
 }
示例#4
0
 /**
  * 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();
 }
示例#5
0
 /**
  * Returns the serialization parameters.
  *
  * @param ctx context
  * @return serialization parameters
  */
 public String parameters(final Context ctx) {
   try {
     qp(args[0], ctx);
     parse(null);
     return qp.qc.serParams().toString();
   } catch (final QueryException ex) {
     error(Util.message(ex));
   } finally {
     qp = null;
   }
   return SerializerOptions.get(true).toString();
 }
示例#6
0
  /** 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"));
    }
  }
示例#7
0
  /** 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("."));
    }
  }
示例#8
0
  /**
   * Default constructor.
   *
   * @param main reference to the main window
   */
  public DialogExport(final GUI main) {
    super(main, EXPORT);

    // create checkboxes
    final BaseXBack p = new BaseXBack(new TableLayout(4, 1, 0, 0));
    p.add(new BaseXLabel(OUTPUT_DIR + COL, true, true).border(0, 0, 6, 0));

    // output label
    BaseXBack pp = new BaseXBack(new TableLayout(1, 2, 8, 0));

    path = new BaseXTextField(main.gopts.get(GUIOptions.INPUTPATH), this);
    pp.add(path.history(GUIOptions.INPUTS, this));

    final BaseXButton browse = new BaseXButton(BROWSE_D, this);
    browse.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(final ActionEvent e) {
            choose();
          }
        });
    pp.add(browse);
    p.add(pp);

    // provide components for method and encoding
    final MainOptions opts = gui.context.options;
    final SerializerOptions sopts = opts.get(MainOptions.EXPORTER);

    // method (ignore last entry)
    final StringList sl = new StringList();
    for (final SerialMethod sm : SerialMethod.values()) sl.add(sm.name());
    sl.remove(sl.size() - 1);
    method = new BaseXCombo(this, sl.finish());
    final SerialMethod sm = sopts.get(SerializerOptions.METHOD);
    method.setSelectedItem((sm == null ? SerialMethod.BASEX : sm).name());

    mparams = new BaseXTextField(this);
    mparams.setColumns(24);

    final BaseXBack mth = new BaseXBack(new TableLayout(1, 2, 8, 0));
    mth.add(method);
    mth.add(mparams);

    encoding = new BaseXCombo(this, ENCODINGS);
    String enc = sopts.get(SerializerOptions.ENCODING);
    boolean f = false;
    for (final String s : ENCODINGS) f |= s.equals(enc);
    if (!f) {
      enc = enc.toUpperCase(Locale.ENGLISH);
      for (final String s : ENCODINGS) f |= s.equals(enc);
    }
    encoding.setSelectedItem(f ? enc : sopts.get(SerializerOptions.ENCODING));

    params = new BaseXTextField(sopts.toString(), this);
    params.setToolTipText(tooltip(SerializerMode.DEFAULT.get()));

    pp = new BaseXBack(new TableLayout(3, 2, 16, 6)).border(8, 0, 8, 0);
    pp.add(new BaseXLabel(METHOD + COL, true, true));
    pp.add(mth);
    pp.add(new BaseXLabel(ENCODING + COL, true, true));
    pp.add(encoding);
    pp.add(new BaseXLabel(PARAMETERS + COL, true, true));
    pp.add(params);
    p.add(pp);
    info = new BaseXLabel(" ").border(8, 0, 0, 0);
    p.add(info);

    // indentation
    set(p, BorderLayout.CENTER);

    // buttons
    pp = new BaseXBack(new BorderLayout());
    buttons = okCancel();
    pp.add(buttons, BorderLayout.EAST);
    set(pp, BorderLayout.SOUTH);

    action(method);
    finish(null);
  }
示例#9
0
 /** Creates the database context. */
 @BeforeClass
 public static void start() {
   // turn off pretty printing
   set(MainOptions.SERIALIZER, SerializerOptions.get(false));
 }
示例#10
0
 /**
  * Sets the item separator.
  *
  * @param def default separator
  */
 protected final void itemsep(final String def) {
   final String is = sopts.get(ITEM_SEPARATOR);
   itemsep = sopts.contains(ITEM_SEPARATOR) ? token(is) : def == null ? null : token(def);
 }