예제 #1
0
/**
 * Parse an XML schema file and populate the priv->root virtual dentry tree
 * with the nodes found during the scanning.
 * @param filename XML input file.
 * @param idx LTFS index.
 * @param vol LTFS volume to which the index belongs. May be NULL.
 * @return 0 on success or a negative value on error.
 */
int xml_schema_from_file(const char *filename, struct ltfs_index *idx, struct ltfs_volume *vol)
{
	int ret;
	xmlTextReaderPtr reader;
	xmlDocPtr doc;

	CHECK_ARG_NULL(filename, -LTFS_NULL_ARG);
	CHECK_ARG_NULL(idx, -LTFS_NULL_ARG);

	reader = xmlReaderForFile(filename, NULL, XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
	if (! reader) {
		ltfsmsg(LTFS_ERR, "17011E", filename);
		return -1;
	}

	/* Workaround for old libxml2 version on OS X 10.5: the method used to preserve
	 * unknown tags modifies the behavior of xmlFreeTextReader so that an additional
	 * xmlDocFree call is required to free all memory. */
	doc = xmlTextReaderCurrentDoc(reader);
	ret = _xml_parse_schema(reader, idx, vol);
	if (ret < 0)
		ltfsmsg(LTFS_ERR, "17012E", filename);
	if (doc)
		xmlFreeDoc(doc);
	xmlFreeTextReader(reader);

#ifdef DEBUG
	/* dump the tree if it isn't too large */
	if (ret == 0 && idx->file_count < 1000)
		fs_dump_tree(idx->root);
#endif

	return ret;
}
예제 #2
0
static void rxml_reader_mark(xmlTextReaderPtr xreader)
{
  xmlDocPtr xdoc = xmlTextReaderCurrentDoc(xreader);

  if (xdoc && xdoc->_private)
    rb_gc_mark((VALUE) xdoc->_private);
}
예제 #3
0
/*
 * call-seq:
 *    reader.expand -> node
 *
 * Returns the current node and its full subtree. Note the returned node
 * is valid ONLY until the next read call.  
 */
static VALUE rxml_reader_expand(VALUE self)
{
  xmlTextReaderPtr xreader = rxml_text_reader_get(self);
  xmlNodePtr xnode = NULL;

  /* At this point we need to wrap the reader's document as explained above. */
  xmlDocPtr xdoc = xmlTextReaderCurrentDoc(xreader);

  if (!xdoc)
    rb_raise(rb_eRuntimeError, "The reader does not have a document.  Did you forget to call read?");

  rxml_document_wrap(xdoc);

  /* And now hook in a mark function */
  RDATA(self)->dmark = (RUBY_DATA_FUNC)rxml_reader_mark;

  xnode = xmlTextReaderExpand(xreader);
  
  if (!xnode)
  {
    return Qnil;
  }
  else
  {
    return rxml_node_wrap(xnode);
  }
}
예제 #4
0
static void processDoc(xmlTextReaderPtr readerPtr) {
    int ret;
    xmlDocPtr docPtr;
    const xmlChar *URL;

    ret = xmlTextReaderRead(readerPtr);
    while (ret == 1) {
      ret = xmlTextReaderRead(readerPtr);
    }

    /*
     * One can obtain the document pointer to get insteresting
     * information about the document like the URL, but one must also
     * be sure to clean it up at the end (see below).
     */
    docPtr = xmlTextReaderCurrentDoc(readerPtr);
    if (NULL == docPtr) {
      fprintf(stderr, "failed to obtain document\n");      
      return;
    }
      
    URL = docPtr->URL;
    if (NULL == URL) {
      fprintf(stderr, "Failed to obtain URL\n");      
    }

    if (ret != 0) {
      fprintf(stderr, "%s: Failed to parse\n", URL);
      return;
    }

    printf("%s: Processed ok\n", (const char *)URL);
}
예제 #5
0
파일: tcphelpers.c 프로젝트: olger/xmlbus
xmlDocPtr _waitForAnswerBACKUP(int sockfd) {
    xmlTextReaderPtr reader = NULL;
    int ret;
    int processNodeRet = 0;
    xmlDocPtr soapDoc = NULL;
	
    reader = xmlReaderForFd(sockfd, NULL, NULL, 0);
    if (reader != NULL) {
        ret = xmlTextReaderRead(reader);
        while(ret == 1) {
            ret = xmlTextReaderRead(reader);
        }
        soapDoc = xmlTextReaderCurrentDoc(reader);
        xmlFreeTextReader(reader);
 	}
	return soapDoc;
}
예제 #6
0
/**
 * streamFile:
 * @filename: the file name to parse
 *
 * Parse and print information about an XML file.
 *
 * Returns the resulting doc with just the elements preserved.
 */
static xmlDocPtr
extractFile(const char *filename, const xmlChar *pattern) {
    xmlDocPtr doc;
    xmlTextReaderPtr reader;
    int ret;

    /*
     * build an xmlReader for that file
     */
    reader = xmlReaderForFile(filename, NULL, 0);
    if (reader != NULL) {
        /*
	 * add the pattern to preserve
	 */
        if (xmlTextReaderPreservePattern(reader, pattern, NULL) < 0) {
            fprintf(stderr, "%s : failed add preserve pattern %s\n",
	            filename, (const char *) pattern);
	}
	/*
	 * Parse and traverse the tree, collecting the nodes in the process
	 */
        ret = xmlTextReaderRead(reader);
        while (ret == 1) {
            ret = xmlTextReaderRead(reader);
        }
        if (ret != 0) {
            fprintf(stderr, "%s : failed to parse\n", filename);
	    xmlFreeTextReader(reader);
	    return(NULL);
        }
	/*
	 * get the resulting nodes
	 */
	doc = xmlTextReaderCurrentDoc(reader);
	/*
	 * Free up the reader
	 */
        xmlFreeTextReader(reader);
    } else {
        fprintf(stderr, "Unable to open %s\n", filename);
	return(NULL);
    }
    return(doc);
}
예제 #7
0
/*
 * call-seq:
 *    reader.expand -> node
 *
 * Read the contents of the current node and the full subtree. It then makes
 * the subtree available until the next read call.
 *
 * Return an XML::Node object, or nil in case of error.
 */
static VALUE rxml_reader_expand(VALUE self)
{
  xmlNodePtr node;
  xmlDocPtr doc;
  xmlTextReaderPtr reader = rxml_text_reader_get(self);
  node = xmlTextReaderExpand(reader);

  if (!node)
    return Qnil;

  /* Okay this is tricky.  By accessing the returned node, we
   take ownership of the reader's document.  Thus we need to
   tell the reader to not free it.  Otherwise it will be
   freed twice - once when the Ruby document wrapper goes
   out of scope and once when the reader goes out of scope. */

  xmlTextReaderPreserve(reader);
  doc = xmlTextReaderCurrentDoc(reader);
  rxml_document_wrap(doc);

  return rxml_node_wrap(node);
}
예제 #8
0
/**
 * Parse an extent list from a file and populate provided dentry with the extents read during
 * the scanning.
 *
 * @param filename File name from where to read the extent list from.
 * @param d Dentry where the extents are to be appended to.
 * @return 0 on success or a negative value on error.
 */
static int xml_extentlist_from_file(const char *filename, struct dentry *d)
{
	declare_extent_parser_vars("extentinfo");
	xmlTextReaderPtr reader;
	xmlDocPtr doc;
	int ret = 0;

	CHECK_ARG_NULL(filename, -LTFS_NULL_ARG);
	CHECK_ARG_NULL(d, -LTFS_NULL_ARG);

	reader = xmlReaderForFile(filename, NULL, XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
	if (! reader) {
		ltfsmsg(LTFS_ERR, "17011E", filename);
		return -1;
	}

	/* Workaround for old libxml2 version on OS X 10.5: the method used to preserve
	 * unknown tags modifies the behavior of xmlFreeTextReader so that an additional
	 * xmlDocFree call is required to free all memory. */
	doc = xmlTextReaderCurrentDoc(reader);

	while (true) { /* BEAM: loop doesn't iterate - Because get_next_tag() macro uses "break", at most once loop is needed here. */
		get_next_tag();
		if (! strcmp(name, "extentinfo")) {
			ret = _xml_parse_extents(reader, IDX_VERSION_SPARSE, d);
			if (ret < 0) {
				/* XML parser: failed to read extent list from file (%d) */
				ltfsmsg(LTFS_ERR, "17084E", ret);
			}
		}
		break;
	}

	if (doc)
		xmlFreeDoc(doc);
	xmlFreeTextReader(reader);

	return ret;
}
BEXMLTextReader::BEXMLTextReader ( const string path )
{
	last_node = false;
	
	boost::filesystem::path file = path;
	bool file_exists = boost::filesystem::exists ( file );

	if ( file_exists ) {

		reader = xmlNewTextReaderFilename ( path.c_str() );

		if ( reader != NULL ) {
			xml_document = xmlTextReaderCurrentDoc ( reader );
		} else {
			throw BEXMLReaderInterface_Exception ( last_error() );
		}
		
	} else {
		throw BEXMLReaderInterface_Exception ( kNoSuchFileOrDirectoryError );
	}

}
예제 #10
0
/**
 * Parse an Index from tape and populate the vol->index->root virtual dentry tree
 * with the nodes found during the scanning.
 * If a file mark is encountered at the end of the Index, the tape is positioned before
 * the file mark.
 * @param eod_pos EOD block position for the current partition, or 0 to assume EOD will not be
 *                encountered during parsing.
 * @param vol LTFS volume.
 * @return 0 on success, 1 if parsing succeeded but no file mark was encountered,
 *         or a negative value on error.
 */
int xml_schema_from_tape(uint64_t eod_pos, struct ltfs_volume *vol)
{
	int ret;
	struct tc_position current_pos;
	struct xml_input_tape *ctx;
	xmlParserInputBufferPtr read_buf;
	xmlTextReaderPtr reader;
	xmlDocPtr doc;

	CHECK_ARG_NULL(vol, -LTFS_NULL_ARG);

	ret = tape_get_position(vol->device, &current_pos);
	if (ret < 0) {
		ltfsmsg(LTFS_ERR, "17013E", ret);
		return ret;
	}

	/* Create output callback context data structure. */
	ctx = malloc(sizeof(struct xml_input_tape));
	if (! ctx) {
		ltfsmsg(LTFS_ERR, "10001E", "xml_schema_from_tape: ctx");
		return -LTFS_NO_MEMORY;
	}
	ctx->buf = malloc(vol->label->blocksize + LTFS_CRC_SIZE);
	if (! ctx->buf) {
		ltfsmsg(LTFS_ERR, "10001E", "xml_schema_from_tape: ctx->buf");
		free(ctx);
		return -LTFS_NO_MEMORY;
	}
	ctx->vol = vol;
	ctx->current_pos = current_pos.block;
	ctx->eod_pos = eod_pos;
	ctx->saw_small_block = false;
	ctx->saw_file_mark = false;
	ctx->buf_size = vol->label->blocksize;
	ctx->buf_start = 0;
	ctx->buf_used = 0;

	/* Create input buffer pointer. */
	read_buf = xmlParserInputBufferCreateIO(xml_input_tape_read_callback,
											xml_input_tape_close_callback,
											ctx, XML_CHAR_ENCODING_NONE);
	if (! read_buf) {
		ltfsmsg(LTFS_ERR, "17014E");
		free(ctx->buf);
		free(ctx);
		return -LTFS_LIBXML2_FAILURE;
	}

	/* Create XML reader. */
	reader = xmlNewTextReader(read_buf, NULL);
	if (! reader) {
		ltfsmsg(LTFS_ERR, "17015E");
		xmlFreeParserInputBuffer(read_buf);
		return -LTFS_LIBXML2_FAILURE;
	}

	/* Workaround for old libxml2 version on OS X 10.5. See comment in xml_schema_from_file()
	 * for details. */
	doc = xmlTextReaderCurrentDoc(reader);

	/* Generate the Index. */
	ret = _xml_parse_schema(reader, vol->index, vol);
	if (ret < 0) {
		ltfsmsg(LTFS_ERR, "17016E");
		if ((ret != -LTFS_UNSUPPORTED_INDEX_VERSION)&&( ret != -LTFS_SYMLINK_CONFLICT)) {
			if (ret == -LTFS_NO_MEMORY)
				ret = -LTFS_NO_MEMORY;
			else
				ret = -LTFS_INDEX_INVALID;
		}
	} else if (ret == 0) {
		if( ! ctx->saw_file_mark)
			ret = 1;
	}
	if (doc)
		xmlFreeDoc(doc);
	xmlFreeTextReader(reader);
	xmlFreeParserInputBuffer(read_buf);

#ifdef DEBUG
	/* dump the tree if it isn't too large */
	if (ret >= 0 && vol->index->file_count < 1000)
		fs_dump_tree(vol->index->root);
#endif

	return ret;
}
예제 #11
0
int main(int argc, char **argv) {
    xmlTextReaderPtr readerPtr;
    int i;
    xmlDocPtr docPtr;

    if (argc < 2)
        return(1);

    /*
     * this initialises the library and check potential ABI mismatches
     * between the version it was compiled for and the actual shared
     * library used.
     */
    LIBXML_TEST_VERSION

    /*
     * Create a new reader for the first file and process the
     * document.
     */
    readerPtr = xmlReaderForFile(argv[1], NULL, 0);
    if (NULL == readerPtr) {
      fprintf(stderr, "%s: failed to create reader\n", argv[1]);      
      return(1);
    }
    processDoc(readerPtr);

    /*
     * The reader can be reused for subsequent files.
     */
    for (i=2; i < argc; ++i) {
      	xmlReaderNewFile(readerPtr, argv[i], NULL, 0);
	if (NULL == readerPtr) {
	  fprintf(stderr, "%s: failed to create reader\n", argv[i]);      
	  return(1);
	}
        processDoc(readerPtr);
    }

    /*
     * Since we've called xmlTextReaderCurrentDoc, we now have to
     * clean up after ourselves.  We only have to do this the last
     * time, because xmlReaderNewFile calls xmlCtxtReset which takes
     * care of it.
     */
    docPtr = xmlTextReaderCurrentDoc(readerPtr);
    if (docPtr != NULL)
      xmlFreeDoc(docPtr);

    /*
     * Clean up the reader.
     */
    xmlFreeTextReader(readerPtr);

    /*
     * Cleanup function for the XML library.
     */
    xmlCleanupParser();
    /*
     * this is to debug memory for regression tests
     */
    xmlMemoryDump();
    return(0);
}