Date: 2004-09-15T21:00:56
Editor: TonyEdwards <[EMAIL PROTECTED]>
Wiki: Cocoon Wiki
Page: FlowBasedXMLEditor
URL: http://wiki.apache.org/cocoon/FlowBasedXMLEditor
no comment
Change Log:
------------------------------------------------------------------------------
@@ -28,36 +28,15 @@
[[BR]]importClass(org.w3c.dom.Node);
[[BR]]importClass(org.w3c.dom.Element);
[[BR]]importClass(org.w3c.dom.NodeList);
-[[BR]]// Create a new document, given the name of its root element
-[[BR]]function newDocument(root, attributeName, attributeVal) {
-[[BR]] var result =
DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
-[[BR]] // Create a processing instruction targeted for xml.
-[[BR]] var node = result.createProcessingInstruction("xml",
"version='1.0'");
-[[BR]] result.appendChild(node);
-[[BR]] node = null;
-[[BR]]
-[[BR]] // Create a comment for the document.
-[[BR]] node = result.createComment(
-[[BR]] "sample xml file created using XML DOM object.");
-[[BR]] result.appendChild(node);
-[[BR]] node=null;
-[[BR]]
-[[BR]] result.appendChild(result.createElement(root));
-[[BR]] print("Add attributes to root..." + attributeName);
-[[BR]] if (attributeVal != null){
-[[BR]] try {
-[[BR]] print("creating attribute for root: " + attributeName);
-[[BR]] var root = result.getFirstChild();
-[[BR]] root.setAttribute(attributeName, attributeVal);
-[[BR]]
-[[BR]] } catch (error) {
-[[BR]] cocoon.log.error("Could not add attribute node to root: " +
error);
-[[BR]] }
-[[BR]]
-[[BR]] }
-[[BR]]
-[[BR]] return result;
-[[BR]]}
-
-
+[[BR]] Thereafter is became an exercise in wrapping the DOM functions in flow
script. Attached is my general purpose jsUtils.js file which contains all the
necessary methods to carry out DOM manipulation. If anyone can see any obvious
flaws, please let me know!
+To create a new document, pass the name of the document to the newDocument
function. Adding attributes is as simple as calling addAttribute. If the
nodeParent parameter is null, the attribute gets assigned to the document root.
+The use of the sessionManager is interesting. I couldn't for the life of me
get a reference to the DOM if I used the cocoon.session object. I don't know
why. I scoured all manner of places and found the use of the sessionManager,
stuck the document in there, and then I was able to access the DOM from the
pipeline. Maybe I missed something, but it works.
+ var sXmlName = "TestDoc";
+ var xmlDoc = newDocument("hierarchy","description",Trim(sXmlName));
+ //Make its hierarchy number = 0 so we know its a newy
+ addAttribute(xmlDoc,null,"hrcyNumber",0);
+ var sessionManager =
cocoon.getComponent("org.apache.cocoon.webapps.session.SessionManager");
+ var session = sessionManager.getSession(true);
+ session.setAttribute("hrcyDoc", hrcyDoc);
+ cocoon.sendPage("tree-menu");