All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.thaiopensource.validate.picl.KeyConstraint Maven / Gradle / Ivy

Go to download

Jing is a validator for RELAX NG and other schema languages. This project was taken from http://code.google.com/p/jing-trang and mavenized for inclusion in the Wicket Stuff HTML Validator. The code was taken from the 20091111 release.

There is a newer version: 1.11
Show newest version
package com.thaiopensource.validate.picl;

import org.xml.sax.Locator;

import java.util.Hashtable;
import java.util.Vector;
import java.util.Enumeration;

class KeyConstraint implements Constraint {
  private final Pattern key;

  KeyConstraint(Pattern key) {
    this.key = key;
  }

  static class KeyIndex {
    private final Hashtable table;
    KeyIndex() {
      table = new Hashtable();
    }

    KeyInfo lookupCreate(Object key) {
      KeyInfo info = (KeyInfo)table.get(key);
      if (info == null) {
        info = new KeyInfo();
        table.put(key, info);
      }
      return info;
    }

    Enumeration keys() {
      return table.keys();
    }
  }

  static class KeyInfo {
    String representation;
    Locator firstKeyLocator;
    Vector pendingRefLocators;
  }

  static class KeySelectionHandler extends SelectedValueHandler {
    private final KeyIndex index;

    KeySelectionHandler(KeyIndex index) {
      this.index = index;
    }

    void select(ErrorContext ec, Locator locator, Object value, String representation) {
      KeyInfo info = index.lookupCreate(value);
      if (info.firstKeyLocator == null) {
        if (locator == null)
          locator = ec.saveLocator();
        info.firstKeyLocator = locator;
        info.pendingRefLocators = null;
        info.representation = representation;
      }
      else
        ec.error(locator, "duplicate_key", representation);
    }
  }

  public void activate(PatternManager pm) {
    activate(pm, new KeyIndex());
  }

  void activate(PatternManager pm, KeyIndex index) {
    pm.registerPattern(key, new ValueSelectionHandler(new KeySelectionHandler(index)));
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy