# Path: libtaxii/validation.py
# class SchemaValidator(object):
#     """
#     A helper class for TAXII Schema Validation.
# 
#     Example:
#         See validate_etree(...) for an example how to use this class
#     """
# 
#     # Create class-level variables equal to module-level variables for
#     # backwards-compatibility
#     TAXII_10_SCHEMA = TAXII_10_SCHEMA
#     TAXII_11_SCHEMA = TAXII_11_SCHEMA
# 
#     def __init__(self, schema_file):
#         """
#         Args:
#             schema_file (str) - The file location of the schema to
#                                 validate against. Use the TAXII_11_SCHEMA
#                                 and TAXII_10_SCHEMA constants to validate
#                                 against TAXII 1.1 / 1.0. This schema file
#                                 will be used when validate_file/string/etree
#                                 is used.
#         """
#         schema_doc = parse(schema_file, allow_file=True)
#         self.xml_schema = etree.XMLSchema(schema_doc)
# 
#     def validate_file(self, file_location):
#         """
#         A wrapper for validate_etree. Parses file_location,
#         turns it into an etree, then calls validate_etree( ... )
#         """
# 
#         with open(file_location, 'r') as f:
#             etree_xml = parse(f, allow_file=True)
# 
#         return self.validate_etree(etree_xml)
# 
#     def validate_string(self, xml_string):
#         """
#         A wrapper for validate_etree. Parses xml_string,
#         turns it into an etree, then calls validate_etree( ... )
#         """
#         etree_xml = parse(xml_string, allow_file=False)
#         return self.validate_etree(etree_xml)
# 
#     def validate_etree(self, etree_xml):
#         """Validate an LXML etree with the specified schema_file.
# 
#         Args:
#             etree_xml (etree): The XML to validate.
#             schema_file (str): The schema file to validate against
# 
#         Returns:
#             A SchemaValidationResult object
# 
#         Raises:
#             lxml.etree.XMLSyntaxError: When the XML to be validated is not well formed
# 
#         Example:
#             .. code-block:: python
# 
#                 from libtaxii import messages_11
#                 from libtaxii.validation import SchemaValidator, TAXII_11_SCHEMA
#                 from lxml.etree import XMLSyntaxError
# 
#                 sv = SchemaValidator(TAXII_11_SCHEMA)
# 
#                 try:
#                    result = sv.validate_etree(some_etree)
#                    # Note that validate_string() and validate_file() can also be used
#                 except XMLSyntaxError:
#                     # Handle this exception, which occurs when
#                     # some_xml_string is not valid XML (e.g., 'foo')
# 
#                 if not result.valid:
#                     for error in result.error_log:
#                         print error
#                     sys.exit(1)
# 
#                 # At this point, the XML is schema valid
#                 do_something(some_xml_string)
#         """
#         valid = self.xml_schema.validate(etree_xml)
#         return SchemaValidationResult(valid, self.xml_schema.error_log)


import datetime
import unittest
import warnings
import inspect
import libtaxii as t
import libtaxii.messages_10 as tm10
import six
import sys
from dateutil.tz import tzutc
from lxml import etree
from libtaxii.validation import SchemaValidator
from libtaxii.constants import *
    timestamp_label=datetime.datetime.now(tzutc()))  # Optional

string_content_block1 = tm10.ContentBlock(
    content_binding='string',  # Required
    content='Sample content',  # Required
    padding='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',  # Optional
    timestamp_label=datetime.datetime.now(tzutc()))  # Optional

delivery_parameters1 = tm10.DeliveryParameters(
    inbox_protocol=VID_TAXII_HTTP_10,  # Required if requesting push messaging
    inbox_address='http://example.com/inbox',  # Yes, if requesting push messaging
    delivery_message_binding=VID_TAXII_XML_10,  # Required if requesting push messaging
    content_bindings=[CB_STIX_XML_10])  # Optional - absence means accept all content bindings


def round_trip_message(taxii_message):
    if not isinstance(taxii_message, tm10.TAXIIMessage):
        raise ValueError('taxii_message was not an instance of TAXIIMessage')

    print('***** Message type = %s; id = %s' % (taxii_message.message_type, taxii_message.message_id))

    xml_string = taxii_message.to_xml()
    # This is the old, deprecated way of validating
    with warnings.catch_warnings():
        warnings.simplefilter('ignore', DeprecationWarning)
        valid = tm10.validate_xml(xml_string)
    if valid is not True:
        raise Exception('\tFailure of test #1 - XML not schema valid: %s' % valid)

    # The new way of validating

