Commit dd71d631 authored by Kjell Ahlstedt's avatar Kjell Ahlstedt

Add XsdSchema and XsdValidator. Deprecate Schema and SchemaValidator

* configure.ac: Add MM_ARG_DISABLE_DEPRECATED_API([LIBXMLXX]).
* Makefile.am: Add new files.
* libxml++config.h.in: Add LIBXMLXX_DISABLE_DEPRECATED.
* libxml++/libxml++.h: Add new header files.
* libxml++/schema.[cc|h]:
* libxml++/validators/schemavalidator.[cc|h]: Deprecate the whole files.
* examples/schemavalidation/main.cc: Add test of XsdValidator.
* libxml++/schemabase.[cc|h]:
* libxml++/xsdschema.[cc|h]:
* libxml++/validators/schemavalidatorbase.[cc|h]:
* libxml++/validators/xsdvalidator.[cc|h]: New files. Bug #737712.
parent 65be758e
......@@ -27,7 +27,9 @@ h_root_sources_public = libxml++/libxml++.h \
libxml++/dtd.h \
libxml++/keepblanks.h \
libxml++/noncopyable.h \
libxml++/schema.h
libxml++/schema.h \
libxml++/schemabase.h \
libxml++/xsdschema.h
h_exceptions_sources_public = libxml++/exceptions/exception.h \
libxml++/exceptions/parse_error.h \
libxml++/exceptions/validity_error.h \
......@@ -53,7 +55,9 @@ h_parsers_sources_public = libxml++/parsers/parser.h \
libxml++/parsers/textreader.h
h_validators_sources_public = libxml++/validators/dtdvalidator.h \
libxml++/validators/schemavalidator.h \
libxml++/validators/validator.h
libxml++/validators/schemavalidatorbase.h \
libxml++/validators/validator.h \
libxml++/validators/xsdvalidator.h
h_sources_public = $(h_root_sources_public) \
$(h_exceptions_sources_public) \
$(h_io_sources_public) \
......@@ -69,6 +73,8 @@ cc_sources = libxml++/attribute.cc \
libxml++/keepblanks.cc \
libxml++/noncopyable.cc \
libxml++/schema.cc \
libxml++/schemabase.cc \
libxml++/xsdschema.cc \
libxml++/exceptions/exception.cc \
libxml++/exceptions/parse_error.cc \
libxml++/exceptions/validity_error.cc \
......@@ -94,7 +100,9 @@ cc_sources = libxml++/attribute.cc \
libxml++/parsers/textreader.cc \
libxml++/validators/dtdvalidator.cc \
libxml++/validators/schemavalidator.cc \
libxml++/validators/validator.cc
libxml++/validators/schemavalidatorbase.cc \
libxml++/validators/validator.cc \
libxml++/validators/xsdvalidator.cc
library_includedir = $(includedir)/libxml++-$(LIBXMLXX_API_VERSION)/libxml++
library_exceptions_includedir = $(library_includedir)/exceptions
......
......@@ -39,10 +39,14 @@ MM_ARG_WITH_TAGFILE_DOC([libstdc++.tag], [mm-common-libstdc++])
MM_ARG_WITH_TAGFILE_DOC([libsigc++-2.0.tag], [sigc++-2.0])
MM_ARG_WITH_TAGFILE_DOC([glibmm-2.4.tag], [glibmm-2.4])
# Evaluate the --enable-warnings=level option.
MM_ARG_ENABLE_WARNINGS([LIBXMLXX_WXXFLAGS],
[-Wall],
[-pedantic -Wall -Wextra -Wno-long-long])
# Offer the ability to omit some API from the library.
MM_ARG_DISABLE_DEPRECATED_API([LIBXMLXX])
AC_PATH_PROG([XMLLINT], [xmllint], [xmllint])
AC_ARG_VAR([XMLLINT], [path to xmllint utility])
AC_PATH_PROGS([DB2LATEX], [db2latex docbook2latex], [db2latex])
......@@ -60,6 +64,7 @@ AC_CONFIG_FILES([Makefile
MSVC_Net2010/libxml++/libxml++.rc
libxml++-2.6.pc])
# Copy the generated configuration headers into the MSVC project directories.
AC_CONFIG_COMMANDS([MSVC_Net2005/libxml++/libxml++config.h],
[cp -f libxml++config.h MSVC_Net2005/libxml++/libxml++config.h])
AC_CONFIG_COMMANDS([MSVC_Net2008/libxml++/libxml++config.h],
......
// -*- C++ -*-
/* main.cc
*
* Copyright (C) 2002 The libxml++ development team
......@@ -19,14 +17,13 @@
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <libxml++/libxml++.h>
#include <iostream>
#include <stdlib.h>
#include <cstdlib>
int main(int argc, char* argv[])
{
......@@ -34,34 +31,46 @@ int main(int argc, char* argv[])
// so we can use std::cout with UTF-8, via Glib::ustring, without exceptions.
std::locale::global(std::locale(""));
std::string schemafilepath("example.xsd"),
docfilepath("example.xml");
std::string docfilepath("example.xml");
std::string xsdschemafilepath("example.xsd");
if(argc!=1 && argc!=3)
if (argc != 1 && argc != 3)
{
std::cout << "usage : " << argv[0] << " [document schema]" << std::endl;
std::cout << "usage : " << argv[0] << " [document schema.xsd]" << std::endl;
return EXIT_FAILURE;
}
if(argc == 3)
if (argc == 3)
{
docfilepath = argv[1];
schemafilepath = argv[2];
xsdschemafilepath = argv[2];
}
Glib::ustring phase;
try
{
xmlpp::SchemaValidator validator(schemafilepath);
Glib::ustring phase;
#ifndef LIBXMLXX_DISABLE_DEPRECATED
phase = "XML"; // XSD schema, old validator class
xmlpp::SchemaValidator schemavalidator(xsdschemafilepath);
#endif // LIBXMLXX_DISABLE_DEPRECATED
phase = "XSD";
xmlpp::XsdValidator xsdvalidator(xsdschemafilepath);
try
{
phase = "parsing";
xmlpp::DomParser parser(docfilepath);
phase = "validating";
validator.validate( parser.get_document() );
std::cout << "Valid document" << std::endl;
#ifndef LIBXMLXX_DISABLE_DEPRECATED
phase = "XML validating";
schemavalidator.validate(parser.get_document());
std::cout << "Valid document, SchemaValidator" << std::endl;
#endif // LIBXMLXX_DISABLE_DEPRECATED
phase = "XSD validating";
xsdvalidator.validate(parser.get_document());
std::cout << "Valid document, XsdValidator" << std::endl;
}
catch (const xmlpp::exception& ex)
{
......@@ -72,10 +81,9 @@ int main(int argc, char* argv[])
}
catch (const xmlpp::exception& ex)
{
std::cerr << "Error parsing the schema" << std::endl;
std::cerr << "Error parsing the " << phase << " schema" << std::endl;
std::cerr << ex.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
......@@ -27,7 +27,7 @@
*
* Include the libxml++ header:
* @code
* #include <libxml++.h>
* #include <libxml++/libxml++.h>
* @endcode
* (You may include individual headers, such as libxml++/document.h instead.)
*
......@@ -66,8 +66,10 @@
#include <libxml++/attributedeclaration.h>
#include <libxml++/attributenode.h>
#include <libxml++/document.h>
#include <libxml++/xsdschema.h>
#include <libxml++/validators/validator.h>
#include <libxml++/validators/dtdvalidator.h>
#include <libxml++/validators/schemavalidator.h>
#include <libxml++/validators/xsdvalidator.h>
#endif //__LIBXMLCPP_H
......@@ -4,6 +4,10 @@
* included with libxml++ as the file COPYING.
*/
#include <libxml++config.h> // LIBXMLXX_DISABLE_DEPRECATED
#ifndef LIBXMLXX_DISABLE_DEPRECATED
#include <libxml++/schema.h>
#include <libxml/tree.h>
......@@ -124,3 +128,4 @@ const _xmlSchema* Schema::cobj() const
}
} //namespace xmlpp
#endif // LIBXMLXX_DISABLE_DEPRECATED
......@@ -12,11 +12,13 @@
#include <list>
#include <map>
#ifndef LIBXMLXX_DISABLE_DEPRECATED
#ifndef DOXYGEN_SHOULD_SKIP_THIS
extern "C" {
struct _xmlSchema;
}
#endif //DOXYGEN_SHOULD_SKIP_THIS4
#endif //DOXYGEN_SHOULD_SKIP_THIS
namespace xmlpp
{
......@@ -24,11 +26,14 @@ namespace xmlpp
/** Represents an XML Schema.
*
* @newin{2,24}
*
* @deprecated Use XsdSchema instead.
*/
class Schema : NonCopyable
{
public:
/** Create a schema from the underlying libxml schema element.
* @deprecated Use XsdSchema instead.
*/
explicit Schema(_xmlSchema* schema);
......@@ -38,6 +43,7 @@ public:
* the schema is deleted or another document is set.
* @throws xmlpp::parse_error
* @throws xmlpp::internal_error If an empty schema document can't be created.
* @deprecated Use XsdSchema instead.
*/
explicit Schema(Document* document = 0, bool embed = false);
~Schema();
......@@ -49,27 +55,40 @@ public:
* @param embed If true, the document will be deleted when the schema is deleted or another document is set.
* @throws xmlpp::parse_error
* @throws xmlpp::internal_error If an empty schema document can't be created.
* @deprecated Use XsdSchema::parse_document() instead.
*/
virtual void set_document(Document* document = 0, bool embed = false);
/** @deprecated There is no replacement.
*/
Glib::ustring get_name() const;
/** @deprecated There is no replacement.
*/
Glib::ustring get_target_namespace() const;
/** @deprecated There is no replacement.
*/
Glib::ustring get_version() const;
/** Get the schema document.
* @returns A pointer to the schema document, or <tt>0</tt> if none exists.
* @deprecated There is no replacement.
*/
Document* get_document();
/** Get the schema document.
* @returns A pointer to the schema document, or <tt>0</tt> if none exists.
* @deprecated There is no replacement.
*/
const Document* get_document() const;
/** Access the underlying libxml implementation. */
/** Access the underlying libxml implementation.
* @deprecated Use XsdSchema::cobj() instead.
*/
_xmlSchema* cobj();
/** Access the underlying libxml implementation. */
/** Access the underlying libxml implementation.
* @deprecated Use XsdSchema::cobj() instead.
*/
const _xmlSchema* cobj() const;
protected:
......@@ -84,4 +103,5 @@ private:
} // namespace xmlpp
#endif // LIBXMLXX_DISABLE_DEPRECATED
#endif //__LIBXMLPP_SCHEMA_H
/* Copyright (C) 2014 The libxml++ development team
*
* This file is part of libxml++.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include <libxml++/schemabase.h>
namespace xmlpp
{
SchemaBase::SchemaBase()
{
}
SchemaBase::~SchemaBase()
{
}
} //namespace xmlpp
/* Copyright (C) 2014 The libxml++ development team
*
* This file is part of libxml++.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __LIBXMLPP_SCHEMABASE_H
#define __LIBXMLPP_SCHEMABASE_H
#include <libxml++/noncopyable.h>
namespace Glib
{
class ustring;
}
namespace xmlpp
{
class Document;
/** Base class for schemas, used for validation of XML files.
*
* @newin{2,38}
*/
class SchemaBase : NonCopyable
{
public:
SchemaBase();
virtual ~SchemaBase();
/** Parse a schema definition file.
* If another schema has been parsed before, that schema is replaced by the new one.
* @param filename The URL of the schema.
* @throws xmlpp::parse_error
*/
virtual void parse_file(const Glib::ustring& filename) = 0;
/** Parse a schema definition from a string.
* If another schema has been parsed before, that schema is replaced by the new one.
* @param contents The schema definition as a string.
* @throws xmlpp::parse_error
*/
virtual void parse_memory(const Glib::ustring& contents) = 0;
/** Parse a schema definition from a document.
* If another schema has been parsed before, that schema is replaced by the new one.
* @param document A preparsed document tree, containing the schema definition.
* @throws xmlpp::parse_error
*/
virtual void parse_document(const Document* document) = 0;
};
} // namespace xmlpp
#endif //__LIBXMLPP_SCHEMABASE_H
......@@ -5,6 +5,10 @@
* included with libxml++ as the file COPYING.
*/
#include <libxml++config.h> // LIBXMLXX_DISABLE_DEPRECATED
#ifndef LIBXMLXX_DISABLE_DEPRECATED
#include "libxml++/validators/schemavalidator.h"
#include "libxml++/schema.h"
......@@ -225,4 +229,4 @@ bool SchemaValidator::validate(const Glib::ustring& file)
}
} // namespace xmlpp
#endif // LIBXMLXX_DISABLE_DEPRECATED
......@@ -12,18 +12,22 @@
#include <libxml++/schema.h>
#include <libxml++/document.h>
#ifndef LIBXMLXX_DISABLE_DEPRECATED
#ifndef DOXYGEN_SHOULD_SKIP_THIS
extern "C" {
struct _xmlSchemaParserCtxt;
struct _xmlSchemaValidCtxt;
}
#endif //DOXYGEN_SHOULD_SKIP_THIS4
#endif //DOXYGEN_SHOULD_SKIP_THIS
namespace xmlpp {
/** XML Schema Validator.
*
* @newin{2,24}
*
* @deprecated Use XsdValidator instead.
*/
class SchemaValidator : public Validator
{
......@@ -33,6 +37,7 @@ public:
/** Create a validator and parse a schema definition file immediately.
* @param file The URL of the schema.
* @throws xmlpp::parse_error
* @deprecated Use XsdValidator instead.
*/
explicit SchemaValidator(const Glib::ustring& file);
......@@ -40,6 +45,7 @@ public:
* @param document A preparsed document tree, containing the schema definition.
* @note The document may be modified during the parsing process.
* @throws xmlpp::parse_error
* @deprecated Use XsdValidator instead.
*/
explicit SchemaValidator(Document& document);
......@@ -49,6 +55,7 @@ public:
* guarantee that the schema exists as long as the validator keeps a
* pointer to it. The caller is responsible for deleting the schema
* when it's no longer needed.
* @deprecated Use XsdValidator instead.
*/
explicit SchemaValidator(Schema* schema);
......@@ -59,6 +66,7 @@ public:
* (deleted if the validator owns the schema).
* @param filename The URL of the schema.
* @throws xmlpp::parse_error
* @deprecated Use XsdValidator::parse_file() instead.
*/
virtual void parse_file(const Glib::ustring& filename);
......@@ -67,6 +75,7 @@ public:
* (deleted if the validator owns the schema).
* @param contents The schema definition as a string.
* @throws xmlpp::parse_error
* @deprecated Use XsdValidator::parse_memory() instead.
*/
virtual void parse_memory(const Glib::ustring& contents);
......@@ -76,6 +85,7 @@ public:
* @param document A preparsed document tree, containing the schema definition.
* @note The document may be modified during the parsing process.
* @throws xmlpp::parse_error
* @deprecated Use XsdValidator::parse_document() instead.
*/
virtual void parse_document(Document& document);
......@@ -87,20 +97,24 @@ public:
* guarantee that the schema exists as long as the validator keeps a
* pointer to it. The caller is responsible for deleting the schema
* when it's no longer needed.
* @deprecated Use XsdValidator::set_schema() instead.
*/
virtual void set_schema(Schema* schema);
/** Test whether a schema has been parsed.
* @deprecated Use XsdValidator::operator BoolExpr() instead.
*/
operator bool() const;
/** Get the parsed schema.
* @returns A pointer to the parsed schema, or <tt>0</tt>.
* @deprecated Use XsdValidator::get_schema() instead.
*/
Schema* get_schema();
/** Get the parsed schema.
* @returns A pointer to the parsed schema, or <tt>0</tt>.
* @deprecated Use XsdValidator::get_schema() instead.
*/
const Schema* get_schema() const;
......@@ -109,6 +123,7 @@ public:
* @returns Whether the document is valid.
* @throws xmlpp::internal_error
* @throws xmlpp::validity_error
* @deprecated Use XsdValidator::validate(const Document*) instead.
*/
bool validate(const Document* doc);
......@@ -117,6 +132,7 @@ public:
* @returns Whether the document is valid.
* @throws xmlpp::internal_error
* @throws xmlpp::validity_error
* @deprecated Use XsdValidator::validate(const Glib::ustring&) instead.
*/
bool validate(const Glib::ustring& file);
......@@ -132,5 +148,5 @@ protected:
} // namespace xmlpp
#endif // LIBXMLXX_DISABLE_DEPRECATED
#endif //__LIBXMLPP_VALIDATOR_SCHEMAVALIDATOR_H
/* Copyright (C) 2014 The libxml++ development team
*
* This file is part of libxml++.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "libxml++/validators/schemavalidatorbase.h"
#include "libxml++/schemabase.h"
namespace xmlpp
{
SchemaValidatorBase::SchemaValidatorBase()
{
}
SchemaValidatorBase::~SchemaValidatorBase()
{
release_underlying();
}
void SchemaValidatorBase::release_underlying()
{
Validator::release_underlying();
}
void SchemaValidatorBase::initialize_valid()
{
Validator::initialize_valid();
}
} // namespace xmlpp
/* Copyright (C) 2014 The libxml++ development team
*
* This file is part of libxml++.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __LIBXMLPP_VALIDATOR_SCHEMAVALIDATORBASE_H
#define __LIBXMLPP_VALIDATOR_SCHEMAVALIDATORBASE_H
#include <libxml++/validators/validator.h>
#include <memory> // std::auto_ptr
namespace Glib
{
class ustring;
}
namespace xmlpp
{
class Document;
/** Base class for schema validators.
*
* @newin{2,38}
*/
class SchemaValidatorBase : public Validator
{
public:
SchemaValidatorBase();
virtual ~SchemaValidatorBase();
/** Parse a schema definition file.
* If the validator already contains a schema, that schema is released
* (deleted if the validator owns the schema).
* @param filename The URL of the schema.
* @throws xmlpp::parse_error
*/
virtual void parse_file(const Glib::ustring& filename) = 0;
/** Parse a schema definition from a string.
* If the validator already contains a schema, that schema is released
* (deleted if the validator owns the schema).
* @param contents The schema definition as a string.
* @throws xmlpp::parse_error
*/
virtual void parse_memory(const Glib::ustring& contents) = 0;
/** Parse a schema definition from a document.
* If the validator already contains a schema, that schema is released
* (deleted if the validator owns the schema).
* @param document A preparsed document tree, containing the schema definition.
* @throws xmlpp::parse_error
*/
virtual void parse_document(const Document* document) = 0;
/** This typedef is just to make it more obvious that
* our operator const void* should be used like operator bool().
*/
typedef const void* BoolExpr;
/** Test whether a schema has been parsed.
* For instance
* @code
* if (validator)
* do_something();
* @endcode
*/
virtual operator BoolExpr() const = 0;
/** Validate a document, using a previously parsed schema.
* @param document Pointer to the document.
* @throws xmlpp::internal_error
* @throws xmlpp::validity_error
*/
virtual void validate(const Document* document) = 0;
/** Validate an XML file, using a previously parsed schema.
* @param filename The URL of the XML file.
* @throws xmlpp::internal_error
* @throws xmlpp::parse_error
* @throws xmlpp::validity_error
*/
virtual void validate(const Glib::ustring& filename) = 0;
protected:
virtual void initialize_valid();
virtual void release_underlying();
};
} // namespace xmlpp
#endif //__LIBXMLPP_VALIDATOR_SCHEMAVALIDATORBASE_H
/* Copyright (C) 2014 The libxml++ development team
*
* This file is part of libxml++.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "libxml++/validators/xsdvalidator.h"
#include "libxml++/xsdschema.h"
#include <libxml/xmlschemas.h>
namespace xmlpp
{
struct XsdValidator::Impl
{
Impl() : schema(0), is_schema_owner(false), context(0) {}
XsdSchema* schema;
bool is_schema_owner;
_xmlSchemaValidCtxt* context;
};
XsdValidator::XsdValidator()
: pimpl_(new Impl)
{
}
XsdValidator::XsdValidator(const Glib::ustring& filename)
: pimpl_(new Impl)
{
parse_file(filename);
}
XsdValidator::XsdValidator(const Document* document)
: pimpl_(new Impl)
{
parse_document(document);
}
XsdValidator::XsdValidator(XsdSchema* schema, bool take_ownership)
: pimpl_(new Impl)
{
set_schema(schema, take_ownership);
}
XsdValidator::~XsdValidator()
{
release_underlying();
}
void XsdValidator::parse_file(const Glib::ustring& filename)
{
set_schema(new XsdSchema(filename), true);
}
void XsdValidator::parse_memory(const Glib::ustring& contents)
{
std::auto_ptr<XsdSchema> schema(new XsdSchema());
schema->parse_memory(contents);
set_schema(schema.release(), true);
}
void XsdValidator::parse_document(const Document* document)
{
set_schema(new XsdSchema(document), true);
}
void XsdValidator::set_schema(XsdSchema* schema, bool take_ownership)
{
release_underlying();
pimpl_->schema = schema;
pimpl_->is_schema_owner = take_ownership;
}
void XsdValidator::release_underlying()
{
if (pimpl_->context)
{
xmlSchemaFreeValidCtxt(pimpl_->context);
pimpl_->context = 0;
}
if (pimpl_->schema)
{
if (pimpl_->is_schema_owner)
delete pimpl_->schema;
pimpl_->schema = 0;
}
SchemaValidatorBase::release_underlying();
}