001: /*
002: * Copyright 2001-2007 Hippo (www.hippo.nl)
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: /**
017: * Based on Gianugo Rabellino's (gianugo.rabellino@orixo.com) code.
018: * Found at : http://marc.theaimsgroup.com/?l=xml-cocoon-users&m=110838989005480&w=2
019: */package nl.hippo.cocoon.forms.binding;
020:
021: import java.io.IOException;
022: import java.io.StringReader;
023: import javax.xml.parsers.DocumentBuilder;
024: import javax.xml.parsers.DocumentBuilderFactory;
025: import javax.xml.parsers.ParserConfigurationException;
026: import org.apache.cocoon.ProcessingException;
027: import org.apache.cocoon.forms.binding.AbstractCustomBinding;
028: import org.apache.cocoon.forms.binding.BindingException;
029: import org.apache.cocoon.forms.formmodel.Widget;
030: import org.apache.cocoon.xml.XMLUtils;
031: import org.apache.commons.jxpath.JXPathContext;
032: import org.apache.commons.jxpath.Pointer;
033: import org.w3c.dom.Document;
034: import org.w3c.dom.Node;
035: import org.xml.sax.InputSource;
036: import org.xml.sax.SAXException;
037: import org.apache.commons.logging.Log;
038: import org.apache.commons.logging.LogFactory;
039:
040: /**
041: * Simple custom binding for HTMLArea and XML data.
042: *
043: * @author <a href="mailto:j.reijn@hippo.nl">Jeroen Reijn</a>
044: * @version $Id: CustomMultiLineHTMLAreaXmlAsStringBinding.java 10725 2008-03-12 09:36:45Z jjoachimsthal $
045: */
046: public class CustomMultiLineHTMLAreaXmlAsStringBinding extends
047: AbstractCustomBinding {
048: private final Log logger = LogFactory
049: .getLog(CustomMultiLineHTMLAreaXmlAsStringBinding.class);
050:
051: /**
052: * Serialize the node as String upon load.
053: *
054: * @see org.apache.cocoon.forms.binding.AbstractCustomBinding#doLoad(org.apache.cocoon.forms.formmodel.Widget,
055: * org.apache.commons.jxpath.JXPathContext)
056: */
057:
058: protected void doLoad(Widget frmModel, JXPathContext context)
059: throws BindingException {
060:
061: Pointer ptr = context.getContextPointer();
062: if (ptr.getNode() != null) {
063: Node value = (Node) ptr.getNode();
064: try {
065: String test = XMLUtils.serializeNode(value);
066: frmModel.setValue(test);
067: test = null;
068: } catch (ProcessingException e) {
069: throw new BindingException("Unable to serialize node",
070: e);
071: }
072: } else {
073: if (logger.isDebugEnabled()) {
074: logger.debug("non-existent path: skipping "
075: + ptr.asPath());
076: }
077: }
078: }
079:
080: /**
081: * Parse and import the XML String upon save.
082: *
083: * @throws IOException
084: * @throws SAXException
085: * @see org.apache.cocoon.forms.binding.AbstractCustomBinding#doSave(org.apache.cocoon.forms.formmodel.Widget,
086: * org.apache.commons.jxpath.JXPathContext)
087: */
088: protected void doSave(Widget frmModel, JXPathContext context)
089: throws BindingException, SAXException, IOException {
090:
091: DocumentBuilder builder = null;
092: DocumentBuilderFactory factory = DocumentBuilderFactory
093: .newInstance();
094: try {
095: builder = factory.newDocumentBuilder();
096: } catch (ParserConfigurationException e) {
097: throw new BindingException("Problem getting a parser", e);
098: }
099: String content = (String) frmModel.getValue();
100:
101: if (content != null) {
102: Pointer ptr = context.getContextPointer();
103: Document dom = builder.parse(new InputSource(
104: new StringReader(content)));
105:
106: Node contextNode = (Node) ptr.getNode();
107: contextNode.removeChild(contextNode.getFirstChild());
108: if (contextNode.getFirstChild() != null) {
109: if (contextNode.getFirstChild().getChildNodes() != null) {
110: contextNode
111: .removeChild(contextNode.getFirstChild());
112: }
113: }
114:
115: Node result = contextNode.getOwnerDocument().importNode(
116: dom.getDocumentElement(), true);
117: result = contextNode.appendChild(result);
118: }
119: content = "";
120: }
121:
122: }
|