# Path: libtaxii/validation.py
# class SchemaValidator(object):
#     """
#     A helper class for TAXII Schema Validation.
# 
#     Example:
#         See validate_etree(...) for an example how to use this class
#     """
# 
#     # Create class-level variables equal to module-level variables for
#     # backwards-compatibility
#     TAXII_10_SCHEMA = TAXII_10_SCHEMA
#     TAXII_11_SCHEMA = TAXII_11_SCHEMA
# 
#     def __init__(self, schema_file):
#         """
#         Args:
#             schema_file (str) - The file location of the schema to
#                                 validate against. Use the TAXII_11_SCHEMA
#                                 and TAXII_10_SCHEMA constants to validate
#                                 against TAXII 1.1 / 1.0. This schema file
#                                 will be used when validate_file/string/etree
#                                 is used.
#         """
#         schema_doc = parse(schema_file, allow_file=True)
#         self.xml_schema = etree.XMLSchema(schema_doc)
# 
#     def validate_file(self, file_location):
#         """
#         A wrapper for validate_etree. Parses file_location,
#         turns it into an etree, then calls validate_etree( ... )
#         """
# 
#         with open(file_location, 'r') as f:
#             etree_xml = parse(f, allow_file=True)
# 
#         return self.validate_etree(etree_xml)
# 
#     def validate_string(self, xml_string):
#         """
#         A wrapper for validate_etree. Parses xml_string,
#         turns it into an etree, then calls validate_etree( ... )
#         """
#         etree_xml = parse(xml_string, allow_file=False)
#         return self.validate_etree(etree_xml)
# 
#     def validate_etree(self, etree_xml):
#         """Validate an LXML etree with the specified schema_file.
# 
#         Args:
#             etree_xml (etree): The XML to validate.
#             schema_file (str): The schema file to validate against
# 
#         Returns:
#             A SchemaValidationResult object
# 
#         Raises:
#             lxml.etree.XMLSyntaxError: When the XML to be validated is not well formed
# 
#         Example:
#             .. code-block:: python
# 
#                 from libtaxii import messages_11
#                 from libtaxii.validation import SchemaValidator, TAXII_11_SCHEMA
#                 from lxml.etree import XMLSyntaxError
# 
#                 sv = SchemaValidator(TAXII_11_SCHEMA)
# 
#                 try:
#                    result = sv.validate_etree(some_etree)
#                    # Note that validate_string() and validate_file() can also be used
#                 except XMLSyntaxError:
#                     # Handle this exception, which occurs when
#                     # some_xml_string is not valid XML (e.g., 'foo')
# 
#                 if not result.valid:
#                     for error in result.error_log:
#                         print error
#                     sys.exit(1)
# 
#                 # At this point, the XML is schema valid
#                 do_something(some_xml_string)
#         """
#         valid = self.xml_schema.validate(etree_xml)
#         return SchemaValidationResult(valid, self.xml_schema.error_log)


# Path: libtaxii/test/messages_10_test.py
import datetime
import unittest
import warnings
import inspect
import libtaxii as t
import libtaxii.messages_10 as tm10
import six
import sys
from dateutil.tz import tzutc
from lxml import etree
from libtaxii.validation import SchemaValidator
from libtaxii.constants import *

    timestamp_label=datetime.datetime.now(tzutc()))  # Optional

string_content_block1 = tm10.ContentBlock(
    content_binding='string',  # Required
    content='Sample content',  # Required
    padding='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',  # Optional
    timestamp_label=datetime.datetime.now(tzutc()))  # Optional

delivery_parameters1 = tm10.DeliveryParameters(
    inbox_protocol=VID_TAXII_HTTP_10,  # Required if requesting push messaging
    inbox_address='http://example.com/inbox',  # Yes, if requesting push messaging
    delivery_message_binding=VID_TAXII_XML_10,  # Required if requesting push messaging
    content_bindings=[CB_STIX_XML_10])  # Optional - absence means accept all content bindings


def round_trip_message(taxii_message):
    if not isinstance(taxii_message, tm10.TAXIIMessage):
        raise ValueError('taxii_message was not an instance of TAXIIMessage')

    print('***** Message type = %s; id = %s' % (taxii_message.message_type, taxii_message.message_id))

    xml_string = taxii_message.to_xml()
    # This is the old, deprecated way of validating
    with warnings.catch_warnings():
        warnings.simplefilter('ignore', DeprecationWarning)
        valid = tm10.validate_xml(xml_string)
    if valid is not True:
        raise Exception('\tFailure of test #1 - XML not schema valid: %s' % valid)

    # The new way of validating

    sv = SchemaValidator(SchemaValidator.TAXII_10_SCHEMA)