Пример #1
0
LowRepomd *
low_repomd_parse (const char *repodata)
{
	struct repodata_context ctx;
	void *buf;
	int len;
	int repodata_file;
	XML_Parser parser;
	XML_ParsingStatus status;

	ctx.state = REPODATA_STATE_BEGIN;

	parser = XML_ParserCreate (NULL);
	XML_SetUserData (parser, &ctx);
	XML_SetElementHandler (parser,
			       low_repomd_start_element,
			       low_repomd_end_element);
	XML_SetCharacterDataHandler (parser, low_repomd_character_data);

	repodata_file = open (repodata, O_RDONLY);
	if (repodata_file < 0) {
		XML_ParserFree (parser);
		return NULL;
	}

	ctx.repomd = malloc (sizeof (LowRepomd));
	memset (ctx.repomd, 0, sizeof (LowRepomd));

	do {
		XML_GetParsingStatus (parser, &status);
		switch (status.parsing) {
			case XML_SUSPENDED:
			case XML_PARSING:
			case XML_INITIALIZED:
				buf = XML_GetBuffer (parser, XML_BUFFER_SIZE);
				len = read (repodata_file, buf,
					    XML_BUFFER_SIZE);
				if (len < 0) {
					fprintf (stderr,
						 "couldn't read input: %s\n",
						 strerror (errno));
					XML_ParserFree (parser);
					low_repomd_free (ctx.repomd);
					return NULL;
				}

				XML_ParseBuffer (parser, len, len == 0);
				break;
			case XML_FINISHED:
			default:
				break;
		}
	} while (status.parsing != XML_FINISHED);

	XML_ParserFree (parser);

	close (repodata_file);
	return ctx.repomd;
}
Пример #2
0
void _Expat_XML_GetParsingStatus(struct ExpatIFace * Self, XML_Parser parser, XML_ParsingStatus * status)
{
	XML_GetParsingStatus(parser, status);
}
Пример #3
0
enum XML_Status get_pstatus(parser_t *parser) {
  XML_ParsingStatus status;
  XML_GetParsingStatus(parser->p, &status);
  return status.parsing;
}
Пример #4
0
/* params : desc : the document descriptor
 *          out  : destination buffer for the paragraph
 * return : the length of the paragraph
 *          NO_MORE_DATA if there is no more paragraph
 *          or an error code
 *
 * parses the next paragraph
 */
int parse(struct doc_descriptor* desc, char *out) {
  char buf[BUFSIZE], tmp[BUFSIZE];
  XML_ParsingStatus status;
  struct meta *meta;
  int len, i;

  /* initializing next paragraph container */
  ((struct ParserState *)(desc->myState))->ch = out;
  ((struct ParserState *)(desc->myState))->chlen = 0;

  /* continuing to next paragraph*/
  if (((struct ParserState *)(desc->myState))->suspended) {
    ((struct ParserState *)(desc->myState))->suspended = 0;
    XML_ResumeParser(desc->parser);
  }

  /* filling a new buffer if the last one has been consumed */
  if (!((struct ParserState *)(desc->myState))->suspended) {

    ((struct ParserState *)(desc->myState))->buflen = 0;
    i = 0;
    len = read(desc->fd, tmp, BUFSIZE);
    while(i < len ) {
      if(strncmp(tmp + i, "\x05", 1)) {
	strncpy(buf + ((struct ParserState *)(desc->myState))->buflen, tmp + i, 1);
	((struct ParserState *)(desc->myState))->buflen += 1;
      }
      i++;
    }
  }

  while (!((struct ParserState *)(desc->myState))->suspended
	 && ((struct ParserState *)(desc->myState))->buflen > 0) {
    /* processing data until a whole paragraph has been parse
       or end of file is reached */

    ((struct ParserState *)(desc->myState))->meta = NULL;

    /* parsing buffer */
    if (XML_Parse(desc->parser, buf, ((struct ParserState *)(desc->myState))->buflen, 0) == XML_STATUS_ERROR) {
      fprintf(stderr, "Parsing error : %s\n",
	      XML_ErrorString(XML_GetErrorCode(desc->parser)));
      return SAX_ERROR;
    }

    /* filling the metadata linked list if if metadata have been parsed */
    while(((struct ParserState *)(desc->myState))->meta_suspended) {
      meta = desc->meta;
      /* adding the new metadata at the end of the list */
      if (meta == NULL) {
	desc->meta = ((struct ParserState *)(desc->myState))->meta;
      } else {
	while(meta->next != NULL) {
	  meta = meta->next;
	}
	meta->next = ((struct ParserState *)(desc->myState))->meta;
      }

      /* resuming parsing */
      ((struct ParserState *)(desc->myState))->meta_suspended = 0;
      XML_ResumeParser(desc->parser);
    }

    /* filling new buffer if the last one has been consumed */
    XML_GetParsingStatus(desc->parser, &status);
    if (status.parsing != XML_SUSPENDED) {

      ((struct ParserState *)(desc->myState))->buflen = 0;
      i = 0;
      len = read(desc->fd, tmp, BUFSIZE);
      while(i < len ) {
	if(strncmp(tmp + i, "\x05", 1)) {
	  strncpy(buf + ((struct ParserState *)(desc->myState))->buflen, tmp + i, 1);
	  ((struct ParserState *)(desc->myState))->buflen += 1;
	}
	i++;
      }
    }
  }

  /* end of file has been reached */
  if (((struct ParserState *)(desc->myState))->buflen == 0) {

    /* resuming parsing if needed (this shouldn't happen) */
    if (((struct ParserState *)(desc->myState))->suspended) {
      XML_ResumeParser(desc->parser);
    }

    /* signaling the end to the parser */
    if (XML_Parse(desc->parser, buf, 0, 1) == XML_STATUS_ERROR) {
      fprintf(stderr, "Parsing error : %s\n",
	      XML_ErrorString(XML_GetErrorCode(desc->parser)));
      return SAX_ERROR;
    }
    return NO_MORE_DATA;
  }

  return ((struct ParserState *)(desc->myState))->chlen;
}
Пример #5
0
/*
 * Starts parsing. The XML data being parsed should be set either set as 
 * a buffer in OSCTXT (pctxt->buffer) or read from its stream (pctxt->pStream).
 */
