class Nokogiri::XML::Schema

def self.from_document document

def self.from_document document
  ctx = LibXML.xmlSchemaNewDocParserCtxt(document.document.cstruct)
  errors = []
  LibXML.xmlSetStructuredErrorFunc(nil, SyntaxError.error_array_pusher(errors))
  unless Nokogiri.is_2_6_16?
    LibXML.xmlSchemaSetParserStructuredErrors(
      ctx,
      SyntaxError.error_array_pusher(errors),
      nil
    )
  end
  schema_ptr = LibXML.xmlSchemaParse(ctx)
  LibXML.xmlSetStructuredErrorFunc(nil, nil)
  LibXML.xmlSchemaFreeParserCtxt(ctx)
  if schema_ptr.null?
    error = LibXML.xmlGetLastError
    if error
      raise SyntaxError.wrap(error)
    else
      raise RuntimeError, "Could not parse document"
    end
  end
  schema = allocate
  schema.cstruct = LibXML::XmlSchema.new schema_ptr
  schema.errors = errors
  schema
end