You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xalan.apache.org by ro...@locus.apache.org on 2000/01/04 00:28:18 UTC
cvs commit: xml-xalan/c/src/XercesParserLiaison XercesParserLiaison.cpp XercesParserLiaison.hpp
robweir 00/01/03 15:28:18
Modified: c/src/PlatformSupport DOMStringHelper.cpp
DOMStringHelper.hpp
c/src/XSLT ElemTemplateElement.cpp
c/src/XercesParserLiaison XercesParserLiaison.cpp
XercesParserLiaison.hpp
Log:
Better error handling. We now cleanly propogate exceptions thrown
by the parsers.
Revision Changes Path
1.2 +73 -0 xml-xalan/c/src/PlatformSupport/DOMStringHelper.cpp
Index: DOMStringHelper.cpp
===================================================================
RCS file: /home/cvs/xml-xalan/c/src/PlatformSupport/DOMStringHelper.cpp,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DOMStringHelper.cpp 1999/12/18 19:47:49 1.1
+++ DOMStringHelper.cpp 2000/01/03 23:28:16 1.2
@@ -761,3 +761,76 @@
}
return true;
}
+
+
+XALAN_PLATFORMSUPPORT_EXPORT_FUNCTION(std::string)
+DOMStringToStdString(const DOMString& domString)
+{
+ XMLCh* toTranscode = domString.rawBuffer();
+ unsigned int len = domString.length();
+
+
+ // Short circuit if its a null pointer
+ if (!toTranscode || (!toTranscode[0]))
+ {
+ return std::string();
+ }
+
+ // See if our XMLCh and wchar_t as the same on this platform
+ const bool isSameSize = (sizeof(XMLCh) == sizeof(wchar_t));
+
+ //
+ // Get the actual number of chars. If the passed len is zero, its null
+ // terminated. Else we have to use the len.
+ //
+ wchar_t realLen = (wchar_t)len;
+ if (!realLen)
+ {
+ //
+ // We cannot just assume we can use wcslen() because we don't know
+ // if our XMLCh is the same as wchar_t on this platform.
+ //
+ const XMLCh* tmpPtr = toTranscode;
+ while (*(tmpPtr++))
+ realLen++;
+ }
+
+ //
+ // If either the passed length was non-zero or our char sizes are not
+ // same, we have to use a temp buffer. Since this is common in these
+ // samples, we just do it anyway.
+ //
+ wchar_t* tmpSource = new wchar_t[realLen + 1];
+ if (isSameSize)
+ {
+ memcpy(tmpSource, toTranscode, realLen * sizeof(wchar_t));
+ }
+ else
+ {
+ for (unsigned int index = 0; index < realLen; index++)
+ tmpSource[index] = (wchar_t)toTranscode[index];
+ }
+ tmpSource[realLen] = 0;
+
+ // See now many chars we need to transcode this guy
+ const unsigned int targetLen = ::wcstombs(0, tmpSource, 0);
+
+ // Allocate out storage member
+ char *localForm = new char[targetLen + 1];
+
+ //
+ // And transcode our temp source buffer to the local buffer. Cap it
+ // off since the converter won't do it (because the null is beyond
+ // where the target will fill up.)
+ //
+ ::wcstombs(localForm, tmpSource, targetLen);
+ localForm[targetLen] = 0;
+
+ std::string ret(localForm);
+
+ // Don't forget to delete our temp buffers
+ delete [] tmpSource;
+ delete [] localForm;
+
+ return ret;
+}
1.2 +2 -13 xml-xalan/c/src/PlatformSupport/DOMStringHelper.hpp
Index: DOMStringHelper.hpp
===================================================================
RCS file: /home/cvs/xml-xalan/c/src/PlatformSupport/DOMStringHelper.hpp,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DOMStringHelper.hpp 1999/12/18 19:47:49 1.1
+++ DOMStringHelper.hpp 2000/01/03 23:28:17 1.2
@@ -568,20 +568,9 @@
}
};
-// not really safe in DBCS environments
-inline std::string DOMStringToStdString(const DOMString& domString)
-{
- std::string ret;
+XALAN_PLATFORMSUPPORT_EXPORT_FUNCTION(std::string)
+DOMStringToStdString(const DOMString& domString);
- for (int i=0; i< domString.length(); i++)
- {
- ret += (char)domString.charAt(i);
- }
-
- return ret;
-}
-
-// safe, but not very efficient
inline std::wstring DOMStringToStdWString(const DOMString& domString)
{
return (c_wstr(domString));
1.2 +6 -2 xml-xalan/c/src/XSLT/ElemTemplateElement.cpp
Index: ElemTemplateElement.cpp
===================================================================
RCS file: /home/cvs/xml-xalan/c/src/XSLT/ElemTemplateElement.cpp,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ElemTemplateElement.cpp 1999/12/18 19:48:03 1.1
+++ ElemTemplateElement.cpp 2000/01/03 23:28:17 1.2
@@ -63,7 +63,9 @@
#include <XPath/MutableNodeRefList.hpp>
+#include <sax/SAXException.hpp>
+
#include "ElemForEach.hpp"
#include "NodeSortKey.hpp"
#include "NodeSorter.hpp"
@@ -760,14 +762,16 @@
/**
- * Throw a template element runtime error. (Note: should we throw a SAXException instead?)
+ * Throw a template element error.
*
* @param msg Description of the error that occured.
*/
void ElemTemplateElement::error(const DOMString& msg) const
{
- throw ("ElemTemplateElement error: " + msg);
+ DOMString errMsg("ElemTemplateElement error: " + msg);
+
+ throw SAXException(toCharArray(errMsg));
}
1.2 +43 -24 xml-xalan/c/src/XercesParserLiaison/XercesParserLiaison.cpp
Index: XercesParserLiaison.cpp
===================================================================
RCS file: /home/cvs/xml-xalan/c/src/XercesParserLiaison/XercesParserLiaison.cpp,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- XercesParserLiaison.cpp 1999/12/18 19:48:14 1.1
+++ XercesParserLiaison.cpp 2000/01/03 23:28:17 1.2
@@ -60,13 +60,15 @@
#include <memory>
+#include <iostream>
-
#include <parsers/DOMParser.hpp>
#include <parsers/SAXParser.hpp>
#include <internal/URLInputSource.hpp>
+#include <sax/SAXParseException.hpp>
+
const char* const theParserName = "Xerces";
@@ -102,18 +104,10 @@
inline DOMParser*
CreateDOMParser(bool fValidating)
{
- return new DOMParser;
+ DOMParser *parser = new DOMParser;
+ parser->setDoValidation(fValidating);
-/*
- if (fValidating == true)
- {
- return new ValidatingDOMParser;
- }
- else
- {
- return new NonValidatingDOMParser;
- }
-*/
+ return parser;
}
@@ -121,18 +115,10 @@
inline SAXParser*
CreateSAXParser(bool fValidating)
{
- return new SAXParser;
+ SAXParser *parser = new SAXParser;
+ parser->setDoValidation(fValidating);
-/*
- if (fValidating == true)
- {
- return new ValidatingSAXParser;
- }
- else
- {
- return new NonValidatingSAXParser;
- }
-*/
+ return parser;
}
};
@@ -144,6 +130,7 @@
{
std::auto_ptr<DOMParser> theParser(CreateDOMParser(m_fUseValidatingParser));
+ theParser->setErrorHandler(this);
theParser->parse(reader);
return theParser->getDocument();
@@ -160,6 +147,7 @@
std::auto_ptr<SAXParser> theParser(CreateSAXParser(m_fUseValidatingParser));
theParser->setDocumentHandler(&handler);
+ theParser->setErrorHandler(this);
theParser->parse(urlInputSource);
}
@@ -173,6 +161,7 @@
{
std::auto_ptr<DOMParser> theParser(CreateDOMParser(m_fUseValidatingParser));
+ theParser->setErrorHandler(this);
theParser->parse(urlInputSource);
return theParser->getDocument();
@@ -187,7 +176,8 @@
const DOMString& /* identifier */)
{
std::auto_ptr<SAXParser> theParser(CreateSAXParser(m_fUseValidatingParser));
-
+
+ theParser->setErrorHandler(this);
theParser->setDocumentHandler(&handler);
theParser->parse(urlInputSource);
@@ -199,4 +189,33 @@
XercesParserLiaison::createDocument()
{
return DOM_Document::createDocument();
+}
+
+
+
+void XercesParserLiaison::fatalError(const SAXParseException& e)
+{
+ std::cerr << "\nFatatal Error at (file " << DOMStringToStdString(e.getSystemId())
+ << ", line " << e.getLineNumber()
+ << ", char " << e.getColumnNumber()
+ << "): " << DOMStringToStdString(e.getMessage()) << std::endl;
+
+ throw e;
+}
+
+void XercesParserLiaison::error(const SAXParseException& e)
+{
+ std::cerr << "\nError at (file " << DOMStringToStdString(e.getSystemId())
+ << ", line " << e.getLineNumber()
+ << ", char " << e.getColumnNumber()
+ << "): " << DOMStringToStdString(e.getMessage()) << std::endl;
+}
+
+
+void XercesParserLiaison::warning(const SAXParseException& e)
+{
+ std::cerr << "\nWarning at (file " << DOMStringToStdString(e.getSystemId())
+ << ", line " << e.getLineNumber()
+ << ", char " << e.getColumnNumber()
+ << "): " << DOMStringToStdString(e.getMessage()) << std::endl;
}
1.2 +9 -2 xml-xalan/c/src/XercesParserLiaison/XercesParserLiaison.hpp
Index: XercesParserLiaison.hpp
===================================================================
RCS file: /home/cvs/xml-xalan/c/src/XercesParserLiaison/XercesParserLiaison.hpp,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- XercesParserLiaison.hpp 1999/12/18 19:48:14 1.1
+++ XercesParserLiaison.hpp 2000/01/03 23:28:17 1.2
@@ -76,6 +76,7 @@
#include <dom/DOM_Document.hpp>
#include <dom/DOMString.hpp>
+#include <sax/ErrorHandler.hpp>
// Base class header file.
@@ -90,7 +91,7 @@
-class XALAN_XERCESPARSERLIAISON_EXPORT XercesParserLiaison : public XMLParserLiaisonDefault
+class XALAN_XERCESPARSERLIAISON_EXPORT XercesParserLiaison : public XMLParserLiaisonDefault, ErrorHandler
{
public:
@@ -178,7 +179,13 @@
*/
virtual DOM_Document
createDocument();
-
+
+ // Implementations for SAX ErrorHandler
+
+ virtual void warning(const SAXParseException& exception);
+ virtual void error(const SAXParseException& exception);
+ virtual void fatalError(const SAXParseException& exception);
+
private:
const bool m_fUseValidatingParser;