int rtSaxCParse (OSXMLREADER* pReader)
{
   long len;
   enum XML_Status stat = XML_STATUS_OK;
   struct OSRTSTREAM* pStream;
   XMLReaderImpl* readerImpl = (XMLReaderImpl*)(void*)pReader;
   OSCTXT* pctxt;
   OSSAXHandlerBase* pSaxBase;

   if (pReader == 0 || pReader->pctxt == 0 || readerImpl->parser == 0)
      return RTERR_NOTINIT;

   pctxt = pReader->pctxt;
   pStream = pctxt->pStream;
   rtxErrReset (pctxt);
   pSaxBase = (OSSAXHandlerBase*)readerImpl->userData;

   if (pStream == 0) {
      
      /* stream is not set - parse just a buffer */

      stat = XML_Parse (readerImpl->parser, (char*)OSRTBUFPTR(pctxt),
                        (int)pctxt->buffer.size, TRUE);
      return (stat == XML_STATUS_ERROR) ? RTERR_XMLPARSE : 0;
   }
   else { /* read from stream and parse */
      do {
         void* pbuf;
         XML_Bool isFinal;
         
         /* get the buffer to read in */
         pbuf = XML_GetBuffer (readerImpl->parser, XML_BUF_SIZE);

         /* read data to the buffer */
         len = rtxStreamRead (pctxt, (OSOCTET*)pbuf, XML_BUF_SIZE);
         if (len < 0)
            break;
         isFinal = (XML_Bool)(!len);

         /* parse the data in the buffer */
         if ((stat = XML_ParseBuffer (readerImpl->parser, len, isFinal)) == 0) 
            break;

         /* the following code is necessary only if it is necessary to 
          * decode several XML documents consequently from one stream. */
         if (pSaxBase->mState == OS_SAX_FINAL_STATE) {
            /* if parsing is finished, but buffer is not empty we need
             * to find the beginning of the next XML message and set
             * this piece of data as pre-read buffer for BufferedStream.*/
            XML_ParsingStatus status;

            XML_GetParsingStatus(readerImpl->parser, &status);
            if (status.parsing == XML_SUSPENDED) {
               int offset, lenn;

               /* Get buffer pointer, offset and length of remaining data.
                  Note, that patching of Expat is necessary to fix two problems:
                  1) even if parser is stopped by XML_StopParser, it will return
                     error "junk after end-of-document" if buffer is not empty;
                  2) XML_GetInputContext worked only if macro XML_CONTEXT_BYTES
                     was defined. But it could work even without it. */
               const char * _pbuf = 
                  XML_GetInputContext(readerImpl->parser, &offset, &lenn);

               if (offset > 0 && lenn - offset > 0) {
                  int stat = 0;
                  const OSUTF8CHAR* prereadBuf = (const OSUTF8CHAR*)_pbuf + offset;
                  size_t prereadBufLen = (size_t)(lenn - offset), i;

                  /* check, is the buffer just whitespaces or not. If yes,
                     discard it */
                  for (i = 0; i < prereadBufLen; i++) {
                     if (!OS_ISSPACE (prereadBuf[i])) {
                  
                        if (OSRTSTREAM_ID (pctxt) != OSRTSTRMID_DIRECTBUF) {
                           stat = rtxStreamBufferedCreate (pctxt, 
                              OSRTSTRMCM_RESTORE_UNDERLAYING_AFTER_RESET);
                           if (stat < 0) len = stat;
                        }
                        if (stat == 0) {
                           stat = rtxStreamBufferedSetPreReadBuf (pctxt, 
                             (const OSOCTET*)prereadBuf + i, prereadBufLen - i);
                           if (stat < 0) len = stat;
                        }
                        break;
                     }
                  }
               }
               stat = XML_STATUS_OK;
            }
            break;
         }
      } while (len > 0);
   }

   if (len < 0) {
      return LOG_RTERR (pctxt, len);
   }
   else if (stat != XML_STATUS_OK) {
      XML_LChar str[256];
      len = RTERR_XMLPARSE;

      EXML_ErrorString (readerImpl->parser, 
                        XML_GetErrorCode (readerImpl->parser), str, 
                        sizeof(str)/sizeof(str[0]));
      LOG_RTERRNEW (pctxt, len);
      rtxErrAddStrParm (pctxt, LSTRX (pctxt, str));
      return len;
   }

   return 0;
}
Пример #6
0
struct razor_set *
razor_set_create_from_yum(void)
{
    struct yum_context ctx;
    void *buf;
    int len, ret;
    gzFile primary, filelists;
    XML_ParsingStatus status;

    ctx.importer = razor_importer_create();
    ctx.state = YUM_STATE_BEGIN;

    ctx.primary_parser = XML_ParserCreate(NULL);
    XML_SetUserData(ctx.primary_parser, &ctx);
    XML_SetElementHandler(ctx.primary_parser,
                          yum_primary_start_element,
                          yum_primary_end_element);
    XML_SetCharacterDataHandler(ctx.primary_parser,
                                yum_character_data);

    ctx.filelists_parser = XML_ParserCreate(NULL);
    XML_SetUserData(ctx.filelists_parser, &ctx);
    XML_SetElementHandler(ctx.filelists_parser,
                          yum_filelists_start_element,
                          yum_filelists_end_element);
    XML_SetCharacterDataHandler(ctx.filelists_parser,
                                yum_character_data);

    primary = gzopen("primary.xml.gz", "rb");
    if (primary == NULL)
        return NULL;
    filelists = gzopen("filelists.xml.gz", "rb");
    if (filelists == NULL)
        return NULL;

    ctx.current_parser = ctx.primary_parser;

    ctx.current = 0;

    do {
        XML_GetParsingStatus(ctx.current_parser, &status);
        switch (status.parsing) {
        case XML_SUSPENDED:
            ret = XML_ResumeParser(ctx.current_parser);
            break;
        case XML_PARSING:
        case XML_INITIALIZED:
            buf = XML_GetBuffer(ctx.current_parser,
                                XML_BUFFER_SIZE);
            if (ctx.current_parser == ctx.primary_parser)
                len = gzread(primary, buf, XML_BUFFER_SIZE);
            else
                len = gzread(filelists, buf, XML_BUFFER_SIZE);
            if (len < 0) {
                fprintf(stderr,
                        "couldn't read input: %s\n",
                        strerror(errno));
                return NULL;
            }

            XML_ParseBuffer(ctx.current_parser, len, len == 0);
            break;
        case XML_FINISHED:
            break;
        }
    } while (status.parsing != XML_FINISHED);


    XML_ParserFree(ctx.primary_parser);
    XML_ParserFree(ctx.filelists_parser);

    gzclose(primary);
    gzclose(filelists);

    printf ("\nsaving\n");
    return razor_importer_finish(ctx.importer);
}
Пример #7
0
void bmx_expat_XML_GetParsingStatus(XML_Parser parser, XML_Parsing * parsing, int * finalBuffer) {
	XML_ParsingStatus status;
	XML_GetParsingStatus(parser, &status);
	*parsing = status.parsing;
	*finalBuffer = status.finalBuffer;
}
Пример #8
0
bool 
OMXMLReaderExpat::next()
{
    TRACE("OMXMLReaderExpat::next");

    if (_status == false)
    {
        return _status;
    }

    while (_status && nextEvent() == NONE)
    {
        if (!_readNextChunk)
        {
            int ret = XML_ResumeParser(_parser);

            if (ret == XML_STATUS_ERROR)
            {
                XML_Error errorCode = XML_GetErrorCode(_parser);
                if (errorCode == XML_ERROR_NOT_SUSPENDED)
                {
                    XML_ParsingStatus pStatus;
                    XML_GetParsingStatus(_parser, &pStatus);
                    if (pStatus.parsing == XML_FINISHED)
                    {
                        // finished
                        _status = false;
                    }
                    else
                    {
                        _readNextChunk = true;
                    }
                }
                else
                {
                    throw OMException(getErrorString());
                }
            }
        }

        if (_readNextChunk)
        {
            void* buffer = XML_GetBuffer(_parser, READ_CHUNK_SIZE);
            _numInBuffer = readNextChunk(buffer, READ_CHUNK_SIZE);

            int ret = XML_ParseBuffer(_parser, _numInBuffer, _numInBuffer < READ_CHUNK_SIZE);

            if (ret == XML_STATUS_ERROR)
            {
                throw OMException(getErrorString());
            }

            if (_status)
            {
                XML_ParsingStatus pStatus;
                XML_GetParsingStatus(_parser, &pStatus);
                if (pStatus.parsing == XML_FINISHED)
                {
                    if (_numInBuffer < READ_CHUNK_SIZE)
                    {
                        _status = false;
                    }
                    else
                    {
                        _readNextChunk = true;
                    }
                }
                else
                {
                    _readNextChunk = false;
                }
            }
        }
    }
    
    return _status;
}