home *** CD-ROM | disk | FTP | other *** search
- #
- # This file is part of OpenVIP (http://openvip.sourceforge.net)
- #
- # Copyright (C) 2002-2003
- # Michal Dvorak, Jiri Sedlar, Antonin Slavik, Vaclav Slavik, Jozef Smizansky
- #
- # This program is licensed under GNU General Public License version 2;
- # see file COPYING in the top level directory for details.
- #
- # $Id: xmlhelpers.py,v 1.3 2003/06/03 21:01:13 vaclavslavik Exp $
- #
- # Utilities to help with XML processing, based on libxml2. Mainly validation
- # and output formatting.
- #
-
- HAVE_LIBXML2 = True
- try:
- import libxml2
- libxml2.initializeCatalog()
- libxml2.loadCatalog('data/dtd/openvip-catalog.xml')
- #libxml2.setEntityLoader(libxml2.xmlNoNetExternalEntityLoader)
- except ImportError:
- print 'libxml2 missing, will not validate inputs'
- HAVE_LIBXML2 = False
-
-
- def validate(filename):
- """Validates XML file against DTD."""
- if not HAVE_LIBXML2: return True
- ctxt = libxml2.createFileParserCtxt(filename)
- ctxt.validate(1)
- ctxt.parseDocument()
- doc = ctxt.doc()
- valid = ctxt.isValid()
- doc.freeDoc()
- return valid
-
-
- def formatNicely(filename):
- """Reformats XML file to look good - in particular, add indentation.
- 'filename' must point to existing valid XML file that is readable
- (/dev/null won't do)."""
- if not HAVE_LIBXML2: return True
- ctxt = libxml2.createFileParserCtxt(filename)
- ctxt.parseDocument()
- doc = ctxt.doc()
- doc.saveFormatFile(filename, 1)
- doc.freeDoc()
- return True
-