mkwan 2002/09/16 12:25:01
Modified: java/src/org/apache/xalan/xsltc/runtime BasisLibrary.java
Log:
For XSLTC extension integration. Implement the object-type extension
function in the EXSLT commons package. Change the behavior of the
nodeList2Iterator interface so that it will do a deep copy on the argument
NodeList, i.e. if a Node in the NodeList is an Element, it will not only
copy the Node itself, but also copy all children and attributes. This is
required to get any extension function that returns a NodeList to work in
XSLTC.
Revision Changes Path
1.50 +107 -63
xml-xalan/java/src/org/apache/xalan/xsltc/runtime/BasisLibrary.java
Index: BasisLibrary.java
===================================================================
RCS file:
/home/cvs/xml-xalan/java/src/org/apache/xalan/xsltc/runtime/BasisLibrary.java,v
retrieving revision 1.49
retrieving revision 1.50
diff -u -r1.49 -r1.50
--- BasisLibrary.java 30 Aug 2002 16:17:19 -0000 1.49
+++ BasisLibrary.java 16 Sep 2002 19:25:01 -0000 1.50
@@ -89,6 +89,7 @@
import org.apache.xalan.xsltc.dom.DOMImpl;
import org.apache.xalan.xsltc.dom.DOMBuilder;
import org.apache.xalan.xsltc.dom.StepIterator;
+import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
@@ -463,6 +464,27 @@
}
/**
+ * Implements the object-type() extension function.
+ *
+ * @see <a href="http://www.exslt.org/">EXSLT</a>
+ */
+ public static String objectTypeF(Object obj)
+ {
+ if (obj instanceof String)
+ return "string";
+ else if (obj instanceof Boolean)
+ return "boolean";
+ else if (obj instanceof Number)
+ return "number";
+ else if (obj instanceof DOMAdapter)
+ return "RTF";
+ else if (obj instanceof NodeIterator)
+ return "node-set";
+ else
+ return "unknown";
+ }
+
+ /**
* Implements the nodeset() extension function.
*/
public static NodeIterator nodesetF(Object obj) {
@@ -945,6 +967,87 @@
}
/**
+ * Utility function used to copy a node list to be under a parent node.
+ */
+ private static void copyNodes(org.w3c.dom.NodeList nodeList,
org.w3c.dom.Document doc, org.w3c.dom.Node parent)
+ {
+ // copy Nodes from NodeList into new w3c DOM
+ for (int i = 0; i < nodeList.getLength(); i++)
+ {
+ org.w3c.dom.Node curr = nodeList.item(i);
+ int nodeType = curr.getNodeType();
+ if (nodeType == org.w3c.dom.Node.DOCUMENT_NODE) {
+ // ignore the root node of node list
+ continue;
+ }
+ String value = null;
+ try {
+ value = curr.getNodeValue();
+ } catch (DOMException ex) {
+ runTimeError(RUN_TIME_INTERNAL_ERR, ex.getMessage());
+ return;
+ }
+
+ String nodeName = curr.getNodeName();
+ org.w3c.dom.Node newNode = null;
+ switch (nodeType){
+ case org.w3c.dom.Node.ATTRIBUTE_NODE:
+ newNode = doc.createAttributeNS(curr.getNamespaceURI(),
nodeName);
+ break;
+ case org.w3c.dom.Node.CDATA_SECTION_NODE:
+ newNode = doc.createCDATASection(value);
+ break;
+ case org.w3c.dom.Node.COMMENT_NODE:
+ newNode = doc.createComment(value);
+ break;
+ case org.w3c.dom.Node.DOCUMENT_FRAGMENT_NODE:
+ newNode = doc.createDocumentFragment();
+ break;
+ case org.w3c.dom.Node.DOCUMENT_TYPE_NODE:
+ // nothing ?
+ break;
+ case org.w3c.dom.Node.ELEMENT_NODE:
+ // For Element node, also copy the children and the
attributes.
+ org.w3c.dom.Element element =
doc.createElementNS(curr.getNamespaceURI(), nodeName);
+ if (curr.hasAttributes())
+ {
+ org.w3c.dom.NamedNodeMap attributes =
curr.getAttributes();
+ for (int k = 0; k < attributes.getLength(); k++)
+ {
+ org.w3c.dom.Node attr = attributes.item(k);
+ element.setAttribute(attr.getNodeName(),
attr.getNodeValue());
+ }
+ }
+ copyNodes(curr.getChildNodes(), doc, element);
+ newNode = element;
+ break;
+ case org.w3c.dom.Node.ENTITY_NODE:
+ // nothing ?
+ break;
+ case org.w3c.dom.Node.ENTITY_REFERENCE_NODE:
+ newNode = doc.createEntityReference(nodeName);
+ break;
+ case org.w3c.dom.Node.NOTATION_NODE:
+ // nothing ?
+ break;
+ case org.w3c.dom.Node.PROCESSING_INSTRUCTION_NODE:
+ newNode = doc.createProcessingInstruction(nodeName,
+ value);
+ break;
+ case org.w3c.dom.Node.TEXT_NODE:
+ newNode = doc.createTextNode(value);
+ break;
+ }
+ try {
+ parent.appendChild(newNode);
+ } catch (DOMException e) {
+ runTimeError(RUN_TIME_INTERNAL_ERR, e.getMessage());
+ return;
+ }
+ }
+ }
+
+ /**
* Utility function used to convert a w3c NodeList into a internal
* DOM iterator.
*/
@@ -968,68 +1071,9 @@
org.w3c.dom.Node topElementNode =
doc.appendChild(doc.createElementNS("", "__top__"));
- // copy Nodes from NodeList into new w3c DOM
- for (int i=0; i<size; i++){
- org.w3c.dom.Node curr = nodeList.item(i);
- int nodeType = curr.getNodeType();
- if (nodeType == org.w3c.dom.Node.DOCUMENT_NODE) {
- // ignore the root node of node list
- continue;
- }
- String value = null;
- try {
- value = curr.getNodeValue();
- } catch (DOMException ex) {
- runTimeError(RUN_TIME_INTERNAL_ERR, ex.getMessage());
- return null;
- }
- String namespaceURI = curr.getNamespaceURI();
- String nodeName = curr.getNodeName();
- org.w3c.dom.Node newNode = null;
- switch (nodeType){
- case org.w3c.dom.Node.ATTRIBUTE_NODE:
- newNode = doc.createAttributeNS(namespaceURI,
- nodeName);
- break;
- case org.w3c.dom.Node.CDATA_SECTION_NODE:
- newNode = doc.createCDATASection(value);
- break;
- case org.w3c.dom.Node.COMMENT_NODE:
- newNode = doc.createComment(value);
- break;
- case org.w3c.dom.Node.DOCUMENT_FRAGMENT_NODE:
- newNode = doc.createDocumentFragment();
- break;
- case org.w3c.dom.Node.DOCUMENT_TYPE_NODE:
- // nothing ?
- break;
- case org.w3c.dom.Node.ELEMENT_NODE:
- newNode = doc.createElementNS(namespaceURI, nodeName);
- break;
- case org.w3c.dom.Node.ENTITY_NODE:
- // nothing ?
- break;
- case org.w3c.dom.Node.ENTITY_REFERENCE_NODE:
- newNode = doc.createEntityReference(nodeName);
- break;
- case org.w3c.dom.Node.NOTATION_NODE:
- // nothing ?
- break;
- case org.w3c.dom.Node.PROCESSING_INSTRUCTION_NODE:
- newNode = doc.createProcessingInstruction(nodeName,
- value);
- break;
- case org.w3c.dom.Node.TEXT_NODE:
- newNode = doc.createTextNode(value);
- break;
- }
- try {
- topElementNode.appendChild(newNode);
- } catch (DOMException e) {
- runTimeError(RUN_TIME_INTERNAL_ERR, e.getMessage());
- return null;
- }
- }
+ // Copy all the nodes in the nodelist to be under the top element
+ copyNodes(nodeList, doc, topElementNode);
+
// w3c DOM -> DOM2SAX -> DOMBuilder -> DOMImpl
DOMImpl idom = new DOMImpl();
final DOM2SAX dom2sax = new DOM2SAX(doc);
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]