<commit_before>package org.basex.build.xml;

import static org.basex.core.Text.*;
import java.io.FileInputStream;
import java.io.IOException;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.sax.SAXSource;
import org.basex.build.FileParser;
import org.basex.core.ProgressException;
import org.basex.core.Prop;
import org.basex.io.IO;
import org.basex.io.IOFile;
import org.basex.util.Util;
import org.xml.sax.InputSource;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import com.sun.org.apache.xml.internal.resolver.CatalogManager;
import com.sun.org.apache.xml.internal.resolver.tools.CatalogResolver;

/**
 * This class parses an XML document with Java's default SAX parser. Note that
 * large file cannot be parsed with the default parser due to entity handling
 * (e.g. the DBLP data).
 *
 * @author Workgroup DBIS, University of Konstanz 2005-10, ISC License
 * @author <NAME>
 */
public final class SAXWrapper extends FileParser {
  /** File counter. */
  long counter;
  /** Current line. */
  int line = 1;

  /** SAX handler reference. */
  private SAXHandler sax;
  /** Optional XML reader. */
  private final SAXSource source;
  /** File length. */
  private long length;
  /** Properties. */
  private final Prop prop;

  /**
   * Constructor.
   * @param s sax source
   * @param pr Properties
   */
  public SAXWrapper(final SAXSource s, final Prop pr) {
    this(s, IO.get(s.getSystemId()).name(), "", pr);
  }

  /**
   * Constructor.
   * @param s sax source
   * @param n name
   * @param ta target to insert into
   * @param pr Properties
   */
  public SAXWrapper(final SAXSource s, final String n, final String ta,
      final Prop pr) {
    super(io(s, n), ta);
    source = s;
    prop = pr;
  }

  /**
   * Returns IO reference.
   * @param s sax source
   * @param n name
   * @return io
   */
  private static IO io(final SAXSource s, final String n) {
    final IO io = IO.get(s.getSystemId());
    io.name(n);
    return io;
  }

  @Override
  public void parse() throws IOException {
    final InputSource is = wrap(source.getInputSource());
    try {
      XMLReader r = source.getXMLReader();
      if(r == null) {
        final SAXParserFactory f = SAXParserFactory.newInstance();
        f.setNamespaceAware(true);
        f.setValidating(false);
        r = f.newSAXParser().getXMLReader();
      }

      sax = new SAXHandler(builder);
      final String cat = prop.get(Prop.CATFILE);
      if(!cat.isEmpty()) {
        final CatalogManager cm = new CatalogManager();
        cm.setCatalogFiles(cat);
        cm.setIgnoreMissingProperties(true);
        cm.setPreferPublic(true);
        cm.setUseStaticCatalog(false);
        cm.setVerbosity(0);
        r.setEntityResolver(new CatalogResolver(cm));
      }

      r.setDTDHandler(sax);<commit_msg>Refactored SAXWrapper to dynamically check the classpath for the presence of Apache XML Commons Resolver. We do not rely on the presence of com.sun.org.apache.xml.internal.resolver anymore to allow building on Ubuntu 10.10, 64bit. See https://mailman.uni-konstanz.de/pipermail/basex-talk/2010-November/000784.html<commit_after>package org.basex.build.xml;

import static org.basex.core.Text.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Method;

import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.sax.SAXSource;
import org.basex.build.FileParser;
import org.basex.core.ProgressException;
import org.basex.core.Prop;
import org.basex.io.IO;
import org.basex.io.IOFile;
import org.basex.util.Util;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;

/**
 * This class parses an XML document with Java's default SAX parser. Note that
 * large file cannot be parsed with the default parser due to entity handling
 * (e.g. the DBLP data).
 *
 * @author Workgroup DBIS, University of Konstanz 2005-10, ISC License
 * @author <NAME>
 */
public final class SAXWrapper extends FileParser {
  
  /** CatalogManager instance, if it is referenced in the classpath. */
  static Object cm;
  static {
    try {
      cm = Class.forName(
       "org.apache.xml.resolver.CatalogManager").newInstance();
    } catch(final Exception ex) {
      cm = null;
    }
  }

  /** File counter. */
  long counter;
  /** Current line. */
  int line = 1;

  /** SAX handler reference. */
  private SAXHandler sax;
  /** Optional XML reader. */
  private final SAXSource source;
  /** File length. */
  private long length;
  /** Properties. */
  private final Prop prop;

  /**
   * Constructor.
   * @param s sax source
   * @param pr Properties
   */
  public SAXWrapper(final SAXSource s, final Prop pr) {
    this(s, IO.get(s.getSystemId()).name(), "", pr);
  }

  /**
   * Constructor.
   * @param s sax source
   * @param n name
   * @param ta target to insert into
   * @param pr Properties
   */
  public SAXWrapper(final SAXSource s, final String n, final String ta,
      final Prop pr) {
    super(io(s, n), ta);
    source = s;
    prop = pr;
  }

  /**
   * Returns IO reference.
   * @param s sax source
   * @param n name
   * @return io
   */
  private static IO io(final SAXSource s, final String n) {
    final IO io = IO.get(s.getSystemId());
    io.name(n);
    return io;
  }

  @Override
  public void parse() throws IOException {
    final InputSource is = wrap(source.getInputSource());
    try {
      XMLReader r = source.getXMLReader();
      if(r == null) {
        final SAXParserFactory f = SAXParserFactory.newInstance();
        f.setNamespaceAware(true);
        f.setValidating(false);
        r = f.newSAXParser().getXMLReader();
      }

      sax = new SAXHandler(builder);
      final String cat = prop.get(Prop.CATFILE);
      if(!cat.isEmpty() && null != cm) {
        Class<?> clazz = Class.forName(
            "org.apache.xml.resolver.CatalogManager");
        Method m = clazz.getMethod("setCatalogFiles", String.class);
        m.invoke(cm, cat);
        m = clazz.getMethod("setIgnoreMissingProperties", boolean.class);
        m.invoke(cm, true);
        m = clazz.getMethod("setPreferPublic", boolean.class);
        m.invoke(cm, true);
        m = clazz.getMethod("setUseStaticCatalog", boolean.class);
        m.invoke(cm, false);
        m = clazz.getMethod("setVerbosity", int.class);
        m.invoke(cm, 0);
        r.setEntityResolver((EntityResolver) Class.forName(
            "org.apache.xml.resolver.tools.CatalogResolver").getConstructor(
            new Class[] { Class.
                forName("org.apache.xml.resolver.CatalogManager")}).newInstance(
            cm));
      }

      r.setDTDHandler(sax);
{'fraction_non_alphanumeric': 0.12293249888243182, 'fraction_numerical': 0.0047682908657428105, 'mean_word_length': 2.9182720373613544, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 20}, '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': '4620477', 'n_tokens_mistral': 2173, 'n_tokens_neox': 2117, 'n_words': 568}