You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by David Braaten <da...@multiactive.com> on 2000/08/21 22:32:58 UTC

Problems with handling Xerces errors...(Xalan C++)


Whenever Xerces encounters one of its fatal errors, eg.
Fatal Error at (file D:\Template\Test\home_content.xsl, line 26, char 84):
Expected entity name for reference

, it throws an exception that doesn't get handled anywhere (except by a
catch (...) ).

More Info:
-I have Xalan 0.40 (nt4sp5, vc++sp4)
-I am working with a program that I based on TestXSLT
-home_content.xsl has a stray & that I placed in it to produce the error
-My program processes multiple stylesheets against the same xml file.

Basically, if one stylesheet in the batch I am processing triggers an error
like this, I can no longer process anymore stylesheets and some destructors
in Xalan cause access violations.

DeleteFunctor<StylesheetRoot>::operator()(const StylesheetRoot * 0x10e84a50)
line 88 + 30 bytes
std::for_each(std::_Tree<StylesheetRoot *,StylesheetRoot
*,std::set<StylesheetRoot *,std::less<StylesheetRoot
*>,std::allocator<StylesheetRoot *> >::_Kfn,std::less<StylesheetRoot
*>,std::allocator<StylesheetRoot *> >::iterator {...}, ...) line 37 + 19
bytes
StylesheetConstructionContextDefault::reset() line 152 + 49 bytes
StylesheetConstructionContextDefault::~StylesheetConstructionContextDefault(
) line 106
CGanymedeApp::~CGanymedeApp()

