<commit_before>package com.thaiopensource.relaxng.mns;

import com.thaiopensource.relaxng.IncorrectSchemaException;
import com.thaiopensource.relaxng.Schema;
import com.thaiopensource.relaxng.ValidatorHandler;
import com.thaiopensource.relaxng.parse.sax.XmlBaseHandler;
import com.thaiopensource.util.Localizer;
import com.thaiopensource.util.Uri;
import com.thaiopensource.xml.util.StringSplitter;
import com.thaiopensource.xml.util.WellKnownNamespaces;
import org.xml.sax.Attributes;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.LocatorImpl;

import java.io.IOException;
import java.io.StringReader;
import java.util.Hashtable;
import java.util.Enumeration;

class SchemaImpl implements Schema {
  static final String BEARER_URI = "http://www.thaiopensoure.com/mns/instance";
  static final String BEARER_LOCAL_NAME = "globalAttributesBearer";
  private static final String MNS_URI = "http://www.thaiopensource.com/ns/mns";
  private static final String BEARER_PREFIX = "m";
  private final Hashtable modeMap = new Hashtable();
  private Mode startMode;
  private static final String DEFAULT_MODE_NAME = "#default";

  static private final class WrappedIOException extends RuntimeException {
    private final IOException exception;

    private WrappedIOException(IOException exception) {
      this.exception = exception;
    }

    private IOException getException() {
      return exception;
    }
  }

  static class ElementAction {
    private final Schema schema;
    private final Mode mode;
    private final boolean prune;
    private final Hashset covered = new Hashset();

    ElementAction(Schema schema, Mode mode, boolean prune) {
      this.schema = schema;
      this.mode = mode;
      this.prune = prune;
    }

    Mode getMode() {
      return mode;
    }

    Schema getSchema() {
      return schema;
    }

    boolean getPrune() {
      return prune;
    }

    Hashset getCoveredNamespaces() {
      return covered;
    }
  }

  static class Mode {
    private Locator whereDefined;
    private boolean defined = false;
    private boolean strict = false;
    private boolean strictDefined = false;
    private final Hashtable elementMap = new Hashtable();
    private final Hashtable attributesMap = new Hashtable();

    boolean isStrict() {
      return strict;
    }

    Schema getAttributesSchema(String ns) {
      return (Schema)attributesMap.get(ns);
    }

    ElementAction getElementAction(String ns) {
      return (ElementAction)elementMap.get(ns);
    }
  }

  private class Handler extends DelegatingContentHandler {
    private final MnsSchemaFactory factory;
    private final ValidatorHandler validator;
    private ElementAction currentElementAction;
    private boolean hadError = false;
    private final ErrorHandler eh;
    private final Localizer localizer = new Localizer(SchemaImpl.class);
    private Locator locator;
    private final XmlBaseHandler xmlBaseHandler = new XmlBaseHandler();
    private int foreignDepth = 0;

    Handler(MnsSchemaFactory factory, ValidatorHandler validator, ErrorHandler eh) {
      super(validator);
      this.factory = factory;
      this.validator = validator;
      this.eh = eh;
    }

    public void setDocumentLocator(Locator locator) {
      super.setDocumentLocator(locator);
      xmlBaseHandler.setLocator(locator);
      this.locator = locator;
    }

    void checkValid() throws IncorrectSchemaException, SAXException {
      if (!validator.isValidSoFar())
        throw new IncorrectSchemaException();
      for (Enumeration enum = modeMap.keys(); enum.hasMoreElements();) {
        String modeName = (String)enum.nextElement();
        Mode mode = (Mode)modeMap.get(modeName);
        if (!mode.defined && !modeName.equals(DEFAULT_MODE_NAME))
          error("undefined_mode", modeName, mode.whereDefined);
      }
      if (hadError)
        throw new IncorrectSchemaException();
    }

