[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: [xml] Small schema request...



On Wed, Oct 09, 2002 at 04:31:04PM -0400, Jeff Goff wrote:
> I'm aware that this feature is experimental as is, but our testing has
> revealed that it's already rather stable. This is more of a feature

  Hum, stable ? maybe in the small core of implemented features.

> request than anything, so file it where appropriate. Would it be
> possible to add a way to parse a schema file in memory (I.E. from a
> buffer or stream) rather than a file?

  Okay enclosed patch provides xmlSchemaNewMemParserCtxt()
and an example of use in testSchemas.c

Daniel

-- 
Daniel Veillard      | Red Hat Network https://rhn.redhat.com/
veillard redhat com  | libxml GNOME XML XSLT toolkit  http://xmlsoft.org/
http://veillard.com/ | Rpmfind RPM search engine http://rpmfind.net/
Index: ChangeLog
===================================================================
RCS file: /cvs/gnome/gnome-xml/ChangeLog,v
retrieving revision 1.1114
diff -p -r1.1114 ChangeLog
*** ChangeLog	9 Oct 2002 14:24:17 -0000	1.1114
--- ChangeLog	9 Oct 2002 21:12:44 -0000
***************
*** 1,3 ****
--- 1,9 ----
+ Wed Oct  9 23:11:02 CEST 2002 Daniel Veillard <daniel veillard com>
+ 
+ 	* xmlschemas.c include/libxml/xmlschemas.h: added
+ 	  xmlSchemaNewMemParserCtxt to parse a schemas from a memory area
+ 	* testSchemas.c: added --memory to test the new interface
+ 
  Wed Oct  9 16:22:54 CEST 2002 Daniel Veillard <daniel veillard com>
  
  	* doc/index.py doc/search.php: integrated the XSLT indexing,
Index: testSchemas.c
===================================================================
RCS file: /cvs/gnome/gnome-xml/testSchemas.c,v
retrieving revision 1.4
diff -p -r1.4 testSchemas.c
*** testSchemas.c	24 Sep 2002 14:13:11 -0000	1.4
--- testSchemas.c	9 Oct 2002 21:12:44 -0000
***************
*** 32,37 ****
--- 32,44 ----
  #ifdef HAVE_STDLIB_H
  #include <stdlib.h>
  #endif
+ #ifdef HAVE_SYS_MMAN_H
+ #include <sys/mman.h>
+ /* seems needed for Solaris */
+ #ifndef MAP_FAILED
+ #define MAP_FAILED ((void *) -1)
+ #endif
+ #endif
  
  #include <libxml/xmlmemory.h>
  #include <libxml/debugXML.h>
***************
*** 42,47 ****
--- 49,57 ----
  static int debug = 0;
  #endif
  static int noout = 0;
+ #ifdef HAVE_SYS_MMAN_H
+ static int memory = 0;
+ #endif
  
  
  int main(int argc, char **argv) {
*************** int main(int argc, char **argv) {
*** 55,60 ****
--- 65,75 ----
  	    debug++;
  	else
  #endif
+ #ifdef HAVE_SYS_MMAN_H
+ 	if ((!strcmp(argv[i], "-memory")) || (!strcmp(argv[i], "--memory"))) {
+ 	    memory++;
+         } else
+ #endif
  	if ((!strcmp(argv[i], "-noout")) || (!strcmp(argv[i], "--noout"))) {
  	    noout++;
          }
*************** int main(int argc, char **argv) {
*** 65,77 ****
  	    if (schema == NULL) {
  		xmlSchemaParserCtxtPtr ctxt;
  
! 		ctxt = xmlSchemaNewParserCtxt(argv[i]);
! 		xmlSchemaSetParserErrors(ctxt,
! 			(xmlSchemaValidityErrorFunc) fprintf,
! 			(xmlSchemaValidityWarningFunc) fprintf,
! 			stderr);
! 		schema = xmlSchemaParse(ctxt);
! 		xmlSchemaFreeParserCtxt(ctxt);
  #ifdef LIBXML_DEBUG_ENABLED
  		if (debug)
  		    xmlSchemaDump(stdout, schema);
--- 80,119 ----
  	    if (schema == NULL) {
  		xmlSchemaParserCtxtPtr ctxt;
  
! #ifdef HAVE_SYS_MMAN_H
! 		if (memory) {
! 		    int fd;
! 		    struct stat info;
! 		    const char *base;
! 		    if (stat(argv[i], &info) < 0) 
! 			break;
! 		    if ((fd = open(argv[i], O_RDONLY)) < 0)
! 			break;
! 		    base = mmap(NULL, info.st_size, PROT_READ,
! 			        MAP_SHARED, fd, 0) ;
! 		    if (base == (void *) MAP_FAILED)
! 			break;
! 
! 		    ctxt = xmlSchemaNewMemParserCtxt((char *)base,info.st_size);
! 
! 		    xmlSchemaSetParserErrors(ctxt,
! 			    (xmlSchemaValidityErrorFunc) fprintf,
! 			    (xmlSchemaValidityWarningFunc) fprintf,
! 			    stderr);
! 		    schema = xmlSchemaParse(ctxt);
! 		    xmlSchemaFreeParserCtxt(ctxt);
! 		    munmap((char *) base, info.st_size);
! 		} else
! #endif
! 		{
! 		    ctxt = xmlSchemaNewParserCtxt(argv[i]);
! 		    xmlSchemaSetParserErrors(ctxt,
! 			    (xmlSchemaValidityErrorFunc) fprintf,
! 			    (xmlSchemaValidityWarningFunc) fprintf,
! 			    stderr);
! 		    schema = xmlSchemaParse(ctxt);
! 		    xmlSchemaFreeParserCtxt(ctxt);
! 		}
  #ifdef LIBXML_DEBUG_ENABLED
  		if (debug)
  		    xmlSchemaDump(stdout, schema);
*************** int main(int argc, char **argv) {
*** 118,123 ****
--- 160,168 ----
  	printf("\t--debug : dump a debug tree of the in-memory document\n");
  #endif
  	printf("\t--noout : do not print the result\n");
+ #ifdef HAVE_SYS_MMAN_H
+ 	printf("\t--memory : test the schemas in memory parsing\n");
+ #endif
      }
      xmlSchemaCleanupTypes();
      xmlCleanupParser();
Index: xmlschemas.c
===================================================================
RCS file: /cvs/gnome/gnome-xml/xmlschemas.c,v
retrieving revision 1.18
diff -p -r1.18 xmlschemas.c
*** xmlschemas.c	26 Sep 2002 09:47:36 -0000	1.18
--- xmlschemas.c	9 Oct 2002 21:12:45 -0000
*************** struct _xmlSchemaParserCtxt {
*** 68,73 ****
--- 68,76 ----
      xmlChar	      *URL;
      xmlDocPtr          doc;
  
+     const char     *buffer;
+     int               size;
+ 
      /*
       * Used to build complex element content models
       */
*************** xmlSchemaNewParserCtxt(const char *URL) 
*** 3015,3020 ****
--- 3018,3052 ----
  }
  
  /**
+  * xmlSchemaNewMemParserCtxt:
+  * @buffer:  a pointer to a char array containing the schemas
+  * @size:  the size of the array
+  *
+  * Create an XML Schemas parse context for that memory buffer expected
+  * to contain an XML Schemas file.
+  *
+  * Returns the parser context or NULL in case of error
+  */
+ xmlSchemaParserCtxtPtr
+ xmlSchemaNewMemParserCtxt(const char *buffer, int size) {
+     xmlSchemaParserCtxtPtr ret;
+ 
+     if ((buffer == NULL) || (size <= 0))
+ 	return(NULL);
+ 
+     ret = (xmlSchemaParserCtxtPtr) xmlMalloc(sizeof(xmlSchemaParserCtxt));
+     if (ret == NULL) {
+ 	xmlGenericError(xmlGenericErrorContext,
+ 		"Failed to allocate new schama parser context\n");
+         return (NULL);
+     }
+     memset(ret, 0, sizeof(xmlSchemaParserCtxt));
+     ret->buffer = buffer;
+     ret->size = size;
+     return (ret);
+ }
+ 
+ /**
   * xmlSchemaFreeParserCtxt:
   * @ctxt:  the schema parser context
   *
*************** xmlSchemaFreeParserCtxt(xmlSchemaParserC
*** 3026,3031 ****
--- 3058,3065 ----
  	return;
      if (ctxt->URL != NULL)
  	xmlFree(ctxt->URL);
+     if (ctxt->doc != NULL)
+ 	xmlFreeDoc(ctxt->doc);
      xmlFree(ctxt);
  }
  
*************** xmlSchemaParse(xmlSchemaParserCtxtPtr ct
*** 3795,3801 ****
  
      xmlSchemaInitTypes();
  
!     if ((ctxt == NULL) || (ctxt->URL == NULL))
          return (NULL);
  
      ctxt->counter = 0;
--- 3829,3835 ----
  
      xmlSchemaInitTypes();
  
!     if (ctxt == NULL)
          return (NULL);
  
      ctxt->counter = 0;
*************** xmlSchemaParse(xmlSchemaParserCtxtPtr ct
*** 3804,3815 ****
      /*
       * First step is to parse the input document into an DOM/Infoset
       */
!     doc = xmlParseFile((const char *) ctxt->URL);
!     if (doc == NULL) {
!         if (ctxt->error != NULL)
!             ctxt->error(ctxt->userData,
!                         "xmlSchemaParse: could not load %s\n", ctxt->URL);
!         return (NULL);
      }
  
      /*
--- 3838,3866 ----
      /*
       * First step is to parse the input document into an DOM/Infoset
       */
!     if (ctxt->URL != NULL) {
! 	doc = xmlParseFile((const char *) ctxt->URL);
! 	if (doc == NULL) {
! 	    if (ctxt->error != NULL)
! 		ctxt->error(ctxt->userData,
! 			    "xmlSchemaParse: could not load %s\n", ctxt->URL);
! 	    return (NULL);
! 	}
!     } else if (ctxt->buffer != NULL) {
! 	doc = xmlParseMemory(ctxt->buffer, ctxt->size);
! 	if (doc == NULL) {
! 	    if (ctxt->error != NULL)
! 		ctxt->error(ctxt->userData,
! 			    "xmlSchemaParse: could not parse schemas\n");
! 	    return (NULL);
! 	}
! 	doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
! 	ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
!     } else {
! 	if (ctxt->error != NULL)
! 	    ctxt->error(ctxt->userData,
! 			"xmlSchemaParse: nothing to parse\n");
! 	return (NULL);
      }
  
      /*
Index: include/libxml/xmlschemas.h
===================================================================
RCS file: /cvs/gnome/gnome-xml/include/libxml/xmlschemas.h,v
retrieving revision 1.3
diff -p -r1.3 xmlschemas.h
*** include/libxml/xmlschemas.h	22 Apr 2002 16:01:18 -0000	1.3
--- include/libxml/xmlschemas.h	9 Oct 2002 21:12:45 -0000
*************** typedef xmlSchemaValidCtxt *xmlSchemaVal
*** 73,79 ****
  /*
   * Interfaces for parsing.
   */
! xmlSchemaParserCtxtPtr xmlSchemaNewParserCtxt(const char *URL);
  void		xmlSchemaFreeParserCtxt	(xmlSchemaParserCtxtPtr ctxt);
  void		xmlSchemaSetParserErrors(xmlSchemaParserCtxtPtr ctxt,
  					 xmlSchemaValidityErrorFunc err,
--- 73,81 ----
  /*
   * Interfaces for parsing.
   */
! xmlSchemaParserCtxtPtr xmlSchemaNewParserCtxt	(const char *URL);
! xmlSchemaParserCtxtPtr xmlSchemaNewMemParserCtxt(const char *buffer,
! 						 int size);
  void		xmlSchemaFreeParserCtxt	(xmlSchemaParserCtxtPtr ctxt);
  void		xmlSchemaSetParserErrors(xmlSchemaParserCtxtPtr ctxt,
  					 xmlSchemaValidityErrorFunc err,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]