(Of course, it would be nice if David Bertoni tells me "Get the latest cvs,
it's all good there"  ;)  )

I may be able to send someone code if they would like to investigate,
but it is part of a larger system, so that could be tricky...
So, I will include some code excerpts that should show how I'm going about
processing these multiple xsl sheets.  TIA.

// the classes that can be 'reset' are members of my app class
CGanymedeApp::CGanymedeApp():
 xmlParserLiaison(theDOMSupport),
 theXPathSupport(theDOMSupport),
 theXObjectFactory(theXSLProcessorSupport, theXPathSupport),
 processor(xmlParserLiaison, theXPathSupport,
   theXSLProcessorSupport,
   theXObjectFactory, theXPathFactory),
 theStylesheetXObjectFactory(theXSLProcessorSupport, theXPathSupport),
 theConstructionContext(processor,theXSLProcessorSupport,
   theStylesheetXObjectFactory,theStylesheetXPathFactory),
 theExecutionContext(processor, theXSLProcessorSupport,
   theXPathSupport,theXObjectFactory)
{
 theXSLProcessorSupport.setProcessor(&processor);
}

////////////////////////////////////////////////////////////////////////////
// it's so close to testxslt, I even use the Cmdline params struct
void CTEXSLTTask::execute(CTEContext* ptecontext)
{
 CString strOutputFilename;
 CString strXMLFilename;
 CString strXSLTFilename;

 if ( !(m_lFlags & CTEContext::XSLTFileIsFullPath) )
 {
  strXSLTFilename = ptecontext->m_strSrcFolder + m_strXSLTFilename;
 }
 else
 {
  strXSLTFilename = m_strXSLTFilename;
 }

 strXMLFilename = ptecontext->m_strDstFolder + m_strXMLFilename;

 strOutputFilename = ptecontext->m_strDstFolder + m_strOutputFilename;

 // add this to delete list?
 if ( m_lFlags & CTEContext::IntermediaryFile )
 {
  ptecontext->m_FileDeleteList.Add( strOutputFilename );
 }

 CmdLineParams theParams;
 theParams.inFileName = (LPCTSTR)strXMLFilename;

 theParams.xslFileName = (LPCTSTR)strXSLTFilename;

 theParams.outFileName = (LPCTSTR)strOutputFilename;

 theParams.outputType = FormatterListener::OUTPUT_METHOD_XML;

 if ( m_lFlags & CTEContext::OutputHTML )
 {
  theParams.outputType = FormatterListener::OUTPUT_METHOD_HTML;
 }

 int    theResult = 0;

 try
 {
  theResult = RunProcessor(theParams, ptecontext);
 }
 catch (XSLException& e)
 {
  ptecontext->cerr << "\nXSLException ";

  const string type = DOMStringToStdString(e.getType());

  if (!type.empty())
   ptecontext->cerr << "Type is : " << type << endl;

  const string msg = DOMStringToStdString(e.getMessage());

  if (!msg.empty())
   ptecontext->cerr << "Message is : " << msg << endl;

  theResult = -1;
 }
 catch (SAXException& e)
 {
  ptecontext->cerr << "\nSAXException ";

  const string msg = DOMStringToStdString(e.getMessage());

  if (!msg.empty())
   ptecontext->cerr << "Message is : " << msg << endl;

  theResult = -1;
 }
 catch (XMLException& e)
 {
  ptecontext->cerr << "\nXMLException ";

  const string type = DOMStringToStdString(e.getType());

  if (!type.empty())
   ptecontext->cerr << "Type is : " << type << endl;

  const string msg = DOMStringToStdString(e.getMessage());

  if (!msg.empty())
   ptecontext->cerr << "Message is : " << msg << endl;

  theResult = -1;
 }
 catch(const XalanDOMException& e)
 {
  ptecontext->cerr << endl
    << "XalanDOMException caught.  The code is "
    << e.getExceptionCode()
    << "."
    << endl;

  theResult = -1;
 }
 catch (...)
 {
  ptecontext->cerr << "\nUnhandled Exception while performing
transformation\n";
 }

 if (m_lFlags & CTEContext::RemoveEmpty)
 {
  // check strOutputFilename and if empty remove
  try
  {
   CFile fileHTML (strOutputFilename, CFile::modeRead);

   bool bEmpty = true;
   char buf[256];
   UINT cntRead = fileHTML.Read (buf, sizeof (buf));
   while (cntRead > 0 && bEmpty)
   {
    for (UINT i = 0; i < cntRead; i++)
     if (!isspace (buf[i]))
     {
      bEmpty = false;
      break;
     }

    cntRead = fileHTML.Read (buf, sizeof (buf));
   }
   fileHTML.Close ();

   if (bEmpty)
   {
    CFile::Remove (strOutputFilename);
    ptecontext->cout << endl << "\tRemoving empty output file: " << (LPCSTR)
strOutputFilename << endl;
   }
  }
  catch (CException & ex)
  {
   ptecontext->cerr << endl << "Exception while checking for empty file: ";

   TCHAR szMsg [512];
   if (ex.GetErrorMessage (szMsg, sizeof (szMsg)))
    ptecontext->cerr << szMsg;

   ptecontext->cerr << endl;
  }
  catch (CException* ex)
  {
   ptecontext->cerr << endl << "Exception while checking for empty file: ";

   TCHAR szMsg [512];
   if (ex->GetErrorMessage (szMsg, sizeof (szMsg)))
    ptecontext->cerr << szMsg;

   ptecontext->cerr << endl;

   ex->Delete();
  }
  catch (...)
  {
   ptecontext->cerr << endl << "Unhandled Exception while checking for empty
file!" << endl;
  }
 }
}

////////////////////////////////////////////////////////////////////////////
// this does what xsltmain does in testxslt
// ptecontext->m_Doc is the preloaded xml file
// theParser = new DOMParser();
//  theParser->parse((LPCTSTR)strXMLFilename);
//  theDOM = new DOM_Document();
//  (*theDOM) = theParser->getDocument();
//  theDOMSupport = new DOMSupportDefault();
//  theParserLiaison = new XercesParserLiaison(*theDOMSupport);
//  m_ecbuilderDoc = theParserLiaison->createDocument(*theDOM);

int CTEXSLTTask::RunProcessor(const CmdLineParams& params, CTEContext*
ptecontext)
{
 CGanymedeApp* pApp = (CGanymedeApp*)AfxGetApp();

 const XalanDOMString mimeEncoding(XALAN_STATIC_UCODE_STRING("UTF-8"));
 const XalanDOMString encoding(XALAN_STATIC_UCODE_STRING("UTF-8"));

 XercesStdTextOutputStream    theStdErr(ptecontext->cerr);
 XercesDOMPrintWriter     diagnosticsWriter(theStdErr);

 auto_ptr<TraceListener>  theTraceListener(
   createTraceListener(
    params,
    diagnosticsWriter));

 if (theTraceListener.get() != 0)
 {
  pApp->processor.setTraceSelects(params.traceSelectionEvent);
  pApp->processor.addTraceListener(theTraceListener.get());
 }

 /*
  * Set specified processor flags
  */
 pApp->processor.setQuietConflictWarnings(params.setQuietConflictWarnings);

 /*
  * Set specified parser flags
  */
 if (params.indentAmount != 0)
 {
  pApp->xmlParserLiaison.setIndent(params.indentAmount);
 }


pApp->xmlParserLiaison.setSpecialCharacters(params.specialCharacters.c_str()
);

pApp->xmlParserLiaison.SetShouldExpandEntityRefs(params.shouldExpandEntityRe
fs);

 assert(params.inFileName.size() > 0);

 if (!params.setQuietMode)
 {
  pApp->processor.setDiagnosticsOutput(&diagnosticsWriter);
 }

 XalanDOMString xslFileName;

 if(0 != params.xslFileName.size())
 {
  xslFileName = params.xslFileName.c_str();
 }

 const StylesheetRoot* stylesheet = 0;

 if (!isEmpty(xslFileName))
 {
  stylesheet = pApp->processor.processStylesheet(xslFileName,
pApp->theConstructionContext);
 }

 auto_ptr<TextOutputStream> outputFileStream(createOutputStream(params));
 assert(outputFileStream.get() != 0);

 XercesDOMPrintWriter resultWriter(*outputFileStream.get());

 const auto_ptr<FormatterListener> formatter(
   createFormatter(
    params,
    resultWriter,
    pApp->xmlParserLiaison.getIndent(),
    mimeEncoding,
    stylesheet));

 XSLTResultTarget rTreeTarget;

 if(formatter.get() == 0)
 {
  rTreeTarget.setCharacterStream(&resultWriter);
 }
 else
 {
  rTreeTarget.setFormatterListener(formatter.get());
 }


 // Do the transformation...
 XSLTInputSource*  theInputSource = NULL;

 try
 {
  if ( (m_lFlags & CTEContext::DefaultXML) && (ptecontext->m_Doc != NULL) )
  {
   theInputSource = new XSLTInputSource(ptecontext->m_Doc);
  }
  else
  {
   theInputSource = new XSLTInputSource(params.inFileName.c_str());
  }

  if (stylesheet == 0)
  {
   // No stylesheet, so the only hope is that the xml file has
   // PI with the stylesheet...

   // Dummy input source...
   XSLTInputSource  theStylesheetSource;

   pApp->processor.process(
     *theInputSource,
     theStylesheetSource,
     rTreeTarget,
     pApp->theConstructionContext,
     pApp->theExecutionContext);
  }
  else
  {
   pApp->theExecutionContext.setStylesheetRoot(stylesheet);

   pApp->processor.process(
     *theInputSource,
     rTreeTarget,
     pApp->theExecutionContext);
  }
 }
 catch ( ... )
 {
  if ( theInputSource != NULL )
  {
   delete theInputSource;
  }
  pApp->theExecutionContext.reset();

  pApp->theConstructionContext.reset();
  pApp->theStylesheetXPathFactory.reset();
  pApp->theStylesheetXObjectFactory.reset();

  pApp->processor.reset();

  pApp->theXPathFactory.reset();
  pApp->theXObjectFactory.reset();
  pApp->theXSLProcessorSupport.reset();
  pApp->theXPathSupport.reset();

  pApp->xmlParserLiaison.reset();
  throw;
 }

 pApp->theExecutionContext.reset();

 pApp->theConstructionContext.reset();
 pApp->theStylesheetXPathFactory.reset();
 pApp->theStylesheetXObjectFactory.reset();

 pApp->processor.reset();

 pApp->theXPathFactory.reset();
 pApp->theXObjectFactory.reset();
 pApp->theXSLProcessorSupport.reset();
 pApp->theXPathSupport.reset();

 pApp->xmlParserLiaison.reset();

 return 0;
}