<commit_before>package org.basex.data;

import static org.basex.util.Token.*;
import static org.basex.data.DataText.*;
import java.io.IOException;

import org.basex.index.FTTokenizer;
import org.basex.io.PrintOutput;
import org.basex.util.Token;

/**
 * This is an interface for serializing XML results.
 *
 * @author Workgroup DBIS, University of Konstanz 2005-08, ISC License
 * @author <NAME>
 */
public final class XMLSerializer extends Serializer {
  /** Indentation. */
  public static final String INDENT = "  ";

  /** Output stream. */
  public final PrintOutput out;
  /** Pretty printing flag. */
  private final boolean pretty;
  /** XML output flag. */
  private final boolean xml;
  /** Indent flag. */
  private boolean indent;

  /**
   * Constructor.
   * @param o output stream
   * @throws IOException exception
   */
  public XMLSerializer(final PrintOutput o) throws IOException {
    this(o, false, false);
  }

  /**
   * Constructor.
   * @param o output stream
   * @param x xml output 
   * @param p pretty printing
   * @throws IOException exception
   */
  public XMLSerializer(final PrintOutput o, final boolean x, final boolean p)
      throws IOException {
    out = o;
    xml = x;
    pretty = p;
    if(xml) {
      // [CG] XML/Serialize: allow different encodings (incl. original one)
      out.println("<?xml version='1.0' encoding='" + UTF8 + "' ?>");
      openElement(RESULTS);
    }
  }
  
  @Override
  public void close() throws IOException {
    if(xml) closeElement();
  }

  @Override
  public void openResult() throws IOException {
    if(xml) openElement(RESULT);
  }

  @Override
  public void closeResult() throws IOException {
    if(xml) closeElement();
  }

  @Override
  public void attribute(final byte[] n, final byte[] v) throws IOException {
    out.print(' ');
    out.print(n);
    out.print(ATT1);
    for(final byte ch : v) {
      switch(ch) {
        case '&': out.print(E_AMP); break;
        case '>': out.print(E_GT);  break;
        case '<': out.print(E_LT);  break;
        case '"': out.print(E_QU);  break;
        case 0x9: out.print(E_TAB); break;
        case 0xA: out.print(E_NL); break;
        case 0xD: out.print(E_CR); break;
        default:  out.write(ch);
      }
    }
    out.print(ATT2);
  }

  @Override
  public void text(final byte[] b) throws IOException {
    finishElement();
    for(final byte ch : b) {
      switch(ch) {
        case '&': out.print(E_AMP); break;
        case '>': out.print(E_GT); break;
        case '<': out.print(E_LT); break;
        case 0xD: out.print(E_CR); break;
        default : out.write(ch);
      }
    }
    indent = false;
  }

  @Override
  public void text(final byte[] b, final int[] pos, final int[] poi) 
  throws IOException {
    finishElement();
    int c = -1, pp = 0, wl = 0;
    FTTokenizer ftt = new FTTokenizer(b);
    ftt.wc = true;
    while(ftt.more()) {
      c++;
      for (int i = wl; i < ftt.p; i++) {
        switch(b[i]) {
          case '&': out.print(E_AMP); break;
          case '>': out.print(E_GT); break;
          case '<': out.print(E_LT); break;
          case 0xD: out.print(E_CR); break;
          default :
            if (Token.letterOrDigit(b[i]) && pp < pos.length && c == pos[pp]) {
              // write fulltext pointer in front of the token
              // used for coloring the token
              out.print(new byte[]{'&', (byte) poi[pp], '&'});
              pp++;
            }
            out.write(b[i]);
        }
      }
      wl = ftt.p;
    }
    
    while (wl < b.length) {
      switch(b[wl]) {
        case '&': out.print(E_AMP); break;
        case '>': out.print(E_GT); break;
        case '<': out.print(E_LT); break;
        case 0xD: out.print(E_CR); break;
        default :
          out.write(b[wl]);
      }
    }
    indent = false;
  }
  
