001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.forms.binding;
018:
019: import org.apache.cocoon.forms.formmodel.Widget;
020:
021: import org.apache.commons.jxpath.JXPathContext;
022: import org.w3c.dom.Document;
023: import org.w3c.dom.DocumentFragment;
024: import org.w3c.dom.Node;
025:
026: /**
027: * InsertNodeJXPathBinding provides an implementation of a {@link Binding}
028: * that inserts a clone of some 'template document-fragment' into the target
029: * back-end model upon save.
030: * <p>NOTES:
031: * <ol>
032: * <li>This Binding does not perform any actions when loading.</li>
033: * <li>This expects the back-end model to be an XML file.</li>
034: * </ol>
035: *
036: * @version $Id: InsertNodeJXPathBinding.java 517733 2007-03-13 15:37:22Z vgritsenko $
037: */
038: public class InsertNodeJXPathBinding extends JXPathBindingBase {
039:
040: private final DocumentFragment template;
041:
042: /**
043: * Constructs InsertNodeJXPathBinding
044: */
045: public InsertNodeJXPathBinding(
046: JXPathBindingBuilderBase.CommonAttributes commonAtts,
047: DocumentFragment domTemplate) {
048: super (commonAtts);
049: this .template = domTemplate;
050: }
051:
052: public DocumentFragment getTemplate() {
053: return template;
054: }
055:
056: /**
057: * Do-nothing implementation of the interface.
058: */
059: public void doLoad(Widget frmModel, JXPathContext jxpc) {
060: // doesn't do a thing when loading.
061: }
062:
063: /**
064: * Registers a JXPath Factory on the JXPath Context.
065: * <p>
066: * The factory will inserts a clone of the 'template' DocumentFragment
067: * inside this object into the target objectmodel.
068: */
069: public void doSave(Widget frmModel, JXPathContext jxpc) {
070:
071: Node parentNode = (Node) jxpc.getContextBean();
072: Document targetDoc = parentNode.getOwnerDocument();
073: Node toInsert = targetDoc.importNode(this .template, true);
074: parentNode.appendChild(toInsert);
075:
076: if (getLogger().isDebugEnabled())
077: getLogger().debug("InsertNode executed.");
078:
079: // jxpc.setFactory(new AbstractFactory() {
080: // public boolean createObject(JXPathContext context, Pointer pointer,
081: // Object parent, String name, int index) {
082: //
083: // Node parentNode = (Node) parent;
084: // Document targetDoc = parentNode.getOwnerDocument();
085: // Node toInsert = targetDoc.importNode(InsertNodeJXPathBinding.this.template, true);
086: // parentNode.appendChild(toInsert);
087: //
088: // if (getLogger().isDebugEnabled())
089: // getLogger().debug("InsertNode jxpath factory executed for index." + index);
090: // return true;
091: // }
092: // });
093: //
094: // if (getLogger().isDebugEnabled())
095: // getLogger().debug("done registered factory for inserting node -- " + this);
096: }
097:
098: public String toString() {
099: return "InsertNodeJXPathBinding [for nested template]";
100: }
101: }
|