Beispiel #1
0
void
exercise(BinInputStream& stream)
{
	static float percents[] = { 1.0, 0.5, 0.25, 0.1, 0.15, 0.113, 0.333, 0.0015, 0.0013 };
	int numPercents = sizeof(percents) / sizeof(float);
	
	const unsigned int bufferMax = 4096;
	XMLByte buffer[bufferMax];

	int iteration = 0;
	unsigned int bytesRead = 0;
	do {
		// Calculate a percentage of our maximum buffer size, going through
		// them round-robin
		float percent = percents[iteration % numPercents];
		unsigned int bufCnt = (unsigned int)(bufferMax * percent);
		
		// Check to make sure we didn't go out of bounds
		if (bufCnt <= 0)
			bufCnt = 1;
		if (bufCnt > bufferMax)
			bufCnt = bufferMax;
		
		// Read bytes into our buffer
		bytesRead = stream.readBytes(buffer, bufCnt);
		//XERCES_STD_QUALIFIER cerr << "Read " << bytesRead << " bytes into a " << bufCnt << " byte buffer\n";

		if (bytesRead > 0)
		{
			// Write the data to standard out
			XERCES_STD_QUALIFIER cout.write((char*)buffer, bytesRead);
		}
		
		++iteration;
	} while (bytesRead > 0);
}
Components::Cookie* 
AssemblyFactoryImpl::create_assembly (const char* assembly_loc)
throw (Components::Deployment::InvalidLocation, Components::CreateFailure)
{
	DEBUG_OUT2( "AssemblyFactoryImpl: creating new assembly for ", assembly_loc );

	//
	// exclusive
	//
	QedoLock lock(&mutex_);

	//
	// get path
	//
	XMLURL uri(assembly_loc);
	const XMLCh* p = uri.getPath();
	if(!p)
	{
		NORMAL_ERR2( "AssemblyFactoryImpl: invalid assembly location: ", assembly_loc );
		throw Components::Deployment::InvalidLocation();
	}
	std::string name = Qedo::transcode(p);
	std::string::size_type pos = name.find_last_of("/");
	if (pos != std::string::npos)
	{
		name.erase(0, pos + 1);
	}

	//
	// make input stream
	//
	URLInputSource inputSource(uri);
	BinInputStream* inputStream;
	try
	{
		inputStream = inputSource.makeStream();
	}
	catch(...)
	{
		NORMAL_ERR2( "AssemblyFactoryImpl: invalid assembly location: ", assembly_loc );
		throw Components::Deployment::InvalidLocation();
	}
    if (!inputStream)
    {
		NORMAL_ERR2( "AssemblyFactoryImpl: invalid assembly location", assembly_loc );
		throw Components::Deployment::InvalidLocation();
    }

	//
	// create new cookie
	//
	Cookie_impl* new_cookie = new Cookie_impl();
	const char * s =  new_cookie->to_string();
	std::string uuid = s;
	CORBA::string_free(const_cast<char*>(s));

	//
	// store the package
	//
	std::string dir = getPath( packageDirectory_ ) + uuid;
	if( makeDir(dir) )
	{
		NORMAL_ERR3( "AssemblyFactoryImpl: directory ", dir, " can not be created");
		throw Components::CreateFailure();
	}

	std::string package_name = getPath( dir ) + name;
	std::ofstream packageFile(package_name.c_str(), std::ios::binary|std::ios::app);
	if ( ! packageFile)
	{
		NORMAL_ERR2( "AssemblyFactoryImpl: cannot open file ", package_name );
		throw Components::CreateFailure();
	}
    unsigned char* buf = new unsigned char[4096];
    unsigned int len = inputStream->readBytes(buf, 4096);
    while (len)
    {
		packageFile.write((const char*)buf, len);
		len = inputStream->readBytes(buf, 4096);
	}
	delete [] buf;
	packageFile.close();
	delete inputStream;

	//
	// create new assembly
	//
	AssemblyImpl* ass = new AssemblyImpl(package_name, new_cookie, nameService_);
	assemblies_.push_back(ass);

	DEBUG_OUT( "..... done" );

	return new_cookie;
}
Beispiel #3
0
Sequence FunctionUnparsedText::createSequence(DynamicContext* context, int flags) const
{
  Item::Ptr uriArg = getParamNumber(1, context)->next(context);
  
  if(uriArg.isNull()) {
    return Sequence(context->getMemoryManager());
  }
  
  const XMLCh *uri = uriArg->asString(context);

  if(!XPath2Utils::isValidURI(uri, context->getMemoryManager()))
    XQThrow(FunctionException, X("FunctionUnparsedText::createSequence"), X("The argument to fn:unparsed-text() is not a valid URI [err:XTDE1170]"));

  // TBD Implement a URIResolver method for resolving unparsed text - jpcs

  const XMLCh *baseUri = context->getBaseURI();

  InputSource *srcToUse = 0;
  if(context->getXMLEntityResolver()){
    XMLResourceIdentifier resourceIdentifier(XMLResourceIdentifier::UnKnown, uri, 0, XMLUni::fgZeroLenString, baseUri);
    srcToUse = context->getXMLEntityResolver()->resolveEntity(&resourceIdentifier);
  }

  if(srcToUse == 0) {
    try {
      // Resolve the uri against the base uri
      XMLURL urlTmp;

      if(baseUri && *baseUri) {
        urlTmp.setURL(baseUri, uri);
      }
      else {
        urlTmp.setURL(uri);
      }

      srcToUse = new URLInputSource(urlTmp);
    }
    catch(const MalformedURLException &e) {
    }
  }

  if(srcToUse == 0) {
    // It's not a URL, so let's assume it's a local file name.
    if(baseUri && *baseUri) {
      AutoDeallocate<XMLCh> tmpBuf(XMLPlatformUtils::weavePaths(baseUri, uri), XMLPlatformUtils::fgMemoryManager);
      srcToUse = new LocalFileInputSource(tmpBuf);
    }
    else {
      srcToUse = new LocalFileInputSource(uri);
    }
  }

  Janitor<InputSource> janIS(srcToUse);

  if(getNumArgs() == 2) {
    const XMLCh *encoding = getParamNumber(2, context)->next(context)->asString(context);
    srcToUse->setEncoding(encoding);
  }

  XMLBuffer result;
  try {
    BinInputStream *stream = srcToUse->makeStream();
    if(stream == NULL) {
      XMLBuffer buf;
      buf.set(X("Cannot read unparsed content from "));
      buf.append(uri);
      buf.append(X(" [err:XTDE1170]"));
      XQThrow2(FunctionException,X("FunctionUnparsedText::createSequence"), buf.getRawBuffer());
    }
    Janitor<BinInputStream> janStream(stream);

#ifdef HAVE_GETCONTENTTYPE
    if(FunctionMatches::matches(stream->getContentType(), X("(text|application)/(xml|[^ +;]+\\+xml)"), X("i"))) {
      srcToUse->setEncoding(0);
      srcToUse->setEncoding(FindXMLEncoding().start(*srcToUse, context));
    }
#endif

    XPath2Utils::readSource(stream, context->getMemoryManager(), result, srcToUse->getEncoding());
  }
  catch(XMLException &e) {
    XMLBuffer buf;
    buf.set(X("Exception reading unparsed content: "));
    buf.append(e.getMessage());
    buf.append(X(" [err:XTDE1190]"));
    XQThrow2(FunctionException,X("FunctionUnparsedText::createSequence"), buf.getRawBuffer());
  }

  return Sequence(context->getItemFactory()->createString(result.getRawBuffer(), context), context->getMemoryManager());
}