dbertoni 01/03/02 11:09:40
Modified: c/src/XMLSupport FormatterToDOM.cpp FormatterToDOM.hpp
Log:
Added PrefixResolver support.
Revision Changes Path
1.14 +144 -19 xml-xalan/c/src/XMLSupport/FormatterToDOM.cpp
Index: FormatterToDOM.cpp
===================================================================
RCS file: /home/cvs/xml-xalan/c/src/XMLSupport/FormatterToDOM.cpp,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -r1.13 -r1.14
--- FormatterToDOM.cpp 2001/02/08 21:14:44 1.13
+++ FormatterToDOM.cpp 2001/03/02 19:09:27 1.14
@@ -83,6 +83,14 @@
+#include <DOMSupport/PrefixResolver.hpp>
+
+
+
+const XalanDOMString FormatterToDOM::s_emptyString;
+
+
+
FormatterToDOM::FormatterToDOM(
XalanDocument* doc,
XalanDocumentFragment* docFrag,
@@ -91,7 +99,10 @@
m_doc(doc),
m_docFrag(docFrag),
m_currentElem(currentElement),
- m_elemStack()
+ m_elemStack(),
+ m_buffer1(),
+ m_buffer2(),
+ m_prefixResolver(0)
{
assert(m_doc != 0 && m_docFrag != 0);
}
@@ -105,7 +116,10 @@
m_doc(doc),
m_docFrag(0),
m_currentElem(elem),
- m_elemStack()
+ m_elemStack(),
+ m_buffer1(),
+ m_buffer2(),
+ m_prefixResolver(0)
{
assert(m_doc != 0);
}
@@ -147,15 +161,9 @@
const XMLCh* const name,
AttributeList& attrs)
{
- XalanElement* const elem =
m_doc->createElement(XalanDOMString(name));
-
- const int nAtts = attrs.getLength();
+ XalanElement* const elem = createElement(name, attrs);
+ assert(elem != 0);
- for(int i = 0; i < nAtts; i++)
- {
- elem->setAttribute(XalanDOMString(attrs.getName(i)),
XalanDOMString(attrs.getValue(i)));
- }
-
append(elem);
m_elemStack.push_back(m_currentElem);
@@ -188,9 +196,9 @@
const XMLCh* const chars,
const unsigned int length)
{
- assign(m_buffer, chars, length);
+ assign(m_buffer1, chars, length);
- append(m_doc->createTextNode(m_buffer));
+ append(m_doc->createTextNode(m_buffer1));
}
@@ -212,7 +220,9 @@
void
FormatterToDOM::entityReference(const XMLCh* const name)
{
- append(m_doc->createEntityReference(XalanDOMString(name)));
+ assign(m_buffer1, name);
+
+ append(m_doc->createEntityReference(m_buffer1));
}
@@ -222,9 +232,9 @@
const XMLCh* const chars,
const unsigned int length)
{
- assign(m_buffer, chars, length);
+ assign(m_buffer1, chars, length);
- append(m_doc->createTextNode(m_buffer));
+ append(m_doc->createTextNode(m_buffer1));
}
@@ -234,7 +244,10 @@
const XMLCh* const target,
const XMLCh* const data)
{
- append(m_doc->createProcessingInstruction(XalanDOMString(target),
XalanDOMString(data)));
+ assign(m_buffer1, target);
+ assign(m_buffer2, data);
+
+ append(m_doc->createProcessingInstruction(m_buffer1, m_buffer2));
}
@@ -249,7 +262,9 @@
void
FormatterToDOM::comment(const XMLCh* const data)
{
- append(m_doc->createComment(XalanDOMString(data)));
+ assign(m_buffer1, data);
+
+ append(m_doc->createComment(m_buffer1));
}
@@ -259,9 +274,9 @@
const XMLCh* const ch,
const unsigned int length)
{
- assign(m_buffer, ch, length);
+ assign(m_buffer1, ch, length);
- append(m_doc->createCDATASection(m_buffer));
+ append(m_doc->createCDATASection(m_buffer1));
}
@@ -282,5 +297,115 @@
else
{
m_doc->appendChild(newNode);
+ }
+}
+
+
+
+XalanElement*
+FormatterToDOM::createElement(
+ const XalanDOMChar* theElementName,
+ AttributeList& attrs)
+{
+ XalanElement* theElement = 0;
+
+ assign(m_buffer1, theElementName);
+
+ if (m_prefixResolver == 0)
+ {
+ theElement = m_doc->createElement(m_buffer1);
+
+ addAttributes(theElement, attrs);
+ }
+ else
+ {
+ // Check for the namespace...
+ const XalanDOMString& theNamespace =
+ getNamespaceForPrefix(theElementName,
*m_prefixResolver, m_buffer2);
+
+ if (length(theNamespace) == 0)
+ {
+ theElement = m_doc->createElement(m_buffer1);
+ }
+ else
+ {
+ theElement = m_doc->createElementNS(theNamespace,
m_buffer1);
+ }
+
+ addAttributes(theElement, attrs);
+ }
+
+ return theElement;
+}
+
+
+
+void
+FormatterToDOM::addAttributes(
+ XalanElement* theElement,
+ AttributeList& attrs)
+{
+ const unsigned int nAtts = attrs.getLength();
+
+ if (m_prefixResolver == 0)
+ {
+ for(unsigned int i = 0; i < nAtts; i++)
+ {
+ assign(m_buffer1, attrs.getName(i));
+ assign(m_buffer2, attrs.getValue(i));
+
+ theElement->setAttribute(m_buffer1, m_buffer2);
+ }
+ }
+ else
+ {
+ for(unsigned int i = 0; i < nAtts; i++)
+ {
+ const XalanDOMChar* const theName =
attrs.getName(i);
+ assert(theName != 0);
+
+ // Check for the namespace...
+ const XalanDOMString& theNamespace =
+ getNamespaceForPrefix(theName,
*m_prefixResolver, m_buffer2);
+
+ assign(m_buffer1, theName);
+ assign(m_buffer2, attrs.getValue(i));
+
+ if (length(theNamespace) == 0)
+ {
+ theElement->setAttribute(m_buffer1, m_buffer2);
+ }
+ else
+ {
+ theElement->setAttributeNS(theNamespace,
m_buffer1, m_buffer2);
+ }
+ }
+ }
+}
+
+
+
+const XalanDOMString&
+FormatterToDOM::getNamespaceForPrefix(
+ const XalanDOMChar* theName,
+ const PrefixResolver& thePrefixResolver,
+ XalanDOMString& thePrefix)
+{
+ const unsigned int theLength = length(theName);
+ const unsigned int theColonIndex = indexOf(theName,
XalanUnicode::charColon);
+
+ if (theColonIndex == theLength)
+ {
+ clear(thePrefix);
+
+ return thePrefixResolver.getNamespaceForPrefix(s_emptyString);
+ }
+ else
+ {
+ // Get the prefix from theName...
+ assign(thePrefix, theName, theColonIndex);
+ assert(length(thePrefix) != 0);
+
+ return thePrefixResolver.getNamespaceForPrefix(thePrefix);
}
}
1.13 +43 -1 xml-xalan/c/src/XMLSupport/FormatterToDOM.hpp
Index: FormatterToDOM.hpp
===================================================================
RCS file: /home/cvs/xml-xalan/c/src/XMLSupport/FormatterToDOM.hpp,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- FormatterToDOM.hpp 2001/02/08 21:14:46 1.12
+++ FormatterToDOM.hpp 2001/03/02 19:09:31 1.13
@@ -77,6 +77,7 @@
+class PrefixResolver;
class XalanDocument;
class XalanDocumentFragment;
class XalanElement;
@@ -120,6 +121,18 @@
~FormatterToDOM();
+ const PrefixResolver*
+ getPrefixResolver() const
+ {
+ return m_prefixResolver;
+ }
+
+ void
+ setPrefixResolver(const PrefixResolver* thePrefixResolver)
+ {
+ m_prefixResolver = thePrefixResolver;
+ }
+
// These methods are inherited from DocumentHandler ...
virtual void
@@ -217,7 +230,30 @@
void
append(XalanNode* newNode);
+ /**
+ * Create the appropriate element, complete with attributes set.
+ *
+ * @param theElementName The name for the new element
+ * @param attrs The SAX AttributeList for the new attributes.
+ * @return A pointer to the new instance.
+ */
+ XalanElement*
+ createElement(
+ const XalanDOMChar* theElementName,
+ AttributeList& attrs);
+
+ void
+ addAttributes(
+ XalanElement* theElement,
+ AttributeList& attrs);
+
+ const XalanDOMString&
+ getNamespaceForPrefix(
+ const XalanDOMChar* theName,
+ const PrefixResolver& thePrefixResolver,
+ XalanDOMString& thePrefix);
+
// Data members...
XalanDocument* m_doc;
@@ -232,8 +268,14 @@
#endif
ElementStackType m_elemStack;
+
+ XalanDOMString m_buffer1;
+
+ XalanDOMString m_buffer2;
+
+ const PrefixResolver* m_prefixResolver;
- XalanDOMString m_buffer;
+ static const XalanDOMString s_emptyString;
};