    public void startElement(String uri, String localName,
                             String qName, Attributes attributes)
            throws SAXException {
      super.startElement(uri, localName, qName, attributes);
      xmlBaseHandler.startElement();
      String xmlBase = attributes.getValue(WellKnownNamespaces.XML, "base");
      if (xmlBase != null)
        xmlBaseHandler.xmlBaseAttribute(xmlBase);
      if (!MNS_URI.equals(uri) || foreignDepth > 0) {
        foreignDepth++;
        return;
      }
      if (!validator.isValidSoFar())
        return;
      if (localName.equals("rules")) {
        String modeName = attributes.getValue("", "startMode");
        if (modeName == null)
          modeName = DEFAULT_MODE_NAME;
        startMode = lookupCreateMode(modeName);
        return;
      }
      if (localName.equals("cover")) {
        currentElementAction.covered.add(attributes.getValue("", "ns"));
        return;
      }
      String modesValue = attributes.getValue("", "modes");
      String[] modeNames;
      if (modesValue == null)
        modeNames = new String[] { DEFAULT_MODE_NAME };
      else
        modeNames = StringSplitter.split(modesValue);
      Mode[] modes = new Mode[modeNames.length];
      for (int i = 0; i < modes.length; i++) {
        modes[i] = lookupCreateMode(modeNames[i]);
        modes[i].defined = true;
      }
      if (localName.equals("strict") || localName.equals("lax")) {
        boolean strict = localName.equals("strict");
        for (int i = 0; i < modes.length; i++) {
          if (modes[i].strictDefined)
            error("strict_multiply_defined", modeNames[i]);
          else {
            modes[i].strict = strict;
            modes[i].strictDefined = true;
          }
        }
        return;
      }
      boolean isAttribute = localName.equals("validateAttributes");
      String ns = attributes.getValue("", "ns");
      if (!Uri.isAbsolute(ns))
        error("ns_absolute");
      String schemaUri = attributes.getValue("", "schema");
      if (Uri.hasFragmentId(schemaUri))
        error("schema_fragment_id");
      schemaUri = Uri.resolve(xmlBaseHandler.getBaseUri(),
                              Uri.escapeDisallowedChars(schemaUri));
      try {
        if (isAttribute) {
          Schema schema = factory.createChildSchema(wrapAttributesSchema(schemaUri));
          for (int i = 0; i < modes.length; i++) {
            if (modes[i].attributesMap.get(ns) != null)
              error("validate_attributes_multiply_defined", modeNames[i], ns);
            else
              modes[i].attributesMap.put(ns, schema);
          }
        }<commit_msg>Namespace URI for element can be empty<commit_after>package com.thaiopensource.relaxng.mns;

import com.thaiopensource.relaxng.IncorrectSchemaException;
import com.thaiopensource.relaxng.Schema;
import com.thaiopensource.relaxng.ValidatorHandler;
import com.thaiopensource.relaxng.parse.sax.XmlBaseHandler;
import com.thaiopensource.util.Localizer;
import com.thaiopensource.util.Uri;
import com.thaiopensource.xml.util.StringSplitter;
import com.thaiopensource.xml.util.WellKnownNamespaces;
import org.xml.sax.Attributes;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.LocatorImpl;

import java.io.IOException;
import java.io.StringReader;
import java.util.Hashtable;
import java.util.Enumeration;

class SchemaImpl implements Schema {
  static final String BEARER_URI = "http://www.thaiopensoure.com/mns/instance";
  static final String BEARER_LOCAL_NAME = "globalAttributesBearer";
  private static final String MNS_URI = "http://www.thaiopensource.com/ns/mns";
  private static final String BEARER_PREFIX = "m";
  private final Hashtable modeMap = new Hashtable();
  private Mode startMode;
  private static final String DEFAULT_MODE_NAME = "#default";

  static private final class WrappedIOException extends RuntimeException {
    private final IOException exception;

    private WrappedIOException(IOException exception) {
      this.exception = exception;
    }

    private IOException getException() {
      return exception;
    }
  }

  static class ElementAction {
    private final Schema schema;
    private final Mode mode;
    private final boolean prune;
    private final Hashset covered = new Hashset();

    ElementAction(Schema schema, Mode mode, boolean prune) {
      this.schema = schema;
      this.mode = mode;
      this.prune = prune;
    }

    Mode getMode() {
      return mode;
    }

    Schema getSchema() {
      return schema;
    }

    boolean getPrune() {
      return prune;
    }

    Hashset getCoveredNamespaces() {
      return covered;
    }
  }

  static class Mode {
    private Locator whereDefined;
    private boolean defined = false;
    private boolean strict = false;
    private boolean strictDefined = false;
    private final Hashtable elementMap = new Hashtable();
    private final Hashtable attributesMap = new Hashtable();

    boolean isStrict() {
      return strict;
    }

    Schema getAttributesSchema(String ns) {
      return (Schema)attributesMap.get(ns);
    }

    ElementAction getElementAction(String ns) {
      return (ElementAction)elementMap.get(ns);
    }
  }

  private class Handler extends DelegatingContentHandler {
    private final MnsSchemaFactory factory;
    private final ValidatorHandler validator;
    private ElementAction currentElementAction;
    private boolean hadError = false;
    private final ErrorHandler eh;
    private final Localizer localizer = new Localizer(SchemaImpl.class);
    private Locator locator;
    private final XmlBaseHandler xmlBaseHandler = new XmlBaseHandler();
    private int foreignDepth = 0;

    Handler(MnsSchemaFactory factory, ValidatorHandler validator, ErrorHandler eh) {
      super(validator);
      this.factory = factory;
      this.validator = validator;
      this.eh = eh;
    }

    public void setDocumentLocator(Locator locator) {
      super.setDocumentLocator(locator);
      xmlBaseHandler.setLocator(locator);
      this.locator = locator;
    }

    void checkValid() throws IncorrectSchemaException, SAXException {
      if (!validator.isValidSoFar())
        throw new IncorrectSchemaException();
      for (Enumeration enum = modeMap.keys(); enum.hasMoreElements();) {
        String modeName = (String)enum.nextElement();
        Mode mode = (Mode)modeMap.get(modeName);
        if (!mode.defined && !modeName.equals(DEFAULT_MODE_NAME))
          error("undefined_mode", modeName, mode.whereDefined);
      }
      if (hadError)
        throw new IncorrectSchemaException();
    }

    public void startElement(String uri, String localName,
                             String qName, Attributes attributes)
            throws SAXException {
      super.startElement(uri, localName, qName, attributes);
      xmlBaseHandler.startElement();
      String xmlBase = attributes.getValue(WellKnownNamespaces.XML, "base");
      if (xmlBase != null)
        xmlBaseHandler.xmlBaseAttribute(xmlBase);
      if (!MNS_URI.equals(uri) || foreignDepth > 0) {
        foreignDepth++;
        return;
      }
      if (!validator.isValidSoFar())
        return;
      if (localName.equals("rules")) {
        String modeName = attributes.getValue("", "startMode");
        if (modeName == null)
          modeName = DEFAULT_MODE_NAME;
        startMode = lookupCreateMode(modeName);
        return;
      }
      if (localName.equals("cover")) {
        currentElementAction.covered.add(attributes.getValue("", "ns"));
        return;
      }
      String modesValue = attributes.getValue("", "modes");
      String[] modeNames;
      if (modesValue == null)
        modeNames = new String[] { DEFAULT_MODE_NAME };
      else
        modeNames = StringSplitter.split(modesValue);
      Mode[] modes = new Mode[modeNames.length];
      for (int i = 0; i < modes.length; i++) {
        modes[i] = lookupCreateMode(modeNames[i]);
        modes[i].defined = true;
      }
      if (localName.equals("strict") || localName.equals("lax")) {
        boolean strict = localName.equals("strict");
        for (int i = 0; i < modes.length; i++) {
          if (modes[i].strictDefined)
            error("strict_multiply_defined", modeNames[i]);
          else {
            modes[i].strict = strict;
            modes[i].strictDefined = true;
          }
        }
        return;
      }
      boolean isAttribute = localName.equals("validateAttributes");
      String ns = attributes.getValue("", "ns");
      if (!Uri.isAbsolute(ns) && (isAttribute || !ns.equals("")))
        error("ns_absolute");
      String schemaUri = attributes.getValue("", "schema");
      if (Uri.hasFragmentId(schemaUri))
        error("schema_fragment_id");
      schemaUri = Uri.resolve(xmlBaseHandler.getBaseUri(),
                              Uri.escapeDisallowedChars(schemaUri));
      try {
        if (isAttribute) {
          Schema schema = factory.createChildSchema(wrapAttributesSchema(schemaUri));
          for (int i = 0; i < modes.length; i++) {
            if (modes[i].attributesMap.get(ns) != null)
              error("validate_attributes_multiply_defined", modeNames[i], ns);
            else
              modes[i].attributesMap.put(ns, schema);
          }
        }
{'fraction_non_alphanumeric': 0.10234351824384455, 'fraction_numerical': 0.0007416196974191634, 'mean_word_length': 2.9849290780141846, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 38}, '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': '3515458', 'n_tokens_mistral': 3843, 'n_tokens_neox': 3763, 'n_words': 892}