  @Override<commit_msg>minor<commit_after>package org.basex.data;

import static org.basex.util.Token.*;
import static org.basex.data.DataText.*;
import java.io.IOException;

import org.basex.index.FTTokenizer;
import org.basex.io.PrintOutput;
import org.basex.util.Token;

/**
 * This is an interface for serializing XML results.
 *
 * @author Workgroup DBIS, University of Konstanz 2005-08, ISC License
 * @author <NAME>
 */
public final class XMLSerializer extends Serializer {
  /** Indentation. */
  public static final String INDENT = "  ";

  /** Output stream. */
  public final PrintOutput out;
  /** Pretty printing flag. */
  private final boolean pretty;
  /** XML output flag. */
  private final boolean xml;
  /** Indent flag. */
  private boolean indent;

  /**
   * Constructor.
   * @param o output stream
   * @throws IOException exception
   */
  public XMLSerializer(final PrintOutput o) throws IOException {
    this(o, false, false);
  }

  /**
   * Constructor.
   * @param o output stream
   * @param x xml output 
   * @param p pretty printing
   * @throws IOException exception
   */
  public XMLSerializer(final PrintOutput o, final boolean x, final boolean p)
      throws IOException {
    out = o;
    xml = x;
    pretty = p;
    if(xml) {
      // [CG] XML/Serialize: allow different encodings (incl. original one)
      out.println("<?xml version='1.0' encoding='" + UTF8 + "' ?>");
      openElement(RESULTS);
    }
  }
  
  @Override
  public void close() throws IOException {
    if(xml) closeElement();
  }

  @Override
  public void openResult() throws IOException {
    if(xml) openElement(RESULT);
  }

  @Override
  public void closeResult() throws IOException {
    if(xml) closeElement();
  }

  @Override
  public void attribute(final byte[] n, final byte[] v) throws IOException {
    out.print(' ');
    out.print(n);
    out.print(ATT1);
    for(final byte ch : v) {
      switch(ch) {
        case '&': out.print(E_AMP); break;
        case '>': out.print(E_GT);  break;
        case '<': out.print(E_LT);  break;
        case '"': out.print(E_QU);  break;
        case 0x9: out.print(E_TAB); break;
        case 0xA: out.print(E_NL); break;
        case 0xD: out.print(E_CR); break;
        default:  out.write(ch);
      }
    }
    out.print(ATT2);
  }

  @Override
  public void text(final byte[] b) throws IOException {
    finishElement();
    for(final byte ch : b) {
      switch(ch) {
        case '&': out.print(E_AMP); break;
        case '>': out.print(E_GT); break;
        case '<': out.print(E_LT); break;
        case 0xD: out.print(E_CR); break;
        default : out.write(ch);
      }
    }
    indent = false;
  }

  @Override
  public void text(final byte[] b, final int[] pos, final int[] poi) 
  throws IOException {
    finishElement();
    int c = -1, pp = 0, wl = 0;
    FTTokenizer ftt = new FTTokenizer(b);
    ftt.wc = true;
    while(ftt.more()) {
      c++;
      for (int i = wl; i < ftt.p; i++) {
        switch(b[i]) {
          case '&': out.print(E_AMP); break;
          case '>': out.print(E_GT); break;
          case '<': out.print(E_LT); break;
          case 0xD: out.print(E_CR); break;
          default :
            if (Token.letterOrDigit(b[i]) && pp < pos.length && c == pos[pp]) {
              // write fulltext pointer in front of the token
              // used for coloring the token
              out.print(new byte[]{'&', (byte) poi[pp], '&'});
              pp++;
            }
            out.write(b[i]);
        }
      }
      wl = ftt.p;
    }
    
    while (wl < b.length) {
      switch(b[wl]) {
        case '&': out.print(E_AMP); break;
        case '>': out.print(E_GT); break;
        case '<': out.print(E_LT); break;
        case 0xD: out.print(E_CR); break;
        default :
          out.write(b[wl]);
      }
      wl++;
    }
    indent = false;
  }
  
  @Override
{'fraction_non_alphanumeric': 0.14513205592957018, 'fraction_numerical': 0.0054375970999482135, 'mean_word_length': 2.237636211232188, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 2, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 16}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1869607', 'n_tokens_mistral': 2583, 'n_tokens_neox': 2536, 'n_words': 678}