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: */
020: package nl.hippo.cocoon.forms.binding;
021:
022: import java.io.IOException;
023: import java.io.StringReader;
024: import javax.xml.parsers.DocumentBuilder;
025: import javax.xml.parsers.DocumentBuilderFactory;
026: import javax.xml.parsers.ParserConfigurationException;
027: import org.apache.cocoon.ProcessingException;
028: import org.apache.cocoon.forms.binding.AbstractCustomBinding;
029: import org.apache.cocoon.forms.binding.BindingException;
030: import org.apache.cocoon.forms.formmodel.Widget;
031: import org.apache.cocoon.xml.XMLUtils;
032: import org.apache.commons.jxpath.JXPathContext;
033: import org.apache.commons.jxpath.Pointer;
034: import org.w3c.dom.Document;
035: import org.w3c.dom.Node;
036: import org.xml.sax.InputSource;
037: import org.xml.sax.SAXException;
038:
039: /**
040: * Simple custom binding for HTMLArea and XML data.
041: *
042: * @author <a href="mailto:j.reijn@hippo.com">Jeroen Reijn </a>
043: * @version $Id: CustomSingleLineHTMLAreaXmlAsStringBinding.java 9281 2007-12-13 13:37:22Z nvankampenhout $
044: */
045: public class CustomSingleLineHTMLAreaXmlAsStringBinding extends
046: AbstractCustomBinding {
047:
048: /**
049: * Serialize the node as String upon load.
050: *
051: * @see org.apache.cocoon.forms.binding.AbstractCustomBinding#doLoad(org.apache.cocoon.forms.formmodel.Widget,
052: * org.apache.commons.jxpath.JXPathContext)
053: */
054:
055: protected void doLoad(Widget frmModel, JXPathContext context)
056: throws BindingException {
057:
058: Node value = (Node) context.getContextPointer().getNode();
059:
060: try {
061: String balue = XMLUtils.serializeNode(value);
062: frmModel.setValue(balue);
063: } catch (ProcessingException e) {
064: throw new BindingException("Unable to serialize node", e);
065: }
066: }
067:
068: /**
069: * Parse and import the XML String upon save.
070: *
071: * @throws IOException
072: * @throws SAXException
073: * @see org.apache.cocoon.forms.binding.AbstractCustomBinding#doSave(org.apache.cocoon.forms.formmodel.Widget,
074: * org.apache.commons.jxpath.JXPathContext)
075: */
076: protected void doSave(Widget frmModel, JXPathContext context)
077: throws BindingException, SAXException, IOException {
078:
079: DocumentBuilder builder = null;
080: DocumentBuilderFactory factory = DocumentBuilderFactory
081: .newInstance();
082:
083: try {
084: builder = factory.newDocumentBuilder();
085: } catch (ParserConfigurationException e) {
086: throw new BindingException("Problem getting a parser", e);
087: }
088:
089: String content = (String) frmModel.getValue();
090: if (content != null) {
091:
092: Pointer ptr = context.getContextPointer();
093: Document dom = builder.parse(new InputSource(
094: new StringReader(content)));
095:
096: Node contextNode = (Node) ptr.getNode();
097: contextNode.removeChild(contextNode.getFirstChild());
098: if (contextNode.getFirstChild() != null) {
099: if (contextNode.getFirstChild().getChildNodes() != null) {
100: contextNode
101: .removeChild(contextNode.getFirstChild());
102: }
103: }
104:
105: Node result = contextNode.getOwnerDocument().importNode(
106: dom.getDocumentElement(), true);
107: result = contextNode.appendChild(result);
108: }
109: content = "";
110:
111: }
112:
113: }
|