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

com.thaiopensource.validate.rng.impl.SchemaReceiverImpl 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.rng.impl;

import com.thaiopensource.validate.IncorrectSchemaException;
import com.thaiopensource.validate.auto.SchemaReceiver;
import com.thaiopensource.validate.Schema;
import com.thaiopensource.validate.ValidateProperty;
import com.thaiopensource.validate.prop.rng.RngProperty;
import com.thaiopensource.validate.prop.wrap.WrapProperty;
import com.thaiopensource.validate.auto.SchemaFuture;
import com.thaiopensource.relaxng.parse.ParseReceiver;
import com.thaiopensource.relaxng.parse.BuildException;
import com.thaiopensource.relaxng.parse.IllegalSchemaException;
import com.thaiopensource.relaxng.pattern.SchemaPatternBuilder;
import com.thaiopensource.relaxng.pattern.PatternFuture;
import com.thaiopensource.relaxng.pattern.SchemaBuilderImpl;
import com.thaiopensource.util.PropertyMap;
import com.thaiopensource.datatype.DatatypeLibraryLoader;
import org.relaxng.datatype.DatatypeLibraryFactory;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import java.io.IOException;

public class SchemaReceiverImpl implements SchemaReceiver {
  private final ParseReceiver parser;
  private final PropertyMap properties;

  public SchemaReceiverImpl(ParseReceiver parser, PropertyMap properties) {
    this.parser = parser;
    this.properties = properties;
  }

  public SchemaFuture installHandlers(XMLReader xr) throws SAXException {
    final SchemaPatternBuilder pb = new SchemaPatternBuilder();
    ErrorHandler eh = properties.get(ValidateProperty.ERROR_HANDLER);
    DatatypeLibraryFactory dlf = properties.get(RngProperty.DATATYPE_LIBRARY_FACTORY);
    if (dlf == null)
      dlf = new DatatypeLibraryLoader();
    final PatternFuture pf = SchemaBuilderImpl.installHandlers(parser, xr, eh, dlf, pb);
    return new SchemaFuture() {
      public Schema getSchema() throws IncorrectSchemaException, SAXException, IOException {
        try {
          return SchemaReaderImpl.wrapPattern(pf.getPattern(properties.contains(WrapProperty.ATTRIBUTE_OWNER)),
                                              pb, properties);
        }
        catch (IllegalSchemaException e) {
          throw new IncorrectSchemaException();
        }
      }
      public RuntimeException unwrapException(RuntimeException e) throws SAXException, IOException, IncorrectSchemaException {
        if (e instanceof BuildException) {
          try {
            return SchemaBuilderImpl.unwrapBuildException((BuildException)e);
          }
          catch (IllegalSchemaException ise) {
            throw new IncorrectSchemaException();
          }
        }
        return e;
      }
